TubeLab-App-Android/app/src/main/java/app/fedilab/fedilabtube/drawer/CommentListAdapter.java

386 lines
18 KiB
Java
Raw Normal View History

2020-06-26 14:34:39 +02:00
package app.fedilab.fedilabtube.drawer;
2020-07-01 16:36:08 +02:00
/* 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>. */
2020-06-26 14:34:39 +02:00
import android.annotation.SuppressLint;
2020-10-01 17:42:03 +02:00
import android.app.Activity;
2020-10-14 16:06:17 +02:00
import android.app.AlertDialog;
2020-06-26 14:34:39 +02:00
import android.content.Context;
2020-09-25 18:58:04 +02:00
import android.os.Build;
2020-10-01 17:42:03 +02:00
import android.os.Handler;
import android.os.Looper;
2020-09-25 18:58:04 +02:00
import android.text.Html;
2020-06-26 14:34:39 +02:00
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
2020-10-01 17:42:03 +02:00
import android.widget.EditText;
2020-06-26 14:34:39 +02:00
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
2020-10-01 17:42:03 +02:00
import androidx.appcompat.widget.PopupMenu;
2020-10-15 18:11:37 +02:00
import androidx.constraintlayout.widget.ConstraintLayout;
2020-06-26 14:34:39 +02:00
import androidx.core.content.ContextCompat;
2020-09-08 10:11:11 +02:00
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelStoreOwner;
2020-06-26 14:34:39 +02:00
import androidx.recyclerview.widget.RecyclerView;
2020-06-27 11:21:25 +02:00
2020-06-26 14:34:39 +02:00
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2020-10-15 15:16:41 +02:00
import app.fedilab.fedilabtube.PeertubeActivity;
2020-06-26 14:34:39 +02:00
import app.fedilab.fedilabtube.R;
2020-09-08 10:11:11 +02:00
import app.fedilab.fedilabtube.client.APIResponse;
2020-09-25 18:58:04 +02:00
import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI;
import app.fedilab.fedilabtube.client.data.CommentData.Comment;
2020-10-01 17:42:03 +02:00
import app.fedilab.fedilabtube.client.entities.Report;
2020-10-15 15:16:41 +02:00
import app.fedilab.fedilabtube.helper.EmojiHelper;
2020-06-26 14:34:39 +02:00
import app.fedilab.fedilabtube.helper.Helper;
2020-09-08 10:11:11 +02:00
import app.fedilab.fedilabtube.viewmodel.PostActionsVM;
2020-06-26 14:34:39 +02:00
import es.dmoral.toasty.Toasty;
2020-10-15 18:11:37 +02:00
import studio.carbonylgroup.textfieldboxes.ExtendedEditText;
2020-06-26 14:34:39 +02:00
2020-10-13 17:50:52 +02:00
import static app.fedilab.fedilabtube.client.RetrofitPeertubeAPI.ActionType.MUTE;
2020-10-15 18:11:37 +02:00
import static app.fedilab.fedilabtube.client.RetrofitPeertubeAPI.ActionType.REPLY;
import static app.fedilab.fedilabtube.helper.Helper.isLoggedIn;
2020-10-13 17:50:52 +02:00
2020-06-26 14:34:39 +02:00
2020-09-25 18:58:04 +02:00
public class CommentListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
2020-06-26 14:34:39 +02:00
2020-10-01 17:42:03 +02:00
public AllCommentRemoved allCommentRemoved;
2020-06-26 14:34:39 +02:00
private Context context;
2020-10-14 16:06:17 +02:00
private final List<Comment> comments;
private final CommentListAdapter commentListAdapter;
2020-10-13 17:50:52 +02:00
boolean isVideoOwner;
2020-06-26 14:34:39 +02:00
2020-10-13 17:50:52 +02:00
public CommentListAdapter(List<Comment> comments, boolean isVideoOwner) {
2020-09-25 18:58:04 +02:00
this.comments = comments;
commentListAdapter = this;
2020-10-13 17:50:52 +02:00
this.isVideoOwner = isVideoOwner;
2020-06-26 14:34:39 +02:00
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemCount() {
2020-09-25 18:58:04 +02:00
return comments.size();
2020-06-26 14:34:39 +02:00
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
LayoutInflater layoutInflater = LayoutInflater.from(this.context);
2020-10-01 17:42:03 +02:00
return new ViewHolder(layoutInflater.inflate(R.layout.drawer_comment, parent, false));
2020-06-26 14:34:39 +02:00
}
@SuppressLint({"SetJavaScriptEnabled", "ClickableViewAccessibility"})
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder viewHolder, int i) {
context = viewHolder.itemView.getContext();
final ViewHolder holder = (ViewHolder) viewHolder;
2020-09-25 18:58:04 +02:00
final Comment comment = comments.get(i);
2020-06-26 14:34:39 +02:00
2020-09-25 18:58:04 +02:00
if (comment == null)
2020-06-26 14:34:39 +02:00
return;
2020-10-15 18:11:37 +02:00
holder.main_container.setTag(i);
2020-06-26 14:34:39 +02:00
2020-10-01 17:42:03 +02:00
holder.more_actions.setOnClickListener(view -> {
PopupMenu popup = new PopupMenu(context, holder.more_actions);
popup.getMenuInflater()
.inflate(R.menu.comment_menu, popup.getMenu());
if (!Helper.isOwner(context, comment.getAccount())) {
popup.getMenu().findItem(R.id.action_delete).setVisible(false);
2020-10-13 17:50:52 +02:00
}else {
popup.getMenu().findItem(R.id.action_mute).setVisible(false);
popup.getMenu().findItem(R.id.action_remove_comments).setVisible(false);
popup.getMenu().findItem(R.id.action_report).setVisible(false);
}
if( !isVideoOwner) {
popup.getMenu().findItem(R.id.action_remove_comments).setVisible(false);
}
if( !Helper.isLoggedIn(context)) {
popup.getMenu().findItem(R.id.action_mute).setVisible(false);
popup.getMenu().findItem(R.id.action_remove_comments).setVisible(false);
popup.getMenu().findItem(R.id.action_delete).setVisible(false);
2020-10-01 17:42:03 +02:00
}
popup.setOnMenuItemClickListener(item -> {
2020-10-14 16:06:17 +02:00
int itemId = item.getItemId();
if (itemId == R.id.action_delete) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.delete_comment);
builder.setMessage(R.string.delete_comment_confirm);
builder.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(R.string.delete, (dialog, which) -> {
new Thread(() -> {
new RetrofitPeertubeAPI(context).post(RetrofitPeertubeAPI.ActionType.PEERTUBEDELETECOMMENT, comment.getVideoId(), comment.getId());
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> {
comments.remove(comment);
notifyDataSetChanged();
if (comments.size() == 0) {
allCommentRemoved.onAllCommentRemoved();
}
};
mainHandler.post(myRunnable);
}).start();
dialog.dismiss();
})
.setNegativeButton(R.string.no, (dialog, which) -> dialog.dismiss())
.show();
} else if (itemId == R.id.action_report) {
reportComment(comment);
} else if (itemId == R.id.action_mute) {
PostActionsVM viewModel = new ViewModelProvider((ViewModelStoreOwner) context).get(PostActionsVM.class);
2020-10-15 18:11:37 +02:00
viewModel.post(MUTE, comment.getAccount().getAcct(), null).observe((LifecycleOwner) context, apiResponse -> manageVIewPostActions(MUTE, 0, apiResponse));
2020-10-14 16:06:17 +02:00
comments.remove(comment);
notifyDataSetChanged();
if (comments.size() == 0) {
allCommentRemoved.onAllCommentRemoved();
}
} else if (itemId == R.id.action_remove_comments) {
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.delete_account_comment);
builder.setMessage(R.string.delete_account_comment_confirm);
builder.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(R.string.delete, (dialog, which) -> {
new Thread(() -> {
new RetrofitPeertubeAPI(context).post(RetrofitPeertubeAPI.ActionType.PEERTUBE_DELETE_ALL_COMMENT_FOR_ACCOUNT, comment.getAccount().getAcct(), "my-videos");
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> {
comments.remove(comment);
notifyDataSetChanged();
if (comments.size() == 0) {
allCommentRemoved.onAllCommentRemoved();
}
};
mainHandler.post(myRunnable);
}).start();
dialog.dismiss();
})
.setNegativeButton(R.string.no, (dialog, which) -> dialog.dismiss())
.show();
2020-10-01 17:42:03 +02:00
}
return true;
});
popup.show();
});
holder.comment_content.setOnTouchListener((view, motionEvent) -> {
2020-06-26 14:34:39 +02:00
if (motionEvent.getAction() == MotionEvent.ACTION_UP && !view.hasFocus()) {
try {
view.requestFocus();
} catch (Exception ignored) {
}
}
return false;
});
2020-09-25 18:58:04 +02:00
Spanned commentSpan;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
2020-10-15 15:16:41 +02:00
commentSpan = Html.fromHtml(EmojiHelper.shortnameToUnicode(comment.getText()), Html.FROM_HTML_MODE_COMPACT);
2020-09-25 18:58:04 +02:00
else
2020-10-15 15:16:41 +02:00
commentSpan = Html.fromHtml(EmojiHelper.shortnameToUnicode(comment.getText()));
2020-10-01 17:42:03 +02:00
holder.comment_content.setText(commentSpan, TextView.BufferType.SPANNABLE);
2020-06-26 14:34:39 +02:00
2020-10-01 17:42:03 +02:00
holder.comment_content.setMovementMethod(LinkMovementMethod.getInstance());
2020-06-26 14:34:39 +02:00
2020-10-01 17:42:03 +02:00
holder.comment_account_displayname.setText(comment.getAccount().getDisplayName());
2020-06-26 14:34:39 +02:00
2020-10-15 15:16:41 +02:00
if( context instanceof PeertubeActivity && !comment.isReply()) {
2020-10-15 18:11:37 +02:00
holder.main_container.setOnClickListener(v -> ((PeertubeActivity) context).openCommentThread(comment, i));
holder.comment_content.setOnClickListener(v -> ((PeertubeActivity) context).openCommentThread(comment, i));
2020-10-15 15:16:41 +02:00
}
2020-10-14 19:11:53 +02:00
if( comment.getTotalReplies() > 0) {
holder.number_of_replies.setVisibility(View.VISIBLE);
holder.number_of_replies.setText(context.getResources().getQuantityString(R.plurals.number_of_replies, comment.getTotalReplies(), comment.getTotalReplies()));
} else {
holder.number_of_replies.setVisibility(View.GONE);
}
2020-09-25 18:58:04 +02:00
if (comment.getAccount() != null) {
2020-09-20 15:14:48 +02:00
Spannable wordtoSpan;
Pattern hashAcct;
2020-09-25 18:58:04 +02:00
wordtoSpan = new SpannableString("@" + comment.getAccount().getAcct());
hashAcct = Pattern.compile("(@" + comment.getAccount().getAcct() + ")");
2020-09-20 15:14:48 +02:00
Matcher matcherAcct = hashAcct.matcher(wordtoSpan);
while (matcherAcct.find()) {
int matchStart = matcherAcct.start(1);
int matchEnd = matcherAcct.end();
if (wordtoSpan.length() >= matchEnd && matchStart < matchEnd) {
wordtoSpan.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, android.R.color.darker_gray)), matchStart, matchEnd, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
}
2020-10-01 17:42:03 +02:00
holder.comment_account_username.setText(wordtoSpan);
2020-06-26 14:34:39 +02:00
}
2020-10-01 17:42:03 +02:00
holder.comment_date.setText(Helper.dateDiff(context, comment.getCreatedAt()));
2020-06-26 14:34:39 +02:00
2020-10-01 17:42:03 +02:00
Helper.loadGiF(context, comment.getAccount().getAvatar() != null ? comment.getAccount().getAvatar().getPath() : null, holder.comment_account_profile);
2020-10-15 18:11:37 +02:00
if(comment.isReply()) {
holder.replyButton.setVisibility(View.VISIBLE);
}else{
holder.replyButton.setVisibility(View.GONE);
}
if( comment.isReply() && comment.isReplyViewOpen()) {
holder.write_comment_container_reply.setVisibility(View.VISIBLE);
}else{
holder.write_comment_container_reply.setVisibility(View.GONE);
}
2020-10-15 18:37:50 +02:00
if( holder.add_comment_write_reply.getText() == null || holder.add_comment_write_reply.getText().toString().trim().length() == 0) {
holder.add_comment_write_reply.setText(String.format("@%s ", comment.getAccount().getAcct()));
holder.add_comment_write_reply.setSelection(holder.add_comment_write_reply.getText().length());
}
2020-10-15 18:11:37 +02:00
holder.replyButton.setOnClickListener(v->{
comment.setReplyViewOpen(!comment.isReplyViewOpen());
notifyItemChanged(i);
});
holder.send_reply.setOnClickListener(null);
holder.send_reply.setOnClickListener(v -> {
if (isLoggedIn(context)) {
String commentView = holder.add_comment_write_reply.getText().toString();
if (commentView.trim().length() > 0) {
PostActionsVM viewModelComment = new ViewModelProvider((ViewModelStoreOwner) context).get(PostActionsVM.class);
viewModelComment.comment(REPLY, comment.getVideoId(), comment.getId(), commentView).observe((LifecycleOwner) context, apiResponse1 -> manageVIewPostActions(REPLY, (int)holder.main_container.getTag(), apiResponse1));
holder.add_comment_write_reply.setText("");
comment.setReplyViewOpen(false);
notifyItemChanged(i);
}
}
});
2020-06-26 14:34:39 +02:00
}
2020-10-15 18:11:37 +02:00
public void manageVIewPostActions(RetrofitPeertubeAPI.ActionType statusAction, int i, APIResponse apiResponse) {
2020-06-26 14:34:39 +02:00
2020-09-08 10:11:11 +02:00
if (apiResponse.getError() != null) {
Toasty.error(context, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
2020-06-26 14:34:39 +02:00
return;
}
2020-09-25 18:58:04 +02:00
if (statusAction == RetrofitPeertubeAPI.ActionType.PEERTUBEDELETECOMMENT) {
2020-06-26 14:34:39 +02:00
int position = 0;
2020-09-25 18:58:04 +02:00
for (Comment comment : comments) {
if (comment.getId().equals(apiResponse.getTargetedId())) {
comments.remove(comment);
commentListAdapter.notifyItemRemoved(position);
2020-06-26 14:34:39 +02:00
break;
}
position++;
}
2020-10-15 18:11:37 +02:00
} else if (statusAction == REPLY) {
if( apiResponse.getComments() != null && apiResponse.getComments().size() > 0 ) {
comments.add(i+1, apiResponse.getComments().get(0));
notifyItemInserted(i+1);
}
}else if (statusAction == RetrofitPeertubeAPI.ActionType.REPORT_COMMENT) {
2020-10-01 17:42:03 +02:00
Toasty.success(context, context.getString(R.string.successful_report_comment), Toasty.LENGTH_LONG).show();
2020-10-13 17:50:52 +02:00
}else if (statusAction == MUTE) {
Toasty.info(context, context.getString(R.string.muted_done), Toast.LENGTH_LONG).show();
2020-06-26 14:34:39 +02:00
}
}
2020-10-01 17:42:03 +02:00
private void reportComment(Comment comment) {
androidx.appcompat.app.AlertDialog.Builder dialogBuilder = new androidx.appcompat.app.AlertDialog.Builder(context);
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
View dialogView = inflater.inflate(R.layout.popup_report, new LinearLayout(context), false);
dialogBuilder.setView(dialogView);
EditText report_content = dialogView.findViewById(R.id.report_content);
dialogBuilder.setNeutralButton(R.string.cancel, (dialog, id) -> dialog.dismiss());
dialogBuilder.setPositiveButton(R.string.report, (dialog, id) -> {
if (report_content.getText().toString().trim().length() == 0) {
Toasty.info(context, context.getString(R.string.report_comment_size), Toasty.LENGTH_LONG).show();
} else {
PostActionsVM viewModel = new ViewModelProvider((ViewModelStoreOwner) context).get(PostActionsVM.class);
Report report = new Report();
Report.CommentReport commentReport = new Report.CommentReport();
commentReport.setId(comment.getId());
report.setComment(commentReport);
report.setReason(report_content.getText().toString());
2020-10-15 18:11:37 +02:00
viewModel.report(report).observe((LifecycleOwner) context, apiResponse -> manageVIewPostActions(RetrofitPeertubeAPI.ActionType.REPORT_COMMENT, 0, apiResponse));
2020-10-01 17:42:03 +02:00
dialog.dismiss();
}
});
androidx.appcompat.app.AlertDialog alertDialog2 = dialogBuilder.create();
alertDialog2.show();
}
public interface AllCommentRemoved {
void onAllCommentRemoved();
}
2020-06-26 14:34:39 +02:00
static class ViewHolder extends RecyclerView.ViewHolder {
2020-10-01 17:42:03 +02:00
TextView comment_content;
TextView comment_account_username;
TextView comment_account_displayname;
2020-10-15 18:37:50 +02:00
ImageView comment_account_profile, send_reply;
2020-10-15 18:11:37 +02:00
TextView comment_date, replyButton;
2020-06-26 14:34:39 +02:00
LinearLayout main_container;
2020-10-14 19:11:53 +02:00
TextView more_actions, number_of_replies;
2020-10-15 18:11:37 +02:00
ExtendedEditText add_comment_write_reply;
ConstraintLayout write_comment_container_reply;
2020-06-26 14:34:39 +02:00
@SuppressLint("SetJavaScriptEnabled")
ViewHolder(View itemView) {
super(itemView);
2020-10-01 17:42:03 +02:00
comment_content = itemView.findViewById(R.id.comment_content);
comment_account_username = itemView.findViewById(R.id.comment_account_username);
comment_account_profile = itemView.findViewById(R.id.comment_account_profile);
comment_account_displayname = itemView.findViewById(R.id.comment_account_displayname);
comment_date = itemView.findViewById(R.id.comment_date);
2020-06-26 14:34:39 +02:00
main_container = itemView.findViewById(R.id.main_container);
2020-10-01 17:42:03 +02:00
more_actions = itemView.findViewById(R.id.more_actions);
2020-10-14 19:11:53 +02:00
number_of_replies = itemView.findViewById(R.id.number_of_replies);
2020-10-15 18:11:37 +02:00
add_comment_write_reply = itemView.findViewById(R.id.add_comment_write_reply);
send_reply = itemView.findViewById(R.id.send_reply);
replyButton = itemView.findViewById(R.id.replyButton);
write_comment_container_reply = itemView.findViewById(R.id.write_comment_container_reply);
2020-06-26 14:34:39 +02:00
}
public View getView() {
return itemView;
}
}
}