1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-15 19:27:31 +00:00

Popup to first fragment on tab reselect. Closes austinhuang0131/barinsta#1079

This commit is contained in:
Ammar Githam 2021-04-29 01:38:46 +09:00
parent 8f4691a72a
commit 1cd75f2462
3 changed files with 35 additions and 16 deletions

View File

@ -33,7 +33,6 @@ import androidx.core.app.NotificationManagerCompat;
import androidx.core.provider.FontRequest; import androidx.core.provider.FontRequest;
import androidx.emoji.text.EmojiCompat; import androidx.emoji.text.EmojiCompat;
import androidx.emoji.text.FontRequestEmojiCompatConfig; import androidx.emoji.text.FontRequestEmojiCompatConfig;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer; import androidx.lifecycle.Observer;
@ -66,7 +65,6 @@ import awais.instagrabber.customviews.helpers.TextWatcherAdapter;
import awais.instagrabber.databinding.ActivityMainBinding; import awais.instagrabber.databinding.ActivityMainBinding;
import awais.instagrabber.fragments.PostViewV2Fragment; import awais.instagrabber.fragments.PostViewV2Fragment;
import awais.instagrabber.fragments.directmessages.DirectMessageInboxFragmentDirections; import awais.instagrabber.fragments.directmessages.DirectMessageInboxFragmentDirections;
import awais.instagrabber.fragments.main.FeedFragment;
import awais.instagrabber.fragments.settings.PreferenceKeys; import awais.instagrabber.fragments.settings.PreferenceKeys;
import awais.instagrabber.models.IntentModel; import awais.instagrabber.models.IntentModel;
import awais.instagrabber.models.Tab; import awais.instagrabber.models.Tab;
@ -404,16 +402,6 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
firstFragmentGraphIndex); firstFragmentGraphIndex);
navControllerLiveData.observe(this, navController -> setupNavigation(binding.toolbar, navController)); navControllerLiveData.observe(this, navController -> setupNavigation(binding.toolbar, navController));
currentNavControllerLiveData = navControllerLiveData; 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<Tab> tabs) { private void setSelectedTab(final List<Tab> tabs) {

View File

@ -151,11 +151,13 @@ public class FeedFragment extends Fragment implements SwipeRefreshLayout.OnRefre
@Override @Override
public void onNameClick(final Media feedModel, final View profilePicView) { public void onNameClick(final Media feedModel, final View profilePicView) {
if (feedModel.getUser() == null) return;
navigateToProfile("@" + feedModel.getUser().getUsername()); navigateToProfile("@" + feedModel.getUser().getUsername());
} }
@Override @Override
public void onProfilePicClick(final Media feedModel, final View profilePicView) { public void onProfilePicClick(final Media feedModel, final View profilePicView) {
if (feedModel.getUser() == null) return;
navigateToProfile("@" + feedModel.getUser().getUsername()); navigateToProfile("@" + feedModel.getUser().getUsername());
} }

View File

@ -1,6 +1,8 @@
package awais.instagrabber.utils; package awais.instagrabber.utils;
import android.annotation.SuppressLint;
import android.content.Intent; import android.content.Intent;
import android.util.Log;
import android.util.SparseArray; import android.util.SparseArray;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -10,6 +12,7 @@ import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import androidx.navigation.NavController; import androidx.navigation.NavController;
import androidx.navigation.NavDestination;
import androidx.navigation.NavGraph; import androidx.navigation.NavGraph;
import androidx.navigation.fragment.NavHostFragment; import androidx.navigation.fragment.NavHostFragment;
@ -18,13 +21,14 @@ import com.google.android.material.bottomnavigation.BottomNavigationView;
import java.util.List; import java.util.List;
import awais.instagrabber.R; import awais.instagrabber.R;
import awais.instagrabber.fragments.main.FeedFragment;
/** /**
* This is a Java rewrite of <a href="https://github.com/android/architecture-components-samples/blob/master/NavigationAdvancedSample/app/src/main/java/com/example/android/navigationadvancedsample/NavigationExtensions.kt">NavigationExtensions</a> * This is a Java rewrite of <a href="https://github.com/android/architecture-components-samples/blob/master/NavigationAdvancedSample/app/src/main/java/com/example/android/navigationadvancedsample/NavigationExtensions.kt">NavigationExtensions</a>
* from architecture-components-samples. Some modifications have been done, check git history. * from architecture-components-samples. Some modifications have been done, check git history.
*/ */
public class NavigationExtensions { public class NavigationExtensions {
private static final String TAG = NavigationExtensions.class.getSimpleName();
private static String selectedItemTag; private static String selectedItemTag;
private static boolean isOnFirstFragment; private static boolean isOnFirstFragment;
@ -105,7 +109,7 @@ public class NavigationExtensions {
} }
return false; return false;
}); });
// setupItemReselected(bottomNavigationView, graphIdToTagMap, fragmentManager); setupItemReselected(bottomNavigationView, graphIdToTagMap, fragmentManager);
setupDeepLinks(bottomNavigationView, navGraphIds, fragmentManager, containerId, intent); setupDeepLinks(bottomNavigationView, navGraphIds, fragmentManager, containerId, intent);
final int finalFirstFragmentGraphId = firstFragmentGraphId; final int finalFirstFragmentGraphId = firstFragmentGraphId;
fragmentManager.addOnBackStackChangedListener(() -> { fragmentManager.addOnBackStackChangedListener(() -> {
@ -160,6 +164,7 @@ public class NavigationExtensions {
.commitNow(); .commitNow();
} }
@SuppressLint("RestrictedApi")
private static void setupItemReselected(final BottomNavigationView bottomNavigationView, private static void setupItemReselected(final BottomNavigationView bottomNavigationView,
final SparseArray<String> graphIdToTagMap, final SparseArray<String> graphIdToTagMap,
final FragmentManager fragmentManager) { final FragmentManager fragmentManager) {
@ -168,12 +173,36 @@ public class NavigationExtensions {
final Fragment fragmentByTag = fragmentManager.findFragmentByTag(newlySelectedItemTag); final Fragment fragmentByTag = fragmentManager.findFragmentByTag(newlySelectedItemTag);
if (fragmentByTag == null) { if (fragmentByTag == null) {
return; return;
// throw new NullPointerException("null cannot be cast to non-null type NavHostFragment");
} }
final NavHostFragment selectedFragment = (NavHostFragment) fragmentByTag; final NavHostFragment selectedFragment = (NavHostFragment) fragmentByTag;
final NavController navController = selectedFragment.getNavController(); final NavController navController = selectedFragment.getNavController();
final NavGraph navControllerGraph = navController.getGraph(); 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<Fragment> 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);
}
}
}); });
} }