Created
December 7, 2022 13:12
-
-
Save MrNtlu/71cfdde63a4852c28eac905936207367 to your computer and use it in GitHub Desktop.
Jetpack Compose Dark Theme Main Activity Part 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : ComponentActivity() { | |
private val themeViewModel: ThemeViewModel by viewModels() | |
private lateinit var dataStoreUtil: DataStoreUtil | |
override fun onCreate(savedInstanceState: Bundle?) { | |
//... | |
dataStoreUtil = DataStoreUtil(applicationContext) | |
val systemTheme = when (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) { | |
Configuration.UI_MODE_NIGHT_YES -> { true } | |
Configuration.UI_MODE_NIGHT_NO -> { false } | |
else -> { false } | |
} | |
setContent { | |
val theme = dataStoreUtil.getTheme(systemTheme).collectAsState(initial = systemTheme) | |
JetpackComposeDarkThemeTheme( | |
darkTheme = theme.value, | |
) { | |
Surface( | |
//... | |
) { | |
DarkThemeScreen(dataStoreUtil, themeViewModel) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment