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
This commit is contained in:
onlymash 2020-02-19 10:58:52 +08:00 committed by GitHub
parent ef533b4c87
commit 02b5c3da71
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 100 additions and 19 deletions

View file

@ -5,7 +5,9 @@ package eu.kanade.tachiyomi.data.preference
*/ */
object PreferenceKeys { 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" const val rotation = "pref_rotation_type_key"

View file

@ -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"
}

View file

@ -15,6 +15,7 @@ import java.text.DateFormat
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.Locale import java.util.Locale
import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys
import eu.kanade.tachiyomi.data.preference.PreferenceValues as Values
fun <T> Preference<T>.getOrDefault(): T = get() ?: defaultValue()!! fun <T> Preference<T>.getOrDefault(): T = get() ?: defaultValue()!!
@ -53,7 +54,9 @@ class PreferencesHelper(val context: Context) {
fun clear() = prefs.edit().clear().apply() 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) fun rotation() = rxPrefs.getInteger(Keys.rotation, 1)

View file

@ -1,27 +1,44 @@
package eu.kanade.tachiyomi.ui.base.activity package eu.kanade.tachiyomi.ui.base.activity
import android.app.UiModeManager
import android.content.Context
import android.os.Bundle import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.util.system.LocaleHelper import eu.kanade.tachiyomi.util.system.LocaleHelper
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
import eu.kanade.tachiyomi.data.preference.PreferenceValues as Values
abstract class BaseActivity : AppCompatActivity() { abstract class BaseActivity : AppCompatActivity() {
val preferences: PreferencesHelper by injectLazy() 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 { init {
@Suppress("LeakingThis") @Suppress("LeakingThis")
LocaleHelper.updateConfiguration(this) LocaleHelper.updateConfiguration(this)
} }
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
setTheme(when (preferences.theme()) { setTheme(when (preferences.themeMode()) {
2 -> R.style.Theme_Tachiyomi_Dark Values.THEME_MODE_LIGHT -> R.style.Theme_Tachiyomi
3 -> R.style.Theme_Tachiyomi_Amoled Values.THEME_MODE_DARK -> darkTheme
4 -> R.style.Theme_Tachiyomi_DarkBlue else -> {
else -> R.style.Theme_Tachiyomi 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) super.onCreate(savedInstanceState)
} }

View file

@ -1,10 +1,12 @@
package eu.kanade.tachiyomi.ui.setting package eu.kanade.tachiyomi.ui.setting
import android.os.Build
import androidx.preference.PreferenceScreen import androidx.preference.PreferenceScreen
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.util.preference.* import eu.kanade.tachiyomi.util.preference.*
import eu.kanade.tachiyomi.util.system.LocaleHelper import eu.kanade.tachiyomi.util.system.LocaleHelper
import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys
import eu.kanade.tachiyomi.data.preference.PreferenceValues as Values
class SettingsGeneralController : SettingsController() { class SettingsGeneralController : SettingsController() {
@ -48,13 +50,27 @@ class SettingsGeneralController : SettingsController() {
defaultValue = "" defaultValue = ""
summary = "%s" summary = "%s"
} }
intListPreference { listPreference {
key = Keys.theme key = Keys.themeMode
titleRes = R.string.pref_theme titleRes = R.string.pref_theme_mode
entriesRes = arrayOf(R.string.light_theme, R.string.dark_theme, if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
R.string.amoled_theme, R.string.darkblue_theme) entriesRes = arrayOf(
entryValues = arrayOf("1", "2", "3", "4") R.string.theme_light,
defaultValue = "1" 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" summary = "%s"
onChange { onChange {
@ -62,6 +78,27 @@ class SettingsGeneralController : SettingsController() {
true 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 { intListPreference {
key = Keys.startScreen key = Keys.startScreen
titleRes = R.string.pref_start_screen titleRes = R.string.pref_start_screen

View file

@ -118,11 +118,14 @@
<string name="pref_category_about">About</string> <string name="pref_category_about">About</string>
<!-- General section --> <!-- General section -->
<string name="pref_theme">App theme</string> <string name="pref_theme_mode">App theme</string>
<string name="light_theme">Light</string> <string name="theme_light">Light</string>
<string name="dark_theme">Dark</string> <string name="theme_dark">Dark</string>
<string name="amoled_theme">AMOLED dark</string> <string name="theme_system">Follow system</string>
<string name="darkblue_theme">Dark blue</string> <string name="pref_theme_dark">Dark theme</string>
<string name="theme_dark_default">Default</string>
<string name="theme_dark_amoled">AMOLED</string>
<string name="theme_dark_blue">Dark blue</string>
<string name="pref_start_screen">Start screen</string> <string name="pref_start_screen">Start screen</string>
<string name="pref_language">Language</string> <string name="pref_language">Language</string>
<string name="system_default">System default</string> <string name="system_default">System default</string>