From 7087fb85f01c41809d7934f822fc2e5643362718 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Sat, 6 Sep 2025 10:43:54 -0400 Subject: [PATCH] Changes the Theme class to only take the preferred theme directly. --- .../vitorpamplona/amethyst/ui/theme/Preview.kt | 18 ++++-------------- .../vitorpamplona/amethyst/ui/theme/Theme.kt | 14 +++++++------- 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/theme/Preview.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/theme/Preview.kt index 71a5597f9..3b3cb491f 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/theme/Preview.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/theme/Preview.kt @@ -27,25 +27,19 @@ import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier -import androidx.lifecycle.viewmodel.compose.viewModel import com.vitorpamplona.amethyst.model.ThemeType -import com.vitorpamplona.amethyst.ui.screen.SharedPreferencesViewModel @Composable fun ThemeComparisonColumn(toPreview: @Composable () -> Unit) { Column { Box { - val darkTheme: SharedPreferencesViewModel = viewModel() - darkTheme.updateTheme(ThemeType.DARK) - AmethystTheme(darkTheme) { + AmethystTheme(ThemeType.DARK) { Surface(color = MaterialTheme.colorScheme.background) { toPreview() } } } Box { - val lightTheme: SharedPreferencesViewModel = viewModel() - lightTheme.updateTheme(ThemeType.LIGHT) - AmethystTheme(lightTheme) { + AmethystTheme(ThemeType.LIGHT) { Surface(color = MaterialTheme.colorScheme.background) { toPreview() } } } @@ -56,17 +50,13 @@ fun ThemeComparisonColumn(toPreview: @Composable () -> Unit) { fun ThemeComparisonRow(toPreview: @Composable () -> Unit) { Row { Box(modifier = Modifier.weight(1f)) { - val darkTheme: SharedPreferencesViewModel = viewModel() - darkTheme.updateTheme(ThemeType.DARK) - AmethystTheme(darkTheme) { + AmethystTheme(ThemeType.DARK) { Surface(color = MaterialTheme.colorScheme.background) { toPreview() } } } Box(modifier = Modifier.weight(1f)) { - val lightTheme: SharedPreferencesViewModel = viewModel() - lightTheme.updateTheme(ThemeType.LIGHT) - AmethystTheme(lightTheme) { + AmethystTheme(ThemeType.LIGHT) { Surface(color = MaterialTheme.colorScheme.background) { toPreview() } } } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/theme/Theme.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/theme/Theme.kt index 2258053c4..22ee9aa21 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/theme/Theme.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/theme/Theme.kt @@ -38,6 +38,7 @@ import androidx.compose.material3.darkColorScheme import androidx.compose.material3.lightColorScheme import androidx.compose.runtime.Composable import androidx.compose.runtime.SideEffect +import androidx.compose.runtime.getValue import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.Color @@ -58,7 +59,6 @@ import com.halilibo.richtext.ui.resolveDefaults import com.patrykandpatrick.vico.compose.common.VicoTheme import com.patrykandpatrick.vico.compose.common.VicoTheme.CandlestickCartesianLayerColors import com.vitorpamplona.amethyst.model.ThemeType -import com.vitorpamplona.amethyst.ui.screen.SharedPreferencesViewModel private val DarkColorPalette = darkColorScheme( @@ -484,20 +484,20 @@ val ColorScheme.chartStyle: VicoTheme @Composable fun AmethystTheme( - sharedPrefsViewModel: SharedPreferencesViewModel, + prefTheme: ThemeType, content: @Composable () -> Unit, ) { val context = LocalContext.current val darkTheme = - when (sharedPrefsViewModel.sharedPrefs.theme) { + when (prefTheme) { ThemeType.DARK -> { - val uiManager = context.getSystemService(Context.UI_MODE_SERVICE) as UiModeManager? - uiManager!!.nightMode = UiModeManager.MODE_NIGHT_YES + val uiManager = context.getSystemService(Context.UI_MODE_SERVICE) as UiModeManager + uiManager.nightMode = UiModeManager.MODE_NIGHT_YES true } ThemeType.LIGHT -> { - val uiManager = context.getSystemService(Context.UI_MODE_SERVICE) as UiModeManager? - uiManager!!.nightMode = UiModeManager.MODE_NIGHT_NO + val uiManager = context.getSystemService(Context.UI_MODE_SERVICE) as UiModeManager + uiManager.nightMode = UiModeManager.MODE_NIGHT_NO false } else -> isSystemInDarkTheme()