settings refactoring

This commit is contained in:
2024-07-15 22:34:00 +03:00
parent 304c630d1d
commit 789283fcff
20 changed files with 942 additions and 907 deletions
@@ -3,6 +3,7 @@ package com.meloda.app.fast.datastore
import android.content.SharedPreferences
import androidx.core.content.edit
import kotlin.properties.Delegates
import kotlin.reflect.KClass
object SettingsController {
@@ -39,6 +40,29 @@ object SettingsController {
return preferences.getFloat(key, defaultValue)
}
@Suppress("UNCHECKED_CAST")
fun <T : Any> get(clazz: KClass<T>, key: String, defaultValue: T): T {
return when (clazz) {
String::class -> getString(key, defaultValue as String)
Boolean::class -> getBoolean(key, defaultValue as Boolean)
Int::class -> getInt(key, defaultValue as Int)
Long::class -> getLong(key, defaultValue as Long)
Float::class -> getFloat(key, defaultValue as Float)
else -> throw IllegalStateException("Unsupported class: $clazz")
} as T
}
inline fun <reified T> get(key: String, defaultValue: T): T {
return when (T::class) {
String::class -> getString(key, defaultValue as String)
Boolean::class -> getBoolean(key, defaultValue as Boolean)
Int::class -> getInt(key, defaultValue as Int)
Long::class -> getLong(key, defaultValue as Long)
Float::class -> getFloat(key, defaultValue as Float)
else -> throw IllegalStateException("Unsupported class: ${T::class}")
} as T
}
fun <T> put(key: String, newValue: T?) {
preferences.edit {
when (newValue) {
@@ -52,13 +76,13 @@ object SettingsController {
}
var isLongPollInBackgroundEnabled: Boolean
get() = getBoolean(
get() = get(
SettingsKeys.KEY_FEATURES_LONG_POLL_IN_BACKGROUND,
SettingsKeys.DEFAULT_VALUE_FEATURES_LONG_POLL_IN_BACKGROUND
)
set(value) = put(SettingsKeys.KEY_FEATURES_LONG_POLL_IN_BACKGROUND, value)
var deviceId: String
get() = getString("device_id", "").orEmpty()
get() = get("device_id", "")
set(value) = put("device_id", value)
}