Updated on 2026-08-14
This commit is contained in:
parent
ff1ac5d0e7
commit
283bc49d3a
18 changed files with 247 additions and 314 deletions
|
|
@ -1,9 +1,11 @@
|
|||
package com.tangem.data.card
|
||||
|
||||
import androidx.datastore.preferences.core.MutablePreferences
|
||||
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.getObjectList
|
||||
import com.tangem.datasource.local.preferences.utils.getObjectListSync
|
||||
import com.tangem.domain.card.repository.CardRepository
|
||||
import com.tangem.utils.extensions.addOrReplace
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
|
@ -21,23 +23,73 @@ internal class DefaultCardRepository(
|
|||
}
|
||||
|
||||
override suspend fun setCardWasScanned(cardId: String) {
|
||||
appPreferencesStore.editData { mutablePreferences ->
|
||||
val usedCards: List<UsedCardInfo>? = mutablePreferences.getObjectList(
|
||||
key = PreferencesKeys.USED_CARDS_INFO_KEY,
|
||||
)
|
||||
appPreferencesStore.editUsedCards(cardId) { it.copy(isScanned = true) }
|
||||
}
|
||||
|
||||
val updatedUsedCards = usedCards?.updateCard(cardId)
|
||||
?: listOf(UsedCardInfo(cardId = cardId, isScanned = true))
|
||||
override suspend fun startCardActivation(cardId: String) {
|
||||
appPreferencesStore.editUsedCards(cardId) { it.copy(isActivationStarted = true) }
|
||||
}
|
||||
|
||||
mutablePreferences.setObjectList(
|
||||
key = PreferencesKeys.USED_CARDS_INFO_KEY,
|
||||
value = updatedUsedCards,
|
||||
)
|
||||
override suspend fun finishCardActivation(cardId: String) {
|
||||
appPreferencesStore.editUsedCards(cardId) {
|
||||
it.copy(isActivationStarted = true, isActivationFinished = true)
|
||||
}
|
||||
}
|
||||
|
||||
private fun List<UsedCardInfo>.updateCard(cardId: String): List<UsedCardInfo> {
|
||||
val card = find { it.cardId == cardId } ?: UsedCardInfo(cardId = cardId, isScanned = true)
|
||||
return addOrReplace(item = card.copy(isScanned = true), predicate = { it.cardId == cardId })
|
||||
override suspend fun finishCardsActivation(cardIds: List<String>) {
|
||||
appPreferencesStore.editData { mutablePreferences ->
|
||||
val usedCards = mutablePreferences.getUsedCards()
|
||||
|
||||
val updatedUsedCards = cardIds.map { cardId ->
|
||||
usedCards.updateCard(cardId) {
|
||||
it.copy(isActivationStarted = true, isActivationFinished = true)
|
||||
}
|
||||
}
|
||||
|
||||
mutablePreferences.setObjectList(key = PreferencesKeys.USED_CARDS_INFO_KEY, value = updatedUsedCards)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun isActivationStarted(cardId: String): Boolean {
|
||||
return getUsedCardSync(cardId)?.isActivationStarted ?: false
|
||||
}
|
||||
|
||||
override suspend fun isActivationFinished(cardId: String): Boolean {
|
||||
return getUsedCardSync(cardId)?.isActivationFinished ?: false
|
||||
}
|
||||
|
||||
override suspend fun isActivationInProgress(cardId: String): Boolean {
|
||||
val card = getUsedCardSync(cardId) ?: return false
|
||||
|
||||
return card.isActivationStarted && !card.isActivationFinished
|
||||
}
|
||||
|
||||
private suspend fun AppPreferencesStore.editUsedCards(cardId: String, update: (UsedCardInfo) -> UsedCardInfo) {
|
||||
editData { mutablePreferences ->
|
||||
val usedCards = mutablePreferences.getUsedCards()
|
||||
|
||||
val updatedUsedCards = usedCards.updateCard(cardId = cardId, update = update)
|
||||
|
||||
mutablePreferences.setObjectList(key = PreferencesKeys.USED_CARDS_INFO_KEY, value = updatedUsedCards)
|
||||
}
|
||||
}
|
||||
|
||||
private fun MutablePreferences.getUsedCards(): List<UsedCardInfo> {
|
||||
return with(appPreferencesStore) {
|
||||
getObjectListOrDefault(key = PreferencesKeys.USED_CARDS_INFO_KEY, default = mutableListOf())
|
||||
}
|
||||
}
|
||||
|
||||
private fun List<UsedCardInfo>.updateCard(
|
||||
cardId: String,
|
||||
update: (UsedCardInfo) -> UsedCardInfo,
|
||||
): List<UsedCardInfo> {
|
||||
val card = find { it.cardId == cardId } ?: UsedCardInfo(cardId = cardId)
|
||||
return addOrReplace(item = update(card), predicate = { it.cardId == cardId })
|
||||
}
|
||||
|
||||
private suspend fun getUsedCardSync(cardId: String): UsedCardInfo? {
|
||||
return appPreferencesStore.getObjectListSync<UsedCardInfo>(PreferencesKeys.USED_CARDS_INFO_KEY)
|
||||
.firstOrNull { it.cardId == cardId }
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue