Skip to content

Commit

Permalink
feat: 支持动态配色 (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Nov 3, 2023
1 parent f95d20c commit dd4cb3a
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions app/src/main/java/li/songe/gkd/ui/theme/Theme.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package li.songe.gkd.ui.theme

import android.os.Build
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.platform.LocalContext
import li.songe.gkd.util.map
import li.songe.gkd.util.storeFlow

Expand All @@ -16,15 +20,19 @@ val DarkColorScheme = darkColorScheme()

@Composable
fun AppTheme(
useDarkTheme: Boolean = isSystemInDarkTheme(),
content: @Composable () -> Unit,
) {
// https://developer.android.com/jetpack/compose/designsystems/material3?hl=zh-cn
val scope = rememberCoroutineScope()
val enableDarkTheme by storeFlow.map(scope) { s -> s.enableDarkTheme }.collectAsState()
val colorScheme = if (enableDarkTheme ?: useDarkTheme) {
DarkColorScheme
} else {
LightColorScheme
val systemInDarkTheme = isSystemInDarkTheme()
val darkTheme = enableDarkTheme ?: systemInDarkTheme
val dynamicColor = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S
val colorScheme = when {
dynamicColor && darkTheme -> dynamicDarkColorScheme(LocalContext.current)
dynamicColor && !darkTheme -> dynamicLightColorScheme(LocalContext.current)
darkTheme -> DarkColorScheme
else -> LightColorScheme
}
MaterialTheme(
colorScheme = colorScheme, content = content
Expand Down

0 comments on commit dd4cb3a

Please sign in to comment.