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` --- .../passwordstore/ui/crypto/EditPasswordScreen.kt | 5 ++- .../passwordstore/ui/crypto/ViewPasswordScreen.kt | 4 +-- .../java/app/passwordstore/ui/pgp/PGPKeyList.kt | 6 ++-- .../app/passwordstore/ui/pgp/PGPKeyListActivity.kt | 5 +-- ui/compose/build.gradle.kts | 1 + .../app/passwordstore/ui/compose/theme/Theme.kt | 42 +++++++++++++++++----- .../app/passwordstore/ui/compose/theme/utils.kt | 27 -------------- 7 files changed, 43 insertions(+), 47 deletions(-) delete mode 100644 ui/compose/src/main/kotlin/app/passwordstore/ui/compose/theme/utils.kt diff --git a/app/src/main/java/app/passwordstore/ui/crypto/EditPasswordScreen.kt b/app/src/main/java/app/passwordstore/ui/crypto/EditPasswordScreen.kt index 9c3707f7..4a7b1aa2 100644 --- a/app/src/main/java/app/passwordstore/ui/crypto/EditPasswordScreen.kt +++ b/app/src/main/java/app/passwordstore/ui/crypto/EditPasswordScreen.kt @@ -10,7 +10,6 @@ import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.material3.TextField import androidx.compose.runtime.Composable -import androidx.compose.runtime.getValue import androidx.compose.ui.Modifier import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource @@ -20,7 +19,7 @@ import app.passwordstore.R import app.passwordstore.data.passfile.PasswordEntry import app.passwordstore.ui.APSAppBar import app.passwordstore.ui.compose.PasswordField -import app.passwordstore.ui.compose.theme.APSThemePreview +import app.passwordstore.ui.compose.theme.APSTheme import app.passwordstore.util.time.UserClock import app.passwordstore.util.totp.UriTotpFinder @@ -75,7 +74,7 @@ private fun ExtraContent( @Preview @Composable private fun EditPasswordScreenPreview() { - APSThemePreview { + APSTheme { EditPasswordScreen( entryName = "Test Entry", entry = createTestEntry(), diff --git a/app/src/main/java/app/passwordstore/ui/crypto/ViewPasswordScreen.kt b/app/src/main/java/app/passwordstore/ui/crypto/ViewPasswordScreen.kt index 2ebdc3a9..70933ab4 100644 --- a/app/src/main/java/app/passwordstore/ui/crypto/ViewPasswordScreen.kt +++ b/app/src/main/java/app/passwordstore/ui/crypto/ViewPasswordScreen.kt @@ -24,7 +24,7 @@ import app.passwordstore.data.passfile.PasswordEntry import app.passwordstore.ui.APSAppBar import app.passwordstore.ui.compose.CopyButton import app.passwordstore.ui.compose.PasswordField -import app.passwordstore.ui.compose.theme.APSThemePreview +import app.passwordstore.ui.compose.theme.APSTheme import app.passwordstore.util.time.UserClock import app.passwordstore.util.totp.UriTotpFinder import kotlinx.coroutines.flow.first @@ -106,7 +106,7 @@ private fun ExtraContent( @Preview @Composable private fun ViewPasswordScreenPreview() { - APSThemePreview { + APSTheme { ViewPasswordScreen( entryName = "Test Entry", entry = createTestEntry(), diff --git a/app/src/main/java/app/passwordstore/ui/pgp/PGPKeyList.kt b/app/src/main/java/app/passwordstore/ui/pgp/PGPKeyList.kt index 0ee5748c..5b12fd88 100644 --- a/app/src/main/java/app/passwordstore/ui/pgp/PGPKeyList.kt +++ b/app/src/main/java/app/passwordstore/ui/pgp/PGPKeyList.kt @@ -33,7 +33,7 @@ import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import app.passwordstore.R import app.passwordstore.crypto.PGPIdentifier -import app.passwordstore.ui.compose.theme.APSThemePreview +import app.passwordstore.ui.compose.theme.APSTheme import app.passwordstore.util.extensions.conditional import kotlinx.collections.immutable.ImmutableList import kotlinx.collections.immutable.persistentListOf @@ -139,7 +139,7 @@ private inline fun DeleteConfirmationDialog( @Preview @Composable private fun KeyListPreview() { - APSThemePreview { + APSTheme { Box(modifier = Modifier.background(MaterialTheme.colorScheme.background)) { KeyList( identifiers = @@ -157,7 +157,7 @@ private fun KeyListPreview() { @Preview @Composable private fun EmptyKeyListPreview() { - APSThemePreview { + APSTheme { Box(modifier = Modifier.background(MaterialTheme.colorScheme.background)) { KeyList(identifiers = persistentListOf(), onItemClick = {}) } diff --git a/app/src/main/java/app/passwordstore/ui/pgp/PGPKeyListActivity.kt b/app/src/main/java/app/passwordstore/ui/pgp/PGPKeyListActivity.kt index 5c932545..9ae9f879 100644 --- a/app/src/main/java/app/passwordstore/ui/pgp/PGPKeyListActivity.kt +++ b/app/src/main/java/app/passwordstore/ui/pgp/PGPKeyListActivity.kt @@ -13,13 +13,11 @@ import androidx.compose.material3.Icon import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Scaffold import androidx.compose.ui.Modifier -import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import app.passwordstore.R import app.passwordstore.ui.APSAppBar import app.passwordstore.ui.compose.theme.APSTheme -import app.passwordstore.ui.compose.theme.decideColorScheme import app.passwordstore.util.viewmodel.PGPKeyListViewModel import dagger.hilt.android.AndroidEntryPoint @@ -38,8 +36,7 @@ class PGPKeyListActivity : ComponentActivity() { super.onCreate(savedInstanceState) val isSelecting = intent.extras?.getBoolean(EXTRA_KEY_SELECTION) ?: false setContent { - val context = LocalContext.current - APSTheme(colors = decideColorScheme(context)) { + APSTheme { Scaffold( topBar = { APSAppBar( 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