Updated on 2026-08-14

This commit is contained in:
Tangem 2023-09-22 16:53:59 +08:00
parent 0dd4d45b62
commit 479a67f3be
19 changed files with 257 additions and 61 deletions

View file

@ -1,16 +1,53 @@
package com.tangem.data.card
import com.tangem.data.source.preferences.PreferencesDataSource
import com.tangem.datasource.local.card.UsedCardInfo
import com.tangem.datasource.local.card.UsedCardsStore
import com.tangem.domain.card.repository.CardRepository
import com.tangem.utils.coroutines.CoroutineDispatcherProvider
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.channelFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
internal class DefaultCardRepository(
private val preferencesDataSource: PreferencesDataSource,
private val usedCardsStore: UsedCardsStore,
private val dispatchers: CoroutineDispatcherProvider,
) : CardRepository {
override suspend fun wasCardScanned(cardId: String): Boolean {
return withContext(dispatchers.io) { preferencesDataSource.usedCardsPrefStorage.wasScanned(cardId) }
override fun wasCardScanned(cardId: String): Flow<Boolean> {
return channelFlow {
launch(dispatchers.io) {
usedCardsStore.get()
.collect { savedCards ->
send(element = savedCards.any { it.cardId == cardId })
}
}
withContext(dispatchers.io) {
if (usedCardsStore.getSyncOrNull() == null) {
send(element = false)
}
}
}
}
override suspend fun setCardWasScanned(cardId: String) {
withContext(dispatchers.io) {
usedCardsStore.store(
item = usedCardsStore.getSyncOrNull()
?.updateCard(cardId)
?: listOf(UsedCardInfo(cardId = cardId, isScanned = true)),
)
}
}
private fun List<UsedCardInfo>.updateCard(cardId: String): List<UsedCardInfo> {
return map { cardInfo ->
if (cardInfo.cardId == cardId) {
cardInfo.copy(isScanned = true)
} else {
cardInfo
}
}
}
}

View file

@ -4,6 +4,7 @@ import com.tangem.data.card.DefaultCardRepository
import com.tangem.data.card.DefaultCardSdkConfigRepository
import com.tangem.data.card.sdk.CardSdkProvider
import com.tangem.data.source.preferences.PreferencesDataSource
import com.tangem.datasource.local.card.UsedCardsStore
import com.tangem.domain.card.repository.CardRepository
import com.tangem.domain.card.repository.CardSdkConfigRepository
import com.tangem.utils.coroutines.CoroutineDispatcherProvider
@ -32,9 +33,9 @@ internal object CardDataModule {
@Provides
@Singleton
fun provideCardRepository(
preferencesDataSource: PreferencesDataSource,
usedCardsStore: UsedCardsStore,
dispatchers: CoroutineDispatcherProvider,
): CardRepository {
return DefaultCardRepository(preferencesDataSource = preferencesDataSource, dispatchers = dispatchers)
return DefaultCardRepository(usedCardsStore = usedCardsStore, dispatchers = dispatchers)
}
}