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

189 lines
7.7 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.Bundle;
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.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
2019-11-15 16:32:25 +01:00
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
2017-12-15 18:53:17 +01:00
import java.util.ArrayList;
2019-11-15 16:32:25 +01:00
import app.fedilab.android.R;
import app.fedilab.android.asynctasks.ManageListsAsyncTask;
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;
import app.fedilab.android.interfaces.OnListActionInterface;
2019-11-15 16:32:25 +01:00
import es.dmoral.toasty.Toasty;
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);
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-11-07 19:51:53 +01:00
if (theme == Helper.THEME_LIGHT) {
2019-11-07 19:07:19 +01:00
setTheme(R.style.Dialog);
2019-11-07 19:51:53 +01:00
} else {
2019-11-07 19:07:19 +01:00
setTheme(R.style.DialogDark);
}
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 {
2020-04-08 12:42:15 +02:00
Toasty.error(ManageAccountsInListActivity.this, 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);
2021-01-19 17:43:51 +01:00
new ManageListsAsyncTask(ManageAccountsInListActivity.this, ManageListsAsyncTask.action.GET_LIST_ACCOUNT, null, listId, null, ManageAccountsInListActivity.this);
2017-12-15 07:35:13 +01:00
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) {
2021-01-19 17:43:51 +01:00
new ManageListsAsyncTask(ManageAccountsInListActivity.this, s.toString(), ManageAccountsInListActivity.this);
2019-09-06 17:55:14 +02:00
} else {
2017-12-15 20:01:58 +01:00
lv_accounts_search.setVisibility(View.GONE);
lv_accounts_current.setVisibility(View.VISIBLE);
}
}
});
2020-03-08 09:15:12 +01:00
search_account.setOnTouchListener((v, 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("");
2017-12-15 20:01:58 +01:00
}
}
2020-03-08 09:15:12 +01:00
return false;
2017-12-15 20:01:58 +01:00
});
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) {
2020-03-08 09:15:12 +01:00
if (item.getItemId() == android.R.id.home) {
finish();
return true;
2017-12-15 07:35:13 +01:00
}
2020-03-08 09:15:12 +01:00
return super.onOptionsItemSelected(item);
2017-12-15 07:35:13 +01:00
}
@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) {
2020-04-08 12:42:15 +02:00
Toasty.error(ManageAccountsInListActivity.this, 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
}
}
}
}