Updated on 2026-08-14

This commit is contained in:
Tangem 2023-12-07 13:19:03 +04:00
parent a9dfa54eec
commit 9647e6caa3
31 changed files with 302 additions and 40 deletions

View file

@ -44,12 +44,20 @@ class AppPreferencesStore(
return this[key]?.let(adapter::fromJson)
}
/** Get nullable list of data [T] by string [key] */
/** Get list of data [T] by string [key] */
inline fun <reified T> MutablePreferences.getObjectList(key: Preferences.Key<String>): List<T>? {
val adapter = moshi.adapter<List<T>>(Types.newParameterizedType(List::class.java, T::class.java))
return this[key]?.let(adapter::fromJson)
}
/** Get map with [String] key and value [V] by string [key] from [MutablePreferences] */
inline fun <reified V> MutablePreferences.getObjectMap(key: Preferences.Key<String>): Map<String, V>? {
val type = Types.newParameterizedType(Map::class.java, String::class.java, V::class.java)
val adapter = moshi.adapter<Map<String, V>>(type)
return this[key]?.let(adapter::fromJson)
}
/**
* Set data [T] by string [key] to [MutablePreferences]
*
@ -67,4 +75,12 @@ class AppPreferencesStore(
val adapter = moshi.adapter<List<T>>(Types.newParameterizedType(List::class.java, T::class.java))
this[key] = adapter.toJson(value)
}
/** Set map with [String] key and value [V] by string [key] to [MutablePreferences] */
inline fun <reified V> MutablePreferences.setObjectMap(key: Preferences.Key<String>, value: Map<String, V>) {
val type = Types.newParameterizedType(Map::class.java, String::class.java, V::class.java)
val adapter = moshi.adapter<Map<String, V>>(type)
this[key] = adapter.toJson(value)
}
}

View file

@ -36,6 +36,8 @@ object PreferencesKeys {
val WALLETS_SCROLL_PREVIEW_KEY by lazy { booleanPreferencesKey(name = "walletsScrollPreview") }
val SENT_ONE_TIME_EVENTS_KEY by lazy { stringPreferencesKey(name = "sentOneTimeEvents") }
val WALLETS_BALANCES_STATES_KEY by lazy { stringPreferencesKey(name = "walletsBalancesStates") }
}
/** Preferences keys set that should be migrated from "PreferencesDataSource" to a new DataStore<Preferences> */

View file

@ -76,9 +76,31 @@ inline fun <reified T> AppPreferencesStore.getObjectList(key: Preferences.Key<St
return data.map { it[key]?.let(adapter::fromJson) }
}
/** Get nullable list of data [T] by string [key] */
/** Get list of data [T] by string [key], or empty if data is not found */
suspend inline fun <reified T> AppPreferencesStore.getObjectListSync(key: Preferences.Key<String>): List<T> {
val adapter = moshi.adapter<List<T>>(Types.newParameterizedType(List::class.java, T::class.java))
return data.firstOrNull()
?.get(key)
?.let(adapter::fromJson)
.orEmpty()
}
/** Store map with [String] key and value [V] by string [key] */
suspend inline fun <reified V> AppPreferencesStore.storeObjectMap(
key: Preferences.Key<String>,
value: Map<String, V>,
) {
val type = Types.newParameterizedType(Map::class.java, String::class.java, V::class.java)
val adapter = moshi.adapter<Map<String, V>>(type)
edit { it[key] = adapter.toJson(value) }
}
/** Get map with [String] key and value [V] by string [key], or empty if data is not found */
suspend inline fun <reified V> AppPreferencesStore.getObjectMap(key: Preferences.Key<String>): Map<String, V> {
val type = Types.newParameterizedType(Map::class.java, String::class.java, V::class.java)
val adapter = moshi.adapter<Map<String, V>>(type)
return data.firstOrNull()
?.get(key)
?.let(adapter::fromJson)