diff --git a/app/src/main/java/awais/instagrabber/activities/MainActivity.java b/app/src/main/java/awais/instagrabber/activities/MainActivity.java index e9d2265a..3ad1e974 100644 --- a/app/src/main/java/awais/instagrabber/activities/MainActivity.java +++ b/app/src/main/java/awais/instagrabber/activities/MainActivity.java @@ -2,11 +2,13 @@ package awais.instagrabber.activities; import android.annotation.SuppressLint; +import android.content.res.TypedArray; import android.database.MatrixCursor; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.provider.BaseColumns; +import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -29,7 +31,9 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Deque; +import java.util.HashMap; import java.util.List; +import java.util.Map; import awais.instagrabber.R; import awais.instagrabber.adapters.SuggestionsAdapter; @@ -68,7 +72,10 @@ public class MainActivity extends BaseLanguageActivity { R.id.commentsViewerFragment, R.id.followViewerFragment, R.id.directMessagesSettingsFragment); + private static final Map NAV_TO_MENU_ID_MAP = new HashMap<>(); private static final List REMOVE_COLLAPSING_TOOLBAR_SCROLL_DESTINATIONS = Collections.singletonList(R.id.commentsViewerFragment); + private static final String FIRST_FRAGMENT_GRAPH_INDEX_KEY = "firstFragmentGraphIndex"; + private ActivityMainBinding binding; private LiveData currentNavControllerLiveData; private MenuItem searchMenuItem; @@ -77,6 +84,15 @@ public class MainActivity extends BaseLanguageActivity { private SearchView searchView; private boolean showSearch = true; private Handler suggestionsFetchHandler; + private int firstFragmentGraphIndex; + + 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.feed_nav_graph, R.id.feed_nav_graph); + NAV_TO_MENU_ID_MAP.put(R.navigation.profile_nav_graph, R.id.profile_nav_graph); + NAV_TO_MENU_ID_MAP.put(R.navigation.discover_nav_graph, R.id.discover_nav_graph); + NAV_TO_MENU_ID_MAP.put(R.navigation.more_nav_graph, R.id.more_nav_graph); + } @Override protected void onCreate(@Nullable final Bundle savedInstanceState) { @@ -88,7 +104,7 @@ public class MainActivity extends BaseLanguageActivity { final Toolbar toolbar = binding.toolbar; setSupportActionBar(toolbar); if (savedInstanceState == null) { - setupBottomNavigationBar(); + setupBottomNavigationBar(true); } setupScrollingListener(); setupSuggestions(); @@ -255,21 +271,41 @@ public class MainActivity extends BaseLanguageActivity { return true; } - private void setupBottomNavigationBar() { - final List mainNavList = new ArrayList<>(Arrays.asList( - R.navigation.direct_messages_nav_graph, - R.navigation.feed_nav_graph, - R.navigation.profile_nav_graph, - R.navigation.discover_nav_graph, - R.navigation.more_nav_graph - )); + private void setupBottomNavigationBar(final boolean setDefaultFromSettings) { + final TypedArray navIds = getResources().obtainTypedArray(R.array.main_nav_ids); + final List mainNavList = new ArrayList<>(navIds.length()); + final int length = navIds.length(); + for (int i = 0; i < length; i++) { + final int resourceId = navIds.getResourceId(i, -1); + if (resourceId < 0) continue; + mainNavList.add(resourceId); + } + navIds.recycle(); + if (setDefaultFromSettings) { + final String defaultTabIdString = settingsHelper.getString(Constants.DEFAULT_TAB); + if (!Utils.isEmpty(defaultTabIdString)) { + try { + final int defaultNavId = Integer.parseInt(defaultTabIdString); + final int index = mainNavList.indexOf(defaultNavId); + if (index >= 0) { + firstFragmentGraphIndex = index; + final Integer menuId = NAV_TO_MENU_ID_MAP.get(defaultNavId); + if (menuId != null) { + binding.bottomNavView.setSelectedItemId(menuId); + } + } + } catch (NumberFormatException e) { + Log.e(TAG, "Error parsing id", e); + } + } + } final LiveData navControllerLiveData = setupWithNavController( binding.bottomNavView, mainNavList, getSupportFragmentManager(), R.id.main_nav_host, getIntent(), - 0); + firstFragmentGraphIndex); navControllerLiveData.observe(this, this::setupNavigation); currentNavControllerLiveData = navControllerLiveData; } @@ -335,10 +371,22 @@ public class MainActivity extends BaseLanguageActivity { binding.collapsingToolbarLayout.requestLayout(); } + @Override + protected void onSaveInstanceState(@NonNull final Bundle outState) { + outState.putString(FIRST_FRAGMENT_GRAPH_INDEX_KEY, String.valueOf(firstFragmentGraphIndex)); + super.onSaveInstanceState(outState); + } + @Override protected void onRestoreInstanceState(@NonNull final Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); - setupBottomNavigationBar(); + final String key = (String) savedInstanceState.get(FIRST_FRAGMENT_GRAPH_INDEX_KEY); + if (key != null) { + try { + firstFragmentGraphIndex = Integer.parseInt(key); + } catch (NumberFormatException ignored) { } + } + setupBottomNavigationBar(false); } @Override 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 f2d852f1..806a71e6 100644 --- a/app/src/main/java/awais/instagrabber/fragments/settings/SettingsPreferencesFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/settings/SettingsPreferencesFragment.java @@ -1,15 +1,15 @@ package awais.instagrabber.fragments.settings; import android.content.Context; +import android.content.res.TypedArray; import android.os.Bundle; -import android.view.Menu; -import android.view.MenuItem; import android.view.View; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.widget.AppCompatButton; import androidx.appcompat.widget.AppCompatTextView; +import androidx.fragment.app.FragmentActivity; import androidx.preference.DropDownPreference; import androidx.preference.ListPreference; import androidx.preference.Preference; @@ -45,6 +45,7 @@ public class SettingsPreferencesFragment extends BasePreferencesFragment { @Override void setupPreferenceScreen(final PreferenceScreen screen) { screen.addPreference(getLanguagePreference()); + screen.addPreference(getDefaultTabPreference()); screen.addPreference(getThemePreference()); screen.addPreference(getAmoledThemePreference()); screen.addPreference(getDownloadUserFolderPreference()); @@ -90,6 +91,34 @@ public class SettingsPreferencesFragment extends BasePreferencesFragment { return preference; } + private Preference getDefaultTabPreference() { + final DropDownPreference preference = new DropDownPreference(requireContext()); + preference.setSummaryProvider(ListPreference.SimpleSummaryProvider.getInstance()); + final FragmentActivity activity = getActivity(); + if (activity == null) { + return preference; + } + final TypedArray mainNavIds = getResources().obtainTypedArray(R.array.main_nav_ids); + final int length = mainNavIds.length(); + final String[] values = new String[length]; + for (int i = 0; i < length; i++) { + final int resourceId = mainNavIds.getResourceId(i, -1); + if (resourceId < 0) continue; + values[i] = String.valueOf(resourceId); + } + mainNavIds.recycle(); + preference.setKey(Constants.DEFAULT_TAB); + preference.setTitle(R.string.select_default_tab); + preference.setEntries(R.array.main_nav_ids_values); + preference.setEntryValues(values); + preference.setIconSpaceReserved(false); + preference.setOnPreferenceChangeListener((preference1, newValue) -> { + shouldRecreate(); + return true; + }); + return preference; + } + @NonNull private DropDownPreference getThemePreference() { final DropDownPreference preference = new DropDownPreference(requireContext()); @@ -199,7 +228,8 @@ public class SettingsPreferencesFragment extends BasePreferencesFragment { preference.setTitle(R.string.time_settings); preference.setIconSpaceReserved(false); preference.setOnPreferenceClickListener(preference1 -> { - new TimeSettingsDialog(settingsHelper.getBoolean(Constants.CUSTOM_DATE_TIME_FORMAT_ENABLED), + new TimeSettingsDialog( + settingsHelper.getBoolean(Constants.CUSTOM_DATE_TIME_FORMAT_ENABLED), settingsHelper.getString(Constants.CUSTOM_DATE_TIME_FORMAT), settingsHelper.getString(Constants.DATE_TIME_SELECTION), (isCustomFormat, diff --git a/app/src/main/java/awais/instagrabber/utils/Constants.java b/app/src/main/java/awais/instagrabber/utils/Constants.java index 43732957..b2ffd9f4 100755 --- a/app/src/main/java/awais/instagrabber/utils/Constants.java +++ b/app/src/main/java/awais/instagrabber/utils/Constants.java @@ -70,4 +70,5 @@ public final class Constants { public static final int LOGIN_RESULT_CODE = 5000; public static final String FDROID_SHA1_FINGERPRINT = "C1661EB8FD09F618307E687786D5E5056F65084D"; public static final String SKIPPED_VERSION = "skipped_version"; + public static final String DEFAULT_TAB = "default_tab"; } \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/utils/SettingsHelper.java b/app/src/main/java/awais/instagrabber/utils/SettingsHelper.java index 967f675b..b03e9a57 100755 --- a/app/src/main/java/awais/instagrabber/utils/SettingsHelper.java +++ b/app/src/main/java/awais/instagrabber/utils/SettingsHelper.java @@ -21,6 +21,7 @@ import static awais.instagrabber.utils.Constants.CUSTOM_DATE_TIME_FORMAT; import static awais.instagrabber.utils.Constants.CUSTOM_DATE_TIME_FORMAT_ENABLED; import static awais.instagrabber.utils.Constants.DATE_TIME_FORMAT; import static awais.instagrabber.utils.Constants.DATE_TIME_SELECTION; +import static awais.instagrabber.utils.Constants.DEFAULT_TAB; import static awais.instagrabber.utils.Constants.DEVICE_UUID; import static awais.instagrabber.utils.Constants.DM_MARK_AS_SEEN; import static awais.instagrabber.utils.Constants.DOWNLOAD_USER_FOLDER; @@ -120,7 +121,7 @@ public final class SettingsHelper { } @StringDef( - {APP_LANGUAGE, APP_THEME, COOKIE, FOLDER_PATH, DATE_TIME_FORMAT, DATE_TIME_SELECTION, CUSTOM_DATE_TIME_FORMAT, DEVICE_UUID, SKIPPED_VERSION}) + {APP_LANGUAGE, APP_THEME, COOKIE, FOLDER_PATH, DATE_TIME_FORMAT, DATE_TIME_SELECTION, CUSTOM_DATE_TIME_FORMAT, DEVICE_UUID, SKIPPED_VERSION, DEFAULT_TAB}) public @interface StringSettings {} @StringDef({DOWNLOAD_USER_FOLDER, BOTTOM_TOOLBAR, FOLDER_SAVE_TO, AUTOPLAY_VIDEOS, SHOW_QUICK_ACCESS_DIALOG, MUTED_VIDEOS, diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index edea6a66..d05fc96a 100755 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -93,4 +93,18 @@ HH:mm:ss H:mm:ss + + @navigation/direct_messages_nav_graph + @navigation/feed_nav_graph + @navigation/profile_nav_graph + @navigation/discover_nav_graph + @navigation/more_nav_graph + + + @string/title_dm + @string/feed + @string/profile + @string/title_discover + @string/more + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c852be1d..13265767 100755 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -47,14 +47,14 @@ Show user feed (Works only when user is logged in) Save to custom folder Select folder - Theme Settings + Theme Only affects logged-in users: Only affects anonymous users: Privacy Use Instadp for high definition profile pictures Use storiesig for stories and highlights from public users Import/Export - Select language + Language What to do? %s\nPosts %s\nFollowers @@ -98,7 +98,7 @@ Login Logout Send Debug Logs - Post Time Settings + Post time format Visit Project Page Join Telegram Group Join Matrix Room @@ -253,4 +253,5 @@ Mark as seen Do not show again until next update Version + Default tab \ No newline at end of file