2020-09-03 18:56:48 +02:00
|
|
|
package app.fedilab.fedilabtube.drawer;
|
|
|
|
/* Copyright 2020 Thomas Schneider
|
|
|
|
*
|
|
|
|
* This file is a part of TubeLab
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* TubeLab 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 TubeLab; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
import android.content.Context;
|
2020-09-03 18:57:10 +02:00
|
|
|
import android.graphics.Color;
|
2020-09-03 18:56:48 +02:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ImageView;
|
2020-09-03 18:57:10 +02:00
|
|
|
import android.widget.LinearLayout;
|
2020-09-03 18:56:48 +02:00
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
2020-09-03 18:57:10 +02:00
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
import androidx.core.graphics.ColorUtils;
|
2020-09-03 18:56:48 +02:00
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import app.fedilab.fedilabtube.R;
|
|
|
|
import app.fedilab.fedilabtube.client.PeertubeAPI;
|
|
|
|
import app.fedilab.fedilabtube.client.entities.Account;
|
|
|
|
import app.fedilab.fedilabtube.client.entities.Error;
|
|
|
|
import app.fedilab.fedilabtube.helper.Helper;
|
|
|
|
import app.fedilab.fedilabtube.interfaces.OnPostActionInterface;
|
|
|
|
import es.dmoral.toasty.Toasty;
|
|
|
|
|
|
|
|
|
|
|
|
public class AccountsHorizontalListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements OnPostActionInterface {
|
|
|
|
|
2020-09-03 18:57:10 +02:00
|
|
|
EventListener listener;
|
2020-09-03 18:56:48 +02:00
|
|
|
private List<Account> accounts;
|
|
|
|
private Context context;
|
|
|
|
private AccountsHorizontalListAdapter accountsListAdapter;
|
|
|
|
|
2020-09-03 18:57:10 +02:00
|
|
|
public AccountsHorizontalListAdapter(List<Account> accounts, EventListener listener) {
|
2020-09-03 18:56:48 +02:00
|
|
|
this.accounts = accounts;
|
|
|
|
this.accountsListAdapter = this;
|
2020-09-03 18:57:10 +02:00
|
|
|
this.listener = listener;
|
2020-09-03 18:56:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
|
|
context = parent.getContext();
|
|
|
|
LayoutInflater layoutInflater = LayoutInflater.from(context);
|
|
|
|
return new ViewHolder(layoutInflater.inflate(R.layout.drawer_horizontal_account, parent, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
|
|
|
|
final AccountsHorizontalListAdapter.ViewHolder holder = (AccountsHorizontalListAdapter.ViewHolder) viewHolder;
|
|
|
|
final Account account = accounts.get(position);
|
|
|
|
|
|
|
|
if (account.getDisplay_name() != null && !account.getDisplay_name().trim().equals(""))
|
|
|
|
holder.account_dn.setText(account.getDisplay_name());
|
|
|
|
else
|
|
|
|
holder.account_dn.setText(account.getUsername().replace("@", ""));
|
|
|
|
|
|
|
|
//Profile picture
|
|
|
|
Helper.loadGiF(context, account, holder.account_pp, 270);
|
2020-09-03 18:57:10 +02:00
|
|
|
|
|
|
|
if (account.isSelected()) {
|
|
|
|
holder.main_container.setBackgroundColor(ColorUtils.setAlphaComponent(ContextCompat.getColor(context, R.color.colorAccent), 50));
|
|
|
|
} else {
|
|
|
|
holder.main_container.setBackgroundColor(Color.TRANSPARENT);
|
|
|
|
}
|
2020-09-03 18:56:48 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getItemId(int position) {
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return accounts.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
private Account getItemAt(int position) {
|
|
|
|
if (accounts.size() > position)
|
|
|
|
return accounts.get(position);
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPostAction(int statusCode, PeertubeAPI.StatusAction statusAction, String targetedId, Error error) {
|
|
|
|
if (error != null) {
|
|
|
|
Toasty.error(context, error.getError(), Toast.LENGTH_LONG).show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (statusAction == PeertubeAPI.StatusAction.FOLLOW) {
|
|
|
|
/* if (action == RetrieveAccountsAsyncTask.Type.CHANNELS) {
|
|
|
|
SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
|
|
|
new InstancesDAO(context, db).insertInstance(accounts.get(0).getAcct().split("@")[1], accounts.get(0).getAcct().split("@")[0], "PEERTUBE_CHANNEL");
|
|
|
|
} else {
|
|
|
|
for (Account account : accounts) {
|
|
|
|
if (account.getId().equals(targetedId)) {
|
|
|
|
account.setFollowType(Account.followAction.FOLLOW);
|
|
|
|
account.setMakingAction(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
accountsListAdapter.notifyDataSetChanged();
|
|
|
|
}*/
|
|
|
|
}
|
|
|
|
if (statusAction == PeertubeAPI.StatusAction.UNFOLLOW) {
|
|
|
|
for (Account account : accounts) {
|
|
|
|
if (account.getId().equals(targetedId)) {
|
|
|
|
account.setFollowType(Account.followAction.NOT_FOLLOW);
|
|
|
|
account.setMakingAction(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
accountsListAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void notifyAccountChanged(Account account) {
|
|
|
|
for (int i = 0; i < accountsListAdapter.getItemCount(); i++) {
|
|
|
|
//noinspection ConstantConditions
|
|
|
|
if (accountsListAdapter.getItemAt(i) != null && accountsListAdapter.getItemAt(i).getId().equals(account.getId())) {
|
|
|
|
try {
|
|
|
|
accountsListAdapter.notifyItemChanged(i);
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 18:57:10 +02:00
|
|
|
public interface EventListener {
|
|
|
|
void click(String forAccount);
|
|
|
|
}
|
|
|
|
|
2020-09-03 18:56:48 +02:00
|
|
|
|
2020-09-03 18:57:10 +02:00
|
|
|
private class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
|
2020-09-03 18:56:48 +02:00
|
|
|
ImageView account_pp;
|
|
|
|
TextView account_dn;
|
2020-09-03 18:57:10 +02:00
|
|
|
LinearLayout main_container;
|
2020-09-03 18:56:48 +02:00
|
|
|
|
|
|
|
ViewHolder(View itemView) {
|
|
|
|
super(itemView);
|
2020-09-03 18:57:10 +02:00
|
|
|
itemView.setOnClickListener(this);
|
2020-09-03 18:56:48 +02:00
|
|
|
account_pp = itemView.findViewById(R.id.account_pp);
|
|
|
|
account_dn = itemView.findViewById(R.id.account_dn);
|
2020-09-03 18:57:10 +02:00
|
|
|
main_container = itemView.findViewById(R.id.main_container);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
Account account = accounts.get(getAdapterPosition());
|
|
|
|
listener.click(account.getAcct());
|
|
|
|
for (Account acc : accounts) {
|
|
|
|
if (acc.getAcct().compareTo(account.getAcct()) == 0) {
|
|
|
|
acc.setSelected(true);
|
|
|
|
} else {
|
|
|
|
acc.setSelected(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
notifyItemRangeChanged(0, accounts.size());
|
2020-09-03 18:56:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|