Florian
08/28/2021, 7:36 PM@AndroidEntryPoint
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val selectedTheme by appPreferencesViewModel.selectedTheme.collectAsState(null)
selectedTheme?.let { selectedTheme ->
val darkTheme = when (selectedTheme) {
ThemeSelectionValues.LIGHT -> false
ThemeSelectionValues.DARK -> true
ThemeSelectionValues.SYSTEM -> isSystemInDarkTheme()
}
MyAppTheme(darkTheme = darkTheme) {
// ActivityBody with NavHost
}
}
}
}
}
adjpd
08/28/2021, 11:25 PMThemeSelectionValues.SYSTEM
and you change the system's dark/light mode, I guess it changes correctly?
Looks okay to me. That's what I want to do in my next release anyhow.val darkTheme = selectedTheme?.let {
when (it) {
ThemeSelectionValues.LIGHT -> false
ThemeSelectionValues.DARK -> true
ThemeSelectionValues.SYSTEM -> isSystemInDarkTheme()
}
}
MyAppTheme(darkTheme = darkTheme) {
//ActivityBody with NavHost
}
Florian
08/29/2021, 8:50 AMMyAppTheme
has to be inside the ?.let
because we need it to be not nullableAlbert Chang
08/29/2021, 10:47 AMappPreferencesViewModel.selectedTheme
a StateFlow
so that you get the correct value immediately. Now if the user chose dark theme and the system is in light theme, when you app starts, it will render light theme at first, and then render dark theme immediately after that, which is a waste.Florian
08/29/2021, 2:05 PMMyAppTheme
is inside the ?.let
block.