From 3266a1b0333e43ff567e9d2c7fed85cd428e69d3 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Tue, 28 May 2024 00:35:22 +0530 Subject: refactor(compose): make theme decisions within `APSTheme` --- ui/compose/build.gradle.kts | 1 + .../app/passwordstore/ui/compose/theme/Theme.kt | 42 +++++++++++++++++----- .../app/passwordstore/ui/compose/theme/utils.kt | 27 -------------- 3 files changed, 35 insertions(+), 35 deletions(-) delete mode 100644 ui/compose/src/main/kotlin/app/passwordstore/ui/compose/theme/utils.kt (limited to 'ui') diff --git a/ui/compose/build.gradle.kts b/ui/compose/build.gradle.kts index 4996bb10..8ede9ad3 100644 --- a/ui/compose/build.gradle.kts +++ b/ui/compose/build.gradle.kts @@ -23,4 +23,5 @@ dependencies { api(libs.compose.foundation.layout) api(libs.compose.material3) api(libs.compose.ui.core) + implementation(libs.androidx.core.ktx) } diff --git a/ui/compose/src/main/kotlin/app/passwordstore/ui/compose/theme/Theme.kt b/ui/compose/src/main/kotlin/app/passwordstore/ui/compose/theme/Theme.kt index b11aa7c1..6827e01a 100644 --- a/ui/compose/src/main/kotlin/app/passwordstore/ui/compose/theme/Theme.kt +++ b/ui/compose/src/main/kotlin/app/passwordstore/ui/compose/theme/Theme.kt @@ -1,10 +1,20 @@ package app.passwordstore.ui.compose.theme -import androidx.compose.material3.ColorScheme +import android.app.Activity +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.SideEffect +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.toArgb +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.platform.LocalView +import androidx.core.view.WindowCompat internal val LightThemeColors = lightColorScheme( @@ -65,13 +75,29 @@ internal val DarkThemeColors = @Composable public fun APSTheme( - colors: ColorScheme, + darkTheme: Boolean = isSystemInDarkTheme(), + dynamicColor: Boolean = true, content: @Composable () -> Unit, ) { - MaterialTheme(colorScheme = colors, typography = AppTypography, content = content) -} - -@Composable -public fun APSThemePreview(content: @Composable () -> Unit) { - MaterialTheme(colorScheme = LightThemeColors, typography = AppTypography, content = content) + val colorScheme = + when { + dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> { + val context = LocalContext.current + if (darkTheme) { + dynamicDarkColorScheme(context) + } else { + dynamicLightColorScheme(context) + } + } + else -> if (darkTheme) DarkThemeColors else LightThemeColors + } + val view = LocalView.current + if (!view.isInEditMode) { + SideEffect { + val window = (view.context as Activity).window + window.statusBarColor = Color.Transparent.toArgb() + WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = !darkTheme + } + } + MaterialTheme(colorScheme = colorScheme, typography = AppTypography, content = content) } diff --git a/ui/compose/src/main/kotlin/app/passwordstore/ui/compose/theme/utils.kt b/ui/compose/src/main/kotlin/app/passwordstore/ui/compose/theme/utils.kt deleted file mode 100644 index 096862dc..00000000 --- a/ui/compose/src/main/kotlin/app/passwordstore/ui/compose/theme/utils.kt +++ /dev/null @@ -1,27 +0,0 @@ -package app.passwordstore.ui.compose.theme - -import android.content.Context -import android.os.Build -import androidx.compose.foundation.isSystemInDarkTheme -import androidx.compose.material3.ColorScheme -import androidx.compose.material3.dynamicDarkColorScheme -import androidx.compose.material3.dynamicLightColorScheme -import androidx.compose.runtime.Composable - -@Composable -public fun decideColorScheme(context: Context): ColorScheme { - val isDarkTheme = isSystemInDarkTheme() - return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - if (isDarkTheme) { - dynamicDarkColorScheme(context) - } else { - dynamicLightColorScheme(context) - } - } else { - if (isDarkTheme) { - DarkThemeColors - } else { - LightThemeColors - } - } -} -- cgit v1.2.3