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

126 lines
4.2 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.drawers;
2017-10-25 10:51:36 +02:00
/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2017-10-25 10:51:36 +02: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-10-25 10:51:36 +02: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-10-25 10:51:36 +02:00
* see <http://www.gnu.org/licenses>. */
2019-10-17 18:20:21 +02:00
import android.app.Activity;
2017-10-25 10:51:36 +02:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
2017-12-02 11:02:25 +01:00
2019-11-15 16:32:25 +01:00
import androidx.annotation.NonNull;
2017-12-02 11:02:25 +01:00
import com.bumptech.glide.Glide;
2019-10-17 18:20:21 +02:00
import java.lang.ref.WeakReference;
2017-10-25 10:51:36 +02:00
import java.util.List;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.R;
import app.fedilab.android.activities.TootActivity;
2019-11-15 16:32:25 +01:00
import app.fedilab.android.client.Entities.Account;
2017-10-25 10:51:36 +02:00
/**
* Created by Thomas on 25/10/2017.
* Adapter for accounts when replying
*/
2019-09-06 17:55:14 +02:00
public class AccountsReplyAdapter extends BaseAdapter {
2017-10-25 10:51:36 +02:00
private List<Account> accounts;
private boolean[] checked;
2019-10-17 18:20:21 +02:00
private WeakReference<Activity> activityWeakReference;
2017-10-25 10:51:36 +02:00
2019-10-17 18:20:21 +02:00
public AccountsReplyAdapter(WeakReference<Activity> activityWeakReference, List<Account> accounts, boolean[] checked) {
2017-10-25 10:51:36 +02:00
this.accounts = accounts;
this.checked = checked;
2019-10-17 18:20:21 +02:00
this.activityWeakReference = activityWeakReference;
2017-10-25 10:51:36 +02:00
}
2019-10-17 18:20:21 +02:00
public AccountsReplyAdapter(WeakReference<Activity> activityWeakReference, List<Account> accounts, List<Boolean> checked) {
2019-01-23 19:23:26 +01:00
this.accounts = accounts;
this.checked = new boolean[checked.size()];
int index = 0;
for (Boolean val : checked) {
this.checked[index++] = val;
}
2019-10-17 18:20:21 +02:00
this.activityWeakReference = activityWeakReference;
2019-01-23 19:23:26 +01:00
}
2017-10-25 10:51:36 +02:00
@Override
public int getCount() {
return accounts.size();
}
@Override
public Account getItem(int position) {
return accounts.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@NonNull
@Override
public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
2019-10-17 18:20:21 +02:00
LayoutInflater layoutInflater = LayoutInflater.from(activityWeakReference.get());
2017-10-25 10:51:36 +02:00
final Account account = accounts.get(position);
final ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.drawer_account_reply, parent, false);
holder = new ViewHolder();
holder.account_pp = convertView.findViewById(R.id.account_pp);
holder.account_dn = convertView.findViewById(R.id.account_dn);
holder.checkbox = convertView.findViewById(R.id.checkbox);
holder.account_container = convertView.findViewById(R.id.account_container);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
2020-03-07 14:16:28 +01:00
holder.checkbox.setOnCheckedChangeListener((compoundButton, isChecked) -> {
try {
((TootActivity) activityWeakReference.get()).changeAccountReply(isChecked, "@" + account.getAcct());
checked[position] = isChecked;
2020-03-08 10:29:06 +01:00
} catch (Exception ignored) {
}
2017-10-25 10:51:36 +02:00
});
holder.checkbox.setChecked(checked[position]);
holder.account_dn.setText(String.format("@%s", account.getAcct()));
2020-03-07 14:16:28 +01:00
holder.account_container.setOnClickListener(view -> holder.checkbox.performClick());
2017-10-25 10:51:36 +02:00
//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);
2017-10-25 10:51:36 +02:00
return convertView;
}
2020-03-07 14:16:28 +01:00
private static class ViewHolder {
2017-10-25 10:51:36 +02:00
ImageView account_pp;
TextView account_dn;
CheckBox checkbox;
LinearLayout account_container;
}
}