scroll last comment into view

This commit is contained in:
TobiGr 2023-08-31 13:42:07 +02:00 committed by Stypox
parent 5f32d001cc
commit b4016c91c1
No known key found for this signature in database
GPG key ID: 4BDF1B40A49FDD23
5 changed files with 59 additions and 9 deletions

View file

@ -563,10 +563,7 @@ public class MainActivity extends AppCompatActivity {
// to show the top level comments again. // to show the top level comments again.
final FragmentManager.BackStackEntry bse = fm.getBackStackEntryAt( final FragmentManager.BackStackEntry bse = fm.getBackStackEntryAt(
fm.getBackStackEntryCount() - 2); // current fragment is at the top fm.getBackStackEntryCount() - 2); // current fragment is at the top
if (!CommentRepliesFragment.TAG.equals(bse.getName())) { openDetailFragmentFromCommentReplies(fm, bse);
BottomSheetBehavior.from(mainBinding.fragmentPlayerHolder)
.setState(BottomSheetBehavior.STATE_EXPANDED);
}
} }
} else { } else {
@ -652,10 +649,7 @@ public class MainActivity extends AppCompatActivity {
fm.popBackStackImmediate(); fm.popBackStackImmediate();
final FragmentManager.BackStackEntry bse = fm.getBackStackEntryAt( final FragmentManager.BackStackEntry bse = fm.getBackStackEntryAt(
fm.getBackStackEntryCount() - 1); fm.getBackStackEntryCount() - 1);
if (!CommentRepliesFragment.TAG.equals(bse.getName())) { openDetailFragmentFromCommentReplies(fm, bse);
BottomSheetBehavior.from(mainBinding.fragmentPlayerHolder)
.setState(BottomSheetBehavior.STATE_EXPANDED);
}
} else if (!NavigationHelper.tryGotoSearchFragment(fm)) { } else if (!NavigationHelper.tryGotoSearchFragment(fm)) {
// If search fragment wasn't found in the backstack go to the main fragment // If search fragment wasn't found in the backstack go to the main fragment
NavigationHelper.gotoMainFragment(fm); 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() { private boolean bottomSheetHiddenOrCollapsed() {
final BottomSheetBehavior<FrameLayout> bottomSheetBehavior = final BottomSheetBehavior<FrameLayout> bottomSheetBehavior =
BottomSheetBehavior.from(mainBinding.fragmentPlayerHolder); BottomSheetBehavior.from(mainBinding.fragmentPlayerHolder);

View file

@ -74,6 +74,7 @@ import org.schabi.newpipe.error.UserAction;
import org.schabi.newpipe.extractor.Image; import org.schabi.newpipe.extractor.Image;
import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.InfoItem;
import org.schabi.newpipe.extractor.NewPipe; 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.ContentNotSupportedException;
import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.stream.AudioStream; import org.schabi.newpipe.extractor.stream.AudioStream;
@ -1012,6 +1013,14 @@ public final class VideoDetailFragment
updateTabLayoutVisibility(); 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 // Play Utils
//////////////////////////////////////////////////////////////////////////*/ //////////////////////////////////////////////////////////////////////////*/

View file

@ -159,4 +159,10 @@ public final class CommentRepliesFragment
return ItemViewMode.LIST; return ItemViewMode.LIST;
} }
/**
* @return the comment to which the replies are shown
*/
public CommentsInfoItem getCommentsInfoItem() {
return commentsInfoItem;
}
} }

View file

@ -110,4 +110,8 @@ public class CommentsFragment extends BaseListInfoFragment<CommentsInfoItem, Com
protected ItemViewMode getItemViewMode() { protected ItemViewMode getItemViewMode() {
return ItemViewMode.LIST; return ItemViewMode.LIST;
} }
public void scrollToComment(final CommentsInfoItem comment) {
itemsList.scrollToPosition(infoListAdapter.getItemsList().indexOf(comment));
}
} }

View file

@ -504,7 +504,8 @@ public final class NavigationHelper {
public static void openCommentRepliesFragment(@NonNull final FragmentActivity activity, public static void openCommentRepliesFragment(@NonNull final FragmentActivity activity,
final CommentsInfoItem commentsInfoItem) { final CommentsInfoItem commentsInfoItem) {
defaultTransaction(activity.getSupportFragmentManager()) defaultTransaction(activity.getSupportFragmentManager())
.replace(R.id.fragment_holder, new CommentRepliesFragment(commentsInfoItem)) .replace(R.id.fragment_holder, new CommentRepliesFragment(commentsInfoItem),
CommentRepliesFragment.TAG)
.addToBackStack(CommentRepliesFragment.TAG) .addToBackStack(CommentRepliesFragment.TAG)
.commit(); .commit();
} }