2019-05-18 11:10:30 +02:00
|
|
|
package app.fedilab.android.drawers;
|
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>. */
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
2019-11-15 16:32:25 +01:00
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
|
|
|
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
|
|
|
|
2019-08-19 09:11:23 +02:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
2019-09-01 16:29:33 +02:00
|
|
|
import java.util.ArrayList;
|
2017-12-15 07:35:13 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
2019-05-18 11:10:30 +02:00
|
|
|
import app.fedilab.android.R;
|
|
|
|
import app.fedilab.android.activities.ManageAccountsInListActivity;
|
|
|
|
import app.fedilab.android.activities.ShowAccountActivity;
|
|
|
|
import app.fedilab.android.asynctasks.ManageListsAsyncTask;
|
2019-11-15 16:32:25 +01:00
|
|
|
import app.fedilab.android.client.APIResponse;
|
|
|
|
import app.fedilab.android.client.Entities.Account;
|
|
|
|
import app.fedilab.android.helper.Helper;
|
2019-05-18 11:10:30 +02:00
|
|
|
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
|
|
|
|
2020-06-19 17:17:17 +02:00
|
|
|
import static app.fedilab.android.helper.Helper.makeEmojis;
|
|
|
|
|
2017-12-15 07:35:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Thomas on 15/12/2017.
|
|
|
|
* Adapter for accounts in lists
|
|
|
|
*/
|
2020-06-16 18:23:13 +02:00
|
|
|
public class AccountsInAListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements OnListActionInterface {
|
2017-12-15 07:35:13 +01:00
|
|
|
|
2021-01-19 17:43:51 +01:00
|
|
|
private final List<Account> accounts;
|
|
|
|
private final AccountsInAListAdapter accountsInAListAdapter;
|
|
|
|
private final type actionType;
|
|
|
|
private final String listId;
|
2017-12-15 07:35:13 +01:00
|
|
|
private Context context;
|
2019-09-01 16:29:33 +02:00
|
|
|
private List<Account> allAccount = new ArrayList<>();
|
2017-12-15 07:35:13 +01:00
|
|
|
|
2019-09-06 17:55:14 +02:00
|
|
|
public AccountsInAListAdapter(type actionType, String listId, List<Account> accounts) {
|
2017-12-15 07:35:13 +01:00
|
|
|
this.accounts = accounts;
|
|
|
|
this.accountsInAListAdapter = this;
|
|
|
|
this.actionType = actionType;
|
2017-12-15 18:53:17 +01:00
|
|
|
this.listId = listId;
|
2017-12-15 07:35:13 +01:00
|
|
|
}
|
|
|
|
|
2019-09-06 17:55:14 +02:00
|
|
|
public AccountsInAListAdapter(type actionType, String listId, List<Account> allAccount, List<Account> accountSearch) {
|
2019-09-01 16:29:33 +02:00
|
|
|
this.accounts = accountSearch;
|
|
|
|
this.accountsInAListAdapter = this;
|
|
|
|
this.actionType = actionType;
|
|
|
|
this.listId = listId;
|
|
|
|
this.allAccount = allAccount;
|
|
|
|
}
|
|
|
|
|
2019-08-19 09:11:23 +02:00
|
|
|
@NotNull
|
2017-12-15 07:35:13 +01:00
|
|
|
@Override
|
2019-08-19 09:11:23 +02:00
|
|
|
public RecyclerView.ViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
|
|
|
|
|
|
|
|
context = parent.getContext();
|
|
|
|
LayoutInflater layoutInflater = LayoutInflater.from(context);
|
2017-12-15 07:35:13 +01:00
|
|
|
return new ViewHolder(layoutInflater.inflate(R.layout.drawer_account_list, parent, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-08-19 09:11:23 +02:00
|
|
|
public void onBindViewHolder(@NotNull RecyclerView.ViewHolder viewHolder, int position) {
|
2017-12-15 07:35:13 +01:00
|
|
|
final AccountsInAListAdapter.ViewHolder holder = (AccountsInAListAdapter.ViewHolder) viewHolder;
|
|
|
|
final Account account = accounts.get(position);
|
|
|
|
|
|
|
|
|
2020-04-08 15:29:02 +02:00
|
|
|
holder.account_un.setText(account.getDisplay_name());
|
2020-06-19 17:17:17 +02:00
|
|
|
makeEmojis(context, holder.account_un, account.getDisplayNameSpan(), account.getEmojis());
|
2017-12-15 07:35:13 +01:00
|
|
|
holder.account_ac.setText(account.getAcct());
|
2019-09-06 17:55:14 +02:00
|
|
|
if (account.getDisplay_name().equals(account.getAcct()))
|
2017-12-15 07:35:13 +01:00
|
|
|
holder.account_ac.setVisibility(View.GONE);
|
|
|
|
else
|
|
|
|
holder.account_ac.setVisibility(View.VISIBLE);
|
|
|
|
//Profile picture
|
2020-03-28 10:54:18 +01:00
|
|
|
Helper.loadGiF(context, account, holder.account_pp);
|
2017-12-15 07:35:13 +01:00
|
|
|
|
2019-09-06 17:55:14 +02:00
|
|
|
if (actionType == type.CURRENT) {
|
2017-12-15 07:35:13 +01:00
|
|
|
holder.account_action.setImageResource(R.drawable.ic_close);
|
2018-12-10 09:37:04 +01:00
|
|
|
holder.account_action.setContentDescription(context.getString(R.string.remove_account));
|
2019-09-06 17:55:14 +02:00
|
|
|
} else if (actionType == type.SEARCH && !isInList(account)) {
|
2017-12-15 07:35:13 +01:00
|
|
|
holder.account_action.setImageResource(R.drawable.ic_add);
|
2018-12-10 09:37:04 +01:00
|
|
|
holder.account_action.setContentDescription(context.getString(R.string.add_account));
|
2019-09-06 17:55:14 +02:00
|
|
|
} else if (actionType == type.SEARCH) {
|
2019-09-01 16:29:33 +02:00
|
|
|
holder.account_action.setImageResource(R.drawable.ic_close);
|
|
|
|
holder.account_action.setContentDescription(context.getString(R.string.remove_account));
|
2017-12-15 07:35:13 +01:00
|
|
|
}
|
2020-03-07 14:16:28 +01:00
|
|
|
holder.account_action.setOnClickListener(view -> {
|
|
|
|
if (actionType == type.CURRENT) {
|
2021-01-19 17:43:51 +01:00
|
|
|
new ManageListsAsyncTask(context, ManageListsAsyncTask.action.DELETE_USERS, new String[]{account.getId()}, listId, null, AccountsInAListAdapter.this);
|
2020-03-07 14:16:28 +01:00
|
|
|
remove(account);
|
|
|
|
accountsInAListAdapter.notifyDataSetChanged();
|
|
|
|
} else if (actionType == type.SEARCH && !isInList(account)) {
|
2021-01-19 17:43:51 +01:00
|
|
|
new ManageListsAsyncTask(context, ManageListsAsyncTask.action.ADD_USERS, new String[]{account.getId()}, listId, null, AccountsInAListAdapter.this);
|
2020-03-07 14:16:28 +01:00
|
|
|
((ManageAccountsInListActivity) context).addAccount(account);
|
|
|
|
|
|
|
|
} else if (actionType == type.SEARCH) {
|
2021-01-19 17:43:51 +01:00
|
|
|
new ManageListsAsyncTask(context, ManageListsAsyncTask.action.DELETE_USERS, new String[]{account.getId()}, listId, null, AccountsInAListAdapter.this);
|
2020-03-07 14:16:28 +01:00
|
|
|
remove(account);
|
|
|
|
accountsInAListAdapter.notifyDataSetChanged();
|
2017-12-15 07:35:13 +01:00
|
|
|
}
|
|
|
|
});
|
2020-03-07 14:16:28 +01:00
|
|
|
holder.account_container.setOnClickListener(v -> {
|
|
|
|
Intent intent = new Intent(context, ShowAccountActivity.class);
|
|
|
|
Bundle b = new Bundle();
|
|
|
|
b.putParcelable("account", account);
|
|
|
|
intent.putExtras(b);
|
|
|
|
context.startActivity(intent);
|
2017-12-15 07:35:13 +01:00
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-06 17:55:14 +02:00
|
|
|
private void remove(Account account) {
|
2019-09-01 16:29:33 +02:00
|
|
|
int position = 0;
|
2019-09-06 17:55:14 +02:00
|
|
|
for (Account act : allAccount) {
|
|
|
|
if (account.getId().equals(act.getId())) {
|
2019-09-01 16:29:33 +02:00
|
|
|
allAccount.remove(position);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
position++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 17:55:14 +02:00
|
|
|
private boolean isInList(Account account) {
|
|
|
|
if (allAccount != null && allAccount.size() > 0) {
|
2019-09-01 16:29:33 +02:00
|
|
|
for (Account act : allAccount) {
|
|
|
|
if (act.getId().equals(account.getId())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-15 07:35:13 +01:00
|
|
|
@Override
|
|
|
|
public void onActionDone(ManageListsAsyncTask.action actionType, APIResponse apiResponse, int statusCode) {
|
2019-09-06 17:55:14 +02:00
|
|
|
if (actionType == ManageListsAsyncTask.action.DELETE_USERS && statusCode != 200) {
|
|
|
|
Toasty.error(context, context.getString(R.string.toast_error), Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
2017-12-15 07:35:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getItemId(int position) {
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return accounts.size();
|
|
|
|
}
|
|
|
|
|
2019-11-15 16:32:25 +01:00
|
|
|
public enum type {
|
|
|
|
CURRENT,
|
|
|
|
SEARCH
|
|
|
|
}
|
2017-12-15 07:35:13 +01:00
|
|
|
|
2020-03-07 14:16:28 +01:00
|
|
|
private static class ViewHolder extends RecyclerView.ViewHolder {
|
2017-12-15 07:35:13 +01:00
|
|
|
ImageView account_pp;
|
|
|
|
TextView account_ac;
|
|
|
|
TextView account_un;
|
2019-09-06 17:55:14 +02:00
|
|
|
FloatingActionButton account_action;
|
2017-12-15 07:35:13 +01:00
|
|
|
LinearLayout account_container;
|
2019-09-06 17:55:14 +02:00
|
|
|
|
2017-12-15 07:35:13 +01:00
|
|
|
ViewHolder(View itemView) {
|
|
|
|
super(itemView);
|
|
|
|
account_container = itemView.findViewById(R.id.account_container);
|
|
|
|
account_pp = itemView.findViewById(R.id.account_pp);
|
|
|
|
account_ac = itemView.findViewById(R.id.account_ac);
|
|
|
|
account_un = itemView.findViewById(R.id.account_un);
|
|
|
|
account_action = itemView.findViewById(R.id.account_action);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|