Make Appearance saveable

This commit is contained in:
vfsfitvnm 2022-10-07 16:10:27 +02:00
parent 579445423d
commit d466f7f1a6
2 changed files with 50 additions and 5 deletions

View file

@ -38,6 +38,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshotFlow
import androidx.compose.runtime.staticCompositionLocalOf
@ -127,12 +128,11 @@ class MainActivity : ComponentActivity() {
val coroutineScope = rememberCoroutineScope()
val isSystemInDarkTheme = isSystemInDarkTheme()
var appearance by remember(isSystemInDarkTheme) {
var appearance by rememberSaveable(isSystemInDarkTheme, stateSaver = Appearance.Companion) {
with(preferences) {
val colorPaletteName = getEnum(colorPaletteNameKey, ColorPaletteName.Dynamic)
val colorPaletteMode = getEnum(colorPaletteModeKey, ColorPaletteMode.System)
val thumbnailRoundness =
getEnum(thumbnailRoundnessKey, ThumbnailRoundness.Light)
val thumbnailRoundness = getEnum(thumbnailRoundnessKey, ThumbnailRoundness.Light)
val colorPalette =
colorPaletteOf(colorPaletteName, colorPaletteMode, isSystemInDarkTheme)

View file

@ -1,12 +1,57 @@
package it.vfsfitvnm.vimusic.ui.styling
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.SaverScope
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.unit.dp
data class Appearance(
val colorPalette: ColorPalette,
val typography: Typography,
val typography: Typography = typographyOf(colorPalette.text),
val thumbnailShape: Shape
) {
companion object : Saver<Appearance, List<Any>> {
override fun restore(value: List<Any>) = Appearance(
colorPalette = ColorPalette(
background0 = Color((value[0] as Long).toULong()),
background1 = Color((value[1] as Long).toULong()),
background2 = Color((value[2] as Long).toULong()),
accent = Color((value[3] as Long).toULong()),
onAccent = Color((value[4] as Long).toULong()),
red = Color((value[5] as Long).toULong()),
blue = Color((value[6] as Long).toULong()),
text = Color((value[7] as Long).toULong()),
textSecondary = Color((value[8] as Long).toULong()),
textDisabled = Color((value[9] as Long).toULong()),
isDark = value[10] as Boolean
),
thumbnailShape = RoundedCornerShape((value[11] as Int).dp)
)
override fun SaverScope.save(value: Appearance) =
listOf(
value.colorPalette.background0.value.toLong(),
value.colorPalette.background1.value.toLong(),
value.colorPalette.background2.value.toLong(),
value.colorPalette.accent.value.toLong(),
value.colorPalette.onAccent.value.toLong(),
value.colorPalette.red.value.toLong(),
value.colorPalette.blue.value.toLong(),
value.colorPalette.text.value.toLong(),
value.colorPalette.textSecondary.value.toLong(),
value.colorPalette.textDisabled.value.toLong(),
value.colorPalette.isDark,
when (value.thumbnailShape) {
RoundedCornerShape(2.dp) -> 2
RoundedCornerShape(4.dp) -> 4
RoundedCornerShape(8.dp) -> 8
else -> 0
}
)
}
}
val LocalAppearance = staticCompositionLocalOf<Appearance> { TODO() }