From 8f19276fac5ca15430651dc3150baabd09893f7d Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Thu, 5 Sep 2024 10:50:16 -0400 Subject: [PATCH] Fixes blinking on crossfades when the system's light/dark theme is different than the app's theme. --- .../vitorpamplona/amethyst/ui/AppScreen.kt | 16 ---------------- .../vitorpamplona/amethyst/ui/MainActivity.kt | 16 +++++++++++++++- .../vitorpamplona/amethyst/ui/theme/Theme.kt | 19 +++++++++++++++++-- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/AppScreen.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/AppScreen.kt index acc04dd505..b231f0bae3 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/AppScreen.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/AppScreen.kt @@ -26,10 +26,7 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.lifecycle.viewmodel.compose.viewModel import com.google.accompanist.adaptive.calculateDisplayFeatures -import com.vitorpamplona.amethyst.ui.screen.AccountScreen -import com.vitorpamplona.amethyst.ui.screen.AccountStateViewModel import com.vitorpamplona.amethyst.ui.screen.SharedPreferencesViewModel -import com.vitorpamplona.amethyst.ui.theme.AmethystTheme @OptIn(ExperimentalMaterial3WindowSizeClassApi::class) @Composable @@ -53,16 +50,3 @@ fun prepareSharedViewModel(act: MainActivity): SharedPreferencesViewModel { return sharedPreferencesViewModel } - -@Composable -fun AppScreen(sharedPreferencesViewModel: SharedPreferencesViewModel) { - AmethystTheme(sharedPreferencesViewModel) { - val accountStateViewModel: AccountStateViewModel = viewModel() - - LaunchedEffect(key1 = Unit) { - accountStateViewModel.tryLoginExistingAccountAsync() - } - - AccountScreen(accountStateViewModel, sharedPreferencesViewModel) - } -} diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/MainActivity.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/MainActivity.kt index fb55177267..7c787222e7 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/MainActivity.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/MainActivity.kt @@ -34,7 +34,9 @@ import androidx.activity.enableEdgeToEdge import androidx.activity.result.contract.ActivityResultContracts import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatActivity +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.mutableStateOf +import androidx.lifecycle.viewmodel.compose.viewModel import com.vitorpamplona.amethyst.Amethyst import com.vitorpamplona.amethyst.LocalPreferences import com.vitorpamplona.amethyst.debugState @@ -44,6 +46,9 @@ import com.vitorpamplona.amethyst.service.notifications.PushNotificationUtils import com.vitorpamplona.amethyst.ui.components.DEFAULT_MUTED_SETTING import com.vitorpamplona.amethyst.ui.components.keepPlayingMutex import com.vitorpamplona.amethyst.ui.navigation.Route +import com.vitorpamplona.amethyst.ui.screen.AccountScreen +import com.vitorpamplona.amethyst.ui.screen.AccountStateViewModel +import com.vitorpamplona.amethyst.ui.theme.AmethystTheme import com.vitorpamplona.ammolite.service.HttpClientManager import com.vitorpamplona.quartz.encoders.Nip19Bech32 import com.vitorpamplona.quartz.encoders.Nip47WalletConnect @@ -78,7 +83,16 @@ class MainActivity : AppCompatActivity() { setContent { val sharedPreferencesViewModel = prepareSharedViewModel(act = this) - AppScreen(sharedPreferencesViewModel = sharedPreferencesViewModel) + + AmethystTheme(sharedPreferencesViewModel) { + val accountStateViewModel: AccountStateViewModel = viewModel() + + LaunchedEffect(key1 = Unit) { + accountStateViewModel.tryLoginExistingAccountAsync() + } + + AccountScreen(accountStateViewModel, sharedPreferencesViewModel) + } } } 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 143dcac847..d5ec9cbf95 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 @@ -21,6 +21,8 @@ package com.vitorpamplona.amethyst.ui.theme import android.app.Activity +import android.app.UiModeManager +import android.content.Context import androidx.compose.foundation.background import androidx.compose.foundation.border import androidx.compose.foundation.isSystemInDarkTheme @@ -45,12 +47,14 @@ import androidx.compose.ui.graphics.ColorFilter import androidx.compose.ui.graphics.RectangleShape import androidx.compose.ui.graphics.compositeOver import androidx.compose.ui.graphics.toArgb +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalView import androidx.compose.ui.text.SpanStyle import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp +import androidx.core.content.ContextCompat.getSystemService import androidx.core.view.WindowCompat import androidx.lifecycle.viewmodel.compose.viewModel import com.halilibo.richtext.ui.RichTextStyle @@ -451,10 +455,19 @@ fun AmethystTheme( sharedPrefsViewModel: SharedPreferencesViewModel, content: @Composable () -> Unit, ) { + val context = LocalContext.current val darkTheme = when (sharedPrefsViewModel.sharedPrefs.theme) { - ThemeType.DARK -> true - ThemeType.LIGHT -> false + ThemeType.DARK -> { + 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 + false + } else -> isSystemInDarkTheme() } val colors = if (darkTheme) DarkColorPalette else LightColorPalette @@ -477,6 +490,8 @@ fun AmethystTheme( window.statusBarColor = colors.transparentBackground.toArgb() window.navigationBarColor = colors.transparentBackground.toArgb() + + view.setBackgroundColor(colors.background.toArgb()) } } }