Updated on 2026-08-14
This commit is contained in:
parent
f3906a385a
commit
0fae3c188c
5 changed files with 92 additions and 1 deletions
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
@ -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<TxHistoryItem> {
|
||||
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"
|
||||
|
|
|
|||
|
|
@ -23,4 +23,12 @@ interface TxHistoryRepository {
|
|||
): Flow<PagingData<TxHistoryItem>>
|
||||
|
||||
fun getTxExploreUrl(txHash: String, networkId: Network.ID): String
|
||||
|
||||
@Throws(TxHistoryListError::class)
|
||||
suspend fun getFixedSizeTxHistoryItems(
|
||||
userWalletId: UserWalletId,
|
||||
currency: CryptoCurrency,
|
||||
pageSize: Int,
|
||||
refresh: Boolean,
|
||||
): List<TxHistoryItem>
|
||||
}
|
||||
|
|
@ -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<TxHistoryListError, Flow<List<TxHistoryItem>>> {
|
||||
return Either.catch {
|
||||
flow {
|
||||
emit(repository.getFixedSizeTxHistoryItems(userWalletId, currency, pageSize, refresh))
|
||||
}
|
||||
}.mapLeft { TxHistoryListError.DataError(it) }
|
||||
}
|
||||
}
|
||||
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue