Support float preferences

This commit is contained in:
Niels van Velzen 2024-08-04 10:36:17 +02:00 committed by Niels van Velzen
parent 8230479059
commit 4e8fd15508
5 changed files with 25 additions and 2 deletions

View File

@ -82,6 +82,9 @@ abstract class DisplayPreferencesStore(
override fun getLong(key: String, defaultValue: Long) =
cachedPreferences[key]?.toLongOrNull() ?: defaultValue
override fun getFloat(key: String, defaultValue: Float) =
cachedPreferences[key]?.toFloatOrNull() ?: defaultValue
override fun getBool(key: String, defaultValue: Boolean) =
cachedPreferences[key]?.toBooleanStrictOrNull() ?: defaultValue
@ -96,6 +99,10 @@ abstract class DisplayPreferencesStore(
cachedPreferences[key] = value.toString()
}
override fun setFloat(key: String, value: Float) {
cachedPreferences[key] = value.toString()
}
override fun setBool(key: String, value: Boolean) {
cachedPreferences[key] = value.toString()
}

View File

@ -10,6 +10,7 @@ data class Preference<T : Any>(
fun intPreference(key: String, defaultValue: Int) = Preference(key, defaultValue, Int::class)
fun longPreference(key: String, defaultValue: Long) = Preference(key, defaultValue, Long::class)
fun floatPreference(key: String, defaultValue: Float) = Preference(key, defaultValue, Float::class)
fun booleanPreference(key: String, defaultValue: Boolean) = Preference(key, defaultValue, Boolean::class)
fun stringPreference(key: String, defaultValue: String) = Preference(key, defaultValue, String::class)
fun <T : Any> enumPreference(key: String, defaultValue: T, type: KClass<T>) = Preference(key, defaultValue, type)

View File

@ -18,6 +18,7 @@ abstract class PreferenceStore<ME, MV> {
when (preference.defaultValue) {
is Int -> getInt(preference.key, preference.defaultValue)
is Long -> getLong(preference.key, preference.defaultValue)
is Float -> getFloat(preference.key, preference.defaultValue)
is Boolean -> getBool(preference.key, preference.defaultValue)
is String -> getString(preference.key, preference.defaultValue)
else -> throw IllegalArgumentException("${preference.type.simpleName} type is not supported")
@ -31,6 +32,7 @@ abstract class PreferenceStore<ME, MV> {
when (value) {
is Int -> setInt(preference.key, value)
is Long -> setLong(preference.key, value)
is Float -> setFloat(preference.key, value)
is Boolean -> setBool(preference.key, value)
is String -> setString(preference.key, value)
is Enum<*> -> setEnum(preference, value)
@ -52,11 +54,13 @@ abstract class PreferenceStore<ME, MV> {
// it in the abstract common functionality (where it is used)
protected abstract fun getInt(key: String, defaultValue: Int): Int
protected abstract fun getLong(key: String, defaultValue: Long): Long
protected abstract fun getFloat(key: String, defaultValue: Float): Float
protected abstract fun getBool(key: String, defaultValue: Boolean): Boolean
protected abstract fun getString(key: String, defaultValue: String): String
protected abstract fun setInt(key: String, value: Int)
protected abstract fun setLong(key: String, value: Long)
protected abstract fun setFloat(key: String, value: Float)
protected abstract fun setBool(key: String, value: Boolean)
protected abstract fun setString(key: String, value: String)

View File

@ -46,6 +46,9 @@ abstract class SharedPreferenceStore(
override fun getLong(key: String, defaultValue: Long) =
sharedPreferences.getLong(key, defaultValue)
override fun getFloat(key: String, defaultValue: Float) =
sharedPreferences.getFloat(key, defaultValue)
override fun getBool(key: String, defaultValue: Boolean) =
sharedPreferences.getBoolean(key, defaultValue)
@ -54,6 +57,7 @@ abstract class SharedPreferenceStore(
override fun setInt(key: String, value: Int) = transaction { putInt(key, value) }
override fun setLong(key: String, value: Long) = transaction { putLong(key, value) }
override fun setFloat(key: String, value: Float) = transaction { putFloat(key, value) }
override fun setBool(key: String, value: Boolean) =
transaction { putBoolean(key, value) }

View File

@ -20,6 +20,7 @@ class PreferenceStoreTests : FunSpec({
test("Reading and writing primitives works correctly") {
verifySimpleType(1, intPreference("key", 0))
verifySimpleType(1L, longPreference("key", 0L))
verifySimpleType(1f, floatPreference("key", 0f))
verifySimpleType(true, booleanPreference("key", false))
verifySimpleType("string", stringPreference("key", ""))
}
@ -38,6 +39,7 @@ private class TestStub : PreferenceStore<Unit, Unit>() {
var key: String? = null
private var int: Int? = null
private var long: Long? = null
private var float: Float? = null
private var bool: Boolean? = null
private var string: String? = null
private var enum: Enum<*>? = null
@ -47,7 +49,8 @@ private class TestStub : PreferenceStore<Unit, Unit>() {
(this.enum ?: preference.defaultValue) as T
override fun getInt(key: String, defaultValue: Int): Int = int ?: 0
override fun getLong(key: String, defaultValue: Long): Long = long ?: 0
override fun getLong(key: String, defaultValue: Long): Long = long ?: 0L
override fun getFloat(key: String, defaultValue: Float): Float = float ?: 0f
override fun getBool(key: String, defaultValue: Boolean): Boolean = bool ?: false
override fun getString(key: String, defaultValue: String) = string ?: ""
@ -66,6 +69,11 @@ private class TestStub : PreferenceStore<Unit, Unit>() {
long = value
}
override fun setFloat(key: String, value: Float) {
this.key = key
float = value
}
override fun setBool(key: String, value: Boolean) {
this.key = key
bool = value
@ -76,7 +84,6 @@ private class TestStub : PreferenceStore<Unit, Unit>() {
string = value
}
override fun <T : Any> delete(preference: Preference<T>) {
throw NotImplementedError("Not required for tests")
}