androidMain.com.algolia.instantsearch.insights.internal.extension.SharedPreferencesDelegate.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of instantsearch-insights Show documentation
Show all versions of instantsearch-insights Show documentation
InstantSearch Android is a library providing widgets and helpers to help you build the best instant-search experience on Android with Algolia. It is built on top of Algolia's Kotlin API Client to provide you a high-level solution to quickly build various search interfaces.
package com.algolia.instantsearch.insights.internal.extension
import android.content.SharedPreferences
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
internal sealed class SharedPreferencesDelegate(
protected val default: T,
protected val key: kotlin.String? = null,
) : ReadWriteProperty {
class Int(default: kotlin.Int, key: kotlin.String? = null) : SharedPreferencesDelegate(default, key) {
override fun getValue(thisRef: SharedPreferences, property: KProperty<*>): kotlin.Int {
return thisRef.getInt(key ?: property.name, default)
}
override fun setValue(thisRef: SharedPreferences, property: KProperty<*>, value: kotlin.Int) {
thisRef.edit().putInt(key ?: property.name, value).apply()
}
}
class String(default: kotlin.String? = null, key: kotlin.String? = null) :
SharedPreferencesDelegate(default, key) {
override fun getValue(thisRef: SharedPreferences, property: KProperty<*>): kotlin.String? {
return thisRef.getString(key ?: property.name, default)
}
override fun setValue(thisRef: SharedPreferences, property: KProperty<*>, value: kotlin.String?) {
thisRef.edit().putString(key ?: property.name, value).apply()
}
}
class StringSet(default: Set, key: kotlin.String? = null) :
SharedPreferencesDelegate>(default, key) {
override fun getValue(thisRef: SharedPreferences, property: KProperty<*>): Set {
return thisRef.getStringSet(key ?: property.name, default) ?: setOf()
}
override fun setValue(thisRef: SharedPreferences, property: KProperty<*>, value: Set) {
thisRef.edit().putStringSet(key ?: property.name, value).apply()
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy