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 {
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"

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.util.Locale
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()!!
@ -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)

View File

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

View File

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

View File

@ -118,11 +118,14 @@
<string name="pref_category_about">About</string>
<!-- General section -->
<string name="pref_theme">App theme</string>
<string name="light_theme">Light</string>
<string name="dark_theme">Dark</string>
<string name="amoled_theme">AMOLED dark</string>
<string name="darkblue_theme">Dark blue</string>
<string name="pref_theme_mode">App theme</string>
<string name="theme_light">Light</string>
<string name="theme_dark">Dark</string>
<string name="theme_system">Follow system</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_language">Language</string>
<string name="system_default">System default</string>