Updated on 2026-08-14

This commit is contained in:
Tangem 2024-02-18 14:37:31 +03:00
parent ff1ac5d0e7
commit 283bc49d3a
18 changed files with 247 additions and 314 deletions

View file

@ -21,7 +21,7 @@ internal object AppPreferencesStoreModule {
fun provideAppPreferencesStore(
@ApplicationContext appContext: Context,
dispatchers: CoroutineDispatcherProvider,
@NetworkMoshi moshi: Moshi,
@SdkMoshi moshi: Moshi,
): AppPreferencesStore {
return AppPreferencesStore(
preferencesDataStore = PreferencesDataStore.getInstance(context = appContext, dispatcher = dispatchers.io),

View file

@ -45,7 +45,7 @@ class AppPreferencesStore(
* @see getObjectList
* */
inline fun <reified T> MutablePreferences.getObject(key: Preferences.Key<String>): T? {
val adapter = moshi.adapter(T::class.java) // TODO: Support parameterized types
val adapter = moshi.adapter(T::class.java)
return this[key]?.let(adapter::fromJson)
}
@ -55,6 +55,15 @@ class AppPreferencesStore(
return this[key]?.let(adapter::fromJson)
}
/** Get list of data [T] by string [key] or default */
inline fun <reified T> MutablePreferences.getObjectListOrDefault(
key: Preferences.Key<String>,
default: List<T>,
): List<T> {
val adapter = moshi.adapter<List<T>>(Types.newParameterizedType(List::class.java, T::class.java))
return this[key]?.let(adapter::fromJson) ?: default
}
/** 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)