Updated on 2026-08-14
This commit is contained in:
commit
809c622022
4 changed files with 83 additions and 15 deletions
|
|
@ -5,6 +5,7 @@ import androidx.datastore.preferences.core.MutablePreferences
|
|||
import androidx.datastore.preferences.core.Preferences
|
||||
import androidx.datastore.preferences.core.edit
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.Types
|
||||
|
||||
/**
|
||||
* Application preferences store.
|
||||
|
|
@ -31,15 +32,39 @@ class AppPreferencesStore(
|
|||
return edit { transform(it) }
|
||||
}
|
||||
|
||||
/** Get nullable data [T] by string [key] from [MutablePreferences] */
|
||||
/**
|
||||
* Get nullable data [T] by string [key] from [MutablePreferences]
|
||||
*
|
||||
* Warning: This method cannot be used for T with parameterized types (e.g. List, Set, etc.).
|
||||
*
|
||||
* @see getObjectList
|
||||
* */
|
||||
inline fun <reified T> MutablePreferences.getObject(key: Preferences.Key<String>): T? {
|
||||
val adapter = moshi.adapter(T::class.java)
|
||||
val adapter = moshi.adapter(T::class.java) // TODO: Support parameterized types
|
||||
return this[key]?.let(adapter::fromJson)
|
||||
}
|
||||
|
||||
/** Set data [T] by string [key] to [MutablePreferences] */
|
||||
/** Get nullable 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)
|
||||
}
|
||||
|
||||
/**
|
||||
* Set data [T] by string [key] to [MutablePreferences]
|
||||
*
|
||||
* Warning: This method cannot be used for T with parameterized types (e.g. List, Set, etc.).
|
||||
*
|
||||
* @see setObjectList
|
||||
* */
|
||||
inline fun <reified T> MutablePreferences.setObject(key: Preferences.Key<String>, value: T) {
|
||||
val adapter = moshi.adapter(T::class.java)
|
||||
val adapter = moshi.adapter(T::class.java) // TODO: Support parameterized types
|
||||
this[key] = adapter.toJson(value)
|
||||
}
|
||||
|
||||
/** Set list of data [T] by string [key] to [MutablePreferences] */
|
||||
inline fun <reified T> MutablePreferences.setObjectList(key: Preferences.Key<String>, value: List<T>) {
|
||||
val adapter = moshi.adapter<List<T>>(Types.newParameterizedType(List::class.java, T::class.java))
|
||||
this[key] = adapter.toJson(value)
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@ package com.tangem.datasource.local.preferences.utils
|
|||
|
||||
import androidx.datastore.preferences.core.Preferences
|
||||
import androidx.datastore.preferences.core.edit
|
||||
import com.squareup.moshi.Types
|
||||
import com.tangem.datasource.local.preferences.AppPreferencesStore
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.firstOrNull
|
||||
|
|
@ -13,15 +14,27 @@ inline fun <reified T> AppPreferencesStore.getObject(key: Preferences.Key<String
|
|||
return data.map { it[key]?.let(adapter::fromJson) }
|
||||
}
|
||||
|
||||
/** Get flow of data [T] by string [key]. If data is not found, it returns [default] */
|
||||
/**
|
||||
* Get flow of data [T] by string [key]. If data is not found, it returns [default]
|
||||
*
|
||||
* Warning: This method cannot be used for parameterized types (e.g. List, Set, etc.).
|
||||
*
|
||||
* @see getObjectList
|
||||
* */
|
||||
inline fun <reified T> AppPreferencesStore.getObject(key: Preferences.Key<String>, default: T): Flow<T> {
|
||||
val adapter = moshi.adapter(T::class.java)
|
||||
val adapter = moshi.adapter(T::class.java) // TODO: Support parameterized types
|
||||
return data.map { it[key]?.let(adapter::fromJson) ?: default }
|
||||
}
|
||||
|
||||
/** Get nullable data [T] by string [key] */
|
||||
/**
|
||||
* Get nullable data [T] by string [key]
|
||||
*
|
||||
* Warning: This method cannot be used for T with parameterized types (e.g. List, Set, etc.).
|
||||
*
|
||||
* @see getObjectListSync
|
||||
* */
|
||||
suspend inline fun <reified T> AppPreferencesStore.getObjectSyncOrNull(key: Preferences.Key<String>): T? {
|
||||
val adapter = moshi.adapter(T::class.java)
|
||||
val adapter = moshi.adapter(T::class.java) // TODO: Support parameterized types
|
||||
return data.firstOrNull()
|
||||
?.get(key)
|
||||
?.let(adapter::fromJson)
|
||||
|
|
@ -39,8 +52,35 @@ suspend inline fun <reified T> AppPreferencesStore.getObjectSyncOrDefault(
|
|||
?: default
|
||||
}
|
||||
|
||||
/** Store data [value] by string [key] */
|
||||
/**
|
||||
* Store data [value] by string [key]
|
||||
*
|
||||
* Warning: This method cannot be used for T with parameterized types (e.g. List, Set, etc.).
|
||||
*
|
||||
* @see storeObjectList
|
||||
* */
|
||||
suspend inline fun <reified T> AppPreferencesStore.storeObject(key: Preferences.Key<String>, value: T) {
|
||||
val adapter = moshi.adapter(T::class.java)
|
||||
val adapter = moshi.adapter(T::class.java) // TODO: Support parameterized types
|
||||
edit { it[key] = adapter.toJson(value) }
|
||||
}
|
||||
|
||||
/** Store list of data [value] by string [key] */
|
||||
suspend inline fun <reified T> AppPreferencesStore.storeObjectList(key: Preferences.Key<String>, value: List<T>) {
|
||||
val adapter = moshi.adapter<List<T>>(Types.newParameterizedType(List::class.java, T::class.java))
|
||||
edit { it[key] = adapter.toJson(value) }
|
||||
}
|
||||
|
||||
/** Get flow of list of data [T] by string [key]. If data is not found, it returns `null` */
|
||||
inline fun <reified T> AppPreferencesStore.getObjectList(key: Preferences.Key<String>): Flow<List<T>?> {
|
||||
val adapter = moshi.adapter<List<T>>(Types.newParameterizedType(List::class.java, T::class.java))
|
||||
return data.map { it[key]?.let(adapter::fromJson) }
|
||||
}
|
||||
|
||||
/** Get nullable list of data [T] by string [key] */
|
||||
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()
|
||||
}
|
||||
|
|
@ -9,6 +9,7 @@ import com.tangem.datasource.local.appcurrency.AvailableAppCurrenciesStore
|
|||
import com.tangem.datasource.local.preferences.AppPreferencesStore
|
||||
import com.tangem.datasource.local.preferences.PreferencesKeys
|
||||
import com.tangem.datasource.local.preferences.utils.getObject
|
||||
import com.tangem.datasource.local.preferences.utils.getSyncOrNull
|
||||
import com.tangem.datasource.local.preferences.utils.storeObject
|
||||
import com.tangem.domain.appcurrency.model.AppCurrency
|
||||
import com.tangem.domain.appcurrency.repository.AppCurrencyRepository
|
||||
|
|
@ -42,7 +43,9 @@ internal class DefaultAppCurrencyRepository(
|
|||
}
|
||||
|
||||
withContext(dispatchers.io) {
|
||||
fetchDefaultAppCurrency()
|
||||
if (appPreferencesStore.getSyncOrNull(PreferencesKeys.SELECTED_APP_CURRENCY_KEY) == null) {
|
||||
fetchDefaultAppCurrency()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ package com.tangem.data.card
|
|||
import com.tangem.datasource.local.card.UsedCardInfo
|
||||
import com.tangem.datasource.local.preferences.AppPreferencesStore
|
||||
import com.tangem.datasource.local.preferences.PreferencesKeys
|
||||
import com.tangem.datasource.local.preferences.utils.getObject
|
||||
import com.tangem.datasource.local.preferences.utils.getObjectList
|
||||
import com.tangem.domain.card.repository.CardRepository
|
||||
import com.tangem.utils.extensions.addOrReplace
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
|
@ -14,7 +14,7 @@ internal class DefaultCardRepository(
|
|||
) : CardRepository {
|
||||
|
||||
override fun wasCardScanned(cardId: String): Flow<Boolean> {
|
||||
return appPreferencesStore.getObject<List<UsedCardInfo>>(key = PreferencesKeys.USED_CARDS_INFO_KEY)
|
||||
return appPreferencesStore.getObjectList<UsedCardInfo>(key = PreferencesKeys.USED_CARDS_INFO_KEY)
|
||||
.map { savedCards ->
|
||||
savedCards?.any { it.cardId == cardId } ?: false
|
||||
}
|
||||
|
|
@ -22,14 +22,14 @@ internal class DefaultCardRepository(
|
|||
|
||||
override suspend fun setCardWasScanned(cardId: String) {
|
||||
appPreferencesStore.editData { mutablePreferences ->
|
||||
val usedCards: List<UsedCardInfo>? = mutablePreferences.getObject(
|
||||
val usedCards: List<UsedCardInfo>? = mutablePreferences.getObjectList(
|
||||
key = PreferencesKeys.USED_CARDS_INFO_KEY,
|
||||
)
|
||||
|
||||
val updatedUsedCards = usedCards?.updateCard(cardId)
|
||||
?: listOf(UsedCardInfo(cardId = cardId, isScanned = true))
|
||||
|
||||
mutablePreferences.setObject(
|
||||
mutablePreferences.setObjectList(
|
||||
key = PreferencesKeys.USED_CARDS_INFO_KEY,
|
||||
value = updatedUsedCards,
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue