AccountAdapter.java

  1package eu.siacs.conversations.ui.adapter;
  2
  3import android.view.LayoutInflater;
  4import android.view.View;
  5import android.view.ViewGroup;
  6import android.widget.ArrayAdapter;
  7import androidx.annotation.NonNull;
  8import androidx.databinding.DataBindingUtil;
  9import com.google.android.material.color.MaterialColors;
 10import eu.siacs.conversations.R;
 11import eu.siacs.conversations.databinding.ItemAccountBinding;
 12import eu.siacs.conversations.entities.Account;
 13import eu.siacs.conversations.ui.XmppActivity;
 14import eu.siacs.conversations.ui.util.AvatarWorkerTask;
 15import java.util.List;
 16
 17public class AccountAdapter extends ArrayAdapter<Account> {
 18
 19    private final XmppActivity activity;
 20    private final boolean showStateButton;
 21
 22    public AccountAdapter(XmppActivity activity, List<Account> objects, boolean showStateButton) {
 23        super(activity, 0, objects);
 24        this.activity = activity;
 25        this.showStateButton = showStateButton;
 26    }
 27
 28    public AccountAdapter(XmppActivity activity, List<Account> objects) {
 29        super(activity, 0, objects);
 30        this.activity = activity;
 31        this.showStateButton = true;
 32    }
 33
 34    @NonNull
 35    @Override
 36    public View getView(int position, View view, @NonNull ViewGroup parent) {
 37        final Account account = getItem(position);
 38        final ViewHolder viewHolder;
 39        if (view == null) {
 40            ItemAccountBinding binding =
 41                    DataBindingUtil.inflate(
 42                            LayoutInflater.from(parent.getContext()),
 43                            R.layout.item_account,
 44                            parent,
 45                            false);
 46            view = binding.getRoot();
 47            viewHolder = new ViewHolder(binding);
 48            view.setTag(viewHolder);
 49        } else {
 50            viewHolder = (ViewHolder) view.getTag();
 51        }
 52        if (account == null) {
 53            return view;
 54        }
 55        viewHolder.binding.accountJid.setText(account.getJid().asBareJid().toString());
 56        AvatarWorkerTask.loadAvatar(account, viewHolder.binding.accountImage, R.dimen.avatar);
 57        final var status = account.getStatus();
 58        if (account.isServiceOutage()) {
 59            final var sos = account.getServiceOutageStatus();
 60            if (sos != null && sos.isPlanned()) {
 61                viewHolder.binding.accountStatus.setText(
 62                        R.string.account_status_service_outage_scheduled);
 63            } else {
 64                viewHolder.binding.accountStatus.setText(
 65                        R.string.account_status_service_outage_known);
 66            }
 67        } else {
 68            viewHolder.binding.accountStatus.setText(status.getReadableId());
 69        }
 70        switch (status) {
 71            case ONLINE:
 72                viewHolder.binding.accountStatus.setTextColor(
 73                        MaterialColors.getColor(
 74                                viewHolder.binding.accountStatus,
 75                                androidx.appcompat.R.attr.colorPrimary));
 76                break;
 77            case DISABLED:
 78            case LOGGED_OUT:
 79            case CONNECTING:
 80                viewHolder.binding.accountStatus.setTextColor(
 81                        MaterialColors.getColor(
 82                                viewHolder.binding.accountStatus,
 83                                com.google.android.material.R.attr.colorOnSurfaceVariant));
 84                break;
 85            default:
 86                viewHolder.binding.accountStatus.setTextColor(
 87                        MaterialColors.getColor(
 88                                viewHolder.binding.accountStatus,
 89                                androidx.appcompat.R.attr.colorError));
 90                break;
 91        }
 92        final boolean isDisabled = (account.getStatus() == Account.State.DISABLED);
 93        viewHolder.binding.tglAccountStatus.setOnCheckedChangeListener(null);
 94        viewHolder.binding.tglAccountStatus.setChecked(!isDisabled);
 95        if (this.showStateButton) {
 96            viewHolder.binding.tglAccountStatus.setVisibility(View.VISIBLE);
 97        } else {
 98            viewHolder.binding.tglAccountStatus.setVisibility(View.GONE);
 99        }
100        viewHolder.binding.tglAccountStatus.setOnCheckedChangeListener(
101                (compoundButton, b) -> {
102                    if (b == isDisabled && activity instanceof OnTglAccountState tglAccountState) {
103                        tglAccountState.onClickTglAccountState(account, b);
104                    }
105                });
106        return view;
107    }
108
109    private static class ViewHolder {
110        private final ItemAccountBinding binding;
111
112        private ViewHolder(ItemAccountBinding binding) {
113            this.binding = binding;
114        }
115    }
116
117    public interface OnTglAccountState {
118        void onClickTglAccountState(Account account, boolean state);
119    }
120}