From cd81df0cf2177e2f537903d33ee5e875df11a5f8 Mon Sep 17 00:00:00 2001 From: Jays2Kings Date: Mon, 26 Apr 2021 01:12:08 -0400 Subject: [PATCH] Move Themes enum into its own file --- .../data/preference/PreferencesHelper.kt | 6 +- .../tachiyomi/ui/setting/ThemePreference.kt | 9 +- .../kanade/tachiyomi/util/system/ThemeUtil.kt | 252 +----------------- .../eu/kanade/tachiyomi/util/system/Themes.kt | 249 +++++++++++++++++ 4 files changed, 261 insertions(+), 255 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/util/system/Themes.kt 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 b89b866eff..a2a1bb997f 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 @@ -16,7 +16,7 @@ import eu.kanade.tachiyomi.ui.reader.settings.ReaderBottomButton 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 eu.kanade.tachiyomi.util.system.Themes import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job import kotlinx.coroutines.flow.Flow @@ -123,8 +123,8 @@ class PreferencesHelper(val context: Context) { fun nightMode() = flowPrefs.getInt(Keys.nightMode, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) - fun lightTheme() = flowPrefs.getEnum(Keys.lightTheme, ThemeUtil.Themes.PURE_WHITE) - fun darkTheme() = flowPrefs.getEnum(Keys.darkTheme, ThemeUtil.Themes.DARK) + fun lightTheme() = flowPrefs.getEnum(Keys.lightTheme, Themes.PURE_WHITE) + fun darkTheme() = flowPrefs.getEnum(Keys.darkTheme, Themes.DARK) fun rotation() = flowPrefs.getInt(Keys.rotation, 1) 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 00c9761bd8..41f6bc62f4 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 @@ -21,7 +21,7 @@ import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.databinding.ThemeItemBinding import eu.kanade.tachiyomi.databinding.ThemesPreferenceBinding -import eu.kanade.tachiyomi.util.system.ThemeUtil +import eu.kanade.tachiyomi.util.system.Themes import eu.kanade.tachiyomi.util.system.appDelegateNightMode import eu.kanade.tachiyomi.util.system.dpToPx import eu.kanade.tachiyomi.util.system.isInNightMode @@ -74,8 +74,8 @@ class ThemePreference @JvmOverloads constructor(context: Context, attrs: Attribu } } - val enumConstants = ThemeUtil.Themes::class.java.enumConstants - itemAdapter.set(enumConstants?.map(::ThemeItem).orEmpty()) + val enumConstants = Themes.values() + itemAdapter.set(enumConstants.map(::ThemeItem)) isSelectable = false } @@ -113,7 +113,7 @@ class ThemePreference @JvmOverloads constructor(context: Context, attrs: Attribu } } - inner class ThemeItem(val theme: ThemeUtil.Themes) : AbstractItem>() { + inner class ThemeItem(val theme: Themes) : AbstractItem>() { /** defines the type defining this item. must be unique. preferably an id */ override val type: Int = R.id.theme_card_view @@ -129,6 +129,7 @@ class ThemePreference @JvmOverloads constructor(context: Context, attrs: Attribu val colors = theme.getColors() + @Suppress("UNUSED_PARAMETER") override var isSelected: Boolean get() = when (preferences.nightMode().get()) { AppCompatDelegate.MODE_NIGHT_YES -> preferences.darkTheme().get() == theme 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 faaefec92d..e4257c18f3 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 @@ -3,14 +3,9 @@ package eu.kanade.tachiyomi.util.system import android.app.Activity import android.content.Context import android.graphics.Color -import androidx.annotation.ColorInt -import androidx.annotation.StringRes -import androidx.annotation.StyleRes import androidx.appcompat.app.AppCompatDelegate -import androidx.core.graphics.ColorUtils import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.preference.PreferencesHelper -import kotlin.math.roundToInt object ThemeUtil { @@ -56,245 +51,6 @@ object ThemeUtil { else -> Color.WHITE } } - - @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 - ), - DUSK( - R.style.Theme_Tachiyomi_MidnightDusk, - AppCompatDelegate.MODE_NIGHT_YES, - R.string.midnight_dusk - ), - LIME( - R.style.Theme_Tachiyomi_FlatLime, - AppCompatDelegate.MODE_NIGHT_YES, - R.string.flat_lime - ), - AMOLED( - R.style.Theme_Tachiyomi_Amoled, - AppCompatDelegate.MODE_NIGHT_YES, - R.string.amoled_black - ), - BLACK_N_RED( - R.style.Theme_Tachiyomi_BlackAndRed, - AppCompatDelegate.MODE_NIGHT_YES, - R.string.black_and_red - ), - HOT_PINK( - R.style.Theme_Tachiyomi_HotPink, - AppCompatDelegate.MODE_NIGHT_YES, - R.string.hot_pink - ), - LIGHT_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, - R.string.dark_blue - ); - - fun getColors(mode: Int = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM): Colors { - return when (nightMode) { - AppCompatDelegate.MODE_NIGHT_YES -> darkColors() - AppCompatDelegate.MODE_NIGHT_NO -> lightColors() - AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM -> when (mode) { - AppCompatDelegate.MODE_NIGHT_YES -> darkColors() - else -> lightColors() - } - else -> lightColors() - } - } - - private fun lightColors(): Colors { - return Colors( - lightPrimaryText, - lightSecondaryText, - lightBackground, - lightAccent, - lightAppBar, - lightAppBarText, - lightBottomBar, - lightInactiveTab, - lightActiveTab, - ) - } - - private fun darkColors(): Colors { - return Colors( - darkPrimaryText, - darkSecondaryText, - darkBackground, - darkAccent, - darkAppBar, - darkAppBarText, - darkBottomBar, - darkInactiveTab, - darkActiveTab, - ) - } - - /** Complies with textColorPrimary (probably night) */ - @ColorInt - val lightPrimaryText: Int = Color.parseColor( - when (styleRes) { - R.style.Theme_Tachiyomi_MidnightDusk -> "#DE240728" - else -> "#DE000000" - } - ) - - /** Complies with textColorPrimary (probably night) */ - @ColorInt - val darkPrimaryText: Int = Color.parseColor("#FFFFFFFF") - - /** Complies with textColorSecondary (primary with alpha) */ - @ColorInt - val lightSecondaryText: Int = ColorUtils.setAlphaComponent(lightPrimaryText, (0.54f * 255f).roundToInt()) - - /** Complies with textColorSecondary (primary with alpha) */ - @ColorInt - val darkSecondaryText: Int = ColorUtils.setAlphaComponent(darkPrimaryText, (0.54f * 255f).roundToInt()) - - /** Complies with colorBackground */ - @ColorInt - val lightBackground: Int = Color.parseColor( - when (styleRes) { - R.style.Theme_Tachiyomi_MidnightDusk -> "#f6f0f8" - else -> "#FAFAFA" - } - ) - - /** Complies with colorBackground (probably night) */ - @ColorInt - val darkBackground: Int = Color.parseColor( - when (styleRes) { - R.style.Theme_Tachiyomi_Amoled, R.style.Theme_Tachiyomi_BlackAndRed, R.style.Theme_Tachiyomi_HotPink -> "#000000" - R.style.Theme_Tachiyomi_MidnightDusk -> "#16151D" - R.style.Theme_Tachiyomi_FlatLime -> "#202125" - else -> "#1C1C1D" - } - ) - - /** Complies with colorAccent */ - @ColorInt - val lightAccent: Int = Color.parseColor( - when (styleRes) { - R.style.Theme_Tachiyomi_MidnightDusk -> "#c43c97" - else -> "#2979FF" - } - ) - - /** Complies with colorAccent (probably night) */ - @ColorInt - val darkAccent: Int = Color.parseColor( - when (styleRes) { - R.style.Theme_Tachiyomi_MidnightDusk -> "#F02475" - R.style.Theme_Tachiyomi_BlackAndRed -> "#AA2200" - R.style.Theme_Tachiyomi_HotPink -> "#FF3399" - R.style.Theme_Tachiyomi_FlatLime -> "#4AF88A" - else -> "#3399FF" - } - ) - - /** Complies with colorSecondary */ - @ColorInt - val lightAppBar: Int = when (styleRes) { - R.style.Theme_Tachiyomi_AllBlue -> Color.parseColor("#54759E") - else -> lightBackground - } - - /** Complies with colorSecondary (probably night) */ - @ColorInt - val darkAppBar: Int = when (styleRes) { - R.style.Theme_Tachiyomi_AllBlue -> Color.parseColor("#54759E") - else -> darkBackground - } - - /** Complies with actionBarTintColor */ - @ColorInt - val lightAppBarText: Int = when (styleRes) { - R.style.Theme_Tachiyomi_AllBlue -> Color.parseColor("#FFFFFF") - R.style.Theme_Tachiyomi_MidnightDusk -> Color.parseColor("#DE4c0d4b") - else -> lightPrimaryText - } - - /** Complies with actionBarTintColor (probably night) */ - @ColorInt - val darkAppBarText: Int = when (styleRes) { - R.style.Theme_Tachiyomi_AllBlue -> Color.parseColor("#FFFFFF") - else -> darkPrimaryText - } - - /** Complies with colorPrimaryVariant */ - @ColorInt - val lightBottomBar: Int = Color.parseColor( - when (styleRes) { - R.style.Theme_Tachiyomi_AllBlue -> "#54759E" - R.style.Theme_Tachiyomi_MidnightDusk -> "#efe3f3" - else -> "#FFFFFF" - } - ) - - /** Complies with colorPrimaryVariant (probably night) */ - @ColorInt - val darkBottomBar: Int = Color.parseColor( - when (styleRes) { - R.style.Theme_Tachiyomi_AllBlue -> "#54759E" - R.style.Theme_Tachiyomi_Amoled, R.style.Theme_Tachiyomi_BlackAndRed, R.style.Theme_Tachiyomi_HotPink -> "#000000" - R.style.Theme_Tachiyomi_MidnightDusk -> "#201F27" - R.style.Theme_Tachiyomi_FlatLime -> "#282A2E" - else -> "#212121" - } - ) - - /** Complies with tabBarIconInactive */ - @ColorInt - val lightInactiveTab: Int = when (styleRes) { - R.style.Theme_Tachiyomi_AllBlue -> Color.parseColor("#80FFFFFF") - else -> Color.parseColor("#C2424242") - } - - /** Complies with tabBarIconInactive (probably night) */ - @ColorInt - val darkInactiveTab: Int = when (styleRes) { - R.style.Theme_Tachiyomi_AllBlue -> Color.parseColor("#80FFFFFF") - else -> Color.parseColor("#C2FFFFFF") - } - - /** Complies with tabBarIconColor */ - @ColorInt - val lightActiveTab: Int = when (styleRes) { - R.style.Theme_Tachiyomi_AllBlue -> lightAppBarText - else -> lightAccent - } - - /** Complies with tabBarIconColor (probably night) */ - @ColorInt - val darkActiveTab: Int = when (styleRes) { - R.style.Theme_Tachiyomi_AllBlue -> darkAppBarText - else -> darkAccent - } - } - - data class Colors( - @ColorInt val primaryText: Int, - @ColorInt val secondaryText: Int, - @ColorInt val colorBackground: Int, - @ColorInt val colorAccent: Int, - @ColorInt val appBar: Int, - @ColorInt val appBarText: Int, - @ColorInt val bottomBar: Int, - @ColorInt val inactiveTab: Int, - @ColorInt val activeTab: Int, - ) } fun Activity.setThemeAndNight(preferences: PreferencesHelper) { @@ -306,7 +62,7 @@ fun Activity.setThemeAndNight(preferences: PreferencesHelper) { setTheme(theme.styleRes) } -fun Context.getPrefTheme(preferences: PreferencesHelper): ThemeUtil.Themes { +fun Context.getPrefTheme(preferences: PreferencesHelper): Themes { // Using a try catch in case I start to remove themes return try { ( @@ -315,8 +71,8 @@ fun Context.getPrefTheme(preferences: PreferencesHelper): ThemeUtil.Themes { ) preferences.darkTheme() else preferences.lightTheme() ).get() } catch (e: Exception) { - preferences.lightTheme().set(ThemeUtil.Themes.PURE_WHITE) - preferences.darkTheme().set(ThemeUtil.Themes.DARK) - ThemeUtil.Themes.PURE_WHITE + preferences.lightTheme().set(Themes.PURE_WHITE) + preferences.darkTheme().set(Themes.DARK) + Themes.PURE_WHITE } } 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 new file mode 100644 index 0000000000..c720eeb4d9 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/util/system/Themes.kt @@ -0,0 +1,249 @@ +package eu.kanade.tachiyomi.util.system + +import android.graphics.Color +import androidx.annotation.ColorInt +import androidx.annotation.StringRes +import androidx.annotation.StyleRes +import androidx.appcompat.app.AppCompatDelegate +import androidx.core.graphics.ColorUtils +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 + ), + DUSK( + R.style.Theme_Tachiyomi_MidnightDusk, + AppCompatDelegate.MODE_NIGHT_YES, + R.string.midnight_dusk + ), + LIME( + R.style.Theme_Tachiyomi_FlatLime, + AppCompatDelegate.MODE_NIGHT_YES, + R.string.flat_lime + ), + AMOLED( + R.style.Theme_Tachiyomi_Amoled, + AppCompatDelegate.MODE_NIGHT_YES, + R.string.amoled_black + ), + BLACK_N_RED( + R.style.Theme_Tachiyomi_BlackAndRed, + AppCompatDelegate.MODE_NIGHT_YES, + R.string.black_and_red + ), + HOT_PINK( + R.style.Theme_Tachiyomi_HotPink, + AppCompatDelegate.MODE_NIGHT_YES, + R.string.hot_pink + ), + LIGHT_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, + R.string.dark_blue + ); + + fun getColors(mode: Int = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM): Colors { + return when (nightMode) { + AppCompatDelegate.MODE_NIGHT_YES -> darkColors() + AppCompatDelegate.MODE_NIGHT_NO -> lightColors() + AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM -> when (mode) { + AppCompatDelegate.MODE_NIGHT_YES -> darkColors() + else -> lightColors() + } + else -> lightColors() + } + } + + private fun lightColors(): Colors { + return Colors( + lightPrimaryText, + lightSecondaryText, + lightBackground, + lightAccent, + lightAppBar, + lightAppBarText, + lightBottomBar, + lightInactiveTab, + lightActiveTab, + ) + } + + private fun darkColors(): Colors { + return Colors( + darkPrimaryText, + darkSecondaryText, + darkBackground, + darkAccent, + darkAppBar, + darkAppBarText, + darkBottomBar, + darkInactiveTab, + darkActiveTab, + ) + } + + /** Complies with textColorPrimary (probably night) */ + @ColorInt + val lightPrimaryText: Int = Color.parseColor( + when (styleRes) { + R.style.Theme_Tachiyomi_MidnightDusk -> "#DE240728" + else -> "#DE000000" + } + ) + + /** Complies with textColorPrimary (probably night) */ + @ColorInt + val darkPrimaryText: Int = Color.parseColor("#FFFFFFFF") + + /** Complies with textColorSecondary (primary with alpha) */ + @ColorInt + val lightSecondaryText: Int = ColorUtils.setAlphaComponent(lightPrimaryText, (0.54f * 255f).roundToInt()) + + /** Complies with textColorSecondary (primary with alpha) */ + @ColorInt + val darkSecondaryText: Int = ColorUtils.setAlphaComponent(darkPrimaryText, (0.54f * 255f).roundToInt()) + + /** Complies with colorBackground */ + @ColorInt + val lightBackground: Int = Color.parseColor( + when (styleRes) { + R.style.Theme_Tachiyomi_MidnightDusk -> "#f6f0f8" + else -> "#FAFAFA" + } + ) + + /** Complies with colorBackground (probably night) */ + @ColorInt + val darkBackground: Int = Color.parseColor( + when (styleRes) { + R.style.Theme_Tachiyomi_Amoled, R.style.Theme_Tachiyomi_BlackAndRed, R.style.Theme_Tachiyomi_HotPink -> "#000000" + R.style.Theme_Tachiyomi_MidnightDusk -> "#16151D" + R.style.Theme_Tachiyomi_FlatLime -> "#202125" + else -> "#1C1C1D" + } + ) + + /** Complies with colorAccent */ + @ColorInt + val lightAccent: Int = Color.parseColor( + when (styleRes) { + R.style.Theme_Tachiyomi_MidnightDusk -> "#c43c97" + else -> "#2979FF" + } + ) + + /** Complies with colorAccent (probably night) */ + @ColorInt + val darkAccent: Int = Color.parseColor( + when (styleRes) { + R.style.Theme_Tachiyomi_MidnightDusk -> "#F02475" + R.style.Theme_Tachiyomi_BlackAndRed -> "#AA2200" + R.style.Theme_Tachiyomi_HotPink -> "#FF3399" + R.style.Theme_Tachiyomi_FlatLime -> "#4AF88A" + else -> "#3399FF" + } + ) + + /** Complies with colorSecondary */ + @ColorInt + val lightAppBar: Int = when (styleRes) { + R.style.Theme_Tachiyomi_AllBlue -> Color.parseColor("#54759E") + else -> lightBackground + } + + /** Complies with colorSecondary (probably night) */ + @ColorInt + val darkAppBar: Int = when (styleRes) { + R.style.Theme_Tachiyomi_AllBlue -> Color.parseColor("#54759E") + else -> darkBackground + } + + /** Complies with actionBarTintColor */ + @ColorInt + val lightAppBarText: Int = when (styleRes) { + R.style.Theme_Tachiyomi_AllBlue -> Color.parseColor("#FFFFFF") + R.style.Theme_Tachiyomi_MidnightDusk -> Color.parseColor("#DE4c0d4b") + else -> lightPrimaryText + } + + /** Complies with actionBarTintColor (probably night) */ + @ColorInt + val darkAppBarText: Int = when (styleRes) { + R.style.Theme_Tachiyomi_AllBlue -> Color.parseColor("#FFFFFF") + else -> darkPrimaryText + } + + /** Complies with colorPrimaryVariant */ + @ColorInt + val lightBottomBar: Int = Color.parseColor( + when (styleRes) { + R.style.Theme_Tachiyomi_AllBlue -> "#54759E" + R.style.Theme_Tachiyomi_MidnightDusk -> "#efe3f3" + else -> "#FFFFFF" + } + ) + + /** Complies with colorPrimaryVariant (probably night) */ + @ColorInt + val darkBottomBar: Int = Color.parseColor( + when (styleRes) { + R.style.Theme_Tachiyomi_AllBlue -> "#54759E" + R.style.Theme_Tachiyomi_Amoled, R.style.Theme_Tachiyomi_BlackAndRed, R.style.Theme_Tachiyomi_HotPink -> "#000000" + R.style.Theme_Tachiyomi_MidnightDusk -> "#201F27" + R.style.Theme_Tachiyomi_FlatLime -> "#282A2E" + else -> "#212121" + } + ) + + /** Complies with tabBarIconInactive */ + @ColorInt + val lightInactiveTab: Int = when (styleRes) { + R.style.Theme_Tachiyomi_AllBlue -> Color.parseColor("#80FFFFFF") + else -> Color.parseColor("#C2424242") + } + + /** Complies with tabBarIconInactive (probably night) */ + @ColorInt + val darkInactiveTab: Int = when (styleRes) { + R.style.Theme_Tachiyomi_AllBlue -> Color.parseColor("#80FFFFFF") + else -> Color.parseColor("#C2FFFFFF") + } + + /** Complies with tabBarIconColor */ + @ColorInt + val lightActiveTab: Int = when (styleRes) { + R.style.Theme_Tachiyomi_AllBlue -> lightAppBarText + else -> lightAccent + } + + /** Complies with tabBarIconColor (probably night) */ + @ColorInt + val darkActiveTab: Int = when (styleRes) { + R.style.Theme_Tachiyomi_AllBlue -> darkAppBarText + else -> darkAccent + } + + data class Colors( + @ColorInt val primaryText: Int, + @ColorInt val secondaryText: Int, + @ColorInt val colorBackground: Int, + @ColorInt val colorAccent: Int, + @ColorInt val appBar: Int, + @ColorInt val appBarText: Int, + @ColorInt val bottomBar: Int, + @ColorInt val inactiveTab: Int, + @ColorInt val activeTab: Int, + ) +}