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
}
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) {

View File

@ -9,8 +9,8 @@ import kotlinx.coroutines.flow.MutableStateFlow
internal class DefaultThemeRepository : ThemeRepository {
override val uiTheme = MutableStateFlow<UiTheme>(UiTheme.Light)
override val uiFontFamily = MutableStateFlow<UiFontFamily>(UiFontFamily.TitilliumWeb)
override val uiTheme = MutableStateFlow<UiTheme?>(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<Color?>(null)
override val postLayout = MutableStateFlow<PostLayout>(PostLayout.Card)
override fun changeUiTheme(value: UiTheme) {
override fun changeUiTheme(value: UiTheme?) {
uiTheme.value = value
}

View File

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

View File

@ -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,

View File

@ -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),
)
}
}
}
}

View File

@ -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

View File

@ -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,

View File

@ -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
)

View File

@ -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)
}

View File

@ -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) })