Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit 3266a1b

Browse files
committed
refactor(compose): make theme decisions within APSTheme
1 parent 0f9540a commit 3266a1b

File tree

7 files changed

+43
-47
lines changed

7 files changed

+43
-47
lines changed

app/src/main/java/app/passwordstore/ui/crypto/EditPasswordScreen.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import androidx.compose.material3.Scaffold
1010
import androidx.compose.material3.Text
1111
import androidx.compose.material3.TextField
1212
import androidx.compose.runtime.Composable
13-
import androidx.compose.runtime.getValue
1413
import androidx.compose.ui.Modifier
1514
import androidx.compose.ui.res.painterResource
1615
import androidx.compose.ui.res.stringResource
@@ -20,7 +19,7 @@ import app.passwordstore.R
2019
import app.passwordstore.data.passfile.PasswordEntry
2120
import app.passwordstore.ui.APSAppBar
2221
import app.passwordstore.ui.compose.PasswordField
23-
import app.passwordstore.ui.compose.theme.APSThemePreview
22+
import app.passwordstore.ui.compose.theme.APSTheme
2423
import app.passwordstore.util.time.UserClock
2524
import app.passwordstore.util.totp.UriTotpFinder
2625

@@ -75,7 +74,7 @@ private fun ExtraContent(
7574
@Preview
7675
@Composable
7776
private fun EditPasswordScreenPreview() {
78-
APSThemePreview {
77+
APSTheme {
7978
EditPasswordScreen(
8079
entryName = "Test Entry",
8180
entry = createTestEntry(),

app/src/main/java/app/passwordstore/ui/crypto/ViewPasswordScreen.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import app.passwordstore.data.passfile.PasswordEntry
2424
import app.passwordstore.ui.APSAppBar
2525
import app.passwordstore.ui.compose.CopyButton
2626
import app.passwordstore.ui.compose.PasswordField
27-
import app.passwordstore.ui.compose.theme.APSThemePreview
27+
import app.passwordstore.ui.compose.theme.APSTheme
2828
import app.passwordstore.util.time.UserClock
2929
import app.passwordstore.util.totp.UriTotpFinder
3030
import kotlinx.coroutines.flow.first
@@ -106,7 +106,7 @@ private fun ExtraContent(
106106
@Preview
107107
@Composable
108108
private fun ViewPasswordScreenPreview() {
109-
APSThemePreview {
109+
APSTheme {
110110
ViewPasswordScreen(
111111
entryName = "Test Entry",
112112
entry = createTestEntry(),

app/src/main/java/app/passwordstore/ui/pgp/PGPKeyList.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import androidx.compose.ui.tooling.preview.Preview
3333
import androidx.compose.ui.unit.dp
3434
import app.passwordstore.R
3535
import app.passwordstore.crypto.PGPIdentifier
36-
import app.passwordstore.ui.compose.theme.APSThemePreview
36+
import app.passwordstore.ui.compose.theme.APSTheme
3737
import app.passwordstore.util.extensions.conditional
3838
import kotlinx.collections.immutable.ImmutableList
3939
import kotlinx.collections.immutable.persistentListOf
@@ -139,7 +139,7 @@ private inline fun DeleteConfirmationDialog(
139139
@Preview
140140
@Composable
141141
private fun KeyListPreview() {
142-
APSThemePreview {
142+
APSTheme {
143143
Box(modifier = Modifier.background(MaterialTheme.colorScheme.background)) {
144144
KeyList(
145145
identifiers =
@@ -157,7 +157,7 @@ private fun KeyListPreview() {
157157
@Preview
158158
@Composable
159159
private fun EmptyKeyListPreview() {
160-
APSThemePreview {
160+
APSTheme {
161161
Box(modifier = Modifier.background(MaterialTheme.colorScheme.background)) {
162162
KeyList(identifiers = persistentListOf(), onItemClick = {})
163163
}

app/src/main/java/app/passwordstore/ui/pgp/PGPKeyListActivity.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@ import androidx.compose.material3.Icon
1313
import androidx.compose.material3.MaterialTheme
1414
import androidx.compose.material3.Scaffold
1515
import androidx.compose.ui.Modifier
16-
import androidx.compose.ui.platform.LocalContext
1716
import androidx.compose.ui.res.painterResource
1817
import androidx.compose.ui.res.stringResource
1918
import app.passwordstore.R
2019
import app.passwordstore.ui.APSAppBar
2120
import app.passwordstore.ui.compose.theme.APSTheme
22-
import app.passwordstore.ui.compose.theme.decideColorScheme
2321
import app.passwordstore.util.viewmodel.PGPKeyListViewModel
2422
import dagger.hilt.android.AndroidEntryPoint
2523

@@ -38,8 +36,7 @@ class PGPKeyListActivity : ComponentActivity() {
3836
super.onCreate(savedInstanceState)
3937
val isSelecting = intent.extras?.getBoolean(EXTRA_KEY_SELECTION) ?: false
4038
setContent {
41-
val context = LocalContext.current
42-
APSTheme(colors = decideColorScheme(context)) {
39+
APSTheme {
4340
Scaffold(
4441
topBar = {
4542
APSAppBar(

ui/compose/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ dependencies {
2323
api(libs.compose.foundation.layout)
2424
api(libs.compose.material3)
2525
api(libs.compose.ui.core)
26+
implementation(libs.androidx.core.ktx)
2627
}

ui/compose/src/main/kotlin/app/passwordstore/ui/compose/theme/Theme.kt

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
package app.passwordstore.ui.compose.theme
22

3-
import androidx.compose.material3.ColorScheme
3+
import android.app.Activity
4+
import android.os.Build
5+
import androidx.compose.foundation.isSystemInDarkTheme
46
import androidx.compose.material3.MaterialTheme
57
import androidx.compose.material3.darkColorScheme
8+
import androidx.compose.material3.dynamicDarkColorScheme
9+
import androidx.compose.material3.dynamicLightColorScheme
610
import androidx.compose.material3.lightColorScheme
711
import androidx.compose.runtime.Composable
12+
import androidx.compose.runtime.SideEffect
13+
import androidx.compose.ui.graphics.Color
14+
import androidx.compose.ui.graphics.toArgb
15+
import androidx.compose.ui.platform.LocalContext
16+
import androidx.compose.ui.platform.LocalView
17+
import androidx.core.view.WindowCompat
818

919
internal val LightThemeColors =
1020
lightColorScheme(
@@ -65,13 +75,29 @@ internal val DarkThemeColors =
6575

6676
@Composable
6777
public fun APSTheme(
68-
colors: ColorScheme,
78+
darkTheme: Boolean = isSystemInDarkTheme(),
79+
dynamicColor: Boolean = true,
6980
content: @Composable () -> Unit,
7081
) {
71-
MaterialTheme(colorScheme = colors, typography = AppTypography, content = content)
72-
}
73-
74-
@Composable
75-
public fun APSThemePreview(content: @Composable () -> Unit) {
76-
MaterialTheme(colorScheme = LightThemeColors, typography = AppTypography, content = content)
82+
val colorScheme =
83+
when {
84+
dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
85+
val context = LocalContext.current
86+
if (darkTheme) {
87+
dynamicDarkColorScheme(context)
88+
} else {
89+
dynamicLightColorScheme(context)
90+
}
91+
}
92+
else -> if (darkTheme) DarkThemeColors else LightThemeColors
93+
}
94+
val view = LocalView.current
95+
if (!view.isInEditMode) {
96+
SideEffect {
97+
val window = (view.context as Activity).window
98+
window.statusBarColor = Color.Transparent.toArgb()
99+
WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = !darkTheme
100+
}
101+
}
102+
MaterialTheme(colorScheme = colorScheme, typography = AppTypography, content = content)
77103
}

ui/compose/src/main/kotlin/app/passwordstore/ui/compose/theme/utils.kt

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)