2022-04-27 15:20:42 +02:00
|
|
|
package app.fedilab.android.ui.drawer;
|
|
|
|
/* Copyright 2021 Thomas Schneider
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.app.Activity;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.content.res.ColorStateList;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.appcompat.app.AlertDialog;
|
|
|
|
import androidx.core.app.ActivityOptionsCompat;
|
|
|
|
import androidx.lifecycle.LifecycleOwner;
|
|
|
|
import androidx.lifecycle.ViewModelProvider;
|
|
|
|
import androidx.lifecycle.ViewModelStoreOwner;
|
|
|
|
import androidx.preference.PreferenceManager;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
2022-07-18 11:43:23 +02:00
|
|
|
import java.lang.ref.WeakReference;
|
2022-04-27 15:20:42 +02:00
|
|
|
import java.util.Date;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import app.fedilab.android.BaseMainActivity;
|
|
|
|
import app.fedilab.android.R;
|
2022-12-14 14:29:20 +01:00
|
|
|
import app.fedilab.android.activities.MainActivity;
|
2022-04-27 15:20:42 +02:00
|
|
|
import app.fedilab.android.activities.ProfileActivity;
|
2022-05-24 10:12:04 +02:00
|
|
|
import app.fedilab.android.client.entities.api.Account;
|
2022-04-27 15:20:42 +02:00
|
|
|
import app.fedilab.android.databinding.DrawerAccountBinding;
|
|
|
|
import app.fedilab.android.helper.Helper;
|
|
|
|
import app.fedilab.android.helper.MastodonHelper;
|
2022-11-29 10:23:19 +01:00
|
|
|
import app.fedilab.android.helper.ThemeHelper;
|
2022-04-27 15:20:42 +02:00
|
|
|
import app.fedilab.android.viewmodel.mastodon.AccountsVM;
|
|
|
|
import es.dmoral.toasty.Toasty;
|
|
|
|
|
|
|
|
|
|
|
|
public class AccountAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
|
|
|
|
|
|
|
private final List<Account> accountList;
|
2022-12-14 14:29:20 +01:00
|
|
|
private final boolean home_mute;
|
2023-01-01 11:55:32 +01:00
|
|
|
private Context context;
|
2022-12-14 14:29:20 +01:00
|
|
|
|
|
|
|
public AccountAdapter(List<Account> accountList, boolean home_mute) {
|
|
|
|
this.accountList = accountList;
|
|
|
|
this.home_mute = home_mute;
|
|
|
|
}
|
2022-04-27 15:20:42 +02:00
|
|
|
|
|
|
|
public AccountAdapter(List<Account> accountList) {
|
|
|
|
this.accountList = accountList;
|
2022-12-14 14:29:20 +01:00
|
|
|
this.home_mute = false;
|
2022-04-27 15:20:42 +02:00
|
|
|
}
|
|
|
|
|
2022-12-14 14:29:20 +01:00
|
|
|
public static void accountManagement(Context context, AccountViewHolder accountViewHolder, Account account, int position, RecyclerView.Adapter<RecyclerView.ViewHolder> adapter, boolean home_mute) {
|
2022-04-27 15:20:42 +02:00
|
|
|
MastodonHelper.loadPPMastodon(accountViewHolder.binding.avatar, account);
|
|
|
|
|
|
|
|
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
|
|
|
|
AccountsVM accountsVM = new ViewModelProvider((ViewModelStoreOwner) context).get(AccountsVM.class);
|
|
|
|
|
2022-12-08 10:20:03 +01:00
|
|
|
if (sharedpreferences.getBoolean(context.getString(R.string.SET_CARDVIEW), false)) {
|
|
|
|
accountViewHolder.binding.cardviewContainer.setCardElevation(Helper.convertDpToPixel(5, context));
|
|
|
|
accountViewHolder.binding.dividerCard.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
2022-12-14 14:29:20 +01:00
|
|
|
if (home_mute) {
|
|
|
|
accountViewHolder.binding.muteHome.setVisibility(View.VISIBLE);
|
|
|
|
boolean muted = MainActivity.filteredAccounts != null && MainActivity.filteredAccounts.contains(account);
|
|
|
|
accountViewHolder.binding.muteHome.setChecked(muted);
|
|
|
|
accountViewHolder.binding.muteHome.setOnClickListener(v -> {
|
|
|
|
if (muted) {
|
|
|
|
accountsVM.unmuteHome(MainActivity.currentAccount, account).observe((LifecycleOwner) context, account1 -> adapter.notifyItemChanged(accountViewHolder.getLayoutPosition()));
|
|
|
|
} else {
|
|
|
|
accountsVM.muteHome(MainActivity.currentAccount, account).observe((LifecycleOwner) context, account1 -> adapter.notifyItemChanged(accountViewHolder.getLayoutPosition()));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
accountViewHolder.binding.muteHome.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
2022-04-27 15:20:42 +02:00
|
|
|
accountViewHolder.binding.avatar.setOnClickListener(v -> {
|
|
|
|
Intent intent = new Intent(context, ProfileActivity.class);
|
|
|
|
Bundle b = new Bundle();
|
|
|
|
b.putSerializable(Helper.ARG_ACCOUNT, account);
|
|
|
|
intent.putExtras(b);
|
|
|
|
ActivityOptionsCompat options = ActivityOptionsCompat
|
|
|
|
.makeSceneTransitionAnimation((Activity) context, accountViewHolder.binding.avatar, context.getString(R.string.activity_porfile_pp));
|
|
|
|
// start the new activity
|
|
|
|
context.startActivity(intent, options.toBundle());
|
|
|
|
});
|
2022-12-08 10:20:03 +01:00
|
|
|
|
|
|
|
|
2022-11-20 16:30:13 +01:00
|
|
|
accountViewHolder.binding.followAction.setIconResource(R.drawable.ic_baseline_person_add_24);
|
2022-04-27 15:20:42 +02:00
|
|
|
if (account.relationShip != null) {
|
|
|
|
|
2022-11-13 16:07:21 +01:00
|
|
|
ProfileActivity.action doAction = ProfileActivity.action.FOLLOW;
|
2022-11-13 12:14:13 +01:00
|
|
|
accountViewHolder.binding.followAction.setContentDescription(context.getString(R.string.action_follow));
|
2022-04-27 15:20:42 +02:00
|
|
|
accountViewHolder.binding.followAction.setVisibility(View.VISIBLE);
|
|
|
|
accountViewHolder.binding.followAction.setEnabled(true);
|
|
|
|
|
|
|
|
if (account.relationShip.id.compareToIgnoreCase(BaseMainActivity.currentUserID) == 0) {
|
|
|
|
doAction = ProfileActivity.action.NOTHING;
|
|
|
|
accountViewHolder.binding.followAction.setVisibility(View.GONE);
|
|
|
|
accountViewHolder.binding.muteGroup.setVisibility(View.GONE);
|
|
|
|
accountViewHolder.binding.block.setVisibility(View.GONE);
|
|
|
|
} else {
|
|
|
|
accountViewHolder.binding.followAction.setVisibility(View.VISIBLE);
|
|
|
|
accountViewHolder.binding.muteGroup.setVisibility(View.VISIBLE);
|
|
|
|
accountViewHolder.binding.block.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (account.relationShip.following) {
|
2022-11-29 10:23:19 +01:00
|
|
|
accountViewHolder.binding.followAction.setBackgroundTintList(ColorStateList.valueOf(ThemeHelper.getAttColor(context, R.attr.colorError)));
|
2022-04-27 15:20:42 +02:00
|
|
|
doAction = ProfileActivity.action.UNFOLLOW;
|
|
|
|
accountViewHolder.binding.followAction.setIconResource(R.drawable.ic_baseline_person_remove_24);
|
|
|
|
} else if (account.relationShip.requested) {
|
2022-11-29 10:23:19 +01:00
|
|
|
accountViewHolder.binding.followAction.setBackgroundTintList(ColorStateList.valueOf(ThemeHelper.getAttColor(context, R.attr.colorError)));
|
2022-04-27 15:20:42 +02:00
|
|
|
doAction = ProfileActivity.action.NOTHING;
|
|
|
|
accountViewHolder.binding.followAction.setEnabled(false);
|
|
|
|
accountViewHolder.binding.followAction.setIconResource(R.drawable.ic_baseline_hourglass_full_24);
|
2022-04-29 17:12:03 +02:00
|
|
|
} else {
|
2022-11-13 12:14:13 +01:00
|
|
|
accountViewHolder.binding.followAction.setIconResource(R.drawable.ic_baseline_person_add_24);
|
2023-01-13 17:26:25 +01:00
|
|
|
accountViewHolder.binding.followAction.setBackgroundTintList(ColorStateList.valueOf(ThemeHelper.getAttColor(context, R.attr.colorPrimary)));
|
2022-04-27 15:20:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (account.relationShip.blocking) {
|
2022-12-23 18:32:56 +01:00
|
|
|
accountViewHolder.binding.block.setBackgroundTintList(ColorStateList.valueOf(ThemeHelper.getAttColor(context, R.attr.colorError)));
|
|
|
|
accountViewHolder.binding.block.setIconResource(R.drawable.ic_baseline_lock_open_24);
|
|
|
|
accountViewHolder.binding.block.setContentDescription(context.getString(R.string.action_unblock));
|
2022-04-27 15:20:42 +02:00
|
|
|
accountViewHolder.binding.block.setOnClickListener(v -> accountsVM.unblock(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, account.id)
|
|
|
|
.observe((LifecycleOwner) context, relationShip -> {
|
|
|
|
account.relationShip = relationShip;
|
|
|
|
adapter.notifyItemChanged(position);
|
|
|
|
}));
|
|
|
|
} else {
|
2022-12-23 18:32:56 +01:00
|
|
|
accountViewHolder.binding.block.setBackgroundTintList(ColorStateList.valueOf(ThemeHelper.getAttColor(context, R.attr.colorPrimary)));
|
|
|
|
accountViewHolder.binding.block.setIconResource(R.drawable.ic_baseline_block_24);
|
|
|
|
accountViewHolder.binding.block.setContentDescription(context.getString(R.string.more_action_2));
|
2022-04-27 15:20:42 +02:00
|
|
|
accountViewHolder.binding.block.setOnClickListener(v -> accountsVM.block(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, account.id)
|
|
|
|
.observe((LifecycleOwner) context, relationShip -> {
|
|
|
|
account.relationShip = relationShip;
|
|
|
|
adapter.notifyItemChanged(position);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
if (account.mute_expires_at != null && account.mute_expires_at.after(new Date())) {
|
|
|
|
accountViewHolder.binding.muteTimed.setChecked(true);
|
|
|
|
accountViewHolder.binding.muteTimed.setOnClickListener(v -> accountsVM.unmute(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, account.id)
|
|
|
|
.observe((LifecycleOwner) context, relationShip -> {
|
|
|
|
account.relationShip = relationShip;
|
|
|
|
accountsVM.getMutes(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, "1", account.id, null)
|
|
|
|
.observe((LifecycleOwner) context, accounts -> {
|
|
|
|
if (accounts != null && accounts.accounts != null && accounts.accounts.size() > 0) {
|
|
|
|
account.mute_expires_at = accounts.accounts.get(0).mute_expires_at;
|
|
|
|
adapter.notifyItemChanged(position);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
accountViewHolder.binding.muteTimed.setChecked(false);
|
|
|
|
}
|
|
|
|
if (account.relationShip.muting) {
|
|
|
|
accountViewHolder.binding.mute.setChecked(true);
|
|
|
|
accountViewHolder.binding.mute.setOnClickListener(v -> accountsVM.unmute(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, account.id)
|
|
|
|
.observe((LifecycleOwner) context, relationShip -> {
|
|
|
|
account.relationShip = relationShip;
|
|
|
|
adapter.notifyItemChanged(position);
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
accountViewHolder.binding.mute.setChecked(false);
|
|
|
|
accountViewHolder.binding.mute.setOnClickListener(v -> accountsVM.mute(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, account.id, null, 0)
|
|
|
|
.observe((LifecycleOwner) context, relationShip -> {
|
|
|
|
account.relationShip = relationShip;
|
|
|
|
adapter.notifyItemChanged(position);
|
|
|
|
}));
|
|
|
|
accountViewHolder.binding.muteTimed.setEnabled(true);
|
|
|
|
accountViewHolder.binding.muteTimed.setOnClickListener(v -> MastodonHelper.scheduleBoost(context, MastodonHelper.ScheduleType.TIMED_MUTED, null, account, relationShip -> {
|
|
|
|
account.relationShip = relationShip;
|
|
|
|
adapter.notifyItemChanged(position);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (account.relationShip.muting_notifications) {
|
|
|
|
accountViewHolder.binding.muteNotification.setChecked(true);
|
|
|
|
accountViewHolder.binding.muteNotification.setOnClickListener(v -> accountsVM.mute(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, account.id, false, 0)
|
|
|
|
.observe((LifecycleOwner) context, relationShip -> {
|
|
|
|
account.relationShip = relationShip;
|
|
|
|
adapter.notifyItemChanged(position);
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
accountViewHolder.binding.muteNotification.setChecked(false);
|
|
|
|
accountViewHolder.binding.muteNotification.setOnClickListener(v -> accountsVM.mute(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, account.id, true, 0)
|
|
|
|
.observe((LifecycleOwner) context, relationShip -> {
|
|
|
|
account.relationShip = relationShip;
|
|
|
|
adapter.notifyItemChanged(position);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (account.relationShip.blocked_by) {
|
|
|
|
doAction = ProfileActivity.action.NOTHING;
|
|
|
|
accountViewHolder.binding.followAction.setEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
accountViewHolder.binding.followAction.setOnClickListener(null);
|
2022-11-13 16:07:21 +01:00
|
|
|
ProfileActivity.action finalDoAction = doAction;
|
2022-04-27 15:20:42 +02:00
|
|
|
accountViewHolder.binding.followAction.setOnClickListener(v -> {
|
2022-11-13 16:07:21 +01:00
|
|
|
if (finalDoAction == ProfileActivity.action.NOTHING) {
|
2022-04-27 15:20:42 +02:00
|
|
|
Toasty.info(context, context.getString(R.string.nothing_to_do), Toast.LENGTH_LONG).show();
|
2022-11-13 16:07:21 +01:00
|
|
|
} else if (finalDoAction == ProfileActivity.action.FOLLOW) {
|
2022-04-27 15:20:42 +02:00
|
|
|
accountViewHolder.binding.followAction.setEnabled(false);
|
|
|
|
accountsVM.follow(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, account.id, true, false)
|
|
|
|
.observe((LifecycleOwner) context, relationShip -> {
|
|
|
|
account.relationShip = relationShip;
|
|
|
|
adapter.notifyItemChanged(position);
|
|
|
|
});
|
2022-11-13 16:07:21 +01:00
|
|
|
} else if (finalDoAction == ProfileActivity.action.UNFOLLOW) {
|
2022-04-27 15:20:42 +02:00
|
|
|
boolean confirm_unfollow = sharedpreferences.getBoolean(context.getString(R.string.SET_UNFOLLOW_VALIDATION), true);
|
|
|
|
if (confirm_unfollow) {
|
2022-12-02 10:23:06 +01:00
|
|
|
AlertDialog.Builder unfollowConfirm = new AlertDialog.Builder(context, Helper.dialogStyle());
|
2022-04-27 15:20:42 +02:00
|
|
|
unfollowConfirm.setTitle(context.getString(R.string.unfollow_confirm));
|
|
|
|
unfollowConfirm.setMessage(account.acct);
|
|
|
|
unfollowConfirm.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss());
|
|
|
|
unfollowConfirm.setPositiveButton(R.string.yes, (dialog, which) -> {
|
|
|
|
accountViewHolder.binding.followAction.setEnabled(false);
|
|
|
|
accountsVM.unfollow(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, account.id)
|
|
|
|
.observe((LifecycleOwner) context, relationShip -> {
|
|
|
|
account.relationShip = relationShip;
|
|
|
|
adapter.notifyItemChanged(position);
|
|
|
|
});
|
|
|
|
dialog.dismiss();
|
|
|
|
});
|
|
|
|
unfollowConfirm.show();
|
|
|
|
} else {
|
|
|
|
accountsVM.unfollow(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, account.id)
|
|
|
|
.observe((LifecycleOwner) context, relationShip -> {
|
|
|
|
account.relationShip = relationShip;
|
|
|
|
adapter.notifyItemChanged(position);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-07-18 11:43:23 +02:00
|
|
|
accountViewHolder.binding.displayName.setText(
|
|
|
|
account.getSpanDisplayName(context,
|
2022-07-18 14:13:12 +02:00
|
|
|
new WeakReference<>(accountViewHolder.binding.displayName)),
|
2022-07-18 11:43:23 +02:00
|
|
|
TextView.BufferType.SPANNABLE);
|
2022-04-27 15:20:42 +02:00
|
|
|
accountViewHolder.binding.username.setText(String.format("@%s", account.acct));
|
2022-07-18 11:43:23 +02:00
|
|
|
accountViewHolder.binding.bio.setText(
|
|
|
|
account.getSpanNote(context,
|
2022-07-18 14:13:12 +02:00
|
|
|
new WeakReference<>(accountViewHolder.binding.bio)),
|
2022-07-18 11:43:23 +02:00
|
|
|
TextView.BufferType.SPANNABLE);
|
2022-04-27 15:20:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getCount() {
|
|
|
|
return accountList.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Account getItem(int position) {
|
|
|
|
return accountList.get(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
|
|
this.context = parent.getContext();
|
|
|
|
DrawerAccountBinding itemBinding = DrawerAccountBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
|
|
|
|
return new AccountViewHolder(itemBinding);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
|
|
|
|
Account account = accountList.get(position);
|
|
|
|
AccountViewHolder holder = (AccountViewHolder) viewHolder;
|
2022-12-14 14:29:20 +01:00
|
|
|
accountManagement(context, holder, account, position, this, home_mute);
|
2022-04-27 15:20:42 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getItemId(int position) {
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return accountList.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static class AccountViewHolder extends RecyclerView.ViewHolder {
|
|
|
|
DrawerAccountBinding binding;
|
|
|
|
|
|
|
|
AccountViewHolder(DrawerAccountBinding itemView) {
|
|
|
|
super(itemView.getRoot());
|
|
|
|
binding = itemView;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|