Skip to content

Commit

Permalink
fix: using collectImmediatelyAsState in all theme-related places (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
F0x1d authored Jul 7, 2024
1 parent a7b6c8d commit f56a18f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ import androidx.compose.material3.ColorScheme
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import com.xayah.core.datastore.readMonet
import com.xayah.core.ui.material3.tokens.ColorSchemeKeyTokens
import com.xayah.core.ui.theme.Blue10
import com.xayah.core.ui.theme.Blue20
Expand All @@ -52,6 +50,7 @@ import com.xayah.core.ui.theme.Yellow40
import com.xayah.core.ui.theme.Yellow80
import com.xayah.core.ui.theme.Yellow90
import com.xayah.core.ui.theme.darkTheme
import com.xayah.core.ui.theme.observeMonetEnabled

/**
* Helper function for component color tokens. Here is an example on how to use component color
Expand All @@ -62,7 +61,7 @@ import com.xayah.core.ui.theme.darkTheme
internal fun ColorScheme.fromToken(value: ColorSchemeKeyTokens): Color {
val context = LocalContext.current
// Dynamic color is available on Android 12+
val dynamicColor by context.readMonet().collectAsState(initial = true)
val dynamicColor by observeMonetEnabled()
val isDarkTheme = darkTheme()
val tonalPalette = if (dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) dynamicTonalPalette(context) else BaselineTonalPalette

Expand Down
16 changes: 13 additions & 3 deletions source/core/ui/src/main/kotlin/com/xayah/core/ui/theme/Theme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import kotlinx.coroutines.runBlocking

@Composable
fun darkTheme() = run {
val context = LocalContext.current
val themeType by context.readThemeType().collectImmediatelyAsState()
val themeType by observeCurrentTheme()

when (themeType) {
ThemeType.AUTO -> isSystemInDarkTheme()
else -> remember(themeType) { themeType != ThemeType.LIGHT_THEME }
Expand All @@ -48,7 +48,7 @@ fun DataBackupTheme(
val context = LocalContext.current
val darkTheme = darkTheme()
// Dynamic color is available on Android 12+
val dynamicColor by context.readMonet().collectImmediatelyAsState()
val dynamicColor by observeMonetEnabled()
val colorScheme = when {
dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
Expand Down Expand Up @@ -90,6 +90,16 @@ fun DataBackupTheme(
)
}

@Composable
fun observeCurrentTheme(): State<ThemeType> {
return LocalContext.current.readThemeType().collectImmediatelyAsState()
}

@Composable
fun observeMonetEnabled(): State<Boolean> {
return LocalContext.current.readMonet().collectImmediatelyAsState()
}

@Composable
private fun <T> Flow<T>.collectImmediatelyAsState(): State<T> = produceState(
initialValue = runBlocking { first() }, // DataStore shouldn't be really heavy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.xayah.core.datastore.readThemeType
import com.xayah.core.datastore.saveThemeType
import com.xayah.core.model.ThemeType
import com.xayah.core.ui.component.LocalSlotScope
import com.xayah.core.ui.component.Selectable
import com.xayah.core.ui.component.select
import com.xayah.core.ui.model.DialogRadioItem
import com.xayah.core.ui.model.StringResourceToken
import com.xayah.core.ui.theme.observeCurrentTheme
import com.xayah.core.ui.util.fromStringId

@ExperimentalAnimationApi
Expand All @@ -41,7 +40,7 @@ fun DarkThemeSelectable() {
),
)
}
val currentType by context.readThemeType().collectAsStateWithLifecycle(initialValue = ThemeType.AUTO)
val currentType by observeCurrentTheme()
val currentIndex by remember(currentType) { mutableIntStateOf(items.indexOfFirst { it.enum == currentType }) }
Selectable(
title = StringResourceToken.fromStringId(R.string.dark_theme),
Expand All @@ -57,4 +56,4 @@ fun DarkThemeSelectable() {
context.saveThemeType(items[selectedIndex].enum!!)
}
}
}
}

0 comments on commit f56a18f

Please sign in to comment.