Browse Source

save resource entry names in preferences instead of ids

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
25b2b504cb
  1. 11
      app/src/main/java/awais/instagrabber/activities/MainActivity.java
  2. 2
      app/src/main/java/awais/instagrabber/fragments/settings/SettingsPreferencesFragment.java
  3. 4
      app/src/main/java/awais/instagrabber/fragments/settings/ThemePreferencesFragment.java
  4. 12
      app/src/main/java/awais/instagrabber/utils/ThemeUtils.java

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

@ -405,11 +405,14 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
}
final List<Integer> mainNavList = getMainNavList(main_nav_ids);
if (setDefaultFromSettings) {
final String defaultTabIdString = settingsHelper.getString(Constants.DEFAULT_TAB);
final String defaultTabResNameString = settingsHelper.getString(Constants.DEFAULT_TAB);
try {
final int defaultNavId = TextUtils.isEmpty(defaultTabIdString)
? R.navigation.profile_nav_graph
: Integer.parseInt(defaultTabIdString);
int navId = 0;
if (!TextUtils.isEmpty(defaultTabResNameString)) {
navId = getResources().getIdentifier(defaultTabResNameString, "navigation", getPackageName());
}
final int defaultNavId = navId <= 0 ? R.navigation.profile_nav_graph
: navId;
final int index = mainNavList.indexOf(defaultNavId);
if (index >= 0) firstFragmentGraphIndex = index;
setBottomNavSelectedItem(defaultNavId);

2
app/src/main/java/awais/instagrabber/fragments/settings/SettingsPreferencesFragment.java

@ -128,7 +128,7 @@ public class SettingsPreferencesFragment extends BasePreferencesFragment {
for (int i = 0; i < length; i++) {
final int resourceId = mainNavIds.getResourceId(i, -1);
if (resourceId < 0) continue;
values[i] = String.valueOf(resourceId);
values[i] = getResources().getResourceEntryName(resourceId);
}
mainNavIds.recycle();
preference.setKey(Constants.DEFAULT_TAB);

4
app/src/main/java/awais/instagrabber/fragments/settings/ThemePreferencesFragment.java

@ -51,7 +51,7 @@ public class ThemePreferencesFragment extends BasePreferencesFragment {
for (int i = 0; i < length; i++) {
final int resourceId = lightThemeValues.getResourceId(i, -1);
if (resourceId < 0) continue;
values[i] = String.valueOf(resourceId);
values[i] = getResources().getResourceEntryName(resourceId);
}
lightThemeValues.recycle();
preference.setKey(Constants.PREF_LIGHT_THEME);
@ -76,7 +76,7 @@ public class ThemePreferencesFragment extends BasePreferencesFragment {
for (int i = 0; i < length; i++) {
final int resourceId = darkThemeValues.getResourceId(i, -1);
if (resourceId < 0) continue;
values[i] = String.valueOf(resourceId);
values[i] = getResources().getResourceEntryName(resourceId);
}
darkThemeValues.recycle();
preference.setKey(Constants.PREF_DARK_THEME);

12
app/src/main/java/awais/instagrabber/utils/ThemeUtils.java

@ -3,7 +3,6 @@ package awais.instagrabber.utils;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDelegate;
@ -24,14 +23,9 @@ public final class ThemeUtils {
themeCode = AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY;
}
final boolean isNight = isNight(context, themeCode);
int themeResId = 0;
try {
themeResId = Integer.parseInt(isNight ? settingsHelper.getString(Constants.PREF_DARK_THEME)
: settingsHelper.getString(Constants.PREF_LIGHT_THEME));
} catch (NumberFormatException e) {
Log.e(TAG, "changeTheme: ", e);
}
final String themeResName = isNight ? settingsHelper.getString(Constants.PREF_DARK_THEME)
: settingsHelper.getString(Constants.PREF_LIGHT_THEME);
final int themeResId = context.getResources().getIdentifier(themeResName, "style", context.getPackageName());
final int finalThemeResId;
if (themeResId <= 0) {
// Nothing set in settings

Loading…
Cancel
Save