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

397 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
2022-05-04 09:43:54 +02:00
import static app.fedilab.fedilabtube.client.RetrofitPeertubeAPI.ActionType.MUTE;
import static app.fedilab.fedilabtube.client.RetrofitPeertubeAPI.ActionType.REPLY;
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-10-16 18:03:41 +02:00
import android.content.Intent;
2020-09-25 18:58:04 +02:00
import android.os.Build;
2020-10-16 18:03:41 +02:00
import android.os.Bundle;
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.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-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;
2022-05-05 08:53:25 +02:00
import app.fedilab.fedilabtube.activities.PeertubeActivity;
2020-06-26 14:34:39 +02:00
import app.fedilab.fedilabtube.R;
2022-05-05 08:53:25 +02:00
import app.fedilab.fedilabtube.activities.ShowAccountActivity;
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;
2022-05-09 10:25:07 +02:00
import app.fedilab.fedilabtube.databinding.DrawerCommentBinding;
2020-11-09 18:48:10 +01:00
import app.fedilab.fedilabtube.helper.CommentDecorationHelper;
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-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-14 16:06:17 +02:00
private final List<Comment> comments;
private final CommentListAdapter commentListAdapter;
2020-11-11 10:34:28 +01:00
private final boolean isThread;
2020-12-18 14:31:36 +01:00
private final String instance;
private final boolean sepiaSearch;
2020-10-17 18:50:20 +02:00
public AllCommentRemoved allCommentRemoved;
2020-10-13 17:50:52 +02:00
boolean isVideoOwner;
2020-10-17 18:50:20 +02:00
private Context context;
2020-06-26 14:34:39 +02:00
2020-12-06 11:24:36 +01:00
public CommentListAdapter(List<Comment> comments, boolean isVideoOwner, boolean isThread, String instance, boolean sepiaSearch) {
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-11-09 18:48:10 +01:00
this.isThread = isThread;
2020-11-18 15:07:22 +01:00
this.instance = instance;
2020-12-06 11:24:36 +01:00
this.sepiaSearch = sepiaSearch;
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();
2020-11-25 15:08:38 +01:00
DrawerCommentBinding itemBinding = DrawerCommentBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
return new ViewHolder(itemBinding);
2020-06-26 14:34:39 +02:00
}
2020-11-26 14:16:44 +01:00
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-11-25 15:08:38 +01:00
holder.binding.mainContainer.setTag(i);
2020-06-26 14:34:39 +02:00
2020-11-27 11:38:36 +01:00
holder.binding.decorationContainer.removeAllViews();
2020-11-11 10:34:28 +01:00
if (comment.isReply()) {
2020-11-09 18:48:10 +01:00
int ident = CommentDecorationHelper.getIndentation(comment.getInReplyToCommentId(), comments);
2020-11-27 11:38:36 +01:00
for (int j = 0; j <= ident; j++) {
View view = new View(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(1, LinearLayout.LayoutParams.MATCH_PARENT);
params.setMargins((int) Helper.convertDpToPixel(5, context), 0, 0, 0);
view.setBackgroundResource(R.color.colorAccent);
view.setLayoutParams(params);
holder.binding.decorationContainer.addView(view, 0);
2020-11-26 14:16:44 +01:00
}
2020-11-09 18:48:10 +01:00
}
2020-11-25 15:08:38 +01:00
holder.binding.moreActions.setOnClickListener(view -> {
PopupMenu popup = new PopupMenu(context, holder.binding.moreActions);
2020-10-01 17:42:03 +02:00
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-17 18:50:20 +02:00
} else {
2020-10-13 17:50:52 +02:00
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);
}
2020-10-17 18:50:20 +02:00
if (!isVideoOwner) {
2020-10-13 17:50:52 +02:00
popup.getMenu().findItem(R.id.action_remove_comments).setVisible(false);
}
2020-10-17 18:50:20 +02:00
if (!Helper.isLoggedIn(context)) {
2020-10-13 17:50:52 +02:00
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();
});
2020-11-25 15:08:38 +01:00
holder.binding.commentContent.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-11-25 15:08:38 +01:00
holder.binding.commentContent.setText(commentSpan, TextView.BufferType.SPANNABLE);
2020-06-26 14:34:39 +02:00
2020-11-25 15:08:38 +01:00
holder.binding.commentContent.setMovementMethod(LinkMovementMethod.getInstance());
2020-06-26 14:34:39 +02:00
2020-11-25 15:08:38 +01:00
holder.binding.commentAccountDisplayname.setText(comment.getAccount().getDisplayName());
2020-06-26 14:34:39 +02:00
2020-11-09 18:48:10 +01:00
if (context instanceof PeertubeActivity && !isThread) {
2020-11-25 15:08:38 +01:00
holder.binding.mainContainer.setOnClickListener(v -> ((PeertubeActivity) context).openCommentThread(comment));
holder.binding.commentContent.setOnClickListener(v -> ((PeertubeActivity) context).openCommentThread(comment));
holder.binding.replyButton.setOnClickListener(v -> ((PeertubeActivity) context).openCommentThread(comment));
2021-01-02 18:28:13 +01:00
} else if (context instanceof PeertubeActivity) {
holder.binding.replyButton.setOnClickListener(v -> ((PeertubeActivity) context).openPostComment(comment, i));
2020-10-15 15:16:41 +02:00
}
2020-10-17 18:50:20 +02:00
if (comment.getTotalReplies() > 0) {
2020-11-25 15:08:38 +01:00
holder.binding.numberOfReplies.setVisibility(View.VISIBLE);
holder.binding.numberOfReplies.setText(context.getResources().getQuantityString(R.plurals.number_of_replies, comment.getTotalReplies(), comment.getTotalReplies()));
2020-10-14 19:11:53 +02:00
} else {
2020-11-25 15:08:38 +01:00
holder.binding.numberOfReplies.setVisibility(View.GONE);
2020-10-14 19:11:53 +02:00
}
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-11-25 15:08:38 +01:00
holder.binding.commentAccountUsername.setText(wordtoSpan);
2020-06-26 14:34:39 +02:00
}
2020-11-25 15:08:38 +01:00
holder.binding.commentDate.setText(Helper.dateDiff(context, comment.getCreatedAt()));
2020-06-26 14:34:39 +02:00
2020-11-18 15:07:22 +01:00
String avatarUrl;
if (instance != null) {
if (comment.getAccount().getAvatar() != null && comment.getAccount().getAvatar().getPath().startsWith("http")) {
avatarUrl = comment.getAccount().getAvatar().getPath();
} else {
avatarUrl = comment.getAccount().getAvatar() != null ? "https://" + instance + comment.getAccount().getAvatar().getPath() : null;
}
2020-11-18 15:07:22 +01:00
} else {
avatarUrl = comment.getAccount().getAvatar() != null ? comment.getAccount().getAvatar().getPath() : null;
}
2020-11-25 15:08:38 +01:00
Helper.loadGiF(context, avatarUrl, holder.binding.commentAccountProfile);
2020-10-15 18:11:37 +02:00
2020-11-25 15:08:38 +01:00
holder.binding.commentAccountProfile.setOnClickListener(v -> {
2020-10-16 18:03:41 +02:00
Bundle b = new Bundle();
Intent intent = new Intent(context, ShowAccountActivity.class);
b.putParcelable("account", comment.getAccount());
b.putString("accountAcct", comment.getAccount().getAcct());
intent.putExtras(b);
context.startActivity(intent);
});
2020-10-17 18:50:20 +02:00
if (comment.isReply()) {
2020-11-25 15:08:38 +01:00
holder.binding.replyButton.setVisibility(View.VISIBLE);
2020-10-17 18:50:20 +02:00
} else {
2020-11-25 15:08:38 +01:00
holder.binding.replyButton.setVisibility(View.GONE);
2020-10-15 18:11:37 +02:00
}
2020-11-11 10:34:28 +01:00
if (i == 0 && isThread) {
2020-11-25 15:08:38 +01:00
holder.binding.postReplyButton.setVisibility(View.VISIBLE);
2020-11-11 10:34:28 +01:00
} else {
2020-11-25 15:08:38 +01:00
holder.binding.postReplyButton.setVisibility(View.GONE);
2020-10-15 18:11:37 +02:00
}
2020-12-06 11:24:36 +01:00
holder.binding.postReplyButton.setOnClickListener(v -> {
if (Helper.canMakeAction(context) && !sepiaSearch) {
2020-12-06 11:24:36 +01:00
((PeertubeActivity) context).openPostComment(comment, i);
} else {
if (sepiaSearch) {
Toasty.info(context, context.getString(R.string.federation_issue), Toasty.LENGTH_SHORT).show();
} else {
Toasty.error(context, context.getString(R.string.not_logged_in), Toast.LENGTH_SHORT).show();
}
}
});
if (Helper.canMakeAction(context) && !sepiaSearch) {
2020-12-06 11:24:36 +01:00
holder.binding.replyButton.setVisibility(View.VISIBLE);
} else {
holder.binding.replyButton.setVisibility(View.GONE);
}
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) {
2020-10-17 18:50:20 +02:00
if (apiResponse.getComments() != null && apiResponse.getComments().size() > 0) {
comments.add(i + 1, apiResponse.getComments().get(0));
notifyItemInserted(i + 1);
2020-10-15 18:11:37 +02:00
}
2020-10-17 18:50:20 +02:00
} 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-17 18:50:20 +02:00
} else if (statusAction == MUTE) {
2020-10-13 17:50:52 +02:00
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
2020-11-25 15:08:38 +01:00
static class ViewHolder extends RecyclerView.ViewHolder {
DrawerCommentBinding binding;
2020-06-26 14:34:39 +02:00
2020-11-25 15:08:38 +01:00
ViewHolder(DrawerCommentBinding itemView) {
super(itemView.getRoot());
binding = itemView;
2020-06-26 14:34:39 +02:00
}
}
2020-11-25 15:08:38 +01:00
2020-06-26 14:34:39 +02:00
}