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

Generate default tab preference list from current tabs

This commit is contained in:
Ammar Githam 2021-03-25 19:29:56 +09:00
parent b80ae2fcfe
commit 1d3ec52857
2 changed files with 19 additions and 16 deletions

View File

@ -300,7 +300,7 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
final Bundle bundle = new Bundle(); final Bundle bundle = new Bundle();
switch (type) { switch (type) {
case TYPE_LOCATION: case TYPE_LOCATION:
bundle.putLong("locationId", Long.valueOf(query)); bundle.putLong("locationId", Long.parseLong(query));
navController.navigate(R.id.action_global_locationFragment, bundle); navController.navigate(R.id.action_global_locationFragment, bundle);
break; break;
case TYPE_HASHTAG: case TYPE_HASHTAG:
@ -404,10 +404,11 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
@Override @Override
public void onFailure(@NonNull final Call<SearchResponse> call, public void onFailure(@NonNull final Call<SearchResponse> call,
Throwable t) { @NonNull Throwable t) {
if (!call.isCanceled() && t != null) if (!call.isCanceled()) {
Log.e(TAG, "Exception on search:", t); Log.e(TAG, "Exception on search:", t);
} }
}
}; };
private final Runnable runnable = () -> { private final Runnable runnable = () -> {
@ -498,14 +499,15 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
if (!TextUtils.isEmpty(defaultTabResNameString)) { if (!TextUtils.isEmpty(defaultTabResNameString)) {
navId = getResources().getIdentifier(defaultTabResNameString, "navigation", getPackageName()); navId = getResources().getIdentifier(defaultTabResNameString, "navigation", getPackageName());
} }
final int navGraph = isLoggedIn ? R.navigation.feed_nav_graph : R.navigation.profile_nav_graph; final int navGraph = isLoggedIn ? R.navigation.feed_nav_graph
: R.navigation.profile_nav_graph;
final int defaultNavId = navId <= 0 ? navGraph : navId; final int defaultNavId = navId <= 0 ? navGraph : navId;
int index = Iterators.indexOf(tabs.iterator(), tab -> { int index = Iterators.indexOf(tabs.iterator(), tab -> {
if (tab == null) return false; if (tab == null) return false;
return tab.getNavigationResId() == defaultNavId; return tab.getNavigationResId() == defaultNavId;
}); });
if (index >= 0) firstFragmentGraphIndex = index;
if (index < 0 || index >= tabs.size()) index = 0; if (index < 0 || index >= tabs.size()) index = 0;
if (index >= 0) firstFragmentGraphIndex = index;
setBottomNavSelectedTab(tabs.get(index)); setBottomNavSelectedTab(tabs.get(index));
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, "Error parsing id", e); Log.e(TAG, "Error parsing id", e);

View File

@ -1,7 +1,7 @@
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.util.Pair;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.preference.ListPreference; import androidx.preference.ListPreference;
@ -14,9 +14,11 @@ import java.util.List;
import awais.instagrabber.R; import awais.instagrabber.R;
import awais.instagrabber.dialogs.ConfirmDialogFragment; import awais.instagrabber.dialogs.ConfirmDialogFragment;
import awais.instagrabber.dialogs.TabOrderPreferenceDialogFragment; import awais.instagrabber.dialogs.TabOrderPreferenceDialogFragment;
import awais.instagrabber.models.Tab;
import awais.instagrabber.utils.Constants; import awais.instagrabber.utils.Constants;
import awais.instagrabber.utils.CookieUtils; import awais.instagrabber.utils.CookieUtils;
import awais.instagrabber.utils.TextUtils; import awais.instagrabber.utils.TextUtils;
import awais.instagrabber.utils.Utils;
import static awais.instagrabber.utils.Utils.settingsHelper; import static awais.instagrabber.utils.Utils.settingsHelper;
@ -48,19 +50,18 @@ public class GeneralPreferencesFragment extends BasePreferencesFragment implemen
private Preference getDefaultTabPreference(@NonNull final Context context) { private Preference getDefaultTabPreference(@NonNull final Context context) {
final ListPreference preference = new ListPreference(context); final ListPreference preference = new ListPreference(context);
preference.setSummaryProvider(ListPreference.SimpleSummaryProvider.getInstance()); preference.setSummaryProvider(ListPreference.SimpleSummaryProvider.getInstance());
final TypedArray mainNavGraphs = getResources().obtainTypedArray(R.array.main_nav_graphs); final Pair<List<Tab>, List<Tab>> listPair = Utils.getNavTabList(context);
final int length = mainNavGraphs.length(); final List<Tab> tabs = listPair.first;
final String[] navGraphFileNames = new String[length]; final String[] titles = tabs.stream()
for (int i = 0; i < length; i++) { .map(Tab::getTitle)
final int resourceId = mainNavGraphs.getResourceId(i, -1); .toArray(String[]::new);
if (resourceId < 0) continue; final String[] navGraphFileNames = tabs.stream()
navGraphFileNames[i] = getResources().getResourceEntryName(resourceId); .map(Tab::getGraphName)
} .toArray(String[]::new);
mainNavGraphs.recycle();
preference.setKey(Constants.DEFAULT_TAB); preference.setKey(Constants.DEFAULT_TAB);
preference.setTitle(R.string.pref_start_screen); preference.setTitle(R.string.pref_start_screen);
preference.setDialogTitle(R.string.pref_start_screen); preference.setDialogTitle(R.string.pref_start_screen);
preference.setEntries(R.array.main_nav_titles); preference.setEntries(titles);
preference.setEntryValues(navGraphFileNames); preference.setEntryValues(navGraphFileNames);
preference.setIconSpaceReserved(false); preference.setIconSpaceReserved(false);
return preference; return preference;