Skip to content

Instantly share code, notes, and snippets.

@MrNtlu
Created December 7, 2022 13:12
Show Gist options
  • Save MrNtlu/71cfdde63a4852c28eac905936207367 to your computer and use it in GitHub Desktop.
Save MrNtlu/71cfdde63a4852c28eac905936207367 to your computer and use it in GitHub Desktop.
Jetpack Compose Dark Theme Main Activity Part 1
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