Skip to content

Commit

Permalink
scroll last comment into view
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiGr committed Aug 31, 2023
1 parent 0a2ed6d commit 81c4153
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 9 deletions.
46 changes: 38 additions & 8 deletions app/src/main/java/org/schabi/newpipe/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -563,10 +563,7 @@ public void onBackPressed() {
// 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 {
Expand Down Expand Up @@ -652,10 +649,7 @@ private void onHomeButtonPressed() {
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);
Expand Down Expand Up @@ -854,6 +848,42 @@ public void onReceive(final Context context, final Intent intent) {
}
}

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<FrameLayout> bottomSheetBehavior =
BottomSheetBehavior.from(mainBinding.fragmentPlayerHolder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.preference.PreferenceManager;

import com.google.android.exoplayer2.PlaybackException;
Expand All @@ -73,6 +74,7 @@
import org.schabi.newpipe.error.UserAction;
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;
Expand Down Expand Up @@ -994,6 +996,14 @@ public void scrollToTop() {
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
//////////////////////////////////////////////////////////////////////////*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,10 @@ protected ItemViewMode getItemViewMode() {
return ItemViewMode.LIST;
}

/**
* @return the comment to which the replies are shown
*/
public CommentsInfoItem getCommentsInfoItem() {
return commentsInfoItem;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,8 @@ public void onCreateOptionsMenu(@NonNull final Menu menu,
protected ItemViewMode getItemViewMode() {
return ItemViewMode.LIST;
}

public void scrollToComment(final CommentsInfoItem comment) {
itemsList.scrollToPosition(infoListAdapter.getItemsList().indexOf(comment));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,8 @@ public static void openCommentAuthorIfPresent(@NonNull final FragmentActivity ac
public static void openCommentRepliesFragment(@NonNull final FragmentActivity activity,
final CommentsInfoItem commentsInfoItem) {
defaultTransaction(activity.getSupportFragmentManager())
.replace(R.id.fragment_holder, new CommentRepliesFragment(commentsInfoItem))
.replace(R.id.fragment_holder, new CommentRepliesFragment(commentsInfoItem),
CommentRepliesFragment.TAG)
.addToBackStack(CommentRepliesFragment.TAG)
.commit();
}
Expand Down

0 comments on commit 81c4153

Please sign in to comment.