From b4016c91c11c4479263aa1e4ccd102c6fb23b7f4 Mon Sep 17 00:00:00 2001 From: TobiGr Date: Thu, 31 Aug 2023 13:42:07 +0200 Subject: [PATCH] scroll last comment into view --- .../java/org/schabi/newpipe/MainActivity.java | 46 +++++++++++++++---- .../fragments/detail/VideoDetailFragment.java | 9 ++++ .../list/comments/CommentRepliesFragment.java | 6 +++ .../list/comments/CommentsFragment.java | 4 ++ .../schabi/newpipe/util/NavigationHelper.java | 3 +- 5 files changed, 59 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/MainActivity.java b/app/src/main/java/org/schabi/newpipe/MainActivity.java index 300697697..5c56e867c 100644 --- a/app/src/main/java/org/schabi/newpipe/MainActivity.java +++ b/app/src/main/java/org/schabi/newpipe/MainActivity.java @@ -563,10 +563,7 @@ public class MainActivity extends AppCompatActivity { // to show the top level comments again. final FragmentManager.BackStackEntry bse = fm.getBackStackEntryAt( fm.getBackStackEntryCount() - 2); // current fragment is at the top - if (!CommentRepliesFragment.TAG.equals(bse.getName())) { - BottomSheetBehavior.from(mainBinding.fragmentPlayerHolder) - .setState(BottomSheetBehavior.STATE_EXPANDED); - } + openDetailFragmentFromCommentReplies(fm, bse); } } else { @@ -652,10 +649,7 @@ public class MainActivity extends AppCompatActivity { fm.popBackStackImmediate(); final FragmentManager.BackStackEntry bse = fm.getBackStackEntryAt( fm.getBackStackEntryCount() - 1); - if (!CommentRepliesFragment.TAG.equals(bse.getName())) { - BottomSheetBehavior.from(mainBinding.fragmentPlayerHolder) - .setState(BottomSheetBehavior.STATE_EXPANDED); - } + openDetailFragmentFromCommentReplies(fm, bse); } else if (!NavigationHelper.tryGotoSearchFragment(fm)) { // If search fragment wasn't found in the backstack go to the main fragment NavigationHelper.gotoMainFragment(fm); @@ -854,6 +848,42 @@ public class MainActivity extends AppCompatActivity { } } + private void openDetailFragmentFromCommentReplies( + @NonNull final FragmentManager fm, + @NonNull final FragmentManager.BackStackEntry bse) { + if (!CommentRepliesFragment.TAG.equals(bse.getName())) { + final CommentRepliesFragment commentRepliesFragment = + (CommentRepliesFragment) fm.findFragmentByTag( + CommentRepliesFragment.TAG); + final BottomSheetBehavior bsb = BottomSheetBehavior + .from(mainBinding.fragmentPlayerHolder); + bsb.addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() { + @Override + public void onStateChanged(@NonNull final View bottomSheet, + final int newState) { + if (newState == BottomSheetBehavior.STATE_EXPANDED) { + final Fragment detailFragment = fm.findFragmentById( + R.id.fragment_player_holder); + if (detailFragment instanceof VideoDetailFragment + && commentRepliesFragment != null) { + // should always be the case + ((VideoDetailFragment) detailFragment).scrollToComment( + commentRepliesFragment.getCommentsInfoItem()); + } + bsb.removeBottomSheetCallback(this); + } + } + + @Override + public void onSlide(@NonNull final View bottomSheet, + final float slideOffset) { + // not needed, listener is removed once the sheet is expanded + } + }); + bsb.setState(BottomSheetBehavior.STATE_EXPANDED); + } + } + private boolean bottomSheetHiddenOrCollapsed() { final BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(mainBinding.fragmentPlayerHolder); diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java index 7db5e0251..d607c5401 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java @@ -74,6 +74,7 @@ import org.schabi.newpipe.error.UserAction; import org.schabi.newpipe.extractor.Image; import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.NewPipe; +import org.schabi.newpipe.extractor.comments.CommentsInfoItem; import org.schabi.newpipe.extractor.exceptions.ContentNotSupportedException; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.stream.AudioStream; @@ -1012,6 +1013,14 @@ public final class VideoDetailFragment updateTabLayoutVisibility(); } + public void scrollToComment(final CommentsInfoItem comment) { + final Fragment fragment = pageAdapter.getItem( + pageAdapter.getItemPositionByTitle(COMMENTS_TAB_TAG)); + if (fragment instanceof CommentsFragment) { + ((CommentsFragment) fragment).scrollToComment(comment); + } + } + /*////////////////////////////////////////////////////////////////////////// // Play Utils //////////////////////////////////////////////////////////////////////////*/ 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 index b8d0f1e6c..62f97c727 100644 --- 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 @@ -159,4 +159,10 @@ public final class CommentRepliesFragment return ItemViewMode.LIST; } + /** + * @return the comment to which the replies are shown + */ + public CommentsInfoItem getCommentsInfoItem() { + return commentsInfoItem; + } } diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentsFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentsFragment.java index 5a5f84968..9481eba81 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentsFragment.java @@ -110,4 +110,8 @@ public class CommentsFragment extends BaseListInfoFragment