Browse Source

Generate default tab preference list from current tabs

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
1d3ec52857
  1. 12
      app/src/main/java/awais/instagrabber/activities/MainActivity.java
  2. 23
      app/src/main/java/awais/instagrabber/fragments/settings/GeneralPreferencesFragment.java

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

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

23
app/src/main/java/awais/instagrabber/fragments/settings/GeneralPreferencesFragment.java

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

Loading…
Cancel
Save