From 1cd75f246211e18ee6b52acaef22b08513ec2543 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Thu, 29 Apr 2021 01:38:46 +0900 Subject: [PATCH] Popup to first fragment on tab reselect. Closes austinhuang0131/barinsta#1079 --- .../instagrabber/activities/MainActivity.java | 12 ------ .../fragments/main/FeedFragment.java | 2 + .../utils/NavigationExtensions.java | 37 +++++++++++++++++-- 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/activities/MainActivity.java b/app/src/main/java/awais/instagrabber/activities/MainActivity.java index 09cd5967..ad153f80 100644 --- a/app/src/main/java/awais/instagrabber/activities/MainActivity.java +++ b/app/src/main/java/awais/instagrabber/activities/MainActivity.java @@ -33,7 +33,6 @@ import androidx.core.app.NotificationManagerCompat; import androidx.core.provider.FontRequest; import androidx.emoji.text.EmojiCompat; import androidx.emoji.text.FontRequestEmojiCompatConfig; -import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.lifecycle.LiveData; import androidx.lifecycle.Observer; @@ -66,7 +65,6 @@ import awais.instagrabber.customviews.helpers.TextWatcherAdapter; import awais.instagrabber.databinding.ActivityMainBinding; import awais.instagrabber.fragments.PostViewV2Fragment; import awais.instagrabber.fragments.directmessages.DirectMessageInboxFragmentDirections; -import awais.instagrabber.fragments.main.FeedFragment; import awais.instagrabber.fragments.settings.PreferenceKeys; import awais.instagrabber.models.IntentModel; import awais.instagrabber.models.Tab; @@ -404,16 +402,6 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage firstFragmentGraphIndex); navControllerLiveData.observe(this, navController -> setupNavigation(binding.toolbar, navController)); currentNavControllerLiveData = navControllerLiveData; - binding.bottomNavView.setOnNavigationItemReselectedListener(item -> { - // Log.d(TAG, "setupBottomNavigationBar: item: " + item); - final Fragment navHostFragment = getSupportFragmentManager().findFragmentById(R.id.main_nav_host); - if (navHostFragment != null) { - final Fragment fragment = navHostFragment.getChildFragmentManager().getPrimaryNavigationFragment(); - if (fragment instanceof FeedFragment) { - ((FeedFragment) fragment).scrollToTop(); - } - } - }); } private void setSelectedTab(final List tabs) { diff --git a/app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java b/app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java index bfc1dee3..9baaded8 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/FeedFragment.java @@ -151,11 +151,13 @@ public class FeedFragment extends Fragment implements SwipeRefreshLayout.OnRefre @Override public void onNameClick(final Media feedModel, final View profilePicView) { + if (feedModel.getUser() == null) return; navigateToProfile("@" + feedModel.getUser().getUsername()); } @Override public void onProfilePicClick(final Media feedModel, final View profilePicView) { + if (feedModel.getUser() == null) return; navigateToProfile("@" + feedModel.getUser().getUsername()); } diff --git a/app/src/main/java/awais/instagrabber/utils/NavigationExtensions.java b/app/src/main/java/awais/instagrabber/utils/NavigationExtensions.java index d04e44a3..7663a59b 100644 --- a/app/src/main/java/awais/instagrabber/utils/NavigationExtensions.java +++ b/app/src/main/java/awais/instagrabber/utils/NavigationExtensions.java @@ -1,6 +1,8 @@ package awais.instagrabber.utils; +import android.annotation.SuppressLint; import android.content.Intent; +import android.util.Log; import android.util.SparseArray; import androidx.annotation.NonNull; @@ -10,6 +12,7 @@ import androidx.fragment.app.FragmentTransaction; import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; import androidx.navigation.NavController; +import androidx.navigation.NavDestination; import androidx.navigation.NavGraph; import androidx.navigation.fragment.NavHostFragment; @@ -18,13 +21,14 @@ import com.google.android.material.bottomnavigation.BottomNavigationView; import java.util.List; import awais.instagrabber.R; +import awais.instagrabber.fragments.main.FeedFragment; /** * This is a Java rewrite of NavigationExtensions * from architecture-components-samples. Some modifications have been done, check git history. */ public class NavigationExtensions { - + private static final String TAG = NavigationExtensions.class.getSimpleName(); private static String selectedItemTag; private static boolean isOnFirstFragment; @@ -105,7 +109,7 @@ public class NavigationExtensions { } return false; }); - // setupItemReselected(bottomNavigationView, graphIdToTagMap, fragmentManager); + setupItemReselected(bottomNavigationView, graphIdToTagMap, fragmentManager); setupDeepLinks(bottomNavigationView, navGraphIds, fragmentManager, containerId, intent); final int finalFirstFragmentGraphId = firstFragmentGraphId; fragmentManager.addOnBackStackChangedListener(() -> { @@ -160,6 +164,7 @@ public class NavigationExtensions { .commitNow(); } + @SuppressLint("RestrictedApi") private static void setupItemReselected(final BottomNavigationView bottomNavigationView, final SparseArray graphIdToTagMap, final FragmentManager fragmentManager) { @@ -168,12 +173,36 @@ public class NavigationExtensions { final Fragment fragmentByTag = fragmentManager.findFragmentByTag(newlySelectedItemTag); if (fragmentByTag == null) { return; - // throw new NullPointerException("null cannot be cast to non-null type NavHostFragment"); } final NavHostFragment selectedFragment = (NavHostFragment) fragmentByTag; final NavController navController = selectedFragment.getNavController(); final NavGraph navControllerGraph = navController.getGraph(); - navController.popBackStack(navControllerGraph.getStartDestination(), false); + final NavDestination currentDestination = navController.getCurrentDestination(); + final int startDestination = navControllerGraph.getStartDestination(); + int backStackSize = navController.getBackStack().size(); + if (currentDestination != null && backStackSize == 2 && currentDestination.getId() == startDestination) { + // scroll to top + final List fragments = selectedFragment.getChildFragmentManager().getFragments(); + if (fragments.isEmpty()) return; + final Fragment fragment = fragments.get(0); + if (fragment instanceof FeedFragment) { + ((FeedFragment) fragment).scrollToTop(); + } + return; + } + final boolean popped = navController.popBackStack(startDestination, false); + backStackSize = navController.getBackStack().size(); + if (!popped || backStackSize > 2) { + try { + // try loop pop + do { + navController.popBackStack(); + backStackSize = navController.getBackStack().size(); + } while (backStackSize > 2); + } catch (Exception e) { + Log.e(TAG, "setupItemReselected: ", e); + } + } }); }