|
44 | 44 | import android.widget.Spinner; |
45 | 45 |
|
46 | 46 | import androidx.annotation.NonNull; |
47 | | -import androidx.annotation.Nullable; |
48 | 47 | import androidx.appcompat.app.ActionBar; |
49 | 48 | import androidx.appcompat.app.ActionBarDrawerToggle; |
50 | 49 | import androidx.appcompat.app.AppCompatActivity; |
51 | 50 | import androidx.core.app.ActivityCompat; |
52 | 51 | import androidx.core.view.GravityCompat; |
53 | 52 | import androidx.drawerlayout.widget.DrawerLayout; |
54 | 53 | import androidx.fragment.app.Fragment; |
55 | | -import androidx.fragment.app.FragmentContainerView; |
56 | 54 | import androidx.fragment.app.FragmentManager; |
57 | 55 | import androidx.preference.PreferenceManager; |
58 | 56 |
|
|
66 | 64 | import org.schabi.newpipe.error.ErrorUtil; |
67 | 65 | import org.schabi.newpipe.extractor.NewPipe; |
68 | 66 | import org.schabi.newpipe.extractor.StreamingService; |
69 | | -import org.schabi.newpipe.extractor.comments.CommentsInfoItem; |
70 | 67 | import org.schabi.newpipe.extractor.exceptions.ExtractionException; |
71 | 68 | import org.schabi.newpipe.extractor.services.peertube.PeertubeInstance; |
72 | 69 | import org.schabi.newpipe.fragments.BackPressable; |
73 | 70 | import org.schabi.newpipe.fragments.MainFragment; |
74 | 71 | import org.schabi.newpipe.fragments.detail.VideoDetailFragment; |
75 | | -import org.schabi.newpipe.fragments.list.comments.CommentRepliesFragment; |
76 | 72 | import org.schabi.newpipe.fragments.list.search.SearchFragment; |
77 | 73 | import org.schabi.newpipe.local.feed.notifications.NotificationWorker; |
78 | 74 | import org.schabi.newpipe.player.Player; |
@@ -557,39 +553,27 @@ public void onBackPressed() { |
557 | 553 | // In case bottomSheet is not visible on the screen or collapsed we can assume that the user |
558 | 554 | // interacts with a fragment inside fragment_holder so all back presses should be |
559 | 555 | // handled by it |
| 556 | + final var fragmentManager = getSupportFragmentManager(); |
| 557 | + |
560 | 558 | if (bottomSheetHiddenOrCollapsed()) { |
561 | | - final FragmentManager fm = getSupportFragmentManager(); |
562 | | - final Fragment fragment = fm.findFragmentById(R.id.fragment_holder); |
| 559 | + final var fragment = fragmentManager.findFragmentById(R.id.fragment_holder); |
563 | 560 | // If current fragment implements BackPressable (i.e. can/wanna handle back press) |
564 | 561 | // delegate the back press to it |
565 | | - if (fragment instanceof BackPressable) { |
566 | | - if (((BackPressable) fragment).onBackPressed()) { |
567 | | - return; |
568 | | - } |
569 | | - } else if (fragment instanceof CommentRepliesFragment) { |
570 | | - // expand DetailsFragment if CommentRepliesFragment was opened |
571 | | - // to show the top level comments again |
572 | | - // Expand DetailsFragment if CommentRepliesFragment was opened |
573 | | - // and no other CommentRepliesFragments are on top of the back stack |
574 | | - // to show the top level comments again. |
575 | | - openDetailFragmentFromCommentReplies(fm, false); |
| 562 | + if (fragment instanceof BackPressable backPressable && backPressable.onBackPressed()) { |
| 563 | + return; |
576 | 564 | } |
577 | | - |
578 | 565 | } else { |
579 | | - final Fragment fragmentPlayer = getSupportFragmentManager() |
580 | | - .findFragmentById(R.id.fragment_player_holder); |
| 566 | + final var player = fragmentManager.findFragmentById(R.id.fragment_player_holder); |
581 | 567 | // If current fragment implements BackPressable (i.e. can/wanna handle back press) |
582 | 568 | // delegate the back press to it |
583 | | - if (fragmentPlayer instanceof BackPressable) { |
584 | | - if (!((BackPressable) fragmentPlayer).onBackPressed()) { |
585 | | - BottomSheetBehavior.from(mainBinding.fragmentPlayerHolder) |
586 | | - .setState(BottomSheetBehavior.STATE_COLLAPSED); |
587 | | - } |
| 569 | + if (player instanceof BackPressable backPressable && !backPressable.onBackPressed()) { |
| 570 | + BottomSheetBehavior.from(mainBinding.fragmentPlayerHolder) |
| 571 | + .setState(BottomSheetBehavior.STATE_COLLAPSED); |
588 | 572 | return; |
589 | 573 | } |
590 | 574 | } |
591 | 575 |
|
592 | | - if (getSupportFragmentManager().getBackStackEntryCount() == 1) { |
| 576 | + if (fragmentManager.getBackStackEntryCount() == 1) { |
593 | 577 | finish(); |
594 | 578 | } else { |
595 | 579 | super.onBackPressed(); |
@@ -648,15 +632,9 @@ public void onRequestPermissionsResult(final int requestCode, |
648 | 632 | * </pre> |
649 | 633 | */ |
650 | 634 | private void onHomeButtonPressed() { |
651 | | - final FragmentManager fm = getSupportFragmentManager(); |
652 | | - final Fragment fragment = fm.findFragmentById(R.id.fragment_holder); |
653 | | - |
654 | | - if (fragment instanceof CommentRepliesFragment) { |
655 | | - // Expand DetailsFragment if CommentRepliesFragment was opened |
656 | | - // and no other CommentRepliesFragments are on top of the back stack |
657 | | - // to show the top level comments again. |
658 | | - openDetailFragmentFromCommentReplies(fm, true); |
659 | | - } else if (!NavigationHelper.tryGotoSearchFragment(fm)) { |
| 635 | + final var fm = getSupportFragmentManager(); |
| 636 | + |
| 637 | + if (!NavigationHelper.tryGotoSearchFragment(fm)) { |
660 | 638 | // If search fragment wasn't found in the backstack go to the main fragment |
661 | 639 | NavigationHelper.gotoMainFragment(fm); |
662 | 640 | } |
@@ -854,68 +832,6 @@ public void onReceive(final Context context, final Intent intent) { |
854 | 832 | } |
855 | 833 | } |
856 | 834 |
|
857 | | - private void openDetailFragmentFromCommentReplies( |
858 | | - @NonNull final FragmentManager fm, |
859 | | - final boolean popBackStack |
860 | | - ) { |
861 | | - // obtain the name of the fragment under the replies fragment that's going to be popped |
862 | | - @Nullable final String fragmentUnderEntryName; |
863 | | - if (fm.getBackStackEntryCount() < 2) { |
864 | | - fragmentUnderEntryName = null; |
865 | | - } else { |
866 | | - fragmentUnderEntryName = fm.getBackStackEntryAt(fm.getBackStackEntryCount() - 2) |
867 | | - .getName(); |
868 | | - } |
869 | | - |
870 | | - // the root comment is the comment for which the user opened the replies page |
871 | | - @Nullable final CommentRepliesFragment repliesFragment = |
872 | | - (CommentRepliesFragment) fm.findFragmentByTag(CommentRepliesFragment.TAG); |
873 | | - @Nullable final CommentsInfoItem rootComment = |
874 | | - repliesFragment == null ? null : repliesFragment.getCommentsInfoItem(); |
875 | | - |
876 | | - // sometimes this function pops the backstack, other times it's handled by the system |
877 | | - if (popBackStack) { |
878 | | - fm.popBackStackImmediate(); |
879 | | - } |
880 | | - |
881 | | - // only expand the bottom sheet back if there are no more nested comment replies fragments |
882 | | - // stacked under the one that is currently being popped |
883 | | - if (CommentRepliesFragment.TAG.equals(fragmentUnderEntryName)) { |
884 | | - return; |
885 | | - } |
886 | | - |
887 | | - final BottomSheetBehavior<FragmentContainerView> behavior = BottomSheetBehavior |
888 | | - .from(mainBinding.fragmentPlayerHolder); |
889 | | - // do not return to the comment if the details fragment was closed |
890 | | - if (behavior.getState() == BottomSheetBehavior.STATE_HIDDEN) { |
891 | | - return; |
892 | | - } |
893 | | - |
894 | | - // scroll to the root comment once the bottom sheet expansion animation is finished |
895 | | - behavior.addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() { |
896 | | - @Override |
897 | | - public void onStateChanged(@NonNull final View bottomSheet, |
898 | | - final int newState) { |
899 | | - if (newState == BottomSheetBehavior.STATE_EXPANDED) { |
900 | | - final Fragment detailFragment = fm.findFragmentById( |
901 | | - R.id.fragment_player_holder); |
902 | | - if (detailFragment instanceof VideoDetailFragment && rootComment != null) { |
903 | | - // should always be the case |
904 | | - ((VideoDetailFragment) detailFragment).scrollToComment(rootComment); |
905 | | - } |
906 | | - behavior.removeBottomSheetCallback(this); |
907 | | - } |
908 | | - } |
909 | | - |
910 | | - @Override |
911 | | - public void onSlide(@NonNull final View bottomSheet, final float slideOffset) { |
912 | | - // not needed, listener is removed once the sheet is expanded |
913 | | - } |
914 | | - }); |
915 | | - |
916 | | - behavior.setState(BottomSheetBehavior.STATE_EXPANDED); |
917 | | - } |
918 | | - |
919 | 835 | private boolean bottomSheetHiddenOrCollapsed() { |
920 | 836 | final BottomSheetBehavior<FrameLayout> bottomSheetBehavior = |
921 | 837 | BottomSheetBehavior.from(mainBinding.fragmentPlayerHolder); |
|
0 commit comments