From 02b5c3da715adae1efb7a9be92d449823cd33b56 Mon Sep 17 00:00:00 2001 From: onlymash Date: Wed, 19 Feb 2020 10:58:52 +0800 Subject: [PATCH] Support follow system theme[Android 10] (#2603) * Support follow system theme * Show [follow system theme] only on Oreo and newer * Update preference values of theme * Refine theme preference --- .../data/preference/PreferenceKeys.kt | 4 +- .../data/preference/PreferenceValues.kt | 19 +++++++ .../data/preference/PreferencesHelper.kt | 5 +- .../ui/base/activity/BaseActivity.kt | 27 ++++++++-- .../ui/setting/SettingsGeneralController.kt | 51 ++++++++++++++++--- app/src/main/res/values/strings.xml | 13 +++-- 6 files changed, 100 insertions(+), 19 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceValues.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt index 865ae6ba11..bb4efcd0f6 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt @@ -5,7 +5,9 @@ package eu.kanade.tachiyomi.data.preference */ object PreferenceKeys { - const val theme = "pref_theme_key" + const val themeMode = "pref_theme_mode_key" + + const val themeDark = "pref_theme_dark_key" const val rotation = "pref_rotation_type_key" diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceValues.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceValues.kt new file mode 100644 index 0000000000..c12fb8f667 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceValues.kt @@ -0,0 +1,19 @@ +package eu.kanade.tachiyomi.data.preference + +/** + * This class stores the values for the preferences in the application. + */ +object PreferenceValues { + + const val THEME_MODE_LIGHT = "light" + + const val THEME_MODE_DARK = "dark" + + const val THEME_MODE_SYSTEM = "system" + + const val THEME_DARK_DEFAULT = "default" + + const val THEME_DARK_AMOLED = "amoled" + + const val THEME_DARK_BLUE = "blue" +} \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index 162313a189..fa37c318e5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -15,6 +15,7 @@ import java.text.DateFormat import java.text.SimpleDateFormat import java.util.Locale import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys +import eu.kanade.tachiyomi.data.preference.PreferenceValues as Values fun Preference.getOrDefault(): T = get() ?: defaultValue()!! @@ -53,7 +54,9 @@ class PreferencesHelper(val context: Context) { fun clear() = prefs.edit().clear().apply() - fun theme() = prefs.getInt(Keys.theme, 1) + fun themeMode() = prefs.getString(Keys.themeMode, Values.THEME_MODE_LIGHT) + + fun themeDark() = prefs.getString(Keys.themeDark, Values.THEME_DARK_DEFAULT) fun rotation() = rxPrefs.getInteger(Keys.rotation, 1) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt index b42d667150..1d7a792c87 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt @@ -1,27 +1,44 @@ package eu.kanade.tachiyomi.ui.base.activity +import android.app.UiModeManager +import android.content.Context import android.os.Bundle import androidx.appcompat.app.AppCompatActivity +import androidx.appcompat.app.AppCompatDelegate import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.util.system.LocaleHelper import uy.kohesive.injekt.injectLazy +import eu.kanade.tachiyomi.data.preference.PreferenceValues as Values abstract class BaseActivity : AppCompatActivity() { val preferences: PreferencesHelper by injectLazy() + private val darkTheme: Int + get() = when (preferences.themeDark()) { + Values.THEME_DARK_DEFAULT -> R.style.Theme_Tachiyomi_Dark + Values.THEME_DARK_AMOLED -> R.style.Theme_Tachiyomi_Amoled + else -> R.style.Theme_Tachiyomi_DarkBlue + } + init { @Suppress("LeakingThis") LocaleHelper.updateConfiguration(this) } override fun onCreate(savedInstanceState: Bundle?) { - setTheme(when (preferences.theme()) { - 2 -> R.style.Theme_Tachiyomi_Dark - 3 -> R.style.Theme_Tachiyomi_Amoled - 4 -> R.style.Theme_Tachiyomi_DarkBlue - else -> R.style.Theme_Tachiyomi + setTheme(when (preferences.themeMode()) { + Values.THEME_MODE_LIGHT -> R.style.Theme_Tachiyomi + Values.THEME_MODE_DARK -> darkTheme + else -> { + val mode = getSystemService(Context.UI_MODE_SERVICE) as UiModeManager + if (mode.nightMode == AppCompatDelegate.MODE_NIGHT_YES) { + darkTheme + } else { + R.style.Theme_Tachiyomi + } + } }) super.onCreate(savedInstanceState) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt index b257eb294a..de90ef357c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt @@ -1,10 +1,12 @@ package eu.kanade.tachiyomi.ui.setting +import android.os.Build import androidx.preference.PreferenceScreen import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.util.preference.* import eu.kanade.tachiyomi.util.system.LocaleHelper import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys +import eu.kanade.tachiyomi.data.preference.PreferenceValues as Values class SettingsGeneralController : SettingsController() { @@ -48,13 +50,27 @@ class SettingsGeneralController : SettingsController() { defaultValue = "" summary = "%s" } - intListPreference { - key = Keys.theme - titleRes = R.string.pref_theme - entriesRes = arrayOf(R.string.light_theme, R.string.dark_theme, - R.string.amoled_theme, R.string.darkblue_theme) - entryValues = arrayOf("1", "2", "3", "4") - defaultValue = "1" + listPreference { + key = Keys.themeMode + titleRes = R.string.pref_theme_mode + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + entriesRes = arrayOf( + R.string.theme_light, + R.string.theme_dark, + R.string.theme_system) + entryValues = arrayOf( + Values.THEME_MODE_LIGHT, + Values.THEME_MODE_DARK, + Values.THEME_MODE_SYSTEM) + } else { + entriesRes = arrayOf( + R.string.theme_light, + R.string.theme_dark) + entryValues = arrayOf( + Values.THEME_MODE_LIGHT, + Values.THEME_MODE_DARK) + } + defaultValue = Values.THEME_MODE_LIGHT summary = "%s" onChange { @@ -62,6 +78,27 @@ class SettingsGeneralController : SettingsController() { true } } + listPreference { + key = Keys.themeDark + titleRes = R.string.pref_theme_dark + entriesRes = arrayOf( + R.string.theme_dark_default, + R.string.theme_dark_amoled, + R.string.theme_dark_blue) + entryValues = arrayOf( + Values.THEME_DARK_DEFAULT, + Values.THEME_DARK_AMOLED, + Values.THEME_DARK_BLUE) + defaultValue = Values.THEME_DARK_DEFAULT + summary = "%s" + + onChange { + if (preferences.themeMode() != Values.THEME_MODE_LIGHT) { + activity?.recreate() + } + true + } + } intListPreference { key = Keys.startScreen titleRes = R.string.pref_start_screen diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 578a7b620b..e6c676900c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -118,11 +118,14 @@ About - App theme - Light - Dark - AMOLED dark - Dark blue + App theme + Light + Dark + Follow system + Dark theme + Default + AMOLED + Dark blue Start screen Language System default