refactor(compose): make theme decisions within APSTheme

This commit is contained in:
Harsh Shandilya 2024-05-28 00:35:22 +05:30
parent 0f9540a645
commit 3266a1b033
7 changed files with 43 additions and 47 deletions

View file

@ -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(),

View file

@ -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(),

View file

@ -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 = {})
}

View file

@ -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(

View file

@ -23,4 +23,5 @@ dependencies {
api(libs.compose.foundation.layout)
api(libs.compose.material3)
api(libs.compose.ui.core)
implementation(libs.androidx.core.ktx)
}

View file

@ -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)
}

View file

@ -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
}
}
}