From 25b2b504cbc78e0c3f486c1bafccbe5d89576044 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Sat, 7 Nov 2020 16:27:49 +0900 Subject: [PATCH] save resource entry names in preferences instead of ids --- .../awais/instagrabber/activities/MainActivity.java | 11 +++++++---- .../settings/SettingsPreferencesFragment.java | 2 +- .../fragments/settings/ThemePreferencesFragment.java | 4 ++-- .../java/awais/instagrabber/utils/ThemeUtils.java | 12 +++--------- 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/activities/MainActivity.java b/app/src/main/java/awais/instagrabber/activities/MainActivity.java index f30bd0f1..388a57df 100644 --- a/app/src/main/java/awais/instagrabber/activities/MainActivity.java +++ b/app/src/main/java/awais/instagrabber/activities/MainActivity.java @@ -405,11 +405,14 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage } final List 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); 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 fda589f5..4c6b6359 100644 --- a/app/src/main/java/awais/instagrabber/fragments/settings/SettingsPreferencesFragment.java +++ b/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); diff --git a/app/src/main/java/awais/instagrabber/fragments/settings/ThemePreferencesFragment.java b/app/src/main/java/awais/instagrabber/fragments/settings/ThemePreferencesFragment.java index d4dd578a..bf3aad9e 100644 --- a/app/src/main/java/awais/instagrabber/fragments/settings/ThemePreferencesFragment.java +++ b/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); diff --git a/app/src/main/java/awais/instagrabber/utils/ThemeUtils.java b/app/src/main/java/awais/instagrabber/utils/ThemeUtils.java index bf3f94f8..350e4674 100644 --- a/app/src/main/java/awais/instagrabber/utils/ThemeUtils.java +++ b/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