fedilab-Android-App/app/src/main/java/app/fedilab/android/drawers/AccountsFollowRequestAdapte...

162 lines
6.5 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.drawers;
/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
*
* 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
* 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,
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
2017-06-07 13:58:21 +02:00
import android.content.Intent;
import android.graphics.PorterDuff;
import android.os.AsyncTask;
2017-06-07 13:58:21 +02:00
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
2019-09-06 17:55:14 +02:00
2019-11-15 16:32:25 +01:00
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
2017-12-02 11:02:25 +01:00
import com.bumptech.glide.Glide;
2019-09-06 17:55:14 +02:00
2019-08-19 09:11:23 +02:00
import org.jetbrains.annotations.NotNull;
2019-09-06 17:55:14 +02:00
import java.util.ArrayList;
import java.util.List;
2019-09-06 17:55:14 +02:00
2019-11-15 16:32:25 +01:00
import app.fedilab.android.R;
import app.fedilab.android.activities.MainActivity;
2019-11-15 16:32:25 +01:00
import app.fedilab.android.activities.ShowAccountActivity;
import app.fedilab.android.asynctasks.PostActionAsyncTask;
import app.fedilab.android.asynctasks.UpdateAccountInfoAsyncTask;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.API;
import app.fedilab.android.client.Entities.Account;
import app.fedilab.android.client.Entities.Error;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.interfaces.OnPostActionInterface;
2019-11-15 16:32:25 +01:00
import es.dmoral.toasty.Toasty;
2020-06-19 17:17:17 +02:00
import static app.fedilab.android.helper.Helper.makeEmojis;
/**
* Created by Thomas on 07/05/2017.
* Adapter for accounts asking a follow request
*/
2020-06-16 18:23:13 +02:00
public class AccountsFollowRequestAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements OnPostActionInterface {
private List<Account> accounts;
private Context context;
private AccountsFollowRequestAdapter accountsFollowRequestAdapter;
2019-09-06 17:55:14 +02:00
public AccountsFollowRequestAdapter(List<Account> accounts) {
this.accounts = accounts;
accountsFollowRequestAdapter = this;
}
2019-08-19 09:11:23 +02:00
@NotNull
@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);
2020-03-07 14:16:28 +01:00
return new ViewHolder(layoutInflater.inflate(R.layout.drawer_account_follow_request, parent, false));
}
@Override
2019-08-19 09:11:23 +02:00
public void onBindViewHolder(@NotNull RecyclerView.ViewHolder viewHolder, int position) {
2017-10-29 09:30:08 +01:00
final AccountsFollowRequestAdapter.ViewHolder holder = (AccountsFollowRequestAdapter.ViewHolder) viewHolder;
final Account account = accounts.get(position);
2017-06-07 13:58:21 +02:00
holder.btn_authorize.getBackground().setColorFilter(ContextCompat.getColor(context, R.color.green_1), PorterDuff.Mode.MULTIPLY);
holder.btn_reject.getBackground().setColorFilter(ContextCompat.getColor(context, R.color.red_1), PorterDuff.Mode.MULTIPLY);
//TODO: check if Friendica has a way to accept/deny follow requests
2020-03-08 10:29:06 +01:00
if (MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.GNU) {
holder.btn_authorize.setVisibility(View.GONE);
holder.btn_reject.setVisibility(View.GONE);
}
2020-04-08 15:29:02 +02:00
holder.account_dn.setText(account.getDisplay_name());
2020-06-19 17:17:17 +02:00
makeEmojis(context, holder.account_dn, account.getDisplayNameSpan(), account.getEmojis());
holder.account_un.setText(account.getAcct());
//Profile picture
2017-12-02 15:05:54 +01:00
Glide.with(holder.account_pp.getContext())
2017-12-02 11:02:25 +01:00
.load(account.getAvatar())
.into(holder.account_pp);
2020-03-07 14:16:28 +01:00
holder.account_pp.setOnClickListener(v -> openAccountDetails(account));
holder.account_dn.setOnClickListener(v -> openAccountDetails(account));
holder.account_un.setOnClickListener(v -> openAccountDetails(account));
holder.btn_authorize.setOnClickListener(v -> new PostActionAsyncTask(context, API.StatusAction.AUTHORIZE, account.getId(), AccountsFollowRequestAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR));
holder.btn_reject.setOnClickListener(v -> new PostActionAsyncTask(context, API.StatusAction.REJECT, account.getId(), AccountsFollowRequestAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR));
2017-10-29 09:30:08 +01:00
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemCount() {
return accounts.size();
}
2019-09-06 17:55:14 +02:00
private void openAccountDetails(Account account) {
2017-06-07 13:58:21 +02:00
Intent intent = new Intent(context, ShowAccountActivity.class);
Bundle b = new Bundle();
2018-12-22 11:43:28 +01:00
b.putParcelable("account", account);
2017-06-07 13:58:21 +02:00
intent.putExtras(b);
context.startActivity(intent);
}
@Override
public void onPostAction(int statusCode, API.StatusAction statusAction, String userId, Error error) {
2019-09-06 17:55:14 +02:00
if (error != null) {
Toasty.error(context, error.getError(), Toast.LENGTH_LONG).show();
return;
}
Helper.manageMessageStatusCode(context, statusCode, statusAction);
//When authorizing or rejecting an account, this account is removed from the list
List<Account> accountToRemove = new ArrayList<>();
2019-09-06 17:55:14 +02:00
if (statusAction == API.StatusAction.AUTHORIZE || statusAction == API.StatusAction.REJECT) {
for (Account account : accounts) {
if (account.getId().equals(userId))
accountToRemove.add(account);
}
accounts.removeAll(accountToRemove);
accountsFollowRequestAdapter.notifyDataSetChanged();
}
}
2020-03-07 14:16:28 +01:00
private static class ViewHolder extends RecyclerView.ViewHolder {
ImageView account_pp;
Button btn_authorize;
Button btn_reject;
TextView account_dn;
TextView account_un;
2017-10-29 09:30:08 +01:00
2019-08-19 09:11:23 +02:00
ViewHolder(View itemView) {
2017-10-29 09:30:08 +01:00
super(itemView);
account_pp = itemView.findViewById(R.id.account_pp);
account_dn = itemView.findViewById(R.id.account_dn);
2017-10-29 09:30:08 +01:00
account_un = itemView.findViewById(R.id.account_un);
btn_authorize = itemView.findViewById(R.id.btn_authorize);
btn_reject = itemView.findViewById(R.id.btn_reject);
}
}
}