From b489e5bb56b784a0e611bc53c1f9e12a6c14dcad Mon Sep 17 00:00:00 2001 From: Jays2Kings Date: Wed, 14 Jul 2021 15:15:48 -0400 Subject: [PATCH] Combine light/dark themes in enum Also combined the strawberry themes --- .../data/preference/PreferencesHelper.kt | 4 +- .../tachiyomi/ui/setting/ThemePreference.kt | 55 ++++++++++------ .../ui/webview/BaseWebViewActivity.kt | 2 +- .../kanade/tachiyomi/util/system/ThemeUtil.kt | 48 +++++++++++--- .../eu/kanade/tachiyomi/util/system/Themes.kt | 65 +++++++++---------- app/src/main/res/values-night/themes.xml | 2 +- app/src/main/res/values/themes.xml | 3 +- 7 files changed, 109 insertions(+), 70 deletions(-) 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 37eaa43dec..41cf0a9e17 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 @@ -120,8 +120,8 @@ class PreferencesHelper(val context: Context) { fun themeDarkAmoled() = flowPrefs.getBoolean(Keys.themeDarkAmoled, false) - fun lightTheme() = flowPrefs.getEnum(Keys.lightTheme, Themes.PURE_WHITE) - fun darkTheme() = flowPrefs.getEnum(Keys.darkTheme, Themes.DARK) + fun lightTheme() = flowPrefs.getEnum(Keys.lightTheme, Themes.DEFAULT) + fun darkTheme() = flowPrefs.getEnum(Keys.darkTheme, Themes.DEFAULT) fun pageTransitions() = flowPrefs.getBoolean(Keys.enableTransitions, true) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/ThemePreference.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/ThemePreference.kt index a9f92c3117..a5639d2d4c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/ThemePreference.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/ThemePreference.kt @@ -53,36 +53,45 @@ class ThemePreference @JvmOverloads constructor(context: Context, attrs: Attribu fastAdapterDark = FastAdapter.with(itemAdapterDark) fastAdapterLight.setHasStableIds(true) fastAdapterDark.setHasStableIds(true) - selectExtensionLight = fastAdapterLight.getSelectExtension().setThemeListener() - selectExtensionDark = fastAdapterDark.getSelectExtension().setThemeListener() + selectExtensionLight = fastAdapterLight.getSelectExtension().setThemeListener(false) + selectExtensionDark = fastAdapterDark.getSelectExtension().setThemeListener(true) val enumConstants = Themes.values() - itemAdapterLight.set(enumConstants.filter { !it.isDarkTheme }.map(::ThemeItem)) - itemAdapterDark.set(enumConstants.filter { it.isDarkTheme }.map(::ThemeItem)) + itemAdapterLight.set( + enumConstants + .filter { !it.isDarkTheme || it.followsSystem } + .map { ThemeItem(it, false) } + ) + itemAdapterDark.set( + enumConstants + .filter { it.isDarkTheme || it.followsSystem } + .map { ThemeItem(it, true) } + ) isSelectable = false } - private fun SelectExtension.setThemeListener(): SelectExtension { + private fun SelectExtension.setThemeListener(isDarkMode: Boolean): SelectExtension { isSelectable = true multiSelect = true + val nightMode = if (isDarkMode) AppCompatDelegate.MODE_NIGHT_YES else AppCompatDelegate.MODE_NIGHT_NO selectionListener = object : ISelectionListener { override fun onSelectionChanged(item: ThemeItem, selected: Boolean) { - if (item.theme.isDarkTheme) { + if (isDarkMode) { preferences.darkTheme().set(item.theme) } else { preferences.lightTheme().set(item.theme) } if (!selected) { - preferences.nightMode().set(item.theme.nightMode) + preferences.nightMode().set(nightMode) } else if (preferences.nightMode() .get() != AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM ) { - preferences.nightMode().set(item.theme.nightMode) + preferences.nightMode().set(nightMode) } if (( preferences.nightMode().get() == AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM && - item.theme.nightMode != context.appDelegateNightMode() + nightMode != context.appDelegateNightMode() ) || - (!selected && item.theme.nightMode == context.appDelegateNightMode()) + (!selected && nightMode == context.appDelegateNightMode()) ) { fastAdapterLight.notifyDataSetChanged() fastAdapterDark.notifyDataSetChanged() @@ -155,7 +164,7 @@ class ThemePreference @JvmOverloads constructor(context: Context, attrs: Attribu } } - inner class ThemeItem(val theme: Themes) : AbstractItem>() { + inner class ThemeItem(val theme: Themes, val darkTheme: Boolean) : AbstractItem>() { /** defines the type defining this item. must be unique. preferably an id */ override val type: Int = R.id.theme_card_view @@ -169,15 +178,15 @@ class ThemePreference @JvmOverloads constructor(context: Context, attrs: Attribu return ViewHolder(v) } - val colors = theme.getColors() + val colors = theme.getColors(if (darkTheme) AppCompatDelegate.MODE_NIGHT_YES else AppCompatDelegate.MODE_NIGHT_NO) @Suppress("UNUSED_PARAMETER") override var isSelected: Boolean get() = when (preferences.nightMode().get()) { - AppCompatDelegate.MODE_NIGHT_YES -> preferences.darkTheme().get() == theme - AppCompatDelegate.MODE_NIGHT_NO -> preferences.lightTheme().get() == theme - else -> preferences.darkTheme().get() == theme || - preferences.lightTheme().get() == theme + AppCompatDelegate.MODE_NIGHT_YES -> preferences.darkTheme().get() == theme && darkTheme + AppCompatDelegate.MODE_NIGHT_NO -> preferences.lightTheme().get() == theme && !darkTheme + else -> (preferences.darkTheme().get() == theme && darkTheme) || + (preferences.lightTheme().get() == theme && !darkTheme) } set(value) {} @@ -185,16 +194,22 @@ class ThemePreference @JvmOverloads constructor(context: Context, attrs: Attribu val binding = ThemeItemBinding.bind(view) override fun bindView(item: ThemeItem, payloads: List) { - binding.themeNameText.setText(item.theme.nameRes) + binding.themeNameText.setText( + if (item.darkTheme) { + item.theme.darkNameRes + } else { + item.theme.nameRes + } + ) binding.checkbox.isVisible = item.isSelected binding.themeSelected.isInvisible = !item.isSelected if (binding.checkbox.isVisible) { val themeMatchesApp = if (context.isInNightMode()) { - item.theme.isDarkTheme + item.darkTheme } else { - !item.theme.isDarkTheme + !item.darkTheme } binding.themeSelected.alpha = if (themeMatchesApp) 1f else 0.5f binding.checkbox.alpha = if (themeMatchesApp) 1f else 0.5f @@ -220,7 +235,7 @@ class ThemePreference @JvmOverloads constructor(context: Context, attrs: Attribu binding.themeItem3.imageTintList = ColorStateList.valueOf(item.colors.inactiveTab) binding.themeLayout.setBackgroundColor(item.colors.colorBackground) - if (item.theme.isDarkTheme && preferences.themeDarkAmoled().get()) { + if (item.darkTheme && preferences.themeDarkAmoled().get()) { binding.themeLayout.setBackgroundColor(Color.BLACK) if (!ThemeUtil.isColoredTheme(item.theme)) { binding.themeBottomBar.setBackgroundColor(Color.BLACK) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/webview/BaseWebViewActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/webview/BaseWebViewActivity.kt index 91a0de23a7..39a2a0ea71 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/webview/BaseWebViewActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/webview/BaseWebViewActivity.kt @@ -146,7 +146,7 @@ open class BaseWebViewActivity : BaseActivity() { val lightMode = !isInNightMode() val prefTheme = getPrefTheme(preferences) setTheme(prefTheme.styleRes) - if (prefTheme.isDarkTheme && preferences.themeDarkAmoled().get()) { + if (!lightMode && preferences.themeDarkAmoled().get()) { setTheme(R.style.ThemeOverlay_Tachiyomi_Amoled) if (ThemeUtil.isColoredTheme(prefTheme)) { setTheme(R.style.ThemeOverlay_Tachiyomi_AllBlue) diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/system/ThemeUtil.kt b/app/src/main/java/eu/kanade/tachiyomi/util/system/ThemeUtil.kt index 974cf65a60..0da1946ca4 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/system/ThemeUtil.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/system/ThemeUtil.kt @@ -5,7 +5,10 @@ import android.content.res.Resources import android.graphics.Color import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatDelegate +import androidx.core.content.edit +import androidx.preference.PreferenceManager import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.data.preference.PreferenceKeys import eu.kanade.tachiyomi.data.preference.PreferencesHelper import uy.kohesive.injekt.injectLazy @@ -22,18 +25,46 @@ object ThemeUtil { ) preferences.lightTheme().set( when (theme) { - 1 -> Themes.LIGHT_BLUE - else -> Themes.PURE_WHITE + 1 -> Themes.CLASSIC_BLUE + else -> Themes.DEFAULT } ) preferences.darkTheme().set( when (theme) { - 4 -> Themes.DARK_BLUE - else -> Themes.DARK + 4 -> Themes.CLASSIC_BLUE + else -> Themes.DEFAULT } ) } + /** Migration method */ + fun convertNewThemes(context: Context) { + val prefs = PreferenceManager.getDefaultSharedPreferences(context) + val lightTheme = prefs.getString(PreferenceKeys.lightTheme, "DEFAULT") + val darkTheme = prefs.getString(PreferenceKeys.darkTheme, "DEFAULT") + + prefs.edit { + putString( + PreferenceKeys.lightTheme, + when (lightTheme) { + "SPRING" -> Themes.SPRING_AND_DUSK + "STRAWBERRY_DAIQUIRI" -> Themes.STRAWBERRIES + "LIGHT_BLUE" -> Themes.CLASSIC_BLUE + else -> Themes.DEFAULT + }.name + ) + putString( + PreferenceKeys.darkTheme, + when (darkTheme) { + "DUSK" -> Themes.SPRING_AND_DUSK + "CHOCOLATE_STRAWBERRIES" -> Themes.STRAWBERRIES + "DARK_BLUE" -> Themes.CLASSIC_BLUE + else -> Themes.DEFAULT + }.name + ) + } + } + fun isColoredTheme(theme: Themes): Boolean { return theme.styleRes == R.style.Theme_Tachiyomi_AllBlue } @@ -68,7 +99,9 @@ fun AppCompatActivity.setThemeAndNight(preferences: PreferencesHelper) { fun AppCompatActivity.getThemeWithExtras(theme: Resources.Theme, preferences: PreferencesHelper): Resources.Theme { val prefTheme = getPrefTheme(preferences) - if (prefTheme.isDarkTheme && preferences.themeDarkAmoled().get()) { + if ((isInNightMode() || preferences.nightMode().get() == AppCompatDelegate.MODE_NIGHT_YES) && + preferences.themeDarkAmoled().get() + ) { theme.applyStyle(R.style.ThemeOverlay_Tachiyomi_Amoled, true) if (ThemeUtil.isColoredTheme(prefTheme)) { theme.applyStyle(R.style.ThemeOverlay_Tachiyomi_AllBlue, true) @@ -86,8 +119,7 @@ fun Context.getPrefTheme(preferences: PreferencesHelper): Themes { ) preferences.darkTheme() else preferences.lightTheme() ).get() } catch (e: Exception) { - preferences.lightTheme().set(Themes.PURE_WHITE) - preferences.darkTheme().set(Themes.DARK) - Themes.PURE_WHITE + ThemeUtil.convertNewThemes(preferences.context) + getPrefTheme(preferences) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/system/Themes.kt b/app/src/main/java/eu/kanade/tachiyomi/util/system/Themes.kt index c58ebf75bd..cecf938f2e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/system/Themes.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/system/Themes.kt @@ -10,27 +10,23 @@ import eu.kanade.tachiyomi.R import kotlin.math.roundToInt @Suppress("unused") -enum class Themes(@StyleRes val styleRes: Int, val nightMode: Int, @StringRes val nameRes: Int) { - PURE_WHITE(R.style.Theme_Tachiyomi, AppCompatDelegate.MODE_NIGHT_NO, R.string.white_theme), - DARK(R.style.Theme_Tachiyomi, AppCompatDelegate.MODE_NIGHT_YES, R.string.dark), - SPRING( - R.style.Theme_Tachiyomi_MidnightDusk, - AppCompatDelegate.MODE_NIGHT_NO, - R.string.spring_blossom +enum class Themes(@StyleRes val styleRes: Int, val nightMode: Int, @StringRes val nameRes: Int, @StringRes altNameRes: Int? = null) { + DEFAULT( + R.style.Theme_Tachiyomi, + AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM, + R.string.white_theme, + R.string.dark ), - DUSK( + SPRING_AND_DUSK( R.style.Theme_Tachiyomi_MidnightDusk, - AppCompatDelegate.MODE_NIGHT_YES, + AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM, + R.string.spring_blossom, R.string.midnight_dusk ), - STRAWBERRY_DAIQUIRI( - R.style.Theme_Tachiyomi_StrawberryDaiquiri, - AppCompatDelegate.MODE_NIGHT_NO, - R.string.strawberry_daiquiri - ), - CHOCOLATE_STRAWBERRIES( - R.style.Theme_Tachiyomi_ChocolateStrawberries, - AppCompatDelegate.MODE_NIGHT_YES, + STRAWBERRIES( + R.style.Theme_Tachiyomi_Strawberries, + AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM, + R.string.strawberry_daiquiri, R.string.chocolate_strawberries ), LIME( @@ -43,28 +39,25 @@ enum class Themes(@StyleRes val styleRes: Int, val nightMode: Int, @StringRes va AppCompatDelegate.MODE_NIGHT_NO, R.string.yotsuba ), - YANG( + YIN_AND_YANG( R.style.Theme_Tachiyomi_YinYang, - AppCompatDelegate.MODE_NIGHT_NO, - R.string.yang - ), - YIN( - R.style.Theme_Tachiyomi_YinYang, - AppCompatDelegate.MODE_NIGHT_YES, + AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM, + R.string.yang, R.string.yin ), - LIGHT_BLUE( + CLASSIC_BLUE( R.style.Theme_Tachiyomi_AllBlue, - AppCompatDelegate.MODE_NIGHT_NO, - R.string.light_blue - ), - DARK_BLUE( - R.style.Theme_Tachiyomi_AllBlue, - AppCompatDelegate.MODE_NIGHT_YES, + AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM, + R.string.light_blue, R.string.dark_blue - ); + ), + ; val isDarkTheme = nightMode == AppCompatDelegate.MODE_NIGHT_YES + val followsSystem = nightMode == AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM + + @StringRes + val darkNameRes: Int = altNameRes ?: nameRes fun getColors(mode: Int = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM): Colors { return when (nightMode) { @@ -140,7 +133,7 @@ enum class Themes(@StyleRes val styleRes: Int, val nightMode: Int, @StringRes va @ColorInt val darkBackground: Int = Color.parseColor( when (styleRes) { - R.style.Theme_Tachiyomi_ChocolateStrawberries -> "#1a1716" + R.style.Theme_Tachiyomi_Strawberries -> "#1a1716" R.style.Theme_Tachiyomi_MidnightDusk -> "#16151D" R.style.Theme_Tachiyomi_FlatLime -> "#202125" else -> "#1C1C1D" @@ -153,7 +146,7 @@ enum class Themes(@StyleRes val styleRes: Int, val nightMode: Int, @StringRes va when (styleRes) { R.style.Theme_Tachiyomi_YinYang -> "#000000" R.style.Theme_Tachiyomi_MidnightDusk -> "#c43c97" - R.style.Theme_Tachiyomi_StrawberryDaiquiri -> "#ED4A65" + R.style.Theme_Tachiyomi_Strawberries -> "#ED4A65" R.style.Theme_Tachiyomi_Yotsuba -> "#dc6d3d" else -> "#2979FF" } @@ -165,7 +158,7 @@ enum class Themes(@StyleRes val styleRes: Int, val nightMode: Int, @StringRes va when (styleRes) { R.style.Theme_Tachiyomi_YinYang -> "#FFFFFF" R.style.Theme_Tachiyomi_MidnightDusk -> "#F02475" - R.style.Theme_Tachiyomi_ChocolateStrawberries -> "#AA2200" + R.style.Theme_Tachiyomi_Strawberries -> "#AA2200" R.style.Theme_Tachiyomi_FlatLime -> "#4AF88A" else -> "#3399FF" } @@ -214,7 +207,7 @@ enum class Themes(@StyleRes val styleRes: Int, val nightMode: Int, @StringRes va @ColorInt val darkBottomBar: Int = Color.parseColor( when (styleRes) { - R.style.Theme_Tachiyomi_ChocolateStrawberries -> "#211b19" + R.style.Theme_Tachiyomi_Strawberries -> "#211b19" R.style.Theme_Tachiyomi_AllBlue -> "#54759E" R.style.Theme_Tachiyomi_MidnightDusk -> "#201F27" R.style.Theme_Tachiyomi_FlatLime -> "#282A2E" diff --git a/app/src/main/res/values-night/themes.xml b/app/src/main/res/values-night/themes.xml index b3d22b4087..d82d8ff260 100644 --- a/app/src/main/res/values-night/themes.xml +++ b/app/src/main/res/values-night/themes.xml @@ -18,7 +18,7 @@ @color/colorAmoledPrimary - -