This repository has been archived by the owner on Jul 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
39 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,64 @@ | ||
package com.sanmer.geomag.ui.theme | ||
|
||
import android.graphics.Color | ||
import androidx.activity.ComponentActivity | ||
import androidx.activity.SystemBarStyle | ||
import androidx.activity.enableEdgeToEdge | ||
import androidx.compose.foundation.isSystemInDarkTheme | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.runtime.SideEffect | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.toArgb | ||
import androidx.compose.ui.platform.LocalContext | ||
|
||
@Composable | ||
fun ComponentActivity.AppTheme( | ||
darkMode: Boolean, | ||
fun AppTheme( | ||
themeColor: Int, | ||
darkMode: Boolean = isSystemInDarkTheme(), | ||
content: @Composable () -> Unit | ||
) { | ||
val lightScrim = Color.argb(0xe6, 0xFF, 0xFF, 0xFF) | ||
val darkScrim = Color.argb(0x80, 0x1b, 0x1b, 0x1b) | ||
|
||
DisposableEffect(darkMode) { | ||
enableEdgeToEdge( | ||
statusBarStyle = SystemBarStyle.auto( | ||
Color.TRANSPARENT, | ||
Color.TRANSPARENT, | ||
) { darkMode }, | ||
navigationBarStyle = SystemBarStyle.auto( | ||
lightScrim, | ||
darkScrim, | ||
) { darkMode } | ||
) | ||
onDispose {} | ||
} | ||
|
||
val color = Colors.getColor(id = themeColor) | ||
val colorScheme = when { | ||
darkMode -> color.darkColorScheme | ||
else -> color.lightColorScheme | ||
} | ||
|
||
SystemBarStyle( | ||
darkMode = darkMode | ||
) | ||
|
||
MaterialTheme( | ||
colorScheme = colorScheme, | ||
shapes = Shapes, | ||
typography = Typography, | ||
content = content | ||
) | ||
} | ||
|
||
@Composable | ||
private fun SystemBarStyle( | ||
darkMode: Boolean, | ||
statusBarScrim: Color = Color.Transparent, | ||
navigationBarScrim: Color = Color.Transparent | ||
) { | ||
val context = LocalContext.current | ||
val activity = context as ComponentActivity | ||
|
||
SideEffect { | ||
activity.enableEdgeToEdge( | ||
statusBarStyle = SystemBarStyle.auto( | ||
statusBarScrim.toArgb(), | ||
statusBarScrim.toArgb(), | ||
) { darkMode }, | ||
navigationBarStyle = when { | ||
darkMode -> SystemBarStyle.dark( | ||
navigationBarScrim.toArgb() | ||
) | ||
else -> SystemBarStyle.light( | ||
navigationBarScrim.toArgb(), | ||
navigationBarScrim.toArgb(), | ||
) | ||
} | ||
) | ||
} | ||
} |