From 96c236e5c38248c875f2ac7596cd51845aa651ea Mon Sep 17 00:00:00 2001 From: MajorTanya <39014446+MajorTanya@users.noreply.github.com> Date: Wed, 14 Feb 2024 21:35:16 +0100 Subject: [PATCH] Minor refactor of theming when expressions (#396) * Minor refactor of theming when expressions Avoids triggering detekt's CyclomaticComplexMethod warning because of too many when branches, which would happen with one more theme being added in these two locations. In TachiyomiTheme, the Monet theme is separated because it requires the current Compose context to function. The other themes do not and are delegated to a Map. * Implement requested changes - moved themeResources out of the ThemingDelegate interface - replaced single condition when with if expression --- .../presentation/theme/TachiyomiTheme.kt | 33 +++++----- .../ui/base/delegate/ThemingDelegate.kt | 61 ++++++------------- 2 files changed, 36 insertions(+), 58 deletions(-) diff --git a/app/src/main/java/eu/kanade/presentation/theme/TachiyomiTheme.kt b/app/src/main/java/eu/kanade/presentation/theme/TachiyomiTheme.kt index 4dd9a9754..af550aa5d 100644 --- a/app/src/main/java/eu/kanade/presentation/theme/TachiyomiTheme.kt +++ b/app/src/main/java/eu/kanade/presentation/theme/TachiyomiTheme.kt @@ -8,6 +8,7 @@ import androidx.compose.runtime.ReadOnlyComposable import androidx.compose.ui.platform.LocalContext import eu.kanade.domain.ui.UiPreferences import eu.kanade.domain.ui.model.AppTheme +import eu.kanade.presentation.theme.colorscheme.BaseColorScheme import eu.kanade.presentation.theme.colorscheme.GreenAppleColorScheme import eu.kanade.presentation.theme.colorscheme.LavenderColorScheme import eu.kanade.presentation.theme.colorscheme.MidnightDuskColorScheme @@ -62,23 +63,27 @@ private fun getThemeColorScheme( appTheme: AppTheme, isAmoled: Boolean, ): ColorScheme { - val colorScheme = when (appTheme) { - AppTheme.DEFAULT -> TachiyomiColorScheme - AppTheme.MONET -> MonetColorScheme(LocalContext.current) - AppTheme.GREEN_APPLE -> GreenAppleColorScheme - AppTheme.LAVENDER -> LavenderColorScheme - AppTheme.MIDNIGHT_DUSK -> MidnightDuskColorScheme - AppTheme.NORD -> NordColorScheme - AppTheme.STRAWBERRY_DAIQUIRI -> StrawberryColorScheme - AppTheme.TAKO -> TakoColorScheme - AppTheme.TEALTURQUOISE -> TealTurqoiseColorScheme - AppTheme.TIDAL_WAVE -> TidalWaveColorScheme - AppTheme.YINYANG -> YinYangColorScheme - AppTheme.YOTSUBA -> YotsubaColorScheme - else -> TachiyomiColorScheme + val colorScheme = if (appTheme == AppTheme.MONET) { + MonetColorScheme(LocalContext.current) + } else { + colorSchemes.getOrDefault(appTheme, TachiyomiColorScheme) } return colorScheme.getColorScheme( isSystemInDarkTheme(), isAmoled, ) } + +private val colorSchemes: Map = mapOf( + AppTheme.DEFAULT to TachiyomiColorScheme, + AppTheme.GREEN_APPLE to GreenAppleColorScheme, + AppTheme.LAVENDER to LavenderColorScheme, + AppTheme.MIDNIGHT_DUSK to MidnightDuskColorScheme, + AppTheme.NORD to NordColorScheme, + AppTheme.STRAWBERRY_DAIQUIRI to StrawberryColorScheme, + AppTheme.TAKO to TakoColorScheme, + AppTheme.TEALTURQUOISE to TealTurqoiseColorScheme, + AppTheme.TIDAL_WAVE to TidalWaveColorScheme, + AppTheme.YINYANG to YinYangColorScheme, + AppTheme.YOTSUBA to YotsubaColorScheme, +) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/delegate/ThemingDelegate.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/delegate/ThemingDelegate.kt index c792333e9..8b3572f08 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/delegate/ThemingDelegate.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/delegate/ThemingDelegate.kt @@ -12,51 +12,10 @@ interface ThemingDelegate { companion object { fun getThemeResIds(appTheme: AppTheme, isAmoled: Boolean): List { - val resIds = mutableListOf() - when (appTheme) { - AppTheme.MONET -> { - resIds += R.style.Theme_Tachiyomi_Monet - } - AppTheme.GREEN_APPLE -> { - resIds += R.style.Theme_Tachiyomi_GreenApple - } - AppTheme.LAVENDER -> { - resIds += R.style.Theme_Tachiyomi_Lavender - } - AppTheme.MIDNIGHT_DUSK -> { - resIds += R.style.Theme_Tachiyomi_MidnightDusk - } - AppTheme.NORD -> { - resIds += R.style.Theme_Tachiyomi_Nord - } - AppTheme.STRAWBERRY_DAIQUIRI -> { - resIds += R.style.Theme_Tachiyomi_StrawberryDaiquiri - } - AppTheme.TAKO -> { - resIds += R.style.Theme_Tachiyomi_Tako - } - AppTheme.TEALTURQUOISE -> { - resIds += R.style.Theme_Tachiyomi_TealTurquoise - } - AppTheme.YINYANG -> { - resIds += R.style.Theme_Tachiyomi_YinYang - } - AppTheme.YOTSUBA -> { - resIds += R.style.Theme_Tachiyomi_Yotsuba - } - AppTheme.TIDAL_WAVE -> { - resIds += R.style.Theme_Tachiyomi_TidalWave - } - else -> { - resIds += R.style.Theme_Tachiyomi - } + return buildList(2) { + add(themeResources.getOrDefault(appTheme, R.style.Theme_Tachiyomi)) + if (isAmoled) add(R.style.ThemeOverlay_Tachiyomi_Amoled) } - - if (isAmoled) { - resIds += R.style.ThemeOverlay_Tachiyomi_Amoled - } - - return resIds } } } @@ -68,3 +27,17 @@ class ThemingDelegateImpl : ThemingDelegate { .forEach(activity::setTheme) } } + +private val themeResources: Map = mapOf( + AppTheme.MONET to R.style.Theme_Tachiyomi_Monet, + AppTheme.GREEN_APPLE to R.style.Theme_Tachiyomi_GreenApple, + AppTheme.LAVENDER to R.style.Theme_Tachiyomi_Lavender, + AppTheme.MIDNIGHT_DUSK to R.style.Theme_Tachiyomi_MidnightDusk, + AppTheme.NORD to R.style.Theme_Tachiyomi_Nord, + AppTheme.STRAWBERRY_DAIQUIRI to R.style.Theme_Tachiyomi_StrawberryDaiquiri, + AppTheme.TAKO to R.style.Theme_Tachiyomi_Tako, + AppTheme.TEALTURQUOISE to R.style.Theme_Tachiyomi_TealTurquoise, + AppTheme.YINYANG to R.style.Theme_Tachiyomi_YinYang, + AppTheme.YOTSUBA to R.style.Theme_Tachiyomi_Yotsuba, + AppTheme.TIDAL_WAVE to R.style.Theme_Tachiyomi_TidalWave, +)