From 0fae3c188c75ceb7db6038a18eefe83b50d68b7f Mon Sep 17 00:00:00 2001 From: Tangem Date: Tue, 27 Feb 2024 16:11:50 +0500 Subject: [PATCH] Updated on 2026-08-14 --- .../tap/di/domain/TxHistoryDomainModule.kt | 7 ++++ .../repository/DefaultTxHistoryRepository.kt | 40 +++++++++++++++++++ .../repository/TxHistoryRepository.kt | 8 ++++ .../usecase/GetFixedTxHistoryItemsUseCase.kt | 36 +++++++++++++++++ .../usecase/GetTxHistoryItemsUseCase.kt | 2 +- 5 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 domain/txhistory/src/main/kotlin/com/tangem/domain/txhistory/usecase/GetFixedTxHistoryItemsUseCase.kt diff --git a/app/src/main/java/com/tangem/tap/di/domain/TxHistoryDomainModule.kt b/app/src/main/java/com/tangem/tap/di/domain/TxHistoryDomainModule.kt index 7ba9e05cff..7f0c9abe88 100644 --- a/app/src/main/java/com/tangem/tap/di/domain/TxHistoryDomainModule.kt +++ b/app/src/main/java/com/tangem/tap/di/domain/TxHistoryDomainModule.kt @@ -3,6 +3,7 @@ package com.tangem.tap.di.domain import com.tangem.domain.tokens.* import com.tangem.domain.txhistory.repository.TxHistoryRepository import com.tangem.domain.txhistory.usecase.GetExplorerTransactionUrlUseCase +import com.tangem.domain.txhistory.usecase.GetFixedTxHistoryItemsUseCase import com.tangem.domain.txhistory.usecase.GetTxHistoryItemsCountUseCase import com.tangem.domain.txhistory.usecase.GetTxHistoryItemsUseCase import dagger.Module @@ -34,4 +35,10 @@ internal object TxHistoryDomainModule { ): GetExplorerTransactionUrlUseCase { return GetExplorerTransactionUrlUseCase(repository = txHistoryRepository) } + + @Provides + @ViewModelScoped + fun providesGetFixedTxHistoryItemsUseCase(txHistoryRepository: TxHistoryRepository): GetFixedTxHistoryItemsUseCase { + return GetFixedTxHistoryItemsUseCase(repository = txHistoryRepository) + } } \ No newline at end of file diff --git a/data/txhistory/src/main/kotlin/com/tangem/data/txhistory/repository/DefaultTxHistoryRepository.kt b/data/txhistory/src/main/kotlin/com/tangem/data/txhistory/repository/DefaultTxHistoryRepository.kt index 9120a85ef1..7b6663b564 100644 --- a/data/txhistory/src/main/kotlin/com/tangem/data/txhistory/repository/DefaultTxHistoryRepository.kt +++ b/data/txhistory/src/main/kotlin/com/tangem/data/txhistory/repository/DefaultTxHistoryRepository.kt @@ -10,11 +10,13 @@ import com.tangem.datasource.local.txhistory.TxHistoryItemsStore import com.tangem.datasource.local.userwallet.UserWalletsStore import com.tangem.domain.tokens.model.CryptoCurrency import com.tangem.domain.tokens.model.Network +import com.tangem.domain.txhistory.models.Page import com.tangem.domain.txhistory.models.TxHistoryItem import com.tangem.domain.txhistory.models.TxHistoryState import com.tangem.domain.txhistory.models.TxHistoryStateError import com.tangem.domain.txhistory.repository.TxHistoryRepository import com.tangem.domain.walletmanager.WalletManagersFacade +import com.tangem.domain.walletmanager.utils.SdkPageConverter import com.tangem.domain.wallets.models.UserWallet import com.tangem.domain.wallets.models.UserWalletId import kotlinx.coroutines.flow.Flow @@ -25,6 +27,7 @@ class DefaultTxHistoryRepository( private val userWalletsStore: UserWalletsStore, private val txHistoryItemsStore: TxHistoryItemsStore, ) : TxHistoryRepository { + private val sdkPageConverter by lazy { SdkPageConverter() } override suspend fun getTxHistoryItemsCount(userWalletId: UserWalletId, currency: CryptoCurrency): Int { val userWallet = getUserWallet(userWalletId) @@ -74,6 +77,43 @@ class DefaultTxHistoryRepository( } } + override suspend fun getFixedSizeTxHistoryItems( + userWalletId: UserWalletId, + currency: CryptoCurrency, + pageSize: Int, + refresh: Boolean, + ): List { + cacheRegistry.invokeOnExpire( + key = getTxHistoryPageKey(currency, userWalletId, Page.Initial), + skipCache = refresh, + block = { fetchFixedSizeTxHistoryItems(userWalletId, currency, pageSize) }, + ) + val txs = txHistoryItemsStore.getSyncOrNull( + key = TxHistoryItemsStore.Key(userWalletId, currency), + page = Page.Initial, + )?.items + return txs ?: emptyList() + } + + private fun getTxHistoryPageKey(currency: CryptoCurrency, userWalletId: UserWalletId, page: Page): String { + return "tx_history_page_${currency}_${userWalletId}_$page" + } + + private suspend fun fetchFixedSizeTxHistoryItems( + userWalletId: UserWalletId, + currency: CryptoCurrency, + pageSize: Int, + ) { + val wrappedItems = walletManagersFacade.getTxHistoryItems( + userWalletId = userWalletId, + currency = currency, + page = sdkPageConverter.convertBack(Page.Initial), + pageSize = pageSize, + ) + + txHistoryItemsStore.store(TxHistoryItemsStore.Key(userWalletId, currency), wrappedItems) + } + private suspend fun getUserWallet(userWalletId: UserWalletId): UserWallet { return requireNotNull(userWalletsStore.getSyncOrNull(userWalletId)) { "Unable to find user wallet with provided ID: $userWalletId" diff --git a/domain/txhistory/src/main/kotlin/com/tangem/domain/txhistory/repository/TxHistoryRepository.kt b/domain/txhistory/src/main/kotlin/com/tangem/domain/txhistory/repository/TxHistoryRepository.kt index 381d469f98..b010a54db4 100644 --- a/domain/txhistory/src/main/kotlin/com/tangem/domain/txhistory/repository/TxHistoryRepository.kt +++ b/domain/txhistory/src/main/kotlin/com/tangem/domain/txhistory/repository/TxHistoryRepository.kt @@ -23,4 +23,12 @@ interface TxHistoryRepository { ): Flow> fun getTxExploreUrl(txHash: String, networkId: Network.ID): String + + @Throws(TxHistoryListError::class) + suspend fun getFixedSizeTxHistoryItems( + userWalletId: UserWalletId, + currency: CryptoCurrency, + pageSize: Int, + refresh: Boolean, + ): List } \ No newline at end of file diff --git a/domain/txhistory/src/main/kotlin/com/tangem/domain/txhistory/usecase/GetFixedTxHistoryItemsUseCase.kt b/domain/txhistory/src/main/kotlin/com/tangem/domain/txhistory/usecase/GetFixedTxHistoryItemsUseCase.kt new file mode 100644 index 0000000000..aa93bec246 --- /dev/null +++ b/domain/txhistory/src/main/kotlin/com/tangem/domain/txhistory/usecase/GetFixedTxHistoryItemsUseCase.kt @@ -0,0 +1,36 @@ +package com.tangem.domain.txhistory.usecase + +import arrow.core.Either +import com.tangem.domain.tokens.model.CryptoCurrency +import com.tangem.domain.txhistory.models.TxHistoryItem +import com.tangem.domain.txhistory.models.TxHistoryListError +import com.tangem.domain.txhistory.repository.TxHistoryRepository +import com.tangem.domain.wallets.models.UserWalletId +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.flow + +/** + * Returns first page with size [DEFAULT_PAGE_SIZE] of tx history. + * Page size is preserved to reuse cached data in Token Details Screen. + * + * IMPORTANT!!! + * If page size bigger than [DEFAULT_PAGE_SIZE] is needed consider to implement another use case + * without use of cached data or increase [DEFAULT_PAGE_SIZE] + */ +class GetFixedTxHistoryItemsUseCase( + private val repository: TxHistoryRepository, +) { + + operator fun invoke( + userWalletId: UserWalletId, + currency: CryptoCurrency, + pageSize: Int = DEFAULT_PAGE_SIZE, + refresh: Boolean = false, + ): Either>> { + return Either.catch { + flow { + emit(repository.getFixedSizeTxHistoryItems(userWalletId, currency, pageSize, refresh)) + } + }.mapLeft { TxHistoryListError.DataError(it) } + } +} \ No newline at end of file diff --git a/domain/txhistory/src/main/kotlin/com/tangem/domain/txhistory/usecase/GetTxHistoryItemsUseCase.kt b/domain/txhistory/src/main/kotlin/com/tangem/domain/txhistory/usecase/GetTxHistoryItemsUseCase.kt index e5570e8077..cb6724fc60 100644 --- a/domain/txhistory/src/main/kotlin/com/tangem/domain/txhistory/usecase/GetTxHistoryItemsUseCase.kt +++ b/domain/txhistory/src/main/kotlin/com/tangem/domain/txhistory/usecase/GetTxHistoryItemsUseCase.kt @@ -11,7 +11,7 @@ import com.tangem.domain.wallets.models.UserWalletId import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.catch -private const val DEFAULT_PAGE_SIZE = 50 +const val DEFAULT_PAGE_SIZE = 50 // TODO: Add tests class GetTxHistoryItemsUseCase(private val repository: TxHistoryRepository) {