Fixes blinking on crossfades when the system's light/dark theme is different than the app's theme.

This commit is contained in:
Vitor Pamplona 2024-09-05 10:50:16 -04:00
parent 3a3c0d74d2
commit 8f19276fac
3 changed files with 32 additions and 19 deletions

View File

@ -26,10 +26,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.LaunchedEffect
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import com.google.accompanist.adaptive.calculateDisplayFeatures 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.screen.SharedPreferencesViewModel
import com.vitorpamplona.amethyst.ui.theme.AmethystTheme
@OptIn(ExperimentalMaterial3WindowSizeClassApi::class) @OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
@Composable @Composable
@ -53,16 +50,3 @@ fun prepareSharedViewModel(act: MainActivity): SharedPreferencesViewModel {
return sharedPreferencesViewModel return sharedPreferencesViewModel
} }
@Composable
fun AppScreen(sharedPreferencesViewModel: SharedPreferencesViewModel) {
AmethystTheme(sharedPreferencesViewModel) {
val accountStateViewModel: AccountStateViewModel = viewModel()
LaunchedEffect(key1 = Unit) {
accountStateViewModel.tryLoginExistingAccountAsync()
}
AccountScreen(accountStateViewModel, sharedPreferencesViewModel)
}
}

View File

@ -34,7 +34,9 @@ import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.viewmodel.compose.viewModel
import com.vitorpamplona.amethyst.Amethyst import com.vitorpamplona.amethyst.Amethyst
import com.vitorpamplona.amethyst.LocalPreferences import com.vitorpamplona.amethyst.LocalPreferences
import com.vitorpamplona.amethyst.debugState 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.DEFAULT_MUTED_SETTING
import com.vitorpamplona.amethyst.ui.components.keepPlayingMutex import com.vitorpamplona.amethyst.ui.components.keepPlayingMutex
import com.vitorpamplona.amethyst.ui.navigation.Route 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.ammolite.service.HttpClientManager
import com.vitorpamplona.quartz.encoders.Nip19Bech32 import com.vitorpamplona.quartz.encoders.Nip19Bech32
import com.vitorpamplona.quartz.encoders.Nip47WalletConnect import com.vitorpamplona.quartz.encoders.Nip47WalletConnect
@ -78,7 +83,16 @@ class MainActivity : AppCompatActivity() {
setContent { setContent {
val sharedPreferencesViewModel = prepareSharedViewModel(act = this) val sharedPreferencesViewModel = prepareSharedViewModel(act = this)
AppScreen(sharedPreferencesViewModel = sharedPreferencesViewModel)
AmethystTheme(sharedPreferencesViewModel) {
val accountStateViewModel: AccountStateViewModel = viewModel()
LaunchedEffect(key1 = Unit) {
accountStateViewModel.tryLoginExistingAccountAsync()
}
AccountScreen(accountStateViewModel, sharedPreferencesViewModel)
}
} }
} }

View File

@ -21,6 +21,8 @@
package com.vitorpamplona.amethyst.ui.theme package com.vitorpamplona.amethyst.ui.theme
import android.app.Activity import android.app.Activity
import android.app.UiModeManager
import android.content.Context
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.border import androidx.compose.foundation.border
import androidx.compose.foundation.isSystemInDarkTheme 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.RectangleShape
import androidx.compose.ui.graphics.compositeOver import androidx.compose.ui.graphics.compositeOver
import androidx.compose.ui.graphics.toArgb import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalView import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.text.SpanStyle import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.core.content.ContextCompat.getSystemService
import androidx.core.view.WindowCompat import androidx.core.view.WindowCompat
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import com.halilibo.richtext.ui.RichTextStyle import com.halilibo.richtext.ui.RichTextStyle
@ -451,10 +455,19 @@ fun AmethystTheme(
sharedPrefsViewModel: SharedPreferencesViewModel, sharedPrefsViewModel: SharedPreferencesViewModel,
content: @Composable () -> Unit, content: @Composable () -> Unit,
) { ) {
val context = LocalContext.current
val darkTheme = val darkTheme =
when (sharedPrefsViewModel.sharedPrefs.theme) { when (sharedPrefsViewModel.sharedPrefs.theme) {
ThemeType.DARK -> true ThemeType.DARK -> {
ThemeType.LIGHT -> false 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() else -> isSystemInDarkTheme()
} }
val colors = if (darkTheme) DarkColorPalette else LightColorPalette val colors = if (darkTheme) DarkColorPalette else LightColorPalette
@ -477,6 +490,8 @@ fun AmethystTheme(
window.statusBarColor = colors.transparentBackground.toArgb() window.statusBarColor = colors.transparentBackground.toArgb()
window.navigationBarColor = colors.transparentBackground.toArgb() window.navigationBarColor = colors.transparentBackground.toArgb()
view.setBackgroundColor(colors.background.toArgb())
} }
} }
} }