diff options
author | Florian <64581222+fm-sys@users.noreply.github.com> | 2020-12-13 11:08:25 +0300 |
---|---|---|
committer | Niedermann IT-Dienstleistungen <stefan-niedermann@users.noreply.github.com> | 2020-12-15 12:29:56 +0300 |
commit | c3181dbb924b93922163762d046cfb991238e850 (patch) | |
tree | 5afd0f80fbedbf46cc9dc93242075b17e8722b10 /app/src/main/java/it/niedermann | |
parent | 3cab6716c9e005a4bba42dfbd342e6e1c764af27 (diff) |
[DarkModeSetting] cleanup dirty code
Diffstat (limited to 'app/src/main/java/it/niedermann')
3 files changed, 21 insertions, 3 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/DeckApplication.java b/app/src/main/java/it/niedermann/nextcloud/deck/DeckApplication.java index ba4088aa9..2fc725784 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/DeckApplication.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/DeckApplication.java @@ -44,7 +44,7 @@ public class DeckApplication extends MultiDexApplication { boolean darkModeEnabled = prefs.getBoolean(PREF_KEY_THEME, false); mode = darkModeEnabled ? DarkModeSetting.DARK.getPreferenceValue(context) : DarkModeSetting.LIGHT.getPreferenceValue(context); } - return DarkModeSetting.valueOf(mode); + return DarkModeSetting.fromPreferenceValue(context, mode); } public static boolean isDarkThemeActive(@NonNull Context context, DarkModeSetting setting) { diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/settings/DarkModeSetting.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/settings/DarkModeSetting.java index 41e894666..14e63423e 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/settings/DarkModeSetting.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/settings/DarkModeSetting.java @@ -6,12 +6,14 @@ import androidx.annotation.NonNull; import androidx.annotation.StringRes; import androidx.appcompat.app.AppCompatDelegate; +import java.util.NoSuchElementException; + import it.niedermann.nextcloud.deck.R; /** * Possible values of the Dark Mode Setting. * <p> - * The Dark Mode Setting can be stored in {@link android.content.SharedPreferences} as String by using {@link DarkModeSetting#getPreferenceValue(Context)} and received via {@link DarkModeSetting#valueOf(String)}. + * The Dark Mode Setting can be stored in {@link android.content.SharedPreferences} as String by using {@link DarkModeSetting#getPreferenceValue(Context)} and received via {@link DarkModeSetting#fromPreferenceValue(Context, String)}. * <p> * Additionally, the equivalent {@link AppCompatDelegate}-Mode can be received via {@link #getModeId()}. * @@ -49,4 +51,20 @@ public enum DarkModeSetting { public String getPreferenceValue(@NonNull Context context) { return context.getString(preferenceValue); } + + /** + * Returns the instance of {@link DarkModeSetting} that corresponds to the preferenceValue + * + * @param preferenceValue String that is stored in shared preferences + * @return An instance of {@link DarkModeSetting} + */ + public static DarkModeSetting fromPreferenceValue(@NonNull Context context, String preferenceValue) { + for (DarkModeSetting value : DarkModeSetting.values()) { + if (context.getString(value.preferenceValue).equals(preferenceValue)) { + return value; + } + } + + throw new NoSuchElementException("No NightMode with preferenceValue \"" + preferenceValue + "\" found"); + } } diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/settings/SettingsFragment.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/settings/SettingsFragment.java index 5a1d82389..1a1591ff7 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/settings/SettingsFragment.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/settings/SettingsFragment.java @@ -43,7 +43,7 @@ public class SettingsFragment extends PreferenceFragmentCompat implements Brande Preference themePref = findPreference(getString(R.string.pref_key_dark_theme)); if (themePref != null) { themePref.setOnPreferenceChangeListener((Preference preference, Object newValue) -> { - final DarkModeSetting darkTheme = DarkModeSetting.valueOf((String) newValue); + final DarkModeSetting darkTheme = DarkModeSetting.fromPreferenceValue(getContext(), (String) newValue); DeckLog.log("appTheme: " + darkTheme); setAppTheme(darkTheme); requireActivity().setResult(Activity.RESULT_OK); |