Mercury-IM/app/src/main/java/org/mercury_im/messenger/ui/account/AccountsRecyclerViewAdapter...

113 lines
4.0 KiB
Java

package org.mercury_im.messenger.ui.account;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;
import org.mercury_im.messenger.R;
import org.mercury_im.messenger.entity.Account;
import org.mercury_im.messenger.ui.avatar.AvatarDrawable;
import org.mercury_im.messenger.ui.account.AccountsFragment.OnAccountListItemClickListener;
import org.mercury_im.messenger.util.AbstractDiffCallback;
import java.util.ArrayList;
import java.util.List;
public class AccountsRecyclerViewAdapter extends RecyclerView.Adapter<AccountsRecyclerViewAdapter.ViewHolder> {
private final List<Account> accounts = new ArrayList<>();
private final OnAccountListItemClickListener onAccountClickListener;
private final AccountsViewModel viewModel;
public AccountsRecyclerViewAdapter(AccountsViewModel viewModel, OnAccountListItemClickListener listener) {
onAccountClickListener = listener;
this.viewModel = viewModel;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_account, parent, false);
return new ViewHolder(view);
}
public void setValues(List<Account> values) {
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new AccountsDiffCallback(values, accounts), true);
accounts.clear();
accounts.addAll(values);
diffResult.dispatchUpdatesTo(this);
}
@Override
public int getItemCount() {
return accounts.size();
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
Account account = accounts.get(position);
holder.account = account;
holder.jid.setText(account.getAddress());
holder.avatar.setImageDrawable(new AvatarDrawable(account.getAddress(), account.getAddress()));
holder.enabled.setChecked(account.isEnabled());
holder.enabled.setOnCheckedChangeListener((compoundButton, checked) -> viewModel.setAccountEnabled(account, checked));
holder.mView.setOnClickListener(v -> {
if (null != onAccountClickListener) {
onAccountClickListener.onAccountListItemClick(holder.account);
}
});
holder.mView.setOnLongClickListener(v -> {
if (null != onAccountClickListener) {
onAccountClickListener.onAccountListItemLongClick(holder.account);
}
return true;
});
}
public class ViewHolder extends RecyclerView.ViewHolder {
public Account account;
public final View mView;
public final ImageView avatar;
public final TextView jid;
public final Switch enabled;
public final TextView status;
public ViewHolder(View view) {
super(view);
mView = view;
avatar = view.findViewById(R.id.avatar_account);
jid = view.findViewById(R.id.text_account_jid);
enabled = view.findViewById(R.id.switch_account_enabled);
status = view.findViewById(R.id.text_account_status);
}
}
public class AccountsDiffCallback extends AbstractDiffCallback<Account> {
public AccountsDiffCallback(List<Account> newItems, List<Account> oldItems) {
super(newItems, oldItems);
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return oldItems.get(oldItemPosition).getId() == newItems.get(newItemPosition).getId();
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
Account oldM = oldItems.get(oldItemPosition);
Account newM = newItems.get(newItemPosition);
return oldM.equals(newM);
}
}
}