Browse Source

Handle bottom nav and settings when logged out

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
6b221c463b
  1. 14
      app/src/main/java/awais/instagrabber/activities/MainActivity.java
  2. 18
      app/src/main/java/awais/instagrabber/fragments/settings/SettingsPreferencesFragment.java
  3. 17
      app/src/main/res/menu/logged_out_bottom_navigation_menu.xml
  4. 5
      app/src/main/res/values/arrays.xml

14
app/src/main/java/awais/instagrabber/activities/MainActivity.java

@ -89,6 +89,7 @@ public class MainActivity extends BaseLanguageActivity {
private boolean showSearch = true; private boolean showSearch = true;
private Handler suggestionsFetchHandler; private Handler suggestionsFetchHandler;
private int firstFragmentGraphIndex; private int firstFragmentGraphIndex;
private boolean isLoggedIn;
static { static {
NAV_TO_MENU_ID_MAP.put(R.navigation.direct_messages_nav_graph, R.id.direct_messages_nav_graph); 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()); setContentView(binding.getRoot());
final Toolbar toolbar = binding.toolbar; final Toolbar toolbar = binding.toolbar;
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
isLoggedIn = !Utils.isEmpty(cookie) && Utils.getUserIdFromCookie(cookie) != null;
if (savedInstanceState == null) { if (savedInstanceState == null) {
setupBottomNavigationBar(true); setupBottomNavigationBar(true);
} }
@ -312,7 +314,13 @@ public class MainActivity extends BaseLanguageActivity {
} }
private void setupBottomNavigationBar(final boolean setDefaultFromSettings) { 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<Integer> mainNavList = new ArrayList<>(navIds.length()); final List<Integer> mainNavList = new ArrayList<>(navIds.length());
final int length = navIds.length(); final int length = navIds.length();
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
@ -324,7 +332,9 @@ public class MainActivity extends BaseLanguageActivity {
if (setDefaultFromSettings) { if (setDefaultFromSettings) {
final String defaultTabIdString = settingsHelper.getString(Constants.DEFAULT_TAB); final String defaultTabIdString = settingsHelper.getString(Constants.DEFAULT_TAB);
try { 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); final int index = mainNavList.indexOf(defaultNavId);
if (index >= 0) { if (index >= 0) {
firstFragmentGraphIndex = index; firstFragmentGraphIndex = index;

18
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 { public class SettingsPreferencesFragment extends BasePreferencesFragment {
private static final String TAG = "SettingsPrefsFrag"; private static final String TAG = "SettingsPrefsFrag";
private static AppCompatTextView customPathTextView; private static AppCompatTextView customPathTextView;
private boolean isLoggedIn;
@Override @Override
public void onCreate(@Nullable final Bundle savedInstanceState) { public void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setHasOptionsMenu(true); setHasOptionsMenu(true);
final String cookie = settingsHelper.getString(Constants.COOKIE);
isLoggedIn = !Utils.isEmpty(cookie) && Utils.getUserIdFromCookie(cookie) != null;
} }
@Override @Override
@ -75,12 +78,14 @@ public class SettingsPreferencesFragment extends BasePreferencesFragment {
localeCategory.addPreference(getLanguagePreference()); localeCategory.addPreference(getLanguagePreference());
localeCategory.addPreference(getPostTimePreference()); 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()); final PreferenceCategory anonUsersPreferenceCategory = new PreferenceCategory(requireContext());
screen.addPreference(anonUsersPreferenceCategory); screen.addPreference(anonUsersPreferenceCategory);
@ -115,6 +120,7 @@ public class SettingsPreferencesFragment extends BasePreferencesFragment {
private Preference getDefaultTabPreference() { private Preference getDefaultTabPreference() {
final ListPreference preference = new ListPreference(requireContext()); final ListPreference preference = new ListPreference(requireContext());
preference.setEnabled(isLoggedIn);
preference.setSummaryProvider(ListPreference.SimpleSummaryProvider.getInstance()); preference.setSummaryProvider(ListPreference.SimpleSummaryProvider.getInstance());
final FragmentActivity activity = getActivity(); final FragmentActivity activity = getActivity();
if (activity == null) { if (activity == null) {

17
app/src/main/res/menu/logged_out_bottom_navigation_menu.xml

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/profile_nav_graph"
android:icon="@drawable/ic_profile"
android:title="@string/profile" />
<item
android:id="@+id/discover_nav_graph"
android:icon="@drawable/ic_discover"
android:title="@string/title_discover" />
<item
android:id="@+id/more_nav_graph"
android:icon="@drawable/ic_more_horiz_24"
android:title="@string/more" />
</menu>

5
app/src/main/res/values/arrays.xml

@ -107,4 +107,9 @@
<item>@string/title_discover</item> <item>@string/title_discover</item>
<item>@string/more</item> <item>@string/more</item>
</string-array> </string-array>
<array name="logged_out_main_nav_ids">
<item>@navigation/profile_nav_graph</item>
<item>@navigation/discover_nav_graph</item>
<item>@navigation/more_nav_graph</item>
</array>
</resources> </resources>
Loading…
Cancel
Save