diff --git a/core-appearance/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/appearance/data/UiTheme.kt b/core-appearance/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/appearance/data/UiTheme.kt index 49065d8d1..edb4ace06 100644 --- a/core-appearance/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/appearance/data/UiTheme.kt +++ b/core-appearance/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/appearance/data/UiTheme.kt @@ -14,23 +14,26 @@ sealed interface UiTheme { data object Black : UiTheme } -fun Int.toUiTheme() = when (this) { +fun Int?.toUiTheme(): UiTheme? = when (this) { 2 -> UiTheme.Black 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.Dark -> 1 UiTheme.Light -> 0 + else -> null } @Composable -fun UiTheme.toReadableName() = when (this) { +fun UiTheme?.toReadableName() = when (this) { UiTheme.Black -> stringResource(MR.strings.settings_theme_black) UiTheme.Dark -> stringResource(MR.strings.settings_theme_dark) UiTheme.Light -> stringResource(MR.strings.settings_theme_light) + else -> stringResource(MR.strings.settings_font_family_default) } fun UiTheme.toIcon() = when (this) { diff --git a/core-appearance/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/appearance/repository/DefaultThemeRepository.kt b/core-appearance/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/appearance/repository/DefaultThemeRepository.kt index c6b62aaf0..cf37ef7d4 100644 --- a/core-appearance/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/appearance/repository/DefaultThemeRepository.kt +++ b/core-appearance/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/appearance/repository/DefaultThemeRepository.kt @@ -9,8 +9,8 @@ import kotlinx.coroutines.flow.MutableStateFlow internal class DefaultThemeRepository : ThemeRepository { - override val uiTheme = MutableStateFlow(UiTheme.Light) - override val uiFontFamily = MutableStateFlow(UiFontFamily.TitilliumWeb) + override val uiTheme = MutableStateFlow(null) + override val uiFontFamily = MutableStateFlow(UiFontFamily.Poppins) override val uiFontScale = MutableStateFlow(1f) override val contentFontScale = MutableStateFlow(1f) override val navItemTitles = MutableStateFlow(false) @@ -20,7 +20,7 @@ internal class DefaultThemeRepository : ThemeRepository { override val downvoteColor = MutableStateFlow(null) override val postLayout = MutableStateFlow(PostLayout.Card) - override fun changeUiTheme(value: UiTheme) { + override fun changeUiTheme(value: UiTheme?) { uiTheme.value = value } diff --git a/core-appearance/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/appearance/repository/ThemeRepository.kt b/core-appearance/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/appearance/repository/ThemeRepository.kt index 673115668..9e2504d60 100644 --- a/core-appearance/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/appearance/repository/ThemeRepository.kt +++ b/core-appearance/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/appearance/repository/ThemeRepository.kt @@ -10,7 +10,7 @@ import kotlinx.coroutines.flow.StateFlow @Stable interface ThemeRepository { - val uiTheme: StateFlow + val uiTheme: StateFlow val uiFontFamily: StateFlow val uiFontScale: StateFlow val contentFontScale: StateFlow @@ -21,7 +21,7 @@ interface ThemeRepository { val downvoteColor: StateFlow val postLayout: StateFlow - fun changeUiTheme(value: UiTheme) + fun changeUiTheme(value: UiTheme?) fun changeUiFontFamily(value: UiFontFamily) diff --git a/core-appearance/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/appearance/theme/AppTheme.kt b/core-appearance/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/appearance/theme/AppTheme.kt index 6ef04207f..bf63a379a 100644 --- a/core-appearance/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/appearance/theme/AppTheme.kt +++ b/core-appearance/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/appearance/theme/AppTheme.kt @@ -1,5 +1,6 @@ package com.github.diegoberaldin.raccoonforlemmy.core.appearance.theme +import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.material3.MaterialTheme import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState @@ -12,7 +13,7 @@ import com.github.diegoberaldin.raccoonforlemmy.core.appearance.di.getThemeRepos @Composable fun AppTheme( - theme: UiTheme, + theme: UiTheme?, contentFontScale: Float, useDynamicColors: Boolean, content: @Composable () -> Unit, @@ -26,10 +27,15 @@ fun AppTheme( val themeState by repository.uiTheme.collectAsState() val customSeedColor by repository.customSeedColor.collectAsState() + val defaultTheme = if (isSystemInDarkTheme()) { + UiTheme.Dark + } else { + UiTheme.Light + } val colorSchemeProvider = remember { getColorSchemeProvider() } val colorScheme = colorSchemeProvider.getColorScheme( - theme = themeState, + theme = themeState ?: defaultTheme, dynamic = useDynamicColors, customSeed = customSeedColor, ) @@ -38,7 +44,7 @@ fun AppTheme( val typography = getTypography(fontFamily) val barColorProvider = remember { getBarColorProvider() } - barColorProvider.setBarColorAccordingToTheme(theme) + barColorProvider.setBarColorAccordingToTheme(theme ?: defaultTheme) MaterialTheme( colorScheme = colorScheme, diff --git a/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/modals/ThemeBottomSheet.kt b/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/modals/ThemeBottomSheet.kt index af71d1967..7eb9a4313 100644 --- a/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/modals/ThemeBottomSheet.kt +++ b/core-commonui/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/commonui/modals/ThemeBottomSheet.kt @@ -62,6 +62,7 @@ class ThemeBottomSheet : Screen { UiTheme.Light, UiTheme.Dark, UiTheme.Black, + null, ) Column( modifier = Modifier.fillMaxWidth().verticalScroll(rememberScrollState()), @@ -89,11 +90,13 @@ class ThemeBottomSheet : Screen { color = MaterialTheme.colorScheme.onBackground, ) Spacer(modifier = Modifier.weight(1f)) - Image( - imageVector = value.toIcon(), - contentDescription = null, - colorFilter = ColorFilter.tint(MaterialTheme.colorScheme.onBackground), - ) + if (value != null) { + Image( + imageVector = value.toIcon(), + contentDescription = null, + colorFilter = ColorFilter.tint(MaterialTheme.colorScheme.onBackground), + ) + } } } } diff --git a/core-notifications/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/notifications/NotificationCenterEvent.kt b/core-notifications/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/notifications/NotificationCenterEvent.kt index 5e0c23842..1d61645c7 100644 --- a/core-notifications/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/notifications/NotificationCenterEvent.kt +++ b/core-notifications/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/core/notifications/NotificationCenterEvent.kt @@ -25,7 +25,7 @@ sealed interface NotificationCenterEvent { 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 ChangeUiFontSize(val value: Float) : NotificationCenterEvent data class ChangeFontFamily(val value: UiFontFamily) : NotificationCenterEvent diff --git a/feature-settings/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/settings/main/SettingsMviModel.kt b/feature-settings/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/settings/main/SettingsMviModel.kt index b48d56d6a..5ee3adf07 100644 --- a/feature-settings/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/settings/main/SettingsMviModel.kt +++ b/feature-settings/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/settings/main/SettingsMviModel.kt @@ -18,7 +18,7 @@ interface SettingsMviModel : ScreenModel { 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 ChangeUiFontFamily(val value: UiFontFamily) : Intent data class ChangeContentFontSize(val value: Float) : Intent @@ -51,7 +51,7 @@ interface SettingsMviModel : data class UiState( val isLogged: Boolean = false, - val uiTheme: UiTheme = UiTheme.Light, + val uiTheme: UiTheme? = null, val uiFontFamily: UiFontFamily = UiFontFamily.TitilliumWeb, val customSeedColor: Color? = null, val upvoteColor: Color? = null, diff --git a/feature-settings/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/settings/main/SettingsScreen.kt b/feature-settings/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/settings/main/SettingsScreen.kt index 8ade177f5..169473f42 100644 --- a/feature-settings/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/settings/main/SettingsScreen.kt +++ b/feature-settings/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/settings/main/SettingsScreen.kt @@ -1,6 +1,7 @@ package com.github.diegoberaldin.raccoonforlemmy.feature.settings.main import androidx.compose.foundation.Image +import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box 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.screen.Screen 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.di.getColorSchemeProvider import com.github.diegoberaldin.raccoonforlemmy.core.appearance.di.getThemeRepository @@ -107,6 +109,12 @@ class SettingsScreen : Screen { val lang by languageRepository.currentLanguage.collectAsState() var uiFontSizeWorkaround by remember { mutableStateOf(true) } val themeRepository = remember { getThemeRepository() } + val colorSchemeProvider = remember { getColorSchemeProvider() } + val defaultTheme = if (isSystemInDarkTheme()) { + UiTheme.Dark + } else { + UiTheme.Light + } var upvoteColorDialogOpened by remember { mutableStateOf(false) } var downvoteColorDialogOpened 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 SettingsColorRow( title = stringResource(MR.strings.settings_custom_seed_color), value = uiState.customSeedColor ?: colorSchemeProvider.getColorScheme( - theme = uiState.uiTheme, + theme = uiState.uiTheme ?: defaultTheme, dynamic = uiState.dynamicColors, ).primary, onTap = rememberCallback { @@ -645,7 +652,7 @@ class SettingsScreen : Screen { onReset = rememberCallback(model) { upvoteColorDialogOpened = false val scheme = getColorSchemeProvider().getColorScheme( - theme = uiState.uiTheme, + theme = uiState.uiTheme ?: defaultTheme, dynamic = uiState.dynamicColors, customSeed = uiState.customSeedColor ) diff --git a/feature-settings/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/settings/main/SettingsViewModel.kt b/feature-settings/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/settings/main/SettingsViewModel.kt index 2a502fdcb..89ae15299 100644 --- a/feature-settings/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/settings/main/SettingsViewModel.kt +++ b/feature-settings/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/feature/settings/main/SettingsViewModel.kt @@ -241,11 +241,11 @@ class SettingsViewModel( } } - private fun changeTheme(value: UiTheme) { + private fun changeTheme(value: UiTheme?) { themeRepository.changeUiTheme(value) mvi.scope?.launch { val settings = settingsRepository.currentSettings.value.copy( - theme = value.toInt() + theme = value?.toInt() ) saveSettings(settings) } diff --git a/shared/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/App.kt b/shared/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/App.kt index b5947c920..ac113ae81 100644 --- a/shared/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/App.kt +++ b/shared/src/commonMain/kotlin/com/github/diegoberaldin/raccoonforlemmy/App.kt @@ -76,10 +76,10 @@ fun App(onLoadingFinished: () -> Unit = {}) { val crashReportConfiguration = remember { getCrashReportConfiguration() } val themeRepository = remember { getThemeRepository() } val defaultTheme = if (isSystemInDarkTheme()) { - UiTheme.Dark.toInt() + UiTheme.Dark } else { - UiTheme.Light.toInt() - } + UiTheme.Light + }.toInt() val defaultLocale = stringResource(MR.strings.lang) val languageRepository = remember { getLanguageRepository() } val locale by derivedStateOf { settings.locale } @@ -137,7 +137,7 @@ fun App(onLoadingFinished: () -> Unit = {}) { LaunchedEffect(settings) { with(themeRepository) { - changeUiTheme((settings.theme ?: defaultTheme).toUiTheme()) + changeUiTheme(settings.theme.toUiTheme()) changeNavItemTitles(settings.navigationTitlesVisible) changeDynamicColors(settings.dynamicColors) changeCustomSeedColor(settings.customSeedColor?.let { Color(it) })