Skip to content

Commit

Permalink
fix: statusBar appearance
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Dec 26, 2023
1 parent 589df8f commit 396a914
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions app/src/main/kotlin/li/songe/gkd/ui/theme/Theme.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package li.songe.gkd.ui.theme

import android.os.Build
import androidx.activity.ComponentActivity
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.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.platform.LocalContext
import androidx.core.view.WindowInsetsControllerCompat
import li.songe.gkd.util.map
import li.songe.gkd.util.storeFlow

Expand All @@ -23,17 +26,23 @@ fun AppTheme(
content: @Composable () -> Unit,
) {
// https://developer.android.com/jetpack/compose/designsystems/material3?hl=zh-cn
val context = LocalContext.current as ComponentActivity
val scope = rememberCoroutineScope()
val enableDarkTheme by storeFlow.map(scope) { s -> s.enableDarkTheme }.collectAsState()
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)
dynamicColor && darkTheme -> dynamicDarkColorScheme(context)
dynamicColor && !darkTheme -> dynamicLightColorScheme(context)
darkTheme -> DarkColorScheme
else -> LightColorScheme
}
LaunchedEffect(darkTheme) {
WindowInsetsControllerCompat(context.window, context.window.decorView).apply {
isAppearanceLightStatusBars = !darkTheme
}
}
MaterialTheme(
colorScheme = colorScheme, content = content
)
Expand Down

0 comments on commit 396a914

Please sign in to comment.