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 865ae6ba1..bb4efcd0f 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 000000000..c12fb8f66 --- /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 162313a18..fa37c318e 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 b42d66715..1d7a792c8 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 b257eb294..de90ef357 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 578a7b620..e6c676900 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