Save a SideEffect when setting system bars color

This commit is contained in:
vfsfitvnm 2022-08-10 15:57:05 +02:00
parent e929d3b219
commit 648a72fb2e

View file

@ -30,7 +30,6 @@ import androidx.compose.material.ripple.rememberRipple
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.neverEqualPolicy import androidx.compose.runtime.neverEqualPolicy
@ -76,6 +75,7 @@ import it.vfsfitvnm.vimusic.utils.thumbnailRoundnessKey
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class MainActivity : ComponentActivity() { class MainActivity : ComponentActivity() {
private val serviceConnection = object : ServiceConnection { private val serviceConnection = object : ServiceConnection {
@ -115,6 +115,7 @@ class MainActivity : ComponentActivity() {
setContent { setContent {
val coroutineScope = rememberCoroutineScope() val coroutineScope = rememberCoroutineScope()
val isSystemInDarkTheme = isSystemInDarkTheme() val isSystemInDarkTheme = isSystemInDarkTheme()
val systemUiController = rememberSystemUiController()
var appearance by remember(isSystemInDarkTheme) { var appearance by remember(isSystemInDarkTheme) {
with(preferences) { with(preferences) {
@ -126,6 +127,8 @@ class MainActivity : ComponentActivity() {
val colorPalette = val colorPalette =
colorPaletteOf(colorPaletteName, colorPaletteMode, isSystemInDarkTheme) colorPaletteOf(colorPaletteName, colorPaletteMode, isSystemInDarkTheme)
systemUiController.setSystemBarsColor(colorPalette.background0)
mutableStateOf( mutableStateOf(
Appearance( Appearance(
colorPalette = colorPalette, colorPalette = colorPalette,
@ -148,6 +151,8 @@ class MainActivity : ComponentActivity() {
val colorPalette = val colorPalette =
colorPaletteOf(ColorPaletteName.Dynamic, colorPaletteMode, isSystemInDarkTheme) colorPaletteOf(ColorPaletteName.Dynamic, colorPaletteMode, isSystemInDarkTheme)
systemUiController.setSystemBarsColor(colorPalette.background0)
appearance = appearance.copy( appearance = appearance.copy(
colorPalette = colorPalette, colorPalette = colorPalette,
typography = typographyOf(colorPalette.text) typography = typographyOf(colorPalette.text)
@ -158,6 +163,9 @@ class MainActivity : ComponentActivity() {
bitmapListenerJob = coroutineScope.launch(Dispatchers.IO) { bitmapListenerJob = coroutineScope.launch(Dispatchers.IO) {
dynamicColorPaletteOf(bitmap, isDark)?.let { dynamicColorPaletteOf(bitmap, isDark)?.let {
withContext(Dispatchers.Main) {
systemUiController.setSystemBarsColor(it.background0)
}
appearance = appearance.copy(colorPalette = it, typography = typographyOf(it.text)) appearance = appearance.copy(colorPalette = it, typography = typographyOf(it.text))
} }
} }
@ -186,6 +194,8 @@ class MainActivity : ComponentActivity() {
isSystemInDarkTheme isSystemInDarkTheme
) )
systemUiController.setSystemBarsColor(colorPalette.background0)
appearance = appearance.copy( appearance = appearance.copy(
colorPalette = colorPalette, colorPalette = colorPalette,
typography = typographyOf(colorPalette.text), typography = typographyOf(colorPalette.text),
@ -219,8 +229,6 @@ class MainActivity : ComponentActivity() {
} }
} }
val systemUiController = rememberSystemUiController()
val rippleTheme = val rippleTheme =
remember(appearance.colorPalette.text, appearance.colorPalette.isDark) { remember(appearance.colorPalette.text, appearance.colorPalette.isDark) {
object : RippleTheme { object : RippleTheme {
@ -256,13 +264,6 @@ class MainActivity : ComponentActivity() {
) )
} }
SideEffect {
systemUiController.setSystemBarsColor(
appearance.colorPalette.background1,
!appearance.colorPalette.isDark
)
}
CompositionLocalProvider( CompositionLocalProvider(
LocalAppearance provides appearance, LocalAppearance provides appearance,
LocalOverscrollConfiguration provides null, LocalOverscrollConfiguration provides null,