androidMain.com.softartdev.themepref.AndroidPreferenceHelper.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 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)
}