From 6b221c463bb5c3f6ebb998d4f5672576aac50bc0 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Tue, 8 Sep 2020 19:46:21 +0900 Subject: [PATCH] Handle bottom nav and settings when logged out --- .../instagrabber/activities/MainActivity.java | 14 ++++++++++++-- .../settings/SettingsPreferencesFragment.java | 18 ++++++++++++------ .../menu/logged_out_bottom_navigation_menu.xml | 17 +++++++++++++++++ app/src/main/res/values/arrays.xml | 5 +++++ 4 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 app/src/main/res/menu/logged_out_bottom_navigation_menu.xml diff --git a/app/src/main/java/awais/instagrabber/activities/MainActivity.java b/app/src/main/java/awais/instagrabber/activities/MainActivity.java index 1017d65a..331a469e 100644 --- a/app/src/main/java/awais/instagrabber/activities/MainActivity.java +++ b/app/src/main/java/awais/instagrabber/activities/MainActivity.java @@ -89,6 +89,7 @@ public class MainActivity extends BaseLanguageActivity { private boolean showSearch = true; private Handler suggestionsFetchHandler; private int firstFragmentGraphIndex; + private boolean isLoggedIn; static { NAV_TO_MENU_ID_MAP.put(R.navigation.direct_messages_nav_graph, R.id.direct_messages_nav_graph); @@ -107,6 +108,7 @@ public class MainActivity extends BaseLanguageActivity { setContentView(binding.getRoot()); final Toolbar toolbar = binding.toolbar; setSupportActionBar(toolbar); + isLoggedIn = !Utils.isEmpty(cookie) && Utils.getUserIdFromCookie(cookie) != null; if (savedInstanceState == null) { setupBottomNavigationBar(true); } @@ -312,7 +314,13 @@ public class MainActivity extends BaseLanguageActivity { } private void setupBottomNavigationBar(final boolean setDefaultFromSettings) { - final TypedArray navIds = getResources().obtainTypedArray(R.array.main_nav_ids); + int main_nav_ids = R.array.main_nav_ids; + if (!isLoggedIn) { + main_nav_ids = R.array.logged_out_main_nav_ids; + binding.bottomNavView.getMenu().clear(); + binding.bottomNavView.inflateMenu(R.menu.logged_out_bottom_navigation_menu); + } + final TypedArray navIds = getResources().obtainTypedArray(main_nav_ids); final List mainNavList = new ArrayList<>(navIds.length()); final int length = navIds.length(); for (int i = 0; i < length; i++) { @@ -324,7 +332,9 @@ public class MainActivity extends BaseLanguageActivity { if (setDefaultFromSettings) { final String defaultTabIdString = settingsHelper.getString(Constants.DEFAULT_TAB); try { - final int defaultNavId = Utils.isEmpty(defaultTabIdString) ? R.navigation.profile_nav_graph : Integer.parseInt(defaultTabIdString); + final int defaultNavId = Utils.isEmpty(defaultTabIdString) || !isLoggedIn + ? R.navigation.profile_nav_graph + : Integer.parseInt(defaultTabIdString); final int index = mainNavList.indexOf(defaultNavId); if (index >= 0) { firstFragmentGraphIndex = index; diff --git a/app/src/main/java/awais/instagrabber/fragments/settings/SettingsPreferencesFragment.java b/app/src/main/java/awais/instagrabber/fragments/settings/SettingsPreferencesFragment.java index f80acb4d..abea091d 100644 --- a/app/src/main/java/awais/instagrabber/fragments/settings/SettingsPreferencesFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/settings/SettingsPreferencesFragment.java @@ -35,11 +35,14 @@ import static awais.instagrabber.utils.Utils.settingsHelper; public class SettingsPreferencesFragment extends BasePreferencesFragment { private static final String TAG = "SettingsPrefsFrag"; private static AppCompatTextView customPathTextView; + private boolean isLoggedIn; @Override public void onCreate(@Nullable final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); + final String cookie = settingsHelper.getString(Constants.COOKIE); + isLoggedIn = !Utils.isEmpty(cookie) && Utils.getUserIdFromCookie(cookie) != null; } @Override @@ -75,12 +78,14 @@ public class SettingsPreferencesFragment extends BasePreferencesFragment { localeCategory.addPreference(getLanguagePreference()); localeCategory.addPreference(getPostTimePreference()); - final PreferenceCategory loggedInUsersPreferenceCategory = new PreferenceCategory(requireContext()); - screen.addPreference(loggedInUsersPreferenceCategory); - loggedInUsersPreferenceCategory.setIconSpaceReserved(false); - loggedInUsersPreferenceCategory.setTitle(R.string.login_settings); - loggedInUsersPreferenceCategory.addPreference(getMarkStoriesSeenPreference()); - loggedInUsersPreferenceCategory.addPreference(getEnableActivityNotificationsPreference()); + if (isLoggedIn) { + final PreferenceCategory loggedInUsersPreferenceCategory = new PreferenceCategory(requireContext()); + screen.addPreference(loggedInUsersPreferenceCategory); + loggedInUsersPreferenceCategory.setIconSpaceReserved(false); + loggedInUsersPreferenceCategory.setTitle(R.string.login_settings); + loggedInUsersPreferenceCategory.addPreference(getMarkStoriesSeenPreference()); + loggedInUsersPreferenceCategory.addPreference(getEnableActivityNotificationsPreference()); + } final PreferenceCategory anonUsersPreferenceCategory = new PreferenceCategory(requireContext()); screen.addPreference(anonUsersPreferenceCategory); @@ -115,6 +120,7 @@ public class SettingsPreferencesFragment extends BasePreferencesFragment { private Preference getDefaultTabPreference() { final ListPreference preference = new ListPreference(requireContext()); + preference.setEnabled(isLoggedIn); preference.setSummaryProvider(ListPreference.SimpleSummaryProvider.getInstance()); final FragmentActivity activity = getActivity(); if (activity == null) { diff --git a/app/src/main/res/menu/logged_out_bottom_navigation_menu.xml b/app/src/main/res/menu/logged_out_bottom_navigation_menu.xml new file mode 100644 index 00000000..38ca76c4 --- /dev/null +++ b/app/src/main/res/menu/logged_out_bottom_navigation_menu.xml @@ -0,0 +1,17 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index d05fc96a..62f921fc 100755 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -107,4 +107,9 @@ @string/title_discover @string/more + + @navigation/profile_nav_graph + @navigation/discover_nav_graph + @navigation/more_nav_graph +