chore: default system theme (#227)

This commit is contained in:
Diego Beraldin 2023-12-06 10:43:01 +01:00 committed by GitHub
parent 09fb8ab11c
commit fcc2c8fe2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 48 additions and 29 deletions

View File

@ -14,23 +14,26 @@ sealed interface UiTheme {
data object Black : UiTheme data object Black : UiTheme
} }
fun Int.toUiTheme() = when (this) { fun Int?.toUiTheme(): UiTheme? = when (this) {
2 -> UiTheme.Black 2 -> UiTheme.Black
1 -> UiTheme.Dark 1 -> UiTheme.Dark
else -> UiTheme.Light 0 -> UiTheme.Light
else -> null
} }
fun UiTheme.toInt() = when (this) { fun UiTheme?.toInt(): Int? = when (this) {
UiTheme.Black -> 2 UiTheme.Black -> 2
UiTheme.Dark -> 1 UiTheme.Dark -> 1
UiTheme.Light -> 0 UiTheme.Light -> 0
else -> null
} }
@Composable @Composable
fun UiTheme.toReadableName() = when (this) { fun UiTheme?.toReadableName() = when (this) {
UiTheme.Black -> stringResource(MR.strings.settings_theme_black) UiTheme.Black -> stringResource(MR.strings.settings_theme_black)
UiTheme.Dark -> stringResource(MR.strings.settings_theme_dark) UiTheme.Dark -> stringResource(MR.strings.settings_theme_dark)
UiTheme.Light -> stringResource(MR.strings.settings_theme_light) UiTheme.Light -> stringResource(MR.strings.settings_theme_light)
else -> stringResource(MR.strings.settings_font_family_default)
} }
fun UiTheme.toIcon() = when (this) { fun UiTheme.toIcon() = when (this) {

View File

@ -9,8 +9,8 @@ import kotlinx.coroutines.flow.MutableStateFlow
internal class DefaultThemeRepository : ThemeRepository { internal class DefaultThemeRepository : ThemeRepository {
override val uiTheme = MutableStateFlow<UiTheme>(UiTheme.Light) override val uiTheme = MutableStateFlow<UiTheme?>(null)
override val uiFontFamily = MutableStateFlow<UiFontFamily>(UiFontFamily.TitilliumWeb) override val uiFontFamily = MutableStateFlow(UiFontFamily.Poppins)
override val uiFontScale = MutableStateFlow(1f) override val uiFontScale = MutableStateFlow(1f)
override val contentFontScale = MutableStateFlow(1f) override val contentFontScale = MutableStateFlow(1f)
override val navItemTitles = MutableStateFlow(false) override val navItemTitles = MutableStateFlow(false)
@ -20,7 +20,7 @@ internal class DefaultThemeRepository : ThemeRepository {
override val downvoteColor = MutableStateFlow<Color?>(null) override val downvoteColor = MutableStateFlow<Color?>(null)
override val postLayout = MutableStateFlow<PostLayout>(PostLayout.Card) override val postLayout = MutableStateFlow<PostLayout>(PostLayout.Card)
override fun changeUiTheme(value: UiTheme) { override fun changeUiTheme(value: UiTheme?) {
uiTheme.value = value uiTheme.value = value
} }

View File

@ -10,7 +10,7 @@ import kotlinx.coroutines.flow.StateFlow
@Stable @Stable
interface ThemeRepository { interface ThemeRepository {
val uiTheme: StateFlow<UiTheme> val uiTheme: StateFlow<UiTheme?>
val uiFontFamily: StateFlow<UiFontFamily> val uiFontFamily: StateFlow<UiFontFamily>
val uiFontScale: StateFlow<Float> val uiFontScale: StateFlow<Float>
val contentFontScale: StateFlow<Float> val contentFontScale: StateFlow<Float>
@ -21,7 +21,7 @@ interface ThemeRepository {
val downvoteColor: StateFlow<Color?> val downvoteColor: StateFlow<Color?>
val postLayout: StateFlow<PostLayout> val postLayout: StateFlow<PostLayout>
fun changeUiTheme(value: UiTheme) fun changeUiTheme(value: UiTheme?)
fun changeUiFontFamily(value: UiFontFamily) fun changeUiFontFamily(value: UiFontFamily)

View File

@ -1,5 +1,6 @@
package com.github.diegoberaldin.raccoonforlemmy.core.appearance.theme package com.github.diegoberaldin.raccoonforlemmy.core.appearance.theme
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState import androidx.compose.runtime.collectAsState
@ -12,7 +13,7 @@ import com.github.diegoberaldin.raccoonforlemmy.core.appearance.di.getThemeRepos
@Composable @Composable
fun AppTheme( fun AppTheme(
theme: UiTheme, theme: UiTheme?,
contentFontScale: Float, contentFontScale: Float,
useDynamicColors: Boolean, useDynamicColors: Boolean,
content: @Composable () -> Unit, content: @Composable () -> Unit,
@ -26,10 +27,15 @@ fun AppTheme(
val themeState by repository.uiTheme.collectAsState() val themeState by repository.uiTheme.collectAsState()
val customSeedColor by repository.customSeedColor.collectAsState() val customSeedColor by repository.customSeedColor.collectAsState()
val defaultTheme = if (isSystemInDarkTheme()) {
UiTheme.Dark
} else {
UiTheme.Light
}
val colorSchemeProvider = remember { getColorSchemeProvider() } val colorSchemeProvider = remember { getColorSchemeProvider() }
val colorScheme = colorSchemeProvider.getColorScheme( val colorScheme = colorSchemeProvider.getColorScheme(
theme = themeState, theme = themeState ?: defaultTheme,
dynamic = useDynamicColors, dynamic = useDynamicColors,
customSeed = customSeedColor, customSeed = customSeedColor,
) )
@ -38,7 +44,7 @@ fun AppTheme(
val typography = getTypography(fontFamily) val typography = getTypography(fontFamily)
val barColorProvider = remember { getBarColorProvider() } val barColorProvider = remember { getBarColorProvider() }
barColorProvider.setBarColorAccordingToTheme(theme) barColorProvider.setBarColorAccordingToTheme(theme ?: defaultTheme)
MaterialTheme( MaterialTheme(
colorScheme = colorScheme, colorScheme = colorScheme,

View File

@ -62,6 +62,7 @@ class ThemeBottomSheet : Screen {
UiTheme.Light, UiTheme.Light,
UiTheme.Dark, UiTheme.Dark,
UiTheme.Black, UiTheme.Black,
null,
) )
Column( Column(
modifier = Modifier.fillMaxWidth().verticalScroll(rememberScrollState()), modifier = Modifier.fillMaxWidth().verticalScroll(rememberScrollState()),
@ -89,11 +90,13 @@ class ThemeBottomSheet : Screen {
color = MaterialTheme.colorScheme.onBackground, color = MaterialTheme.colorScheme.onBackground,
) )
Spacer(modifier = Modifier.weight(1f)) Spacer(modifier = Modifier.weight(1f))
Image( if (value != null) {
imageVector = value.toIcon(), Image(
contentDescription = null, imageVector = value.toIcon(),
colorFilter = ColorFilter.tint(MaterialTheme.colorScheme.onBackground), contentDescription = null,
) colorFilter = ColorFilter.tint(MaterialTheme.colorScheme.onBackground),
)
}
} }
} }
} }

View File

@ -25,7 +25,7 @@ sealed interface NotificationCenterEvent {
NotificationCenterEvent NotificationCenterEvent
data class ChangeInboxType(val unreadOnly: Boolean) : NotificationCenterEvent data class ChangeInboxType(val unreadOnly: Boolean) : NotificationCenterEvent
data class ChangeTheme(val value: UiTheme) : NotificationCenterEvent data class ChangeTheme(val value: UiTheme?) : NotificationCenterEvent
data class ChangeContentFontSize(val value: Float) : NotificationCenterEvent data class ChangeContentFontSize(val value: Float) : NotificationCenterEvent
data class ChangeUiFontSize(val value: Float) : NotificationCenterEvent data class ChangeUiFontSize(val value: Float) : NotificationCenterEvent
data class ChangeFontFamily(val value: UiFontFamily) : NotificationCenterEvent data class ChangeFontFamily(val value: UiFontFamily) : NotificationCenterEvent

View File

@ -18,7 +18,7 @@ interface SettingsMviModel :
ScreenModel { ScreenModel {
sealed interface Intent { sealed interface Intent {
data class ChangeUiTheme(val value: UiTheme) : Intent data class ChangeUiTheme(val value: UiTheme?) : Intent
data class ChangeUiFontSize(val value: Float) : Intent data class ChangeUiFontSize(val value: Float) : Intent
data class ChangeUiFontFamily(val value: UiFontFamily) : Intent data class ChangeUiFontFamily(val value: UiFontFamily) : Intent
data class ChangeContentFontSize(val value: Float) : Intent data class ChangeContentFontSize(val value: Float) : Intent
@ -51,7 +51,7 @@ interface SettingsMviModel :
data class UiState( data class UiState(
val isLogged: Boolean = false, val isLogged: Boolean = false,
val uiTheme: UiTheme = UiTheme.Light, val uiTheme: UiTheme? = null,
val uiFontFamily: UiFontFamily = UiFontFamily.TitilliumWeb, val uiFontFamily: UiFontFamily = UiFontFamily.TitilliumWeb,
val customSeedColor: Color? = null, val customSeedColor: Color? = null,
val upvoteColor: Color? = null, val upvoteColor: Color? = null,

View File

@ -1,6 +1,7 @@
package com.github.diegoberaldin.raccoonforlemmy.feature.settings.main package com.github.diegoberaldin.raccoonforlemmy.feature.settings.main
import androidx.compose.foundation.Image import androidx.compose.foundation.Image
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
@ -40,6 +41,7 @@ import androidx.compose.ui.unit.toSize
import cafe.adriel.voyager.core.model.rememberScreenModel import cafe.adriel.voyager.core.model.rememberScreenModel
import cafe.adriel.voyager.core.screen.Screen import cafe.adriel.voyager.core.screen.Screen
import com.github.diegoberaldin.raccoonforlemmy.core.appearance.data.FontScale import com.github.diegoberaldin.raccoonforlemmy.core.appearance.data.FontScale
import com.github.diegoberaldin.raccoonforlemmy.core.appearance.data.UiTheme
import com.github.diegoberaldin.raccoonforlemmy.core.appearance.data.toReadableName import com.github.diegoberaldin.raccoonforlemmy.core.appearance.data.toReadableName
import com.github.diegoberaldin.raccoonforlemmy.core.appearance.di.getColorSchemeProvider import com.github.diegoberaldin.raccoonforlemmy.core.appearance.di.getColorSchemeProvider
import com.github.diegoberaldin.raccoonforlemmy.core.appearance.di.getThemeRepository import com.github.diegoberaldin.raccoonforlemmy.core.appearance.di.getThemeRepository
@ -107,6 +109,12 @@ class SettingsScreen : Screen {
val lang by languageRepository.currentLanguage.collectAsState() val lang by languageRepository.currentLanguage.collectAsState()
var uiFontSizeWorkaround by remember { mutableStateOf(true) } var uiFontSizeWorkaround by remember { mutableStateOf(true) }
val themeRepository = remember { getThemeRepository() } val themeRepository = remember { getThemeRepository() }
val colorSchemeProvider = remember { getColorSchemeProvider() }
val defaultTheme = if (isSystemInDarkTheme()) {
UiTheme.Dark
} else {
UiTheme.Light
}
var upvoteColorDialogOpened by remember { mutableStateOf(false) } var upvoteColorDialogOpened by remember { mutableStateOf(false) }
var downvoteColorDialogOpened by remember { mutableStateOf(false) } var downvoteColorDialogOpened by remember { mutableStateOf(false) }
var infoDialogOpened by remember { mutableStateOf(false) } var infoDialogOpened by remember { mutableStateOf(false) }
@ -298,12 +306,11 @@ class SettingsScreen : Screen {
) )
} }
val colorSchemeProvider = remember { getColorSchemeProvider() }
// custom scheme seed color // custom scheme seed color
SettingsColorRow( SettingsColorRow(
title = stringResource(MR.strings.settings_custom_seed_color), title = stringResource(MR.strings.settings_custom_seed_color),
value = uiState.customSeedColor ?: colorSchemeProvider.getColorScheme( value = uiState.customSeedColor ?: colorSchemeProvider.getColorScheme(
theme = uiState.uiTheme, theme = uiState.uiTheme ?: defaultTheme,
dynamic = uiState.dynamicColors, dynamic = uiState.dynamicColors,
).primary, ).primary,
onTap = rememberCallback { onTap = rememberCallback {
@ -645,7 +652,7 @@ class SettingsScreen : Screen {
onReset = rememberCallback(model) { onReset = rememberCallback(model) {
upvoteColorDialogOpened = false upvoteColorDialogOpened = false
val scheme = getColorSchemeProvider().getColorScheme( val scheme = getColorSchemeProvider().getColorScheme(
theme = uiState.uiTheme, theme = uiState.uiTheme ?: defaultTheme,
dynamic = uiState.dynamicColors, dynamic = uiState.dynamicColors,
customSeed = uiState.customSeedColor customSeed = uiState.customSeedColor
) )

View File

@ -241,11 +241,11 @@ class SettingsViewModel(
} }
} }
private fun changeTheme(value: UiTheme) { private fun changeTheme(value: UiTheme?) {
themeRepository.changeUiTheme(value) themeRepository.changeUiTheme(value)
mvi.scope?.launch { mvi.scope?.launch {
val settings = settingsRepository.currentSettings.value.copy( val settings = settingsRepository.currentSettings.value.copy(
theme = value.toInt() theme = value?.toInt()
) )
saveSettings(settings) saveSettings(settings)
} }

View File

@ -76,10 +76,10 @@ fun App(onLoadingFinished: () -> Unit = {}) {
val crashReportConfiguration = remember { getCrashReportConfiguration() } val crashReportConfiguration = remember { getCrashReportConfiguration() }
val themeRepository = remember { getThemeRepository() } val themeRepository = remember { getThemeRepository() }
val defaultTheme = if (isSystemInDarkTheme()) { val defaultTheme = if (isSystemInDarkTheme()) {
UiTheme.Dark.toInt() UiTheme.Dark
} else { } else {
UiTheme.Light.toInt() UiTheme.Light
} }.toInt()
val defaultLocale = stringResource(MR.strings.lang) val defaultLocale = stringResource(MR.strings.lang)
val languageRepository = remember { getLanguageRepository() } val languageRepository = remember { getLanguageRepository() }
val locale by derivedStateOf { settings.locale } val locale by derivedStateOf { settings.locale }
@ -137,7 +137,7 @@ fun App(onLoadingFinished: () -> Unit = {}) {
LaunchedEffect(settings) { LaunchedEffect(settings) {
with(themeRepository) { with(themeRepository) {
changeUiTheme((settings.theme ?: defaultTheme).toUiTheme()) changeUiTheme(settings.theme.toUiTheme())
changeNavItemTitles(settings.navigationTitlesVisible) changeNavItemTitles(settings.navigationTitlesVisible)
changeDynamicColors(settings.dynamicColors) changeDynamicColors(settings.dynamicColors)
changeCustomSeedColor(settings.customSeedColor?.let { Color(it) }) changeCustomSeedColor(settings.customSeedColor?.let { Color(it) })