1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-22 22:57:29 +00:00

Add setting for choosing default tab

This commit is contained in:
Ammar Githam 2020-09-07 00:37:05 +09:00
parent c9d342471b
commit 17a5f978e8
6 changed files with 113 additions and 18 deletions

View File

@ -2,11 +2,13 @@ package awais.instagrabber.activities;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.res.TypedArray;
import android.database.MatrixCursor; import android.database.MatrixCursor;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.provider.BaseColumns; import android.provider.BaseColumns;
import android.util.Log;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
@ -29,7 +31,9 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Deque; import java.util.Deque;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import awais.instagrabber.R; import awais.instagrabber.R;
import awais.instagrabber.adapters.SuggestionsAdapter; import awais.instagrabber.adapters.SuggestionsAdapter;
@ -68,7 +72,10 @@ public class MainActivity extends BaseLanguageActivity {
R.id.commentsViewerFragment, R.id.commentsViewerFragment,
R.id.followViewerFragment, R.id.followViewerFragment,
R.id.directMessagesSettingsFragment); R.id.directMessagesSettingsFragment);
private static final Map<Integer, Integer> NAV_TO_MENU_ID_MAP = new HashMap<>();
private static final List<Integer> REMOVE_COLLAPSING_TOOLBAR_SCROLL_DESTINATIONS = Collections.singletonList(R.id.commentsViewerFragment); private static final List<Integer> REMOVE_COLLAPSING_TOOLBAR_SCROLL_DESTINATIONS = Collections.singletonList(R.id.commentsViewerFragment);
private static final String FIRST_FRAGMENT_GRAPH_INDEX_KEY = "firstFragmentGraphIndex";
private ActivityMainBinding binding; private ActivityMainBinding binding;
private LiveData<NavController> currentNavControllerLiveData; private LiveData<NavController> currentNavControllerLiveData;
private MenuItem searchMenuItem; private MenuItem searchMenuItem;
@ -77,6 +84,15 @@ public class MainActivity extends BaseLanguageActivity {
private SearchView searchView; private SearchView searchView;
private boolean showSearch = true; private boolean showSearch = true;
private Handler suggestionsFetchHandler; 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 @Override
protected void onCreate(@Nullable final Bundle savedInstanceState) { protected void onCreate(@Nullable final Bundle savedInstanceState) {
@ -88,7 +104,7 @@ public class MainActivity extends BaseLanguageActivity {
final Toolbar toolbar = binding.toolbar; final Toolbar toolbar = binding.toolbar;
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
if (savedInstanceState == null) { if (savedInstanceState == null) {
setupBottomNavigationBar(); setupBottomNavigationBar(true);
} }
setupScrollingListener(); setupScrollingListener();
setupSuggestions(); setupSuggestions();
@ -255,21 +271,41 @@ public class MainActivity extends BaseLanguageActivity {
return true; return true;
} }
private void setupBottomNavigationBar() { private void setupBottomNavigationBar(final boolean setDefaultFromSettings) {
final List<Integer> mainNavList = new ArrayList<>(Arrays.asList( final TypedArray navIds = getResources().obtainTypedArray(R.array.main_nav_ids);
R.navigation.direct_messages_nav_graph, final List<Integer> mainNavList = new ArrayList<>(navIds.length());
R.navigation.feed_nav_graph, final int length = navIds.length();
R.navigation.profile_nav_graph, for (int i = 0; i < length; i++) {
R.navigation.discover_nav_graph, final int resourceId = navIds.getResourceId(i, -1);
R.navigation.more_nav_graph 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<NavController> navControllerLiveData = setupWithNavController( final LiveData<NavController> navControllerLiveData = setupWithNavController(
binding.bottomNavView, binding.bottomNavView,
mainNavList, mainNavList,
getSupportFragmentManager(), getSupportFragmentManager(),
R.id.main_nav_host, R.id.main_nav_host,
getIntent(), getIntent(),
0); firstFragmentGraphIndex);
navControllerLiveData.observe(this, this::setupNavigation); navControllerLiveData.observe(this, this::setupNavigation);
currentNavControllerLiveData = navControllerLiveData; currentNavControllerLiveData = navControllerLiveData;
} }
@ -335,10 +371,22 @@ public class MainActivity extends BaseLanguageActivity {
binding.collapsingToolbarLayout.requestLayout(); 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 @Override
protected void onRestoreInstanceState(@NonNull final Bundle savedInstanceState) { protected void onRestoreInstanceState(@NonNull final Bundle savedInstanceState) {
super.onRestoreInstanceState(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 @Override

View File

@ -1,15 +1,15 @@
package awais.instagrabber.fragments.settings; package awais.instagrabber.fragments.settings;
import android.content.Context; import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle; import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatButton; import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatTextView; import androidx.appcompat.widget.AppCompatTextView;
import androidx.fragment.app.FragmentActivity;
import androidx.preference.DropDownPreference; import androidx.preference.DropDownPreference;
import androidx.preference.ListPreference; import androidx.preference.ListPreference;
import androidx.preference.Preference; import androidx.preference.Preference;
@ -45,6 +45,7 @@ public class SettingsPreferencesFragment extends BasePreferencesFragment {
@Override @Override
void setupPreferenceScreen(final PreferenceScreen screen) { void setupPreferenceScreen(final PreferenceScreen screen) {
screen.addPreference(getLanguagePreference()); screen.addPreference(getLanguagePreference());
screen.addPreference(getDefaultTabPreference());
screen.addPreference(getThemePreference()); screen.addPreference(getThemePreference());
screen.addPreference(getAmoledThemePreference()); screen.addPreference(getAmoledThemePreference());
screen.addPreference(getDownloadUserFolderPreference()); screen.addPreference(getDownloadUserFolderPreference());
@ -90,6 +91,34 @@ public class SettingsPreferencesFragment extends BasePreferencesFragment {
return preference; 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 @NonNull
private DropDownPreference getThemePreference() { private DropDownPreference getThemePreference() {
final DropDownPreference preference = new DropDownPreference(requireContext()); final DropDownPreference preference = new DropDownPreference(requireContext());
@ -199,7 +228,8 @@ public class SettingsPreferencesFragment extends BasePreferencesFragment {
preference.setTitle(R.string.time_settings); preference.setTitle(R.string.time_settings);
preference.setIconSpaceReserved(false); preference.setIconSpaceReserved(false);
preference.setOnPreferenceClickListener(preference1 -> { 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.CUSTOM_DATE_TIME_FORMAT),
settingsHelper.getString(Constants.DATE_TIME_SELECTION), settingsHelper.getString(Constants.DATE_TIME_SELECTION),
(isCustomFormat, (isCustomFormat,

View File

@ -70,4 +70,5 @@ public final class Constants {
public static final int LOGIN_RESULT_CODE = 5000; public static final int LOGIN_RESULT_CODE = 5000;
public static final String FDROID_SHA1_FINGERPRINT = "C1661EB8FD09F618307E687786D5E5056F65084D"; public static final String FDROID_SHA1_FINGERPRINT = "C1661EB8FD09F618307E687786D5E5056F65084D";
public static final String SKIPPED_VERSION = "skipped_version"; public static final String SKIPPED_VERSION = "skipped_version";
public static final String DEFAULT_TAB = "default_tab";
} }

View File

@ -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.CUSTOM_DATE_TIME_FORMAT_ENABLED;
import static awais.instagrabber.utils.Constants.DATE_TIME_FORMAT; import static awais.instagrabber.utils.Constants.DATE_TIME_FORMAT;
import static awais.instagrabber.utils.Constants.DATE_TIME_SELECTION; 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.DEVICE_UUID;
import static awais.instagrabber.utils.Constants.DM_MARK_AS_SEEN; import static awais.instagrabber.utils.Constants.DM_MARK_AS_SEEN;
import static awais.instagrabber.utils.Constants.DOWNLOAD_USER_FOLDER; import static awais.instagrabber.utils.Constants.DOWNLOAD_USER_FOLDER;
@ -120,7 +121,7 @@ public final class SettingsHelper {
} }
@StringDef( @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 {} public @interface StringSettings {}
@StringDef({DOWNLOAD_USER_FOLDER, BOTTOM_TOOLBAR, FOLDER_SAVE_TO, AUTOPLAY_VIDEOS, SHOW_QUICK_ACCESS_DIALOG, MUTED_VIDEOS, @StringDef({DOWNLOAD_USER_FOLDER, BOTTOM_TOOLBAR, FOLDER_SAVE_TO, AUTOPLAY_VIDEOS, SHOW_QUICK_ACCESS_DIALOG, MUTED_VIDEOS,

View File

@ -93,4 +93,18 @@
<item>HH:mm:ss</item> <item>HH:mm:ss</item>
<item>H:mm:ss</item> <item>H:mm:ss</item>
</string-array> </string-array>
<array name="main_nav_ids">
<item>@navigation/direct_messages_nav_graph</item>
<item>@navigation/feed_nav_graph</item>
<item>@navigation/profile_nav_graph</item>
<item>@navigation/discover_nav_graph</item>
<item>@navigation/more_nav_graph</item>
</array>
<string-array name="main_nav_ids_values">
<item>@string/title_dm</item>
<item>@string/feed</item>
<item>@string/profile</item>
<item>@string/title_discover</item>
<item>@string/more</item>
</string-array>
</resources> </resources>

View File

@ -47,14 +47,14 @@
<string name="show_feed">Show user feed (Works only when user is logged in)</string> <string name="show_feed">Show user feed (Works only when user is logged in)</string>
<string name="save_to_folder">Save to custom folder</string> <string name="save_to_folder">Save to custom folder</string>
<string name="select_folder">Select folder</string> <string name="select_folder">Select folder</string>
<string name="theme_settings">Theme Settings</string> <string name="theme_settings">Theme</string>
<string name="login_settings">Only affects logged-in users:</string> <string name="login_settings">Only affects logged-in users:</string>
<string name="anonymous_settings">Only affects anonymous users:</string> <string name="anonymous_settings">Only affects anonymous users:</string>
<string name="privacy_warning">Privacy</string> <string name="privacy_warning">Privacy</string>
<string name="instadp_settings">Use Instadp for high definition profile pictures</string> <string name="instadp_settings">Use Instadp for high definition profile pictures</string>
<string name="storiesig_settings">Use storiesig for stories and highlights from public users</string> <string name="storiesig_settings">Use storiesig for stories and highlights from public users</string>
<string name="import_export">Import/Export</string> <string name="import_export">Import/Export</string>
<string name="select_language">Select language</string> <string name="select_language">Language</string>
<string name="what_to_do_dialog">What to do?</string> <string name="what_to_do_dialog">What to do?</string>
<string name="main_posts_count">%s\nPosts</string> <string name="main_posts_count">%s\nPosts</string>
<string name="main_posts_followers">%s\nFollowers</string> <string name="main_posts_followers">%s\nFollowers</string>
@ -98,7 +98,7 @@
<string name="login">Login</string> <string name="login">Login</string>
<string name="logout">Logout</string> <string name="logout">Logout</string>
<string name="send_logs">Send Debug Logs</string> <string name="send_logs">Send Debug Logs</string>
<string name="time_settings">Post Time Settings</string> <string name="time_settings">Post time format</string>
<string name="project_link">Visit Project Page</string> <string name="project_link">Visit Project Page</string>
<string name="telegram_link">Join Telegram Group</string> <string name="telegram_link">Join Telegram Group</string>
<string name="matrix_link">Join Matrix Room</string> <string name="matrix_link">Join Matrix Room</string>
@ -253,4 +253,5 @@
<string name="mark_as_seen">Mark as seen</string> <string name="mark_as_seen">Mark as seen</string>
<string name="skip_update_checkbox">Do not show again until next update</string> <string name="skip_update_checkbox">Do not show again until next update</string>
<string name="version">Version</string> <string name="version">Version</string>
<string name="select_default_tab">Default tab</string>
</resources> </resources>