fedilab-Android-App/app/src/main/java/app/fedilab/android/activities/ManageAccountsInListActivit...

211 lines
9.1 KiB
Java
Raw Normal View History

2017-12-15 07:35:13 +01:00
/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2017-12-15 07:35:13 +01:00
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
2019-05-18 11:10:30 +02:00
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2017-12-15 07:35:13 +01:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
2017-12-15 07:35:13 +01:00
* see <http://www.gnu.org/licenses>. */
2019-05-18 11:10:30 +02:00
package app.fedilab.android.activities;
2017-12-15 07:35:13 +01:00
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
2019-09-06 17:55:14 +02:00
2019-06-11 19:38:26 +02:00
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
2019-09-06 17:55:14 +02:00
2017-12-15 20:01:58 +01:00
import android.text.Editable;
import android.text.TextWatcher;
2017-12-15 07:35:13 +01:00
import android.view.MenuItem;
2017-12-15 20:01:58 +01:00
import android.view.MotionEvent;
2017-12-15 07:35:13 +01:00
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
2017-12-15 18:53:17 +01:00
import java.util.ArrayList;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.APIResponse;
import app.fedilab.android.client.Entities.Account;
import app.fedilab.android.drawers.AccountsInAListAdapter;
import app.fedilab.android.helper.Helper;
2018-11-25 10:45:16 +01:00
import es.dmoral.toasty.Toasty;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.R;
import app.fedilab.android.asynctasks.ManageListsAsyncTask;
import app.fedilab.android.interfaces.OnListActionInterface;
2017-12-15 07:35:13 +01:00
/**
* Created by Thomas on 15/12/2017.
* Manage accounts in Lists
*/
public class ManageAccountsInListActivity extends BaseActivity implements OnListActionInterface {
2017-12-15 20:01:58 +01:00
public EditText search_account;
private LinearLayout main_account_container;
private RelativeLayout loader;
2017-12-15 07:35:13 +01:00
private RecyclerView lv_accounts_current, lv_accounts_search;
private String title, listId;
private java.util.List<Account> accounts;
2017-12-15 20:01:58 +01:00
private AccountsInAListAdapter accountsInAListAdapter;
2017-12-15 07:35:13 +01:00
2017-12-17 13:56:38 +01:00
2017-12-15 07:35:13 +01:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
2019-05-18 11:10:30 +02:00
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
2019-09-06 17:55:14 +02:00
switch (theme) {
case Helper.THEME_LIGHT:
2019-07-20 15:15:47 +02:00
setTheme(R.style.AppTheme_NoActionBar_Fedilab);
getWindow().getDecorView().setBackgroundColor(ContextCompat.getColor(ManageAccountsInListActivity.this, R.color.mastodonC3__));
break;
case Helper.THEME_DARK:
setTheme(R.style.AppThemeDark_NoActionBar);
getWindow().getDecorView().setBackgroundColor(ContextCompat.getColor(ManageAccountsInListActivity.this, R.color.mastodonC1));
break;
case Helper.THEME_BLACK:
setTheme(R.style.AppThemeBlack_NoActionBar);
2018-05-12 12:06:43 +02:00
getWindow().getDecorView().setBackgroundColor(ContextCompat.getColor(ManageAccountsInListActivity.this, R.color.black_3));
break;
default:
setTheme(R.style.AppThemeDark_NoActionBar);
getWindow().getDecorView().setBackgroundColor(ContextCompat.getColor(ManageAccountsInListActivity.this, R.color.mastodonC1));
}
2017-12-15 07:35:13 +01:00
setContentView(R.layout.activity_manage_accounts_list);
2019-06-25 14:52:46 +02:00
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
2019-09-06 17:55:14 +02:00
if (getSupportActionBar() != null)
2017-12-15 07:35:13 +01:00
getSupportActionBar().hide();
Bundle b = getIntent().getExtras();
2019-09-06 17:55:14 +02:00
if (b != null) {
2017-12-15 07:35:13 +01:00
title = b.getString("title");
listId = b.getString("id");
2019-09-06 17:55:14 +02:00
} else {
Toasty.error(getApplicationContext(), getString(R.string.toast_error), Toast.LENGTH_LONG).show();
2017-12-15 07:35:13 +01:00
}
main_account_container = findViewById(R.id.main_account_container);
loader = findViewById(R.id.loader);
2017-12-15 20:01:58 +01:00
TextView list_title = findViewById(R.id.list_title);
2017-12-15 07:35:13 +01:00
search_account = findViewById(R.id.search_account);
lv_accounts_search = findViewById(R.id.lv_accounts_search);
lv_accounts_current = findViewById(R.id.lv_accounts_current);
2017-12-15 20:01:58 +01:00
2017-12-15 18:53:17 +01:00
this.accounts = new ArrayList<>();
2019-08-19 09:11:23 +02:00
accountsInAListAdapter = new AccountsInAListAdapter(AccountsInAListAdapter.type.CURRENT, listId, this.accounts);
2017-12-15 07:35:13 +01:00
lv_accounts_current.setAdapter(accountsInAListAdapter);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(ManageAccountsInListActivity.this);
lv_accounts_current.setLayoutManager(mLayoutManager);
list_title.setText(title);
loader.setVisibility(View.VISIBLE);
new ManageListsAsyncTask(ManageAccountsInListActivity.this, ManageListsAsyncTask.action.GET_LIST_ACCOUNT, null, null, listId, null, ManageAccountsInListActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2017-12-15 20:01:58 +01:00
search_account.addTextChangedListener(new TextWatcher() {
@Override
2019-09-06 17:55:14 +02:00
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
2017-12-15 20:01:58 +01:00
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (count > 0) {
search_account.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_close, 0);
2019-09-06 17:55:14 +02:00
} else {
2017-12-15 20:01:58 +01:00
search_account.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_search, 0);
}
}
2019-09-06 17:55:14 +02:00
2017-12-15 20:01:58 +01:00
@Override
public void afterTextChanged(Editable s) {
2019-09-06 17:55:14 +02:00
if (s != null && s.length() > 0) {
new ManageListsAsyncTask(ManageAccountsInListActivity.this, s.toString(), ManageAccountsInListActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
2017-12-15 20:01:58 +01:00
lv_accounts_search.setVisibility(View.GONE);
lv_accounts_current.setVisibility(View.VISIBLE);
}
}
});
search_account.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_RIGHT = 2;
if (event.getAction() == MotionEvent.ACTION_UP) {
if (search_account.length() > 0 && event.getRawX() >= (search_account.getRight() - search_account.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
search_account.setText("");
}
}
return false;
}
});
2017-12-15 07:35:13 +01:00
}
2019-09-06 17:55:14 +02:00
public void addAccount(Account account) {
2017-12-15 20:01:58 +01:00
search_account.setText("");
2019-09-06 17:55:14 +02:00
accounts.add(0, account);
2017-12-15 20:01:58 +01:00
accountsInAListAdapter.notifyItemInserted(0);
}
2017-12-15 07:35:13 +01:00
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onActionDone(ManageListsAsyncTask.action actionType, APIResponse apiResponse, int statusCode) {
loader.setVisibility(View.GONE);
2017-12-15 20:01:58 +01:00
main_account_container.setVisibility(View.VISIBLE);
2019-09-06 17:55:14 +02:00
if (apiResponse.getError() != null) {
Toasty.error(getApplicationContext(), apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
2017-12-15 07:35:13 +01:00
return;
}
2019-09-06 17:55:14 +02:00
if (actionType == ManageListsAsyncTask.action.GET_LIST_ACCOUNT) {
2017-12-15 07:35:13 +01:00
if (apiResponse.getAccounts() != null && apiResponse.getAccounts().size() > 0) {
this.accounts.addAll(apiResponse.getAccounts());
accountsInAListAdapter.notifyDataSetChanged();
2017-12-15 20:01:58 +01:00
lv_accounts_search.setVisibility(View.GONE);
lv_accounts_current.setVisibility(View.VISIBLE);
}
2019-09-06 17:55:14 +02:00
} else if (actionType == ManageListsAsyncTask.action.SEARCH_USER) {
2017-12-15 20:01:58 +01:00
if (apiResponse.getAccounts() != null && apiResponse.getAccounts().size() > 0) {
java.util.List<Account> accountsSearch = new ArrayList<>();
accountsSearch.addAll(apiResponse.getAccounts());
2019-09-01 16:29:33 +02:00
AccountsInAListAdapter accountsSearchInAListAdapter = new AccountsInAListAdapter(AccountsInAListAdapter.type.SEARCH, listId, accounts, accountsSearch);
2017-12-15 20:01:58 +01:00
lv_accounts_search.setAdapter(accountsSearchInAListAdapter);
LinearLayoutManager mLayoutManager1 = new LinearLayoutManager(ManageAccountsInListActivity.this);
lv_accounts_search.setLayoutManager(mLayoutManager1);
lv_accounts_search.setVisibility(View.VISIBLE);
lv_accounts_current.setVisibility(View.GONE);
2017-12-15 07:35:13 +01:00
}
}
}
}