All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.softartdev.themepref.ThemePrefs.kt Maven / Gradle / Ivy

Go to download

Kotlin Multiplatform library for easy switching Dark/Light Material themes on Compose.

There is a newer version: 0.5.5
Show newest version
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) }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy