From f9494a294f4b31547d4bb664489d2503f1b5cfe8 Mon Sep 17 00:00:00 2001
From: Stypox <stypox@pm.me>
Date: Tue, 11 Apr 2023 22:28:48 +0200
Subject: [PATCH] Implement CommentRepliesFragment

---
 .../org/schabi/newpipe/error/UserAction.java  |  1 +
 .../list/comments/CommentRepliesFragment.java | 54 +++++++++++++++++++
 .../list/comments/CommentRepliesInfo.java     | 25 +++++++++
 .../holder/CommentInfoItemHolder.java         | 18 +++++--
 4 files changed, 95 insertions(+), 3 deletions(-)
 create mode 100644 app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentRepliesFragment.java
 create mode 100644 app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentRepliesInfo.java

diff --git a/app/src/main/java/org/schabi/newpipe/error/UserAction.java b/app/src/main/java/org/schabi/newpipe/error/UserAction.java
index 976173373..c8701cd77 100644
--- a/app/src/main/java/org/schabi/newpipe/error/UserAction.java
+++ b/app/src/main/java/org/schabi/newpipe/error/UserAction.java
@@ -19,6 +19,7 @@ public enum UserAction {
     REQUESTED_PLAYLIST("requested playlist"),
     REQUESTED_KIOSK("requested kiosk"),
     REQUESTED_COMMENTS("requested comments"),
+    REQUESTED_COMMENT_REPLIES("requested comment replies"),
     REQUESTED_FEED("requested feed"),
     REQUESTED_BOOKMARK("bookmark"),
     DELETE_FROM_HISTORY("delete from history"),
diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentRepliesFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentRepliesFragment.java
new file mode 100644
index 000000000..1a3508ff3
--- /dev/null
+++ b/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentRepliesFragment.java
@@ -0,0 +1,54 @@
+package org.schabi.newpipe.fragments.list.comments;
+
+import android.os.Bundle;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+
+import org.schabi.newpipe.R;
+import org.schabi.newpipe.error.UserAction;
+import org.schabi.newpipe.extractor.ListExtractor;
+import org.schabi.newpipe.extractor.comments.CommentsInfo;
+import org.schabi.newpipe.extractor.comments.CommentsInfoItem;
+import org.schabi.newpipe.fragments.list.BaseListInfoFragment;
+import org.schabi.newpipe.util.ExtractorHelper;
+
+import io.reactivex.rxjava3.core.Single;
+
+public final class CommentRepliesFragment
+        extends BaseListInfoFragment<CommentsInfoItem, CommentRepliesInfo> {
+
+    // has the same content as super.currentInfo, except that it's never null
+    private final CommentRepliesInfo currentInfo;
+    // the original comments info loaded alongside stream
+    private final CommentsInfo commentsInfo;
+
+    public CommentRepliesFragment(final CommentsInfo commentsInfo,
+                                  final CommentsInfoItem commentsInfoItem) {
+        super(UserAction.REQUESTED_COMMENT_REPLIES);
+        this.currentInfo = CommentRepliesInfo.getInfo(commentsInfoItem);
+        this.commentsInfo = commentsInfo;
+        setInitialData(commentsInfo.getServiceId(), commentsInfo.getUrl(), commentsInfo.getName());
+    }
+
+    @Nullable
+    @Override
+    public View onCreateView(@NonNull final LayoutInflater inflater,
+                             @Nullable final ViewGroup container,
+                             @Nullable final Bundle savedInstanceState) {
+        return inflater.inflate(R.layout.fragment_comments, container, false);
+    }
+
+    @Override
+    protected Single<CommentRepliesInfo> loadResult(final boolean forceLoad) {
+        return Single.just(this.currentInfo);
+    }
+
+    @Override
+    protected Single<ListExtractor.InfoItemsPage<CommentsInfoItem>> loadMoreItemsLogic() {
+        return ExtractorHelper.getMoreCommentItems(serviceId, commentsInfo, currentNextPage);
+    }
+}
diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentRepliesInfo.java b/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentRepliesInfo.java
new file mode 100644
index 000000000..25484296b
--- /dev/null
+++ b/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentRepliesInfo.java
@@ -0,0 +1,25 @@
+package org.schabi.newpipe.fragments.list.comments;
+
+import org.schabi.newpipe.extractor.ListInfo;
+import org.schabi.newpipe.extractor.comments.CommentsInfoItem;
+import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
+
+import java.util.Collections;
+
+public final class CommentRepliesInfo extends ListInfo<CommentsInfoItem> {
+    private CommentRepliesInfo(final int serviceId,
+                               final ListLinkHandler listUrlIdHandler,
+                               final String name) {
+        super(serviceId, listUrlIdHandler, name);
+    }
+
+    public static CommentRepliesInfo getInfo(final CommentsInfoItem comment) {
+        final ListLinkHandler handler =
+                new ListLinkHandler("", "", "", Collections.emptyList(), null);
+        final CommentRepliesInfo relatedItemInfo = new CommentRepliesInfo(
+                comment.getServiceId(), handler, comment.getName());
+        relatedItemInfo.setNextPage(comment.getReplies());
+        relatedItemInfo.setRelatedItems(Collections.emptyList()); // since it must be non-null
+        return relatedItemInfo;
+    }
+}
diff --git a/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentInfoItemHolder.java b/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentInfoItemHolder.java
index f1ac72e89..cbc7096bf 100644
--- a/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentInfoItemHolder.java
+++ b/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentInfoItemHolder.java
@@ -18,15 +18,18 @@ import androidx.annotation.Nullable;
 import androidx.appcompat.app.AppCompatActivity;
 import androidx.core.text.HtmlCompat;
 
+import org.schabi.newpipe.MainActivity;
 import org.schabi.newpipe.R;
 import org.schabi.newpipe.error.ErrorUtil;
 import org.schabi.newpipe.extractor.InfoItem;
 import org.schabi.newpipe.extractor.NewPipe;
 import org.schabi.newpipe.extractor.ServiceList;
 import org.schabi.newpipe.extractor.StreamingService;
+import org.schabi.newpipe.extractor.comments.CommentsInfo;
 import org.schabi.newpipe.extractor.comments.CommentsInfoItem;
 import org.schabi.newpipe.extractor.exceptions.ExtractionException;
 import org.schabi.newpipe.extractor.stream.Description;
+import org.schabi.newpipe.fragments.list.comments.CommentRepliesFragment;
 import org.schabi.newpipe.info_list.InfoItemBuilder;
 import org.schabi.newpipe.local.history.HistoryRecordManager;
 import org.schabi.newpipe.util.DeviceUtils;
@@ -145,7 +148,7 @@ public class CommentInfoItemHolder extends InfoItemHolder {
         itemHeartView.setVisibility(item.isHeartedByUploader() ? View.VISIBLE : View.GONE);
 
         final boolean hasReplies = item.getReplies() != null;
-        repliesButton.setOnClickListener(hasReplies ? (v) -> openRepliesFragment() : null);
+        repliesButton.setOnClickListener(hasReplies ? (v) -> openRepliesFragment(item) : null);
         repliesButton.setVisibility(hasReplies ? View.VISIBLE : View.GONE);
         repliesButton.setText(hasReplies
                 ? Localization.replyCount(itemBuilder.getContext(), item.getReplyCount()) : "");
@@ -303,7 +306,16 @@ public class CommentInfoItemHolder extends InfoItemHolder {
         }
     }
 
-    private void openRepliesFragment() {
-        // TODO
+    private void openRepliesFragment(final CommentsInfoItem commentsInfoItem) {
+        ((MainActivity) itemBuilder.getContext())
+                .getSupportFragmentManager()
+                .beginTransaction()
+                .setCustomAnimations(R.animator.custom_fade_in, R.animator.custom_fade_out,
+                        R.animator.custom_fade_in, R.animator.custom_fade_out)
+                .replace(R.id.fragment_holder,
+                        new CommentRepliesFragment((CommentsInfo) itemBuilder.getSourceListInfo(),
+                                commentsInfoItem))
+                .addToBackStack(null)
+                .commit();
     }
 }