Here's what I do:
1. Create an injectable class:
class AppPreferences(context: Context)
.
2. You can use
this library with
datastore to make things easier for use and future testing as well.
This an example of how it is used with datastore.
3. Now that you have your preferences class setup, define a theme preference in it.
4. Inject the
Apppreferences
into your
MainActivity
and pass the instance to when you will call your theme composable.
@Inject
lateinit var appPreferences: AppPreferences
...
setContent {
YourAppsTheme(appPreferences) { ... }
}
1. Inside your theme, use the app preferences to get theme preference. If you are using the suggested lib, it should be very easy:
@Composable
fun YourAppsTheme(appPreferences: AppPreferences) {
val theme by appPreferences.theme.getFlow().collectAsState()
// Use theme however you want!
}