From c5785e9b6fe97a8edb95815d977b282179d2a923 Mon Sep 17 00:00:00 2001 From: Jays2Kings Date: Fri, 9 Apr 2021 15:01:10 -0400 Subject: [PATCH] Refactoring theme logic, using enum --- .../data/preference/PreferenceKeys.kt | 2 + .../data/preference/PreferencesHelper.kt | 5 +- .../ui/base/activity/BaseActivity.kt | 14 ++++- .../ui/base/activity/BaseThemedActivity.kt | 8 ++- .../ui/manga/MangaDetailsController.kt | 4 +- .../tachiyomi/ui/reader/ReaderActivity.kt | 7 +-- .../ui/setting/SettingsGeneralController.kt | 22 +++----- .../ui/webview/BaseWebViewActivity.kt | 3 +- .../kanade/tachiyomi/util/system/ThemeUtil.kt | 54 +++++++++++++------ .../tachiyomi/util/view/ViewExtensions.kt | 5 +- 10 files changed, 78 insertions(+), 46 deletions(-) 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 127dd91056..4798f399a6 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 @@ -7,6 +7,8 @@ object PreferenceKeys { const val theme = "pref_theme_key" + const val themeStyle = "theme_style" + const val startingTab = "starting_tab" const val backToStart = "back_to_start" 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 fd12187b33..0c681c5fd9 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 @@ -13,6 +13,7 @@ import eu.kanade.tachiyomi.data.track.TrackService import eu.kanade.tachiyomi.ui.reader.viewer.ViewerNavigation import eu.kanade.tachiyomi.ui.reader.viewer.pager.PageLayout import eu.kanade.tachiyomi.ui.recents.RecentMangaAdapter +import eu.kanade.tachiyomi.util.system.ThemeUtil import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.onEach import java.io.File @@ -85,7 +86,9 @@ class PreferencesHelper(val context: Context) { fun clear() = prefs.edit().clear().apply() - fun theme() = prefs.getInt(Keys.theme, 5) + fun oldTheme() = prefs.getInt(Keys.theme, 5) + + fun theme() = flowPrefs.getEnum(Keys.themeStyle, ThemeUtil.Themes.DEFAULT) fun rotation() = flowPrefs.getInt(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 dba53d0440..0ebfcb5f9e 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 @@ -23,8 +23,18 @@ abstract class BaseActivity : AppCompatActivity() { } override fun onCreate(savedInstanceState: Bundle?) { - AppCompatDelegate.setDefaultNightMode(ThemeUtil.nightMode(preferences.theme())) - setTheme(ThemeUtil.theme(preferences.theme())) + if (preferences.theme().isNotSet()) { + ThemeUtil.convertTheme(preferences, preferences.oldTheme()) + } + // Using a try catch in case I start to remove themes + val theme = try { + preferences.theme().get() + } catch (e: Exception) { + preferences.theme().set(ThemeUtil.Themes.DEFAULT) + ThemeUtil.Themes.DEFAULT + } + AppCompatDelegate.setDefaultNightMode(theme.nightMode) + setTheme(theme.styleRes) super.onCreate(savedInstanceState) SecureActivityDelegate.setSecure(this) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseThemedActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseThemedActivity.kt index fa9d257a07..43002573d0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseThemedActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseThemedActivity.kt @@ -12,8 +12,12 @@ abstract class BaseThemedActivity : AppCompatActivity() { val preferences: PreferencesHelper by injectLazy() override fun onCreate(savedInstanceState: Bundle?) { - AppCompatDelegate.setDefaultNightMode(ThemeUtil.nightMode(preferences.theme())) - setTheme(ThemeUtil.theme(preferences.theme())) + if (preferences.theme().isNotSet()) { + ThemeUtil.convertTheme(preferences, preferences.oldTheme()) + } + val theme = preferences.theme().get() + AppCompatDelegate.setDefaultNightMode(theme.nightMode) + setTheme(theme.styleRes) super.onCreate(savedInstanceState) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsController.kt index 13466052e8..797ca96da9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsController.kt @@ -361,7 +361,7 @@ class MangaDetailsController : val activity = activity as? MainActivity ?: return val activityBinding = activityBinding ?: return // if the theme is using inverted toolbar color - if (!activity.isInNightMode() && ThemeUtil.isBlueTheme(presenter.preferences.theme())) { + if (ThemeUtil.hasDarkActionBarInLight(activity, presenter.preferences.theme().get())) { if (forThis) activityBinding.appBar.context.setTheme( R.style.ThemeOverlay_AppCompat_DayNight_ActionBar ) @@ -1414,7 +1414,7 @@ class MangaDetailsController : if (!activity.isInNightMode()) { activityBinding?.appBar?.context?.setTheme( - ThemeUtil.theme(presenter.preferences.theme()) + presenter.preferences.theme().get().styleRes ) val iconPrimary = currColor ?: Color.WHITE diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt index fad2ec16cb..27cafb9caa 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt @@ -198,8 +198,9 @@ class ReaderActivity : * Called when the activity is created. Initializes the presenter and configuration. */ override fun onCreate(savedInstanceState: Bundle?) { - AppCompatDelegate.setDefaultNightMode(ThemeUtil.nightMode(preferences.theme())) - setTheme(ThemeUtil.theme(preferences.theme())) + val theme = preferences.theme().get() + AppCompatDelegate.setDefaultNightMode(theme.nightMode) + setTheme(theme.styleRes) super.onCreate(savedInstanceState) binding = ReaderActivityBinding.inflate(layoutInflater) setContentView(binding.root) @@ -243,7 +244,7 @@ class ReaderActivity : } binding.chaptersSheet.chaptersBottomSheet.setup(this) - if (ThemeUtil.isBlueTheme(preferences.theme())) { + if (ThemeUtil.isColoredTheme(preferences.theme().get())) { binding.chaptersSheet.chapterRecycler.setBackgroundColor(getResourceColor(android.R.attr.colorBackground)) } config = ReaderConfig() 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 5526b310d6..a800e96039 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 @@ -8,6 +8,7 @@ import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.data.updater.UpdaterJob import eu.kanade.tachiyomi.ui.security.SecureActivityDelegate import eu.kanade.tachiyomi.util.system.LocaleHelper +import eu.kanade.tachiyomi.util.system.ThemeUtil import eu.kanade.tachiyomi.widget.preference.IntListMatPreference import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys @@ -87,22 +88,13 @@ class SettingsGeneralController : SettingsController() { preferenceCategory { titleRes = R.string.display - intListPreference(activity) { - key = Keys.theme + listPreference(activity) { + key = Keys.themeStyle titleRes = R.string.app_theme - entriesRes = arrayOf( - R.string.white_theme, - R.string.light_blue, - R.string.dark, - R.string.amoled_black, - R.string.dark_blue, - R.string.system_default, - R.string - .system_default_amoled, - R.string.system_default_all_blue - ) - entryValues = listOf(1, 8, 2, 3, 4, 5, 6, 7) - defaultValue = 5 + val enumConstants = ThemeUtil.Themes::class.java.enumConstants + entriesRes = enumConstants?.map { it.nameRes }.orEmpty().toTypedArray() + entryValues = enumConstants?.map { it.name }.orEmpty() + defaultValue = ThemeUtil.Themes.DEFAULT onChange { activity?.recreate() 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 1d0f08d179..74934d3a37 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 @@ -14,7 +14,6 @@ import androidx.core.graphics.ColorUtils import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.databinding.WebviewActivityBinding import eu.kanade.tachiyomi.ui.base.activity.BaseActivity -import eu.kanade.tachiyomi.util.system.ThemeUtil import eu.kanade.tachiyomi.util.system.getResourceColor import eu.kanade.tachiyomi.util.system.isBottomTappable import eu.kanade.tachiyomi.util.system.isInNightMode @@ -33,7 +32,7 @@ open class BaseWebViewActivity : BaseActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = WebviewActivityBinding.inflate(layoutInflater) - delegate.localNightMode = ThemeUtil.nightMode(preferences.theme()) + delegate.localNightMode = preferences.theme().get().nightMode setContentView(binding.root) setSupportActionBar(binding.toolbar) supportActionBar?.setDisplayHomeAsUpEnabled(true) 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 ec9403b921..eb38de605d 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 @@ -1,24 +1,42 @@ package eu.kanade.tachiyomi.util.system +import android.content.Context import android.graphics.Color +import androidx.annotation.StringRes +import androidx.annotation.StyleRes import androidx.appcompat.app.AppCompatDelegate import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.data.preference.PreferencesHelper object ThemeUtil { - fun isBlueTheme(theme: Int): Boolean { - return theme == 4 || theme == 8 || theme == 7 + + /** Migration method */ + fun convertTheme(preferences: PreferencesHelper, theme: Int) { + preferences.theme().set( + when (theme) { + 0 -> Themes.PURE_WHITE + 1 -> Themes.LIGHT_BLUE + 2 -> Themes.DARK + 3 -> Themes.AMOLED + 4 -> Themes.DARK_BLUE + 5 -> Themes.DEFAULT + 6 -> Themes.DEFAULT_AMOLED + 7 -> Themes.ALL_BLUE + else -> Themes.DEFAULT + } + ) } - fun isAMOLEDTheme(theme: Int): Boolean { - return theme == 3 || theme == 6 + fun isColoredTheme(theme: Themes): Boolean { + return theme.styleRes == R.style.Theme_Tachiyomi_AllBlue } - fun theme(theme: Int): Int { - return when { - isAMOLEDTheme(theme) -> R.style.Theme_Tachiyomi_Amoled - isBlueTheme(theme) -> R.style.Theme_Tachiyomi_AllBlue - else -> R.style.Theme_Tachiyomi - } + fun isPitchBlack(context: Context, theme: Themes): Boolean { + return context.isInNightMode() && theme.styleRes == R.style.Theme_Tachiyomi_Amoled + } + + fun hasDarkActionBarInLight(context: Context, theme: Themes): Boolean { + return !context.isInNightMode() && isColoredTheme(theme) } fun readerBackgroundColor(theme: Int): Int { @@ -28,11 +46,15 @@ object ThemeUtil { } } - fun nightMode(theme: Int): Int { - return when (theme) { - 1, 8 -> AppCompatDelegate.MODE_NIGHT_NO - 2, 3, 4 -> AppCompatDelegate.MODE_NIGHT_YES - else -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM - } + @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), + LIGHT_BLUE(R.style.Theme_Tachiyomi_AllBlue, AppCompatDelegate.MODE_NIGHT_NO, R.string.light_blue), + DARK(R.style.Theme_Tachiyomi, AppCompatDelegate.MODE_NIGHT_YES, R.string.dark), + AMOLED(R.style.Theme_Tachiyomi_Amoled, AppCompatDelegate.MODE_NIGHT_YES, R.string.amoled_black), + DARK_BLUE(R.style.Theme_Tachiyomi_AllBlue, AppCompatDelegate.MODE_NIGHT_YES, R.string.dark_blue), + DEFAULT(R.style.Theme_Tachiyomi, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM, R.string.system_default), + DEFAULT_AMOLED(R.style.Theme_Tachiyomi_Amoled, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM, R.string.system_default_amoled), + ALL_BLUE(R.style.Theme_Tachiyomi_AllBlue, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM, R.string.system_default_all_blue), } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/view/ViewExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/view/ViewExtensions.kt index f2a204a581..8629c529b8 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/view/ViewExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/view/ViewExtensions.kt @@ -36,7 +36,6 @@ import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.util.system.ThemeUtil import eu.kanade.tachiyomi.util.system.contextCompatColor import eu.kanade.tachiyomi.util.system.getResourceColor -import eu.kanade.tachiyomi.util.system.isInNightMode import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get @@ -64,8 +63,8 @@ fun View.snack( if (f != null) { snack.f() } - val theme = Injekt.get().theme() - if (ThemeUtil.isAMOLEDTheme(theme) && context.isInNightMode()) { + val theme = Injekt.get().theme().get() + if (ThemeUtil.isPitchBlack(context, theme)) { val textView: TextView = snack.view.findViewById(com.google.android.material.R.id.snackbar_text) val button: Button? =