Minor refactor of theming when expressions

* 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

Co-authored-by: MajorTanya <39014446+MajorTanya@users.noreply.github.com>
This commit is contained in:
Secozzi 2024-06-05 15:16:07 +02:00
parent 2448072923
commit a2c293de8c
No known key found for this signature in database
GPG key ID: 71E9C97D8DDC2662
2 changed files with 48 additions and 82 deletions

View file

@ -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.CloudflareColorScheme
import eu.kanade.presentation.theme.colorscheme.CottoncandyColorScheme
import eu.kanade.presentation.theme.colorscheme.DoomColorScheme
@ -69,29 +70,33 @@ private fun getThemeColorScheme(
isAmoled: Boolean,
): ColorScheme {
val uiPreferences = Injekt.get<UiPreferences>()
val colorScheme = when (appTheme) {
AppTheme.DEFAULT -> TachiyomiColorScheme
AppTheme.MONET -> MonetColorScheme(LocalContext.current)
AppTheme.CLOUDFLARE -> CloudflareColorScheme
AppTheme.COTTONCANDY -> CottoncandyColorScheme
AppTheme.DOOM -> DoomColorScheme
AppTheme.GREEN_APPLE -> GreenAppleColorScheme
AppTheme.LAVENDER -> LavenderColorScheme
AppTheme.MATRIX -> MatrixColorScheme
AppTheme.MIDNIGHT_DUSK -> MidnightDuskColorScheme
AppTheme.MOCHA -> MochaColorScheme
AppTheme.SAPPHIRE -> SapphireColorScheme
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<AppTheme, BaseColorScheme> = mapOf(
AppTheme.DEFAULT to TachiyomiColorScheme,
AppTheme.CLOUDFLARE to CloudflareColorScheme,
AppTheme.COTTONCANDY to CottoncandyColorScheme,
AppTheme.DOOM to DoomColorScheme,
AppTheme.GREEN_APPLE to GreenAppleColorScheme,
AppTheme.LAVENDER to LavenderColorScheme,
AppTheme.MATRIX to MatrixColorScheme,
AppTheme.MIDNIGHT_DUSK to MidnightDuskColorScheme,
AppTheme.MOCHA to MochaColorScheme,
AppTheme.SAPPHIRE to SapphireColorScheme,
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,
)

View file

@ -12,69 +12,10 @@ interface ThemingDelegate {
companion object {
fun getThemeResIds(appTheme: AppTheme, isAmoled: Boolean): List<Int> {
val resIds = mutableListOf<Int>()
when (appTheme) {
AppTheme.MONET -> {
resIds += R.style.Theme_Tachiyomi_Monet
}
AppTheme.COTTONCANDY -> {
resIds += R.style.Theme_Tachiyomi_CottonCandy
}
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.MOCHA -> {
resIds += R.style.Theme_Tachiyomi_Mocha
}
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.CLOUDFLARE -> {
resIds += R.style.Theme_Tachiyomi_Cloudflare
}
AppTheme.SAPPHIRE -> {
resIds += R.style.Theme_Tachiyomi_Sapphire
}
AppTheme.DOOM -> {
resIds += R.style.Theme_Tachiyomi_Doom
}
AppTheme.MATRIX -> {
resIds += R.style.Theme_Tachiyomi_Matrix
}
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
}
}
}
@ -89,3 +30,23 @@ class ThemingDelegateImpl : ThemingDelegate {
.forEach(activity::setTheme)
}
}
private val themeResources: Map<AppTheme, Int> = mapOf(
AppTheme.MONET to R.style.Theme_Tachiyomi_Monet,
AppTheme.COTTONCANDY to R.style.Theme_Tachiyomi_CottonCandy,
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.MOCHA to R.style.Theme_Tachiyomi_Mocha,
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.CLOUDFLARE to R.style.Theme_Tachiyomi_Cloudflare,
AppTheme.SAPPHIRE to R.style.Theme_Tachiyomi_Sapphire,
AppTheme.DOOM to R.style.Theme_Tachiyomi_Doom,
AppTheme.MATRIX to R.style.Theme_Tachiyomi_Matrix,
AppTheme.TIDAL_WAVE to R.style.Theme_Tachiyomi_TidalWave,
)