commonMain.com.softartdev.themepref.ThemePrefs.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of material-theme-prefs Show documentation
Show all versions of material-theme-prefs Show documentation
Kotlin Multiplatform library for easy switching Dark/Light Material themes on Compose.
package com.softartdev.themepref
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material.Colors
import androidx.compose.material.darkColors
import androidx.compose.material.lightColors
import androidx.compose.runtime.*
class ThemePrefs(
val preferenceHelper: PreferenceHelper,
val dialogHolder: DialogHolder = DialogHolder(),
private val DarkColorPalette: Colors = darkColors(),
private val LightColorPalette: Colors = lightColors()
) {
val darkThemeState: MutableState = mutableStateOf(preferenceHelper.themeEnum)
val colors: Colors
@Composable
@ReadOnlyComposable
get() = when (darkThemeState.value) {
ThemeEnum.Light -> LightColorPalette
ThemeEnum.Dark -> DarkColorPalette
ThemeEnum.SystemDefault -> if (isSystemInDarkTheme()) DarkColorPalette else LightColorPalette
}
fun showDialog() = dialogHolder.showThemeChange(
darkThemeState = darkThemeState,
writePref = preferenceHelper::themeEnum::set
)
@Composable
fun showDialogIfNeed() = dialogHolder.showDialogIfNeed()
}
@Composable
fun rememberThemePrefs(): ThemePrefs {
val preferenceHelper = obtainPreferenceHelper()
return remember { ThemePrefs(preferenceHelper) }
}