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

156 lines
5.9 KiB
Java
Raw Normal View History

2020-03-10 07:28:55 +01:00
package app.fedilab.android.drawers;
2020-03-10 07:34:24 +01:00
/* Copyright 2020 Thomas Schneider
2020-03-10 07:28:55 +01:00
*
* 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>. */
2020-04-24 15:11:48 +02:00
import android.content.Context;
import android.os.AsyncTask;
2020-03-10 07:28:55 +01:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
2020-03-10 19:00:32 +01:00
import android.widget.ImageView;
2020-03-10 07:34:24 +01:00
import android.widget.LinearLayout;
2020-03-10 07:28:55 +01:00
import android.widget.TextView;
2020-04-08 12:42:15 +02:00
2020-03-10 07:28:55 +01:00
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
2020-03-10 19:00:32 +01:00
2020-04-11 19:18:08 +02:00
import com.bumptech.glide.Glide;
2020-04-16 12:22:48 +02:00
import java.util.ArrayList;
2020-03-10 07:28:55 +01:00
import java.util.List;
2020-04-08 12:42:15 +02:00
2020-03-10 07:28:55 +01:00
import app.fedilab.android.R;
2020-04-24 15:11:48 +02:00
import app.fedilab.android.asynctasks.PostActionAsyncTask;
import app.fedilab.android.asynctasks.RetrieveFeedsAsyncTask;
import app.fedilab.android.client.API;
import app.fedilab.android.client.Entities.Error;
2020-03-10 07:34:24 +01:00
import app.fedilab.android.client.Entities.Reaction;
2020-03-10 19:00:32 +01:00
import app.fedilab.android.helper.Helper;
2020-04-24 15:11:48 +02:00
import app.fedilab.android.interfaces.OnPostActionInterface;
2020-03-10 07:28:55 +01:00
/**
2020-03-10 07:34:24 +01:00
* Created by Thomas on 10/03/2020.
* Adapter for reactions on messages
2020-03-10 07:28:55 +01:00
*/
2020-06-16 18:23:13 +02:00
public class ReactionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements OnPostActionInterface {
2020-03-10 07:28:55 +01:00
2020-03-10 07:34:24 +01:00
private List<Reaction> reactions;
2020-04-24 15:11:48 +02:00
private RetrieveFeedsAsyncTask.Type type;
private String statusId;
2020-03-10 07:28:55 +01:00
2020-04-24 15:11:48 +02:00
ReactionAdapter(List<Reaction> reactions, RetrieveFeedsAsyncTask.Type type, String statusId) {
2020-03-10 07:34:24 +01:00
this.reactions = reactions;
2020-04-16 14:34:45 +02:00
if (reactions == null) {
2020-04-16 12:22:48 +02:00
this.reactions = new ArrayList<>();
}
2020-04-24 15:11:48 +02:00
this.type = type;
this.statusId = statusId;
2020-03-10 07:28:55 +01:00
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int position) {
2020-03-10 07:34:24 +01:00
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
return new ViewHolder(layoutInflater.inflate(R.layout.drawer_reaction, parent, false));
2020-03-10 07:28:55 +01:00
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
2020-03-10 07:34:24 +01:00
final Reaction reaction = reactions.get(viewHolder.getAdapterPosition());
2020-03-10 07:28:55 +01:00
ViewHolder holder = (ViewHolder) viewHolder;
2020-04-24 15:11:48 +02:00
Context context = viewHolder.itemView.getContext();
2020-03-10 07:34:24 +01:00
holder.reaction_count.setText(String.valueOf(reaction.getCount()));
2020-04-08 12:42:15 +02:00
if (reaction.isMe()) {
2020-03-10 19:00:32 +01:00
holder.reaction_container.setBackgroundResource(R.drawable.reaction_voted);
2020-04-08 12:42:15 +02:00
} else {
2020-03-10 19:00:32 +01:00
holder.reaction_container.setBackgroundResource(R.drawable.reaction_border);
}
2020-04-08 12:42:15 +02:00
if (reaction.getUrl() != null) {
2020-03-10 19:00:32 +01:00
holder.reaction_name.setVisibility(View.GONE);
holder.reaction_emoji.setVisibility(View.VISIBLE);
holder.reaction_emoji.setContentDescription(reaction.getName());
Helper.loadGiF(holder.itemView.getContext(), reaction.getUrl(), holder.reaction_emoji);
2020-04-11 19:18:08 +02:00
2020-04-16 14:34:45 +02:00
if (!reaction.getUrl().contains("gif")) {
2020-04-11 19:18:08 +02:00
Glide.with(holder.itemView.getContext())
.asDrawable()
.load(reaction.getUrl())
.thumbnail(0.1f)
.into(holder.reaction_emoji);
2020-04-16 14:34:45 +02:00
} else {
2020-04-11 19:18:08 +02:00
Glide.with(holder.itemView.getContext())
.asGif()
.load(reaction.getUrl())
.thumbnail(0.1f)
.into(holder.reaction_emoji);
}
2020-04-08 12:42:15 +02:00
} else {
2020-03-10 19:00:32 +01:00
holder.reaction_name.setText(reaction.getName());
holder.reaction_name.setVisibility(View.VISIBLE);
holder.reaction_emoji.setVisibility(View.GONE);
}
2020-04-24 15:11:48 +02:00
holder.reaction_container.setOnClickListener(v -> {
String emojiStr = reaction.getName();
API.StatusAction statusAction;
if (type == RetrieveFeedsAsyncTask.Type.ANNOUNCEMENTS) {
statusAction = reaction.isMe() ? API.StatusAction.REMOVE_REACTION : API.StatusAction.ADD_REACTION;
} else {
statusAction = reaction.isMe() ? API.StatusAction.REMOVE_PLEROMA_REACTION : API.StatusAction.ADD_PLEROMA_REACTION;
}
reaction.setMe(!reaction.isMe());
2020-04-25 19:20:42 +02:00
if (reaction.isMe()) {
reaction.setCount(reaction.getCount() + 1);
} else {
reaction.setCount(reaction.getCount() - 1);
2020-04-24 15:11:48 +02:00
}
new PostActionAsyncTask(context, statusAction, this.statusId, null, emojiStr, ReactionAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
});
2020-03-10 07:28:55 +01:00
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemCount() {
2020-03-10 07:34:24 +01:00
return reactions.size();
2020-03-10 07:28:55 +01:00
}
2020-04-24 15:11:48 +02:00
@Override
public void onPostAction(int statusCode, API.StatusAction statusAction, String userId, Error error) {
notifyDataSetChanged();
}
2020-03-10 07:28:55 +01:00
static class ViewHolder extends RecyclerView.ViewHolder {
2020-03-10 07:34:24 +01:00
TextView reaction_name, reaction_count;
LinearLayout reaction_container;
2020-03-10 19:00:32 +01:00
ImageView reaction_emoji;
2020-03-10 07:28:55 +01:00
public ViewHolder(@NonNull View itemView) {
super(itemView);
2020-03-10 07:34:24 +01:00
reaction_name = itemView.findViewById(R.id.reaction_name);
reaction_count = itemView.findViewById(R.id.reaction_count);
reaction_container = itemView.findViewById(R.id.reaction_container);
2020-03-10 19:00:32 +01:00
reaction_emoji = itemView.findViewById(R.id.reaction_emoji);
2020-03-10 07:28:55 +01:00
}
}
}