diff options
author | Florian <64581222+fm-sys@users.noreply.github.com> | 2020-12-11 22:36:16 +0300 |
---|---|---|
committer | Niedermann IT-Dienstleistungen <stefan-niedermann@users.noreply.github.com> | 2020-12-12 18:34:04 +0300 |
commit | 3cab6716c9e005a4bba42dfbd342e6e1c764af27 (patch) | |
tree | bfd05df7721198ac87725f7f03bac81c7d421351 /app/src/main/java/it/niedermann | |
parent | 7802386a28c58da31aeebed318315e5856f94f59 (diff) |
Dark mode following system default (fix #392)
Diffstat (limited to 'app/src/main/java/it/niedermann')
4 files changed, 109 insertions, 13 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 cf30a0971..ba4088aa9 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/DeckApplication.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/DeckApplication.java @@ -2,13 +2,14 @@ package it.niedermann.nextcloud.deck; import android.content.Context; import android.content.SharedPreferences; +import android.content.res.Configuration; import androidx.annotation.NonNull; import androidx.multidex.MultiDexApplication; import androidx.preference.PreferenceManager; -import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO; -import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES; +import it.niedermann.nextcloud.deck.ui.settings.DarkModeSetting; + import static androidx.appcompat.app.AppCompatDelegate.setDefaultNightMode; public class DeckApplication extends MultiDexApplication { @@ -17,9 +18,12 @@ public class DeckApplication extends MultiDexApplication { public static final long NO_BOARD_ID = -1L; public static final long NO_STACK_ID = -1L; + private static String PREF_KEY_THEME; + @Override public void onCreate() { - setAppTheme(isDarkTheme(getApplicationContext())); + PREF_KEY_THEME = getString(R.string.pref_key_dark_theme); + setAppTheme(getAppTheme(getApplicationContext())); super.onCreate(); } @@ -27,13 +31,37 @@ public class DeckApplication extends MultiDexApplication { // Day / Night theme // ----------------- - public static void setAppTheme(boolean darkTheme) { - setDefaultNightMode(darkTheme ? MODE_NIGHT_YES : MODE_NIGHT_NO); + public static void setAppTheme(DarkModeSetting setting) { + setDefaultNightMode(setting.getModeId()); } - public static boolean isDarkTheme(@NonNull Context context) { + public static DarkModeSetting getAppTheme(@NonNull Context context) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - return prefs.getBoolean(context.getString(R.string.pref_key_dark_theme), false); + String mode; + try { + mode = prefs.getString(PREF_KEY_THEME, DarkModeSetting.SYSTEM_DEFAULT.getPreferenceValue(context)); + } catch (ClassCastException e) { + boolean darkModeEnabled = prefs.getBoolean(PREF_KEY_THEME, false); + mode = darkModeEnabled ? DarkModeSetting.DARK.getPreferenceValue(context) : DarkModeSetting.LIGHT.getPreferenceValue(context); + } + return DarkModeSetting.valueOf(mode); + } + + public static boolean isDarkThemeActive(@NonNull Context context, DarkModeSetting setting) { + if (setting == DarkModeSetting.SYSTEM_DEFAULT) { + return isDarkThemeActive(context); + } else { + return setting == DarkModeSetting.DARK; + } + } + + public static boolean isDarkThemeActive(@NonNull Context context) { + int uiMode = context.getResources().getConfiguration().uiMode; + return (uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES; + } + + public static boolean isDarkTheme(@NonNull Context context) { + return isDarkThemeActive(context, getAppTheme(context)); } // -------------------------------------- diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/DeckDatabase.java b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/DeckDatabase.java index a491946ed..f06b25230 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/DeckDatabase.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/persistence/sync/adapters/db/DeckDatabase.java @@ -17,6 +17,7 @@ import androidx.sqlite.db.SupportSQLiteDatabase; import it.niedermann.android.util.ColorUtil; import it.niedermann.nextcloud.deck.DeckLog; +import it.niedermann.nextcloud.deck.R; import it.niedermann.nextcloud.deck.api.LastSyncUtil; import it.niedermann.nextcloud.deck.model.AccessControl; import it.niedermann.nextcloud.deck.model.Account; @@ -68,6 +69,7 @@ import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.dao.projects.Oc import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.dao.projects.OcsProjectResourceDao; import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.dao.widgets.SingleCardWidgetModelDao; import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.dao.widgets.StackWidgetModelDao; +import it.niedermann.nextcloud.deck.ui.settings.DarkModeSetting; @Database( entities = { @@ -97,7 +99,7 @@ import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.dao.widgets.Sta UserInBoard.class, }, exportSchema = false, - version = 23 + version = 24 ) @TypeConverters({DateTypeConverter.class}) public abstract class DeckDatabase extends RoomDatabase { @@ -454,6 +456,22 @@ public abstract class DeckDatabase extends RoomDatabase { } }) .addMigrations(MIGRATION_22_23) + .addMigrations(new Migration(23, 24) { + @Override + public void migrate(@NonNull SupportSQLiteDatabase database) { + // https://github.com/stefan-niedermann/nextcloud-deck/issues/392 + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); + final String themePref = context.getString(R.string.pref_key_dark_theme); + + if (sharedPreferences.contains(themePref)) { + SharedPreferences.Editor editor = sharedPreferences.edit(); + final boolean darkTheme = sharedPreferences.getBoolean(themePref, false); + editor.remove(themePref); + editor.putString(themePref, darkTheme ? DarkModeSetting.DARK.getPreferenceValue(context) : DarkModeSetting.LIGHT.getPreferenceValue(context)); + editor.apply(); + } + } + }) .fallbackToDestructiveMigration() .addCallback(ON_CREATE_CALLBACK) .build(); 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 new file mode 100644 index 000000000..41e894666 --- /dev/null +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/settings/DarkModeSetting.java @@ -0,0 +1,52 @@ +package it.niedermann.nextcloud.deck.ui.settings; + +import android.content.Context; + +import androidx.annotation.NonNull; +import androidx.annotation.StringRes; +import androidx.appcompat.app.AppCompatDelegate; + +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)}. + * <p> + * Additionally, the equivalent {@link AppCompatDelegate}-Mode can be received via {@link #getModeId()}. + * + * @see AppCompatDelegate#MODE_NIGHT_YES + * @see AppCompatDelegate#MODE_NIGHT_NO + * @see AppCompatDelegate#MODE_NIGHT_FOLLOW_SYSTEM + */ +public enum DarkModeSetting { + + /** + * Always use light mode. + */ + LIGHT(AppCompatDelegate.MODE_NIGHT_NO, R.string.pref_value_theme_light), + /** + * Always use dark mode. + */ + DARK(AppCompatDelegate.MODE_NIGHT_YES, R.string.pref_value_theme_dark), + /** + * Follow the global system setting for dark mode. + */ + SYSTEM_DEFAULT(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM, R.string.pref_value_theme_system_default); + + private final int modeId; + private final @StringRes int preferenceValue; + + DarkModeSetting(int modeId, @StringRes int preferenceValue) { + this.modeId = modeId; + this.preferenceValue = preferenceValue; + } + + public int getModeId() { + return modeId; + } + + public String getPreferenceValue(@NonNull Context context) { + return context.getString(preferenceValue); + } +} 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 04429f225..5a1d82389 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 @@ -21,7 +21,6 @@ import static it.niedermann.nextcloud.deck.ui.branding.BrandingUtil.readBrandMai public class SettingsFragment extends PreferenceFragmentCompat implements Branded { private BrandedSwitchPreference wifiOnlyPref; - private BrandedSwitchPreference themePref; private BrandedSwitchPreference brandingPref; private BrandedSwitchPreference compactPref; @@ -41,11 +40,11 @@ public class SettingsFragment extends PreferenceFragmentCompat implements Brande DeckLog.error("Could not find preference with key: \"" + getString(R.string.pref_key_wifi_only) + "\""); } - themePref = findPreference(getString(R.string.pref_key_dark_theme)); + Preference themePref = findPreference(getString(R.string.pref_key_dark_theme)); if (themePref != null) { themePref.setOnPreferenceChangeListener((Preference preference, Object newValue) -> { - final Boolean darkTheme = (Boolean) newValue; - DeckLog.log("darkTheme: " + darkTheme); + final DarkModeSetting darkTheme = DarkModeSetting.valueOf((String) newValue); + DeckLog.log("appTheme: " + darkTheme); setAppTheme(darkTheme); requireActivity().setResult(Activity.RESULT_OK); requireActivity().recreate(); @@ -93,7 +92,6 @@ public class SettingsFragment extends PreferenceFragmentCompat implements Brande @Override public void applyBrand(int mainColor) { wifiOnlyPref.applyBrand(mainColor); - themePref.applyBrand(mainColor); brandingPref.applyBrand(mainColor); compactPref.applyBrand(mainColor); } |