Updated on 2026-08-14

This commit is contained in:
Tangem 2023-12-07 15:45:28 +03:00
commit 95c00b8f40
32 changed files with 303 additions and 41 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

@ -39,6 +39,8 @@ object PreferencesKeys {
val SENT_ONE_TIME_EVENTS_KEY by lazy { stringPreferencesKey(name = "sentOneTimeEvents") }
val WALLETS_BALANCES_STATES_KEY by lazy { stringPreferencesKey(name = "walletsBalancesStates") }
val LAST_SWAPPED_CRYPTOCURRENCY_ID_KEY by lazy { stringPreferencesKey(name = "lastSwappedCryptoCurrency") }
}

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)