Get 'show bottom destinations' from tabs. Add Activity to list of available tabs.

This commit is contained in:
Ammar Githam 2021-03-25 01:23:33 +09:00
parent aa5c57e162
commit b80ae2fcfe
7 changed files with 75 additions and 32 deletions

View File

@ -53,6 +53,7 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterators; import com.google.common.collect.Iterators;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque; import java.util.Deque;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -92,14 +93,6 @@ import static awais.instagrabber.utils.Utils.settingsHelper;
public class MainActivity extends BaseLanguageActivity implements FragmentManager.OnBackStackChangedListener { public class MainActivity extends BaseLanguageActivity implements FragmentManager.OnBackStackChangedListener {
private static final String TAG = "MainActivity"; private static final String TAG = "MainActivity";
private static final List<Integer> SHOW_BOTTOM_VIEW_DESTINATIONS = ImmutableList.of(
R.id.directMessagesInboxFragment,
R.id.feedFragment,
R.id.profileFragment,
R.id.discoverFragment,
R.id.morePreferencesFragment,
R.id.favoritesFragment);
private static final String FIRST_FRAGMENT_GRAPH_INDEX_KEY = "firstFragmentGraphIndex"; private static final String FIRST_FRAGMENT_GRAPH_INDEX_KEY = "firstFragmentGraphIndex";
private ActivityMainBinding binding; private ActivityMainBinding binding;
@ -117,6 +110,7 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
private boolean isLoggedIn; private boolean isLoggedIn;
private HideBottomViewOnScrollBehavior<BottomNavigationView> behavior; private HideBottomViewOnScrollBehavior<BottomNavigationView> behavior;
private List<Tab> currentTabs; private List<Tab> currentTabs;
private List<Integer> showBottomViewDestinations = Collections.emptyList();
private final ServiceConnection serviceConnection = new ServiceConnection() { private final ServiceConnection serviceConnection = new ServiceConnection() {
@Override @Override
@ -470,6 +464,9 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
final List<Integer> mainNavList = currentTabs.stream() final List<Integer> mainNavList = currentTabs.stream()
.map(Tab::getNavigationResId) .map(Tab::getNavigationResId)
.collect(Collectors.toList()); .collect(Collectors.toList());
showBottomViewDestinations = currentTabs.stream()
.map(Tab::getStartDestinationFragmentId)
.collect(Collectors.toList());
if (setDefaultTabFromSettings) { if (setDefaultTabFromSettings) {
setSelectedTab(currentTabs); setSelectedTab(currentTabs);
} }
@ -522,16 +519,17 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
false, false,
"profile_nav_graph", "profile_nav_graph",
R.navigation.profile_nav_graph, R.navigation.profile_nav_graph,
R.id.profile_nav_graph); R.id.profile_nav_graph,
R.id.profileFragment);
final Tab moreTab = new Tab(R.drawable.ic_more_horiz_24, final Tab moreTab = new Tab(R.drawable.ic_more_horiz_24,
getString(R.string.more), getString(R.string.more),
false, false,
"more_nav_graph", "more_nav_graph",
R.navigation.more_nav_graph, R.navigation.more_nav_graph,
R.id.more_nav_graph); R.id.more_nav_graph,
R.id.morePreferencesFragment);
final Menu menu = binding.bottomNavView.getMenu(); final Menu menu = binding.bottomNavView.getMenu();
menu.clear(); menu.clear();
// binding.bottomNavView.inflateMenu(R.menu.logged_out_bottom_navigation_menu);
menu.add(0, profileTab.getNavigationRootId(), 0, profileTab.getTitle()).setIcon(profileTab.getIconResId()); menu.add(0, profileTab.getNavigationRootId(), 0, profileTab.getTitle()).setIcon(profileTab.getIconResId());
menu.add(0, moreTab.getNavigationRootId(), 0, moreTab.getTitle()).setIcon(moreTab.getIconResId()); menu.add(0, moreTab.getNavigationRootId(), 0, moreTab.getTitle()).setIcon(moreTab.getIconResId());
if (selectedItemId != R.id.profile_nav_graph && selectedItemId != R.id.more_nav_graph) { if (selectedItemId != R.id.profile_nav_graph && selectedItemId != R.id.more_nav_graph) {
@ -589,7 +587,7 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
final int destinationId = destination.getId(); final int destinationId = destination.getId();
@SuppressLint("RestrictedApi") final Deque<NavBackStackEntry> backStack = navController.getBackStack(); @SuppressLint("RestrictedApi") final Deque<NavBackStackEntry> backStack = navController.getBackStack();
setupMenu(backStack.size(), destinationId); setupMenu(backStack.size(), destinationId);
final boolean contains = SHOW_BOTTOM_VIEW_DESTINATIONS.contains(destinationId); final boolean contains = showBottomViewDestinations.contains(destinationId);
binding.bottomNavView.setVisibility(contains ? View.VISIBLE : View.GONE); binding.bottomNavView.setVisibility(contains ? View.VISIBLE : View.GONE);
if (contains && behavior != null) { if (contains && behavior != null) {
behavior.slideUp(binding.bottomNavView); behavior.slideUp(binding.bottomNavView);
@ -771,7 +769,7 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
final NavController navController = currentNavControllerLiveData.getValue(); final NavController navController = currentNavControllerLiveData.getValue();
if (navController == null) return; if (navController == null) return;
final Bundle bundle = new Bundle(); final Bundle bundle = new Bundle();
bundle.putLong("locationId", Long.valueOf(locationId)); bundle.putLong("locationId", Long.parseLong(locationId));
navController.navigate(R.id.action_global_locationFragment, bundle); navController.navigate(R.id.action_global_locationFragment, bundle);
} }
@ -882,4 +880,8 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
public List<Tab> getCurrentTabs() { public List<Tab> getCurrentTabs() {
return currentTabs; return currentTabs;
} }
public boolean isNavRootInCurrentTabs(@IdRes final int navRootId) {
return showBottomViewDestinations.stream().anyMatch(id -> id == navRootId);
}
} }

View File

@ -581,9 +581,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
} }
private void init() { private void init() {
disableDm = fragmentActivity.getCurrentTabs() disableDm = !fragmentActivity.isNavRootInCurrentTabs(R.id.directMessagesInboxFragment);
.stream()
.noneMatch(tab -> tab.getNavigationRootId() == R.id.direct_messages_nav_graph);
if (getArguments() != null) { if (getArguments() != null) {
final ProfileFragmentArgs fragmentArgs = ProfileFragmentArgs.fromBundle(getArguments()); final ProfileFragmentArgs fragmentArgs = ProfileFragmentArgs.fromBundle(getArguments());
username = fragmentArgs.getUsername(); username = fragmentArgs.getUsername();

View File

@ -56,6 +56,7 @@ public class MorePreferencesFragment extends BasePreferencesFragment {
void setupPreferenceScreen(final PreferenceScreen screen) { void setupPreferenceScreen(final PreferenceScreen screen) {
final String cookie = settingsHelper.getString(Constants.COOKIE); final String cookie = settingsHelper.getString(Constants.COOKIE);
final boolean isLoggedIn = !TextUtils.isEmpty(cookie) && CookieUtils.getUserIdFromCookie(cookie) > 0; final boolean isLoggedIn = !TextUtils.isEmpty(cookie) && CookieUtils.getUserIdFromCookie(cookie) > 0;
final MainActivity activity = (MainActivity) getActivity();
// screen.addPreference(new MoreHeaderPreference(getContext())); // screen.addPreference(new MoreHeaderPreference(getContext()));
final Context context = getContext(); final Context context = getContext();
if (context == null) return; if (context == null) return;
@ -136,13 +137,19 @@ public class MorePreferencesFragment extends BasePreferencesFragment {
screen.addPreference(getDivider(context)); screen.addPreference(getDivider(context));
final NavController navController = NavHostFragment.findNavController(this); final NavController navController = NavHostFragment.findNavController(this);
if (isLoggedIn) { if (isLoggedIn) {
screen.addPreference(getPreference(R.string.action_notif, R.drawable.ic_not_liked, preference -> { boolean showActivity = true;
if (isSafeToNavigate(navController)) { if (activity != null) {
final NavDirections navDirections = MorePreferencesFragmentDirections.actionGlobalNotificationsViewerFragment("notif"); showActivity = !activity.isNavRootInCurrentTabs(R.id.notificationsViewer);
navController.navigate(navDirections); }
} if (showActivity) {
return true; screen.addPreference(getPreference(R.string.action_notif, R.drawable.ic_not_liked, preference -> {
})); if (isSafeToNavigate(navController)) {
final NavDirections navDirections = MorePreferencesFragmentDirections.actionGlobalNotificationsViewerFragment("notif");
navController.navigate(navDirections);
}
return true;
}));
}
screen.addPreference(getPreference(R.string.action_ayml, R.drawable.ic_suggested_users, preference -> { screen.addPreference(getPreference(R.string.action_ayml, R.drawable.ic_suggested_users, preference -> {
if (isSafeToNavigate(navController)) { if (isSafeToNavigate(navController)) {
final NavDirections navDirections = MorePreferencesFragmentDirections.actionGlobalNotificationsViewerFragment("ayml"); final NavDirections navDirections = MorePreferencesFragmentDirections.actionGlobalNotificationsViewerFragment("ayml");
@ -161,11 +168,8 @@ public class MorePreferencesFragment extends BasePreferencesFragment {
// Check if favorites has been added as a tab. And if so, do not add in this list // Check if favorites has been added as a tab. And if so, do not add in this list
boolean showFavorites = true; boolean showFavorites = true;
final MainActivity activity = (MainActivity) getActivity(); if (activity != null) {
if (activity != null && activity.getCurrentTabs() != null) { showFavorites = !activity.isNavRootInCurrentTabs(R.id.favoritesFragment);
showFavorites = activity.getCurrentTabs()
.stream()
.noneMatch(tab -> tab.getNavigationRootId() == R.id.favorites_nav_graph);
} }
if (showFavorites) { if (showFavorites) {
screen.addPreference(getPreference(R.string.title_favorites, R.drawable.ic_star_24, preference -> { screen.addPreference(getPreference(R.string.title_favorites, R.drawable.ic_star_24, preference -> {

View File

@ -30,18 +30,25 @@ public class Tab {
*/ */
private final int navigationRootId; private final int navigationRootId;
/**
* This is the start destination of the nav graph
*/
private final int startDestinationFragmentId;
public Tab(@DrawableRes final int iconResId, public Tab(@DrawableRes final int iconResId,
@NonNull final String title, @NonNull final String title,
final boolean removable, final boolean removable,
@NonNull final String graphName, @NonNull final String graphName,
@NavigationRes final int navigationResId, @NavigationRes final int navigationResId,
@IdRes final int navigationRootId) { @IdRes final int navigationRootId,
@IdRes final int startDestinationFragmentId) {
this.iconResId = iconResId; this.iconResId = iconResId;
this.title = title; this.title = title;
this.removable = removable; this.removable = removable;
this.graphName = graphName; this.graphName = graphName;
this.navigationResId = navigationResId; this.navigationResId = navigationResId;
this.navigationRootId = navigationRootId; this.navigationRootId = navigationRootId;
this.startDestinationFragmentId = startDestinationFragmentId;
} }
public int getIconResId() { public int getIconResId() {
@ -68,6 +75,10 @@ public class Tab {
return navigationRootId; return navigationRootId;
} }
public int getStartDestinationFragmentId() {
return startDestinationFragmentId;
}
@Override @Override
public boolean equals(final Object o) { public boolean equals(final Object o) {
if (this == o) return true; if (this == o) return true;
@ -77,13 +88,14 @@ public class Tab {
removable == tab.removable && removable == tab.removable &&
navigationResId == tab.navigationResId && navigationResId == tab.navigationResId &&
navigationRootId == tab.navigationRootId && navigationRootId == tab.navigationRootId &&
startDestinationFragmentId == tab.startDestinationFragmentId &&
Objects.equals(title, tab.title) && Objects.equals(title, tab.title) &&
Objects.equals(graphName, tab.graphName); Objects.equals(graphName, tab.graphName);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(iconResId, title, removable, graphName, navigationResId, navigationRootId); return Objects.hash(iconResId, title, removable, graphName, navigationResId, navigationRootId, startDestinationFragmentId);
} }
@NonNull @NonNull

View File

@ -425,6 +425,16 @@ public final class Utils {
} }
typedArray.recycle(); typedArray.recycle();
typedArray = resources.obtainTypedArray(R.array.main_nav_start_dest_frag_ids);
length = typedArray.length();
final int[] startDestFragIds = new int[length];
for (int i = 0; i < length; i++) {
final int resourceId = typedArray.getResourceId(i, 0);
if (resourceId == 0) continue;
startDestFragIds[i] = resourceId;
}
typedArray.recycle();
final List<String> currentOrderGraphNames = getCurrentOrderOfGraphNamesFromPref(navGraphNames); final List<String> currentOrderGraphNames = getCurrentOrderOfGraphNamesFromPref(navGraphNames);
if (titleArray.length != iconIds.length || titleArray.length != navGraphNames.length) { if (titleArray.length != iconIds.length || titleArray.length != navGraphNames.length) {
@ -441,7 +451,8 @@ public final class Utils {
!NON_REMOVABLE_NAV_ROOT_IDS.contains(navRootId), !NON_REMOVABLE_NAV_ROOT_IDS.contains(navRootId),
navGraphName, navGraphName,
navigationResIds[i], navigationResIds[i],
navRootId); navRootId,
startDestFragIds[i]);
if (!currentOrderGraphNames.contains(navGraphName)) { if (!currentOrderGraphNames.contains(navGraphName)) {
otherTabs.add(tab); otherTabs.add(tab);
continue; continue;

View File

@ -13,7 +13,8 @@
<argument <argument
android:name="type" android:name="type"
app:argType="string" app:argType="string"
app:nullable="false" /> app:nullable="false"
android:defaultValue="notif"/>
<argument <argument
android:name="targetId" android:name="targetId"
android:defaultValue="0L" android:defaultValue="0L"

View File

@ -96,6 +96,7 @@
<item>@id/more_nav_graph</item> <item>@id/more_nav_graph</item>
<!-- New graphs should go below --> <!-- New graphs should go below -->
<item>@id/favorites_nav_graph</item> <item>@id/favorites_nav_graph</item>
<item>@id/notification_viewer_nav_graph</item>
</array> </array>
<!-- Nav graphs should correspond 1-to-1 with the above nav graph ids --> <!-- Nav graphs should correspond 1-to-1 with the above nav graph ids -->
<array name="main_nav_graphs"> <array name="main_nav_graphs">
@ -105,6 +106,7 @@
<item>@navigation/discover_nav_graph</item> <item>@navigation/discover_nav_graph</item>
<item>@navigation/more_nav_graph</item> <item>@navigation/more_nav_graph</item>
<item>@navigation/favorites_nav_graph</item> <item>@navigation/favorites_nav_graph</item>
<item>@navigation/notification_viewer_nav_graph</item>
</array> </array>
<!-- Titles should correspond 1-to-1 with the above nav graphs --> <!-- Titles should correspond 1-to-1 with the above nav graphs -->
<string-array name="main_nav_titles" translatable="false"> <string-array name="main_nav_titles" translatable="false">
@ -114,6 +116,7 @@
<item>@string/title_discover</item> <item>@string/title_discover</item>
<item>@string/more</item> <item>@string/more</item>
<item>@string/title_favorites</item> <item>@string/title_favorites</item>
<item>@string/title_notifications</item>
</string-array> </string-array>
<!-- Drawable should correspond 1-to-1 with the above titles --> <!-- Drawable should correspond 1-to-1 with the above titles -->
<array name="main_nav_drawables" translatable="false"> <array name="main_nav_drawables" translatable="false">
@ -123,6 +126,18 @@
<item>@drawable/ic_explore_24</item> <item>@drawable/ic_explore_24</item>
<item>@drawable/ic_more_horiz_24</item> <item>@drawable/ic_more_horiz_24</item>
<item>@drawable/ic_star_24</item> <item>@drawable/ic_star_24</item>
<item>@drawable/ic_not_liked</item>
</array>
<!-- fragmentIds should correspond 1-to-1 with the above drawabled -->
<!-- these are the start destination of the corresponding nav graphs -->
<array name="main_nav_start_dest_frag_ids" translatable="false">
<item>@id/directMessagesInboxFragment</item>
<item>@id/feedFragment</item>
<item>@id/profileFragment</item>
<item>@id/discoverFragment</item>
<item>@id/morePreferencesFragment</item>
<item>@id/favoritesFragment</item>
<item>@id/notificationsViewer</item>
</array> </array>
<!--<array name="logged_out_main_nav_graphs">--> <!--<array name="logged_out_main_nav_graphs">-->
<!-- <item>@navigation/profile_nav_graph</item>--> <!-- <item>@navigation/profile_nav_graph</item>-->