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

androidMain.com.softartdev.themepref.AndroidPreferenceHelper.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 android.content.Context
import android.content.SharedPreferences
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext

class AndroidPreferenceHelper(context: Context) : PreferenceHelper {

    private val preferences: SharedPreferences =
        context.getSharedPreferences(context.packageName + "_ThemePref", Context.MODE_PRIVATE)

    override var themeEnum: ThemeEnum
        get() = preferences.getInt(THEME_KEY, ThemeEnum.SystemDefault.ordinal).let(ThemeEnum.values()::get)
        set(value) = preferences.edit().putInt(THEME_KEY, value.ordinal).apply()

    override fun clear() = preferences.edit().clear().apply()
}

@Composable
actual fun obtainPreferenceHelper(): PreferenceHelper {
    val context = LocalContext.current.applicationContext
    return AndroidPreferenceHelper(context)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy