Updated on 2026-08-14

This commit is contained in:
Tangem 2023-08-09 13:09:47 +05:00
parent 3f5f3ef68b
commit d1a09370c7
39 changed files with 317 additions and 301 deletions

View file

@ -10,7 +10,13 @@ android {
}
dependencies {
implementation(projects.core.utils)
implementation(projects.core.datasource)
implementation(projects.domain.legacy)
implementation(projects.domain.tokens.models)
implementation(projects.domain.txhistory)
implementation(projects.domain.txhistory.models)
implementation(projects.domain.wallets.models)
implementation(deps.kotlin.coroutines)
implementation(deps.androidx.paging.runtime)

View file

@ -1,7 +1,9 @@
package com.tangem.data.txhistory.di
import com.tangem.data.txhistory.repository.MockTxHistoryRepository
import com.tangem.data.txhistory.repository.DefaultTxHistoryRepository
import com.tangem.datasource.local.userwallet.UserWalletsStore
import com.tangem.domain.txhistory.repository.TxHistoryRepository
import com.tangem.domain.walletmanager.WalletManagersFacade
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
@ -14,5 +16,11 @@ internal object TxHistoryDataModule {
@Provides
@Singleton
fun provideTxHistoryRepository(): TxHistoryRepository = MockTxHistoryRepository()
fun provideTxHistoryRepository(
walletManagersFacade: WalletManagersFacade,
userWalletsStore: UserWalletsStore,
): TxHistoryRepository = DefaultTxHistoryRepository(
walletManagersFacade = walletManagersFacade,
userWalletsStore = userWalletsStore,
)
}

View file

@ -1,70 +0,0 @@
package com.tangem.data.txhistory.mock
import com.tangem.domain.txhistory.model.TxHistoryItem
import java.math.BigDecimal
internal object MockTxHistoryItems {
private val txHistoryItem1 = TxHistoryItem(
txHash = "noster",
timestamp = System.currentTimeMillis(),
direction = TxHistoryItem.TransactionDirection.Incoming("address"),
status = TxHistoryItem.TxStatus.Confirmed,
type = TxHistoryItem.TransactionType.Transfer,
amount = BigDecimal("1000000000.5"),
)
private val txHistoryItem2 = TxHistoryItem(
txHash = "noster",
timestamp = 1689844346000,
direction = TxHistoryItem.TransactionDirection.Incoming("address2"),
status = TxHistoryItem.TxStatus.Unconfirmed,
type = TxHistoryItem.TransactionType.Transfer,
amount = BigDecimal("1000000000.5"),
)
private val txHistoryItem3 = TxHistoryItem(
txHash = "noster",
timestamp = 1689757946000,
direction = TxHistoryItem.TransactionDirection.Outgoing("address3"),
status = TxHistoryItem.TxStatus.Confirmed,
type = TxHistoryItem.TransactionType.Transfer,
amount = BigDecimal("1000000000.5"),
)
private val txHistoryItem4 = TxHistoryItem(
txHash = "noster",
timestamp = 1689671546000,
direction = TxHistoryItem.TransactionDirection.Incoming("address4"),
status = TxHistoryItem.TxStatus.Confirmed,
type = TxHistoryItem.TransactionType.Transfer,
amount = BigDecimal("1000000000.5"),
)
private val txHistoryItem5 = TxHistoryItem(
txHash = "noster",
timestamp = 1689585146000,
direction = TxHistoryItem.TransactionDirection.Outgoing("address5"),
status = TxHistoryItem.TxStatus.Unconfirmed,
type = TxHistoryItem.TransactionType.Transfer,
amount = BigDecimal("1000000000.5"),
)
private val txHistoryItem6 = TxHistoryItem(
txHash = "noster",
timestamp = 1689585146000,
direction = TxHistoryItem.TransactionDirection.Incoming("address6"),
status = TxHistoryItem.TxStatus.Confirmed,
type = TxHistoryItem.TransactionType.Transfer,
amount = BigDecimal("1000000000.5"),
)
val txHistoryItems = listOf(
txHistoryItem1,
txHistoryItem2,
txHistoryItem3,
txHistoryItem4,
txHistoryItem5,
txHistoryItem6,
)
}

View file

@ -0,0 +1,66 @@
package com.tangem.data.txhistory.repository
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.tangem.data.txhistory.repository.paging.TxHistoryPagingSource
import com.tangem.datasource.local.userwallet.UserWalletsStore
import com.tangem.domain.tokens.models.Network
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.wallets.models.UserWallet
import kotlinx.coroutines.flow.Flow
class DefaultTxHistoryRepository(
private val walletManagersFacade: WalletManagersFacade,
private val userWalletsStore: UserWalletsStore,
) : TxHistoryRepository {
override suspend fun getTxHistoryItemsCount(networkId: Network.ID, derivationPath: String?): Int {
val userWallet = getUserWallet()
val state = walletManagersFacade.getTxHistoryState(
userWalletId = userWallet.walletId,
networkId = networkId,
rawDerivationPath = derivationPath,
)
return when (state) {
is TxHistoryState.Failed.FetchError -> throw TxHistoryStateError.DataError(state.exception)
TxHistoryState.NotImplemented -> throw TxHistoryStateError.TxHistoryNotImplemented
TxHistoryState.Success.Empty -> throw TxHistoryStateError.EmptyTxHistories
is TxHistoryState.Success.HasTransactions -> state.txCount
}
}
override fun getTxHistoryItems(
networkId: Network.ID,
derivationPath: String?,
pageSize: Int,
): Flow<PagingData<TxHistoryItem>> {
val userWallet = getUserWallet()
return Pager(
config = PagingConfig(
pageSize = pageSize,
),
pagingSourceFactory = {
TxHistoryPagingSource(
loadPage = { page: Int, pageSize: Int ->
walletManagersFacade.getTxHistoryItems(
userWalletId = userWallet.walletId,
networkId = networkId,
rawDerivationPath = derivationPath,
page = page,
pageSize = pageSize,
)
},
)
},
).flow
}
private fun getUserWallet(): UserWallet = requireNotNull(userWalletsStore.selectedUserWalletOrNull) {
"Selected wallet must not be null"
}
}

View file

@ -1,25 +0,0 @@
package com.tangem.data.txhistory.repository
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.tangem.data.txhistory.repository.paging.TxHistoryPagingSource
import com.tangem.domain.txhistory.model.TxHistoryItem
import com.tangem.domain.txhistory.repository.TxHistoryRepository
import kotlinx.coroutines.flow.Flow
internal class MockTxHistoryRepository : TxHistoryRepository {
override suspend fun getTxHistoryItemsCount(networkId: String, derivationPath: String): Int {
return 0
}
override fun getTxHistoryItems(networkId: String, pageSize: Int): Flow<PagingData<TxHistoryItem>> {
return Pager(
config = PagingConfig(
pageSize = pageSize,
),
pagingSourceFactory = { TxHistoryPagingSource() },
).flow
}
}

View file

@ -2,12 +2,14 @@ package com.tangem.data.txhistory.repository.paging
import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.tangem.data.txhistory.mock.MockTxHistoryItems
import com.tangem.domain.txhistory.model.TxHistoryItem
import com.tangem.domain.txhistory.models.PaginationWrapper
import com.tangem.domain.txhistory.models.TxHistoryItem
private const val INITIAL_PAGE = 1
internal class TxHistoryPagingSource : PagingSource<Int, TxHistoryItem>() {
internal class TxHistoryPagingSource(
private val loadPage: suspend (page: Int, pageSize: Int) -> PaginationWrapper<TxHistoryItem>,
) : PagingSource<Int, TxHistoryItem>() {
override fun getRefreshKey(state: PagingState<Int, TxHistoryItem>): Int? {
return state.anchorPosition?.let { anchorPosition ->
@ -19,20 +21,12 @@ internal class TxHistoryPagingSource : PagingSource<Int, TxHistoryItem>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, TxHistoryItem> {
val currentPage = params.key ?: INITIAL_PAGE
return try {
// TODO: [REDACTED_JIRA]
// val result = txHistoryManager.getTxHistoryItems(
// networkId = networkId,
// derivationPath = derivationPath,
// page = currentPage,
// pageSize = params.loadSize,
// )
val result = MockTxHistoryItems.txHistoryItems
val result = loadPage(currentPage, params.loadSize)
LoadResult.Page(
data = result,
data = result.items,
prevKey = if (currentPage > INITIAL_PAGE) currentPage.minus(1) else null,
// TODO: handle end of reached [REDACTED_JIRA]
nextKey = null,
nextKey = if (result.page < result.totalPages) currentPage.plus(1) else null,
)
} catch (e: Exception) {
LoadResult.Error(e)