Refactoring theme logic, using enum

This commit is contained in:
Jays2Kings 2021-04-09 15:01:10 -04:00
parent ed1fb9de1a
commit c5785e9b6f
10 changed files with 78 additions and 46 deletions

View File

@ -7,6 +7,8 @@ object PreferenceKeys {
const val theme = "pref_theme_key" const val theme = "pref_theme_key"
const val themeStyle = "theme_style"
const val startingTab = "starting_tab" const val startingTab = "starting_tab"
const val backToStart = "back_to_start" const val backToStart = "back_to_start"

View File

@ -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.ViewerNavigation
import eu.kanade.tachiyomi.ui.reader.viewer.pager.PageLayout import eu.kanade.tachiyomi.ui.reader.viewer.pager.PageLayout
import eu.kanade.tachiyomi.ui.recents.RecentMangaAdapter import eu.kanade.tachiyomi.ui.recents.RecentMangaAdapter
import eu.kanade.tachiyomi.util.system.ThemeUtil
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import java.io.File import java.io.File
@ -85,7 +86,9 @@ class PreferencesHelper(val context: Context) {
fun clear() = prefs.edit().clear().apply() 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) fun rotation() = flowPrefs.getInt(Keys.rotation, 1)

View File

@ -23,8 +23,18 @@ abstract class BaseActivity<VB : ViewBinding> : AppCompatActivity() {
} }
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
AppCompatDelegate.setDefaultNightMode(ThemeUtil.nightMode(preferences.theme())) if (preferences.theme().isNotSet()) {
setTheme(ThemeUtil.theme(preferences.theme())) 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) super.onCreate(savedInstanceState)
SecureActivityDelegate.setSecure(this) SecureActivityDelegate.setSecure(this)
} }

View File

@ -12,8 +12,12 @@ abstract class BaseThemedActivity : AppCompatActivity() {
val preferences: PreferencesHelper by injectLazy() val preferences: PreferencesHelper by injectLazy()
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
AppCompatDelegate.setDefaultNightMode(ThemeUtil.nightMode(preferences.theme())) if (preferences.theme().isNotSet()) {
setTheme(ThemeUtil.theme(preferences.theme())) ThemeUtil.convertTheme(preferences, preferences.oldTheme())
}
val theme = preferences.theme().get()
AppCompatDelegate.setDefaultNightMode(theme.nightMode)
setTheme(theme.styleRes)
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
} }

View File

@ -361,7 +361,7 @@ class MangaDetailsController :
val activity = activity as? MainActivity ?: return val activity = activity as? MainActivity ?: return
val activityBinding = activityBinding ?: return val activityBinding = activityBinding ?: return
// if the theme is using inverted toolbar color // 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( if (forThis) activityBinding.appBar.context.setTheme(
R.style.ThemeOverlay_AppCompat_DayNight_ActionBar R.style.ThemeOverlay_AppCompat_DayNight_ActionBar
) )
@ -1414,7 +1414,7 @@ class MangaDetailsController :
if (!activity.isInNightMode()) { if (!activity.isInNightMode()) {
activityBinding?.appBar?.context?.setTheme( activityBinding?.appBar?.context?.setTheme(
ThemeUtil.theme(presenter.preferences.theme()) presenter.preferences.theme().get().styleRes
) )
val iconPrimary = currColor ?: Color.WHITE val iconPrimary = currColor ?: Color.WHITE

View File

@ -198,8 +198,9 @@ class ReaderActivity :
* Called when the activity is created. Initializes the presenter and configuration. * Called when the activity is created. Initializes the presenter and configuration.
*/ */
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
AppCompatDelegate.setDefaultNightMode(ThemeUtil.nightMode(preferences.theme())) val theme = preferences.theme().get()
setTheme(ThemeUtil.theme(preferences.theme())) AppCompatDelegate.setDefaultNightMode(theme.nightMode)
setTheme(theme.styleRes)
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
binding = ReaderActivityBinding.inflate(layoutInflater) binding = ReaderActivityBinding.inflate(layoutInflater)
setContentView(binding.root) setContentView(binding.root)
@ -243,7 +244,7 @@ class ReaderActivity :
} }
binding.chaptersSheet.chaptersBottomSheet.setup(this) 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)) binding.chaptersSheet.chapterRecycler.setBackgroundColor(getResourceColor(android.R.attr.colorBackground))
} }
config = ReaderConfig() config = ReaderConfig()

View File

@ -8,6 +8,7 @@ import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.data.updater.UpdaterJob import eu.kanade.tachiyomi.data.updater.UpdaterJob
import eu.kanade.tachiyomi.ui.security.SecureActivityDelegate import eu.kanade.tachiyomi.ui.security.SecureActivityDelegate
import eu.kanade.tachiyomi.util.system.LocaleHelper 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.widget.preference.IntListMatPreference
import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys
@ -87,22 +88,13 @@ class SettingsGeneralController : SettingsController() {
preferenceCategory { preferenceCategory {
titleRes = R.string.display titleRes = R.string.display
intListPreference(activity) { listPreference(activity) {
key = Keys.theme key = Keys.themeStyle
titleRes = R.string.app_theme titleRes = R.string.app_theme
entriesRes = arrayOf( val enumConstants = ThemeUtil.Themes::class.java.enumConstants
R.string.white_theme, entriesRes = enumConstants?.map { it.nameRes }.orEmpty().toTypedArray()
R.string.light_blue, entryValues = enumConstants?.map { it.name }.orEmpty()
R.string.dark, defaultValue = ThemeUtil.Themes.DEFAULT
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
onChange { onChange {
activity?.recreate() activity?.recreate()

View File

@ -14,7 +14,6 @@ import androidx.core.graphics.ColorUtils
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.databinding.WebviewActivityBinding import eu.kanade.tachiyomi.databinding.WebviewActivityBinding
import eu.kanade.tachiyomi.ui.base.activity.BaseActivity 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.getResourceColor
import eu.kanade.tachiyomi.util.system.isBottomTappable import eu.kanade.tachiyomi.util.system.isBottomTappable
import eu.kanade.tachiyomi.util.system.isInNightMode import eu.kanade.tachiyomi.util.system.isInNightMode
@ -33,7 +32,7 @@ open class BaseWebViewActivity : BaseActivity<WebviewActivityBinding>() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
binding = WebviewActivityBinding.inflate(layoutInflater) binding = WebviewActivityBinding.inflate(layoutInflater)
delegate.localNightMode = ThemeUtil.nightMode(preferences.theme()) delegate.localNightMode = preferences.theme().get().nightMode
setContentView(binding.root) setContentView(binding.root)
setSupportActionBar(binding.toolbar) setSupportActionBar(binding.toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayHomeAsUpEnabled(true)

View File

@ -1,24 +1,42 @@
package eu.kanade.tachiyomi.util.system package eu.kanade.tachiyomi.util.system
import android.content.Context
import android.graphics.Color import android.graphics.Color
import androidx.annotation.StringRes
import androidx.annotation.StyleRes
import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
object ThemeUtil { 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 { fun isColoredTheme(theme: Themes): Boolean {
return theme == 3 || theme == 6 return theme.styleRes == R.style.Theme_Tachiyomi_AllBlue
} }
fun theme(theme: Int): Int { fun isPitchBlack(context: Context, theme: Themes): Boolean {
return when { return context.isInNightMode() && theme.styleRes == R.style.Theme_Tachiyomi_Amoled
isAMOLEDTheme(theme) -> R.style.Theme_Tachiyomi_Amoled }
isBlueTheme(theme) -> R.style.Theme_Tachiyomi_AllBlue
else -> R.style.Theme_Tachiyomi fun hasDarkActionBarInLight(context: Context, theme: Themes): Boolean {
} return !context.isInNightMode() && isColoredTheme(theme)
} }
fun readerBackgroundColor(theme: Int): Int { fun readerBackgroundColor(theme: Int): Int {
@ -28,11 +46,15 @@ object ThemeUtil {
} }
} }
fun nightMode(theme: Int): Int { @Suppress("unused")
return when (theme) { enum class Themes(@StyleRes val styleRes: Int, val nightMode: Int, @StringRes val nameRes: Int) {
1, 8 -> AppCompatDelegate.MODE_NIGHT_NO PURE_WHITE(R.style.Theme_Tachiyomi, AppCompatDelegate.MODE_NIGHT_NO, R.string.white_theme),
2, 3, 4 -> AppCompatDelegate.MODE_NIGHT_YES LIGHT_BLUE(R.style.Theme_Tachiyomi_AllBlue, AppCompatDelegate.MODE_NIGHT_NO, R.string.light_blue),
else -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM 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),
} }
} }

View File

@ -36,7 +36,6 @@ import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.util.system.ThemeUtil import eu.kanade.tachiyomi.util.system.ThemeUtil
import eu.kanade.tachiyomi.util.system.contextCompatColor import eu.kanade.tachiyomi.util.system.contextCompatColor
import eu.kanade.tachiyomi.util.system.getResourceColor import eu.kanade.tachiyomi.util.system.getResourceColor
import eu.kanade.tachiyomi.util.system.isInNightMode
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
@ -64,8 +63,8 @@ fun View.snack(
if (f != null) { if (f != null) {
snack.f() snack.f()
} }
val theme = Injekt.get<PreferencesHelper>().theme() val theme = Injekt.get<PreferencesHelper>().theme().get()
if (ThemeUtil.isAMOLEDTheme(theme) && context.isInNightMode()) { if (ThemeUtil.isPitchBlack(context, theme)) {
val textView: TextView = val textView: TextView =
snack.view.findViewById(com.google.android.material.R.id.snackbar_text) snack.view.findViewById(com.google.android.material.R.id.snackbar_text)
val button: Button? = val button: Button? =