From 9ce639e08054fb8f57ed76822a6cec9f0d9a4f72 Mon Sep 17 00:00:00 2001 From: Tangem Date: Tue, 8 Aug 2023 23:50:09 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../repository/DefaultCurrenciesRepository.kt | 6 +-- .../repository/DefaultNetworksRepository.kt | 4 +- .../tokens/utils/CardCurrenciesFactory.kt | 4 +- .../tokens/utils/ResponseCurrenciesFactory.kt | 4 +- .../tokens/utils/UserTokensResponseFactory.kt | 2 +- .../tokens/ApplyTokenListSortingUseCase.kt | 19 +++++----- .../domain/tokens/GetTokenListUseCase.kt | 4 +- .../domain/tokens/model/NetworkGroup.kt | 4 +- .../tangem/domain/tokens/model/TokenList.kt | 8 ++-- .../CurrenciesStatusesOperations.kt | 18 ++++----- .../TokenListFiatBalanceOperations.kt | 4 +- .../tokens/operations/TokenListOperations.kt | 24 ++++++------ .../operations/TokenListSortingOperations.kt | 37 +++++++++---------- .../tokens/repository/CurrenciesRepository.kt | 10 ++--- .../ApplyTokenListSortingUseCaseTest.kt | 17 ++++----- .../domain/tokens/GetTokenListUseCaseTest.kt | 4 +- .../domain/tokens/mock/MockNetworksGroups.kt | 20 +++++----- .../domain/tokens/mock/MockTokenLists.kt | 18 ++++----- .../tangem/domain/tokens/mock/MockTokens.kt | 2 +- .../domain/tokens/mock/MockTokensStates.kt | 6 +-- .../repository/MockCurrenciesRepository.kt | 8 ++-- 21 files changed, 110 insertions(+), 113 deletions(-) diff --git a/data/tokens/src/main/kotlin/com/tangem/data/tokens/repository/DefaultCurrenciesRepository.kt b/data/tokens/src/main/kotlin/com/tangem/data/tokens/repository/DefaultCurrenciesRepository.kt index 21c8020e2d..2c1c49b48b 100644 --- a/data/tokens/src/main/kotlin/com/tangem/data/tokens/repository/DefaultCurrenciesRepository.kt +++ b/data/tokens/src/main/kotlin/com/tangem/data/tokens/repository/DefaultCurrenciesRepository.kt @@ -37,7 +37,7 @@ internal class DefaultCurrenciesRepository( override suspend fun saveTokens( userWalletId: UserWalletId, - currencies: Set, + currencies: List, isGroupedByNetwork: Boolean, isSortedByBalance: Boolean, ) = withContext(dispatchers.io) { @@ -64,7 +64,7 @@ internal class DefaultCurrenciesRepository( override fun getMultiCurrencyWalletCurrencies( userWalletId: UserWalletId, refresh: Boolean, - ): Flow> = channelFlow { + ): Flow> = channelFlow { val userWallet = getUserWallet(userWalletId) ensureIsCorrectUserWallet(userWallet, isMultiCurrencyWalletExpected = true) @@ -115,7 +115,7 @@ internal class DefaultCurrenciesRepository( } } - private fun getMultiCurrencyWalletCurrencies(userWallet: UserWallet): Flow> { + private fun getMultiCurrencyWalletCurrencies(userWallet: UserWallet): Flow> { return userTokensStore.get(userWallet.walletId).map { storedTokens -> responseCurrenciesFactory.createCurrencies( response = storedTokens, diff --git a/data/tokens/src/main/kotlin/com/tangem/data/tokens/repository/DefaultNetworksRepository.kt b/data/tokens/src/main/kotlin/com/tangem/data/tokens/repository/DefaultNetworksRepository.kt index 398586bf6c..dc5a8c2223 100644 --- a/data/tokens/src/main/kotlin/com/tangem/data/tokens/repository/DefaultNetworksRepository.kt +++ b/data/tokens/src/main/kotlin/com/tangem/data/tokens/repository/DefaultNetworksRepository.kt @@ -90,7 +90,7 @@ internal class DefaultNetworksRepository( val result = walletManagersFacade.update( userWalletId = userWalletId, networkId = networkId, - extraTokens = currencies.filterIsInstanceTo(hashSetOf()), + extraTokens = currencies.filterIsInstance().toSet(), ) val networkStatus = networkStatusFactory.createNetworkStatus( networkId = networkId, @@ -103,7 +103,7 @@ internal class DefaultNetworksRepository( } } - private suspend fun getCurrencies(userWalletId: UserWalletId): Set { + private suspend fun getCurrencies(userWalletId: UserWalletId): List { val userWallet = requireNotNull(userWalletsStore.getSyncOrNull(userWalletId)) { "Unable to find user wallet with provided ID: $userWalletId" } diff --git a/data/tokens/src/main/kotlin/com/tangem/data/tokens/utils/CardCurrenciesFactory.kt b/data/tokens/src/main/kotlin/com/tangem/data/tokens/utils/CardCurrenciesFactory.kt index 15fd96b0aa..52724b7ac1 100644 --- a/data/tokens/src/main/kotlin/com/tangem/data/tokens/utils/CardCurrenciesFactory.kt +++ b/data/tokens/src/main/kotlin/com/tangem/data/tokens/utils/CardCurrenciesFactory.kt @@ -12,7 +12,7 @@ import com.tangem.blockchain.common.Token as SdkToken internal class CardCurrenciesFactory(private val demoConfig: DemoConfig) { - fun createDefaultCoinsForMultiCurrencyCard(card: CardDTO): Set { + fun createDefaultCoinsForMultiCurrencyCard(card: CardDTO): List { var blockchains = if (demoConfig.isDemoCardId(card.cardId)) { demoConfig.demoBlockchains } else { @@ -23,7 +23,7 @@ internal class CardCurrenciesFactory(private val demoConfig: DemoConfig) { blockchains = blockchains.mapNotNull { it.getTestnetVersion() } } - return blockchains.mapNotNull { createCoin(it, card) }.toSet() + return blockchains.mapNotNull { createCoin(it, card) } } fun createPrimaryCurrencyForSingleCurrencyCard(scanResponse: ScanResponse): CryptoCurrency { diff --git a/data/tokens/src/main/kotlin/com/tangem/data/tokens/utils/ResponseCurrenciesFactory.kt b/data/tokens/src/main/kotlin/com/tangem/data/tokens/utils/ResponseCurrenciesFactory.kt index cbecf6a792..ab27676061 100644 --- a/data/tokens/src/main/kotlin/com/tangem/data/tokens/utils/ResponseCurrenciesFactory.kt +++ b/data/tokens/src/main/kotlin/com/tangem/data/tokens/utils/ResponseCurrenciesFactory.kt @@ -24,8 +24,8 @@ internal class ResponseCurrenciesFactory(private val demoConfig: DemoConfig) { } } - fun createCurrencies(response: UserTokensResponse, card: CardDTO): Set { - return response.tokens.mapNotNull { createCurrency(it, card) }.toSet() + fun createCurrencies(response: UserTokensResponse, card: CardDTO): List { + return response.tokens.mapNotNull { createCurrency(it, card) } } private fun createCurrency(responseToken: UserTokensResponse.Token, card: CardDTO): CryptoCurrency? { diff --git a/data/tokens/src/main/kotlin/com/tangem/data/tokens/utils/UserTokensResponseFactory.kt b/data/tokens/src/main/kotlin/com/tangem/data/tokens/utils/UserTokensResponseFactory.kt index 437100ce08..aac91b05ba 100644 --- a/data/tokens/src/main/kotlin/com/tangem/data/tokens/utils/UserTokensResponseFactory.kt +++ b/data/tokens/src/main/kotlin/com/tangem/data/tokens/utils/UserTokensResponseFactory.kt @@ -7,7 +7,7 @@ import com.tangem.domain.tokens.model.CryptoCurrency internal class UserTokensResponseFactory { fun createUserTokensResponse( - currencies: Set, + currencies: List, isGroupedByNetwork: Boolean, isSortedByBalance: Boolean, ): UserTokensResponse { diff --git a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/ApplyTokenListSortingUseCase.kt b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/ApplyTokenListSortingUseCase.kt index 907fae6af5..187b2e5be0 100644 --- a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/ApplyTokenListSortingUseCase.kt +++ b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/ApplyTokenListSortingUseCase.kt @@ -5,6 +5,7 @@ import arrow.core.raise.Raise import arrow.core.raise.catch import arrow.core.raise.either import arrow.core.raise.ensureNotNull +import arrow.core.toNonEmptyListOrNull import arrow.core.toNonEmptySetOrNull import com.tangem.domain.tokens.error.TokenListSortingError import com.tangem.domain.tokens.model.CryptoCurrency @@ -22,7 +23,7 @@ class ApplyTokenListSortingUseCase( suspend operator fun invoke( userWalletId: UserWalletId, - sortedTokensIds: Set>, + sortedTokensIds: List>, isGroupedByNetwork: Boolean, isSortedByBalance: Boolean, ): Either { @@ -39,16 +40,16 @@ class ApplyTokenListSortingUseCase( } private suspend fun Raise.sortTokens( - sortedTokensIds: Set>, - unsortedTokens: Set, - ): Set = withContext(dispatchers.default) { + sortedTokensIds: List>, + unsortedTokens: List, + ): List = withContext(dispatchers.default) { val nonEmptySortedTokensIds = ensureNotNull(sortedTokensIds.toNonEmptySetOrNull()) { TokenListSortingError.TokenListIsEmpty } val sortedTokens = sortedMapOf() - unsortedTokens.forEach { token -> + unsortedTokens.distinct().forEach { token -> val index = nonEmptySortedTokensIds.indexOfFirst { (networkId, tokenId) -> networkId == token.networkId && tokenId == token.id } @@ -60,12 +61,12 @@ class ApplyTokenListSortingUseCase( } } - ensureNotNull(sortedTokens.values.toNonEmptySetOrNull()) { + ensureNotNull(sortedTokens.values.toNonEmptyListOrNull()) { TokenListSortingError.TokenListIsEmpty } } - private suspend fun Raise.getCurrencies(userWalletId: UserWalletId): Set { + private suspend fun Raise.getCurrencies(userWalletId: UserWalletId): List { val tokens = catch( block = { currenciesRepository.getMultiCurrencyWalletCurrencies(userWalletId, refresh = false).firstOrNull() @@ -73,14 +74,14 @@ class ApplyTokenListSortingUseCase( catch = { raise(TokenListSortingError.DataError(it)) }, ) - return ensureNotNull(tokens?.toNonEmptySetOrNull()) { + return ensureNotNull(tokens?.toNonEmptyListOrNull()) { TokenListSortingError.TokenListIsEmpty } } private suspend fun Raise.applySorting( userWalletId: UserWalletId, - tokens: Set, + tokens: List, isGrouped: Boolean, isSortedByBalance: Boolean, ) = withContext(dispatchers.io) { diff --git a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/GetTokenListUseCase.kt b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/GetTokenListUseCase.kt index 1b046264dd..f27dadfec9 100644 --- a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/GetTokenListUseCase.kt +++ b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/GetTokenListUseCase.kt @@ -43,7 +43,7 @@ class GetTokenListUseCase( private fun getTokensStatuses( userWalletId: UserWalletId, refresh: Boolean, - ): Flow>> { + ): Flow>> { val operations = CurrenciesStatusesOperations( userWalletId = userWalletId, refresh = refresh, @@ -58,7 +58,7 @@ class GetTokenListUseCase( private fun createTokenList( userWalletId: UserWalletId, - tokens: Set, + tokens: List, ): Flow> { val operations = TokenListOperations( userWalletId = userWalletId, diff --git a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/model/NetworkGroup.kt b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/model/NetworkGroup.kt index 89264ac332..d2463c0f16 100644 --- a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/model/NetworkGroup.kt +++ b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/model/NetworkGroup.kt @@ -8,9 +8,9 @@ import com.tangem.domain.tokens.models.Network * This class encapsulates a collection of cryptocurrency statuses, all of which are part of the same blockchain network. * * @property network The blockchain network associated with the group. - * @property currencies A set of cryptocurrency statuses that belong to the network. + * @property currencies A list of cryptocurrency statuses that belong to the network. */ data class NetworkGroup( val network: Network, - val currencies: Set, + val currencies: List, ) \ No newline at end of file diff --git a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/model/TokenList.kt b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/model/TokenList.kt index 55327b82da..c447c3ac8d 100644 --- a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/model/TokenList.kt +++ b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/model/TokenList.kt @@ -18,12 +18,12 @@ sealed class TokenList { /** * Represents tokens that are grouped by their network. * - * @property groups A set of network groups containing tokens. + * @property groups A list of network groups containing tokens. * @property totalFiatBalance The total fiat balance across all groups. * @property sortedBy The criteria used for sorting the tokens within the groups. */ data class GroupedByNetwork( - val groups: Set, + val groups: List, override val totalFiatBalance: FiatBalance, override val sortedBy: SortType, ) : TokenList() @@ -31,12 +31,12 @@ sealed class TokenList { /** * Represents tokens that are not grouped by any specific criteria. * - * @property currencies A set of cryptocurrency statuses. + * @property currencies A list of cryptocurrency statuses. * @property totalFiatBalance The total fiat balance across all currencies. * @property sortedBy The criteria used for sorting the currencies. */ data class Ungrouped( - val currencies: Set, + val currencies: List, override val totalFiatBalance: FiatBalance, override val sortedBy: SortType, ) : TokenList() diff --git a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/CurrenciesStatusesOperations.kt b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/CurrenciesStatusesOperations.kt index 6e1032e5c1..1fbd547bbc 100644 --- a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/CurrenciesStatusesOperations.kt +++ b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/CurrenciesStatusesOperations.kt @@ -33,14 +33,14 @@ internal class CurrenciesStatusesOperations( ) @OptIn(ExperimentalCoroutinesApi::class) - fun getCurrenciesStatusesFlow(): Flow>> { + fun getCurrenciesStatusesFlow(): Flow>> { return getMultiCurrencyWalletCurrencies().flatMapMerge flatMap@{ maybeCurrencies -> val nonEmptyCurrencies = maybeCurrencies.fold( ifLeft = { error -> return@flatMap flowOf(error.left()) }, - ifRight = { it.toNonEmptySetOrNull() }, - ) ?: return@flatMap flowOf(emptySet().right()) + ifRight = { it.toNonEmptyListOrNull() }, + ) ?: return@flatMap flowOf(emptyList().right()) val (networksIds, currenciesIds) = getIds(nonEmptyCurrencies) @@ -96,11 +96,11 @@ internal class CurrenciesStatusesOperations( } private fun createCurrenciesStatuses( - currencies: NonEmptySet, + currencies: NonEmptyList, quotes: Set, networkStatuses: Set, - ): Set { - return currencies.mapTo(hashSetOf()) { token -> + ): List { + return currencies.map { token -> val quote = quotes.firstOrNull { it.currencyId == token.id } val networkStatus = networkStatuses.firstOrNull { it.networkId == token.networkId } @@ -122,9 +122,9 @@ internal class CurrenciesStatusesOperations( return currencyStatusOperations.createTokenStatus() } - private fun getMultiCurrencyWalletCurrencies(): Flow>> { + private fun getMultiCurrencyWalletCurrencies(): Flow>> { return currenciesRepository.getMultiCurrencyWalletCurrencies(userWalletId, refresh) - .map, Either>> { it.right() } + .map, Either>> { it.right() } .catch { emit(Error.DataError(it).left()) } .onEmpty { emit(Error.EmptyCurrencies.left()) } } @@ -157,7 +157,7 @@ internal class CurrenciesStatusesOperations( } private fun getIds( - currencies: NonEmptySet, + currencies: NonEmptyList, ): Pair, NonEmptySet> { val currencyIdToNetworkId = currencies.associate { currency -> currency.id to currency.networkId diff --git a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/TokenListFiatBalanceOperations.kt b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/TokenListFiatBalanceOperations.kt index 2373f7f6ef..830950ae1e 100644 --- a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/TokenListFiatBalanceOperations.kt +++ b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/TokenListFiatBalanceOperations.kt @@ -1,12 +1,12 @@ package com.tangem.domain.tokens.operations -import arrow.core.NonEmptySet +import arrow.core.NonEmptyList import com.tangem.domain.tokens.model.CryptoCurrencyStatus import com.tangem.domain.tokens.model.TokenList import java.math.BigDecimal internal class TokenListFiatBalanceOperations( - private val currencies: NonEmptySet, + private val currencies: NonEmptyList, private val isAnyTokenLoading: Boolean, ) { diff --git a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/TokenListOperations.kt b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/TokenListOperations.kt index ada01f316b..627df695af 100644 --- a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/TokenListOperations.kt +++ b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/TokenListOperations.kt @@ -16,12 +16,12 @@ internal class TokenListOperations( private val currenciesRepository: CurrenciesRepository, private val networksRepository: NetworksRepository, private val userWalletId: UserWalletId, - private val tokens: Set, + private val tokens: List, ) { constructor( userWalletId: UserWalletId, - tokens: Set, + tokens: List, useCase: GetTokenListUseCase, ) : this( currenciesRepository = useCase.currenciesRepository, @@ -42,14 +42,14 @@ internal class TokenListOperations( } private fun Raise.createTokenList(isGrouped: Boolean, isSortedByBalance: Boolean): TokenList { - val tokensNes = tokens.toNonEmptySetOrNull() + val nonEmptyCurrencies = tokens.toNonEmptyListOrNull() ?: return TokenList.NotInitialized - val isAnyTokenLoading = tokensNes.any { it.value is CryptoCurrencyStatus.Loading } - val fiatBalanceOperations = TokenListFiatBalanceOperations(tokensNes, isAnyTokenLoading) + val isAnyTokenLoading = nonEmptyCurrencies.any { it.value is CryptoCurrencyStatus.Loading } + val fiatBalanceOperations = TokenListFiatBalanceOperations(nonEmptyCurrencies, isAnyTokenLoading) return createTokenList( - tokens = tokensNes, + currencies = nonEmptyCurrencies, fiatBalance = fiatBalanceOperations.calculateFiatBalance(), isAnyTokenLoading = isAnyTokenLoading, isGrouped = isGrouped, @@ -58,23 +58,23 @@ internal class TokenListOperations( } private fun Raise.createTokenList( - tokens: NonEmptySet, + currencies: NonEmptyList, fiatBalance: TokenList.FiatBalance, isAnyTokenLoading: Boolean, isGrouped: Boolean, isSortedByBalance: Boolean, ): TokenList { val sortingOperations = TokenListSortingOperations( - currencies = tokens, + currencies = currencies, isAnyTokenLoading = isAnyTokenLoading, sortByBalance = isSortedByBalance, ) - return createTokenList(tokens, sortingOperations, fiatBalance, isGrouped) + return createTokenList(currencies, sortingOperations, fiatBalance, isGrouped) } private fun Raise.createTokenList( - tokens: NonEmptySet, + tokens: NonEmptyList, sortingOperations: TokenListSortingOperations, fiatBalance: TokenList.FiatBalance, isGrouped: Boolean, @@ -92,7 +92,7 @@ internal class TokenListOperations( } } - private fun Raise.getNetworks(tokensNes: NonEmptySet): Set { + private fun Raise.getNetworks(tokensNes: NonEmptyList): Set { val networksIds = tokensNes.map { it.currency.networkId }.toNonEmptySet() return catch( @@ -131,7 +131,7 @@ internal class TokenListOperations( ) private fun createUnsortedUngroupedTokenList( - tokens: Set, + tokens: List, fiatBalance: TokenList.FiatBalance, ): TokenList.Ungrouped { return TokenList.Ungrouped( diff --git a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/TokenListSortingOperations.kt b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/TokenListSortingOperations.kt index 5bf58c6cf7..4e609a140f 100644 --- a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/TokenListSortingOperations.kt +++ b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/operations/TokenListSortingOperations.kt @@ -1,12 +1,10 @@ package com.tangem.domain.tokens.operations -import arrow.core.Either -import arrow.core.NonEmptySet +import arrow.core.* import arrow.core.raise.Raise import arrow.core.raise.either import arrow.core.raise.ensure import arrow.core.raise.ensureNotNull -import arrow.core.toNonEmptySetOrNull import com.tangem.domain.tokens.model.CryptoCurrencyStatus import com.tangem.domain.tokens.model.NetworkGroup import com.tangem.domain.tokens.model.TokenList @@ -14,7 +12,7 @@ import com.tangem.domain.tokens.models.Network import java.math.BigDecimal internal class TokenListSortingOperations( - private val currencies: Set, + private val currencies: List, private val isAnyTokenLoading: Boolean, private val sortByBalance: Boolean, ) { @@ -25,15 +23,15 @@ internal class TokenListSortingOperations( isAnyTokenLoading: Boolean = tokenList.totalFiatBalance is TokenList.FiatBalance.Loading, ) : this( currencies = when (tokenList) { - is TokenList.GroupedByNetwork -> tokenList.groups.flatMap { it.currencies }.toSet() + is TokenList.GroupedByNetwork -> tokenList.groups.flatMap { it.currencies } is TokenList.Ungrouped -> tokenList.currencies - is TokenList.NotInitialized -> emptySet() + is TokenList.NotInitialized -> emptyList() }, isAnyTokenLoading = isAnyTokenLoading, sortByBalance = sortByBalance, ) - fun getGroupedTokens(networks: Set): Either> = either { + fun getGroupedTokens(networks: Set): Either> = either { ensure(currencies.isNotEmpty()) { Error.EmptyTokens } val networksNes = ensureNotNull(networks.toNonEmptySetOrNull()) { Error.EmptyNetworks @@ -46,17 +44,17 @@ internal class TokenListSortingOperations( } } - fun getTokens(): Either> = either { - val tokensNes = ensureNotNull(currencies.toNonEmptySetOrNull()) { + fun getTokens(): Either> = either { + val nonEmptyCurrencies = ensureNotNull(currencies.toNonEmptyListOrNull()) { Error.EmptyTokens } - if (sortByBalance) sortTokensByBalance(tokensNes) else tokensNes + if (sortByBalance) sortTokensByBalance(nonEmptyCurrencies) else nonEmptyCurrencies } fun getSortType(): TokenList.SortType = if (sortByBalance) TokenList.SortType.BALANCE else TokenList.SortType.NONE - private fun Raise.groupTokens(networks: NonEmptySet): NonEmptySet { + private fun Raise.groupTokens(networks: NonEmptySet): NonEmptyList { val groupedTokens = currencies .groupBy { it.currency.networkId } .map { (networkId, tokens) -> @@ -66,22 +64,21 @@ internal class TokenListSortingOperations( NetworkGroup( network = network, - currencies = ensureNotNull(tokens.toNonEmptySetOrNull()) { Error.EmptyTokens }, + currencies = ensureNotNull(tokens.toNonEmptyListOrNull()) { Error.EmptyTokens }, ) } - .toNonEmptySetOrNull() + .toNonEmptyListOrNull() return ensureNotNull(groupedTokens) { Error.EmptyTokens } } - private fun Raise.groupAndSortTokensByBalance(networks: NonEmptySet): NonEmptySet { + private fun Raise.groupAndSortTokensByBalance(networks: NonEmptySet): NonEmptyList { val groupsWithSortedTokens = groupTokens(networks) .map { group -> - val tokens = group.currencies as? NonEmptySet + val tokens = group.currencies as? NonEmptyList ?: error("Tokens can not be empty here") group.copy(currencies = sortTokensByBalance(tokens)) } - .toNonEmptySet() return if (isAnyTokenLoading) { groupsWithSortedTokens @@ -90,22 +87,22 @@ internal class TokenListSortingOperations( } } - private fun sortTokensByBalance(tokens: NonEmptySet): NonEmptySet { + private fun sortTokensByBalance(tokens: NonEmptyList): NonEmptyList { return if (isAnyTokenLoading) { tokens } else { tokens.sortedByDescending { it.value.fiatAmount ?: BigDecimal.ZERO } - .toNonEmptySetOrNull() + .toNonEmptyListOrNull() ?: error("Tokens can not be empty here") } } - private fun sortGroupsByBalance(groupsWithSortedTokens: NonEmptySet): NonEmptySet { + private fun sortGroupsByBalance(groupsWithSortedTokens: NonEmptyList): NonEmptyList { return groupsWithSortedTokens .sortedByDescending { group -> group.currencies.sumOf { it.value.fiatAmount ?: BigDecimal.ZERO } } - .toNonEmptySetOrNull() + .toNonEmptyListOrNull() ?: error("Tokens can not be empty here") } diff --git a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/repository/CurrenciesRepository.kt b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/repository/CurrenciesRepository.kt index 8b5e0fe9e5..85db960be0 100644 --- a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/repository/CurrenciesRepository.kt +++ b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/repository/CurrenciesRepository.kt @@ -10,11 +10,11 @@ import kotlinx.coroutines.flow.Flow interface CurrenciesRepository { /** - * Saves the given set of cryptocurrencies, along with the preferences for grouping and sorting, for a specific + * Saves the given list of cryptocurrencies, along with the preferences for grouping and sorting, for a specific * multi-currency user wallet. * * @param userWalletId The unique identifier of the user wallet. - * @param currencies The set of cryptocurrencies to be saved. + * @param currencies The list of cryptocurrencies to be saved. * @param isGroupedByNetwork A boolean flag indicating whether the tokens should be grouped by network. * @param isSortedByBalance A boolean flag indicating whether the tokens should be sorted by balance. * @throws com.tangem.domain.core.error.DataError.UserWalletError.WrongUserWallet If single-currency user wallet @@ -22,7 +22,7 @@ interface CurrenciesRepository { */ suspend fun saveTokens( userWalletId: UserWalletId, - currencies: Set, + currencies: List, isGroupedByNetwork: Boolean, isSortedByBalance: Boolean, ) @@ -38,7 +38,7 @@ interface CurrenciesRepository { suspend fun getSingleCurrencyWalletPrimaryCurrency(userWalletId: UserWalletId): CryptoCurrency /** - * Retrieves the set of cryptocurrencies within a multi-currency wallet. + * Retrieves the list of cryptocurrencies within a multi-currency wallet. * * @param userWalletId The unique identifier of the user wallet. * @param refresh A boolean flag indicating whether the data should be refreshed. @@ -46,7 +46,7 @@ interface CurrenciesRepository { * @throws com.tangem.domain.core.error.DataError.UserWalletError.WrongUserWallet If single-currency user wallet * ID provided. */ - fun getMultiCurrencyWalletCurrencies(userWalletId: UserWalletId, refresh: Boolean): Flow> + fun getMultiCurrencyWalletCurrencies(userWalletId: UserWalletId, refresh: Boolean): Flow> /** * Retrieves the cryptocurrency for a specific multi-currency user wallet. diff --git a/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/ApplyTokenListSortingUseCaseTest.kt b/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/ApplyTokenListSortingUseCaseTest.kt index 16ee99fff2..b56a18415e 100644 --- a/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/ApplyTokenListSortingUseCaseTest.kt +++ b/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/ApplyTokenListSortingUseCaseTest.kt @@ -32,7 +32,7 @@ internal class ApplyTokenListSortingUseCaseTest { // When val result = useCase( userWalletId = userWalletId, - sortedTokensIds = emptySet(), + sortedTokensIds = emptyList(), isGroupedByNetwork = false, isSortedByBalance = false, ) @@ -54,7 +54,7 @@ internal class ApplyTokenListSortingUseCaseTest { // When val result = useCase( userWalletId = userWalletId, - sortedTokensIds = MockTokens.tokens.map { it.networkId to it.id }.toSet(), + sortedTokensIds = MockTokens.tokens.map { it.networkId to it.id }, isGroupedByNetwork = false, isSortedByBalance = false, ) @@ -76,7 +76,7 @@ internal class ApplyTokenListSortingUseCaseTest { // When useCase( userWalletId = userWalletId, - sortedTokensIds = expectedTokens.map { it.networkId to it.id }.toSet(), + sortedTokensIds = expectedTokens.map { it.networkId to it.id }, isGroupedByNetwork = expectedIsGrouped, isSortedByBalance = expectedIsSorted, ) @@ -100,7 +100,7 @@ internal class ApplyTokenListSortingUseCaseTest { // When useCase( userWalletId = userWalletId, - sortedTokensIds = expectedTokens.map { it.networkId to it.id }.toSet(), + sortedTokensIds = expectedTokens.map { it.networkId to it.id }, isGroupedByNetwork = expectedIsGrouped, isSortedByBalance = expectedIsSorted, ) @@ -124,7 +124,7 @@ internal class ApplyTokenListSortingUseCaseTest { // When useCase( userWalletId = userWalletId, - sortedTokensIds = expectedTokens.map { it.networkId to it.id }.toSet(), + sortedTokensIds = expectedTokens.map { it.networkId to it.id }, isGroupedByNetwork = expectedIsGrouped, isSortedByBalance = expectedIsSorted, ) @@ -148,7 +148,7 @@ internal class ApplyTokenListSortingUseCaseTest { // When useCase( userWalletId = userWalletId, - sortedTokensIds = expectedTokens.map { it.networkId to it.id }.toSet(), + sortedTokensIds = expectedTokens.map { it.networkId to it.id }, isGroupedByNetwork = expectedIsGrouped, isSortedByBalance = expectedIsSorted, ) @@ -170,7 +170,7 @@ internal class ApplyTokenListSortingUseCaseTest { // When val result = useCase( userWalletId = userWalletId, - sortedTokensIds = getSortedTokens().drop(n = 3).map { it.networkId to it.id }.toSet(), + sortedTokensIds = getSortedTokens().drop(n = 3).map { it.networkId to it.id }, isGroupedByNetwork = false, isSortedByBalance = false, ) @@ -181,7 +181,6 @@ internal class ApplyTokenListSortingUseCaseTest { private fun getSortedTokens() = MockTokens.tokens .sortedBy { Random.nextInt(0, MockTokens.tokens.size) } - .toSet() private fun getUseCase(tokensRepository: MockCurrenciesRepository = getTokensRepository()) = ApplyTokenListSortingUseCase( @@ -191,7 +190,7 @@ internal class ApplyTokenListSortingUseCaseTest { private fun getTokensRepository( sortTokensResult: Either = Unit.right(), - tokens: Flow>> = flowOf(MockTokens.tokens.right()), + tokens: Flow>> = flowOf(MockTokens.tokens.right()), ): MockCurrenciesRepository { return MockCurrenciesRepository(sortTokensResult, MockTokens.token1.right(), tokens, emptyFlow(), emptyFlow()) } diff --git a/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/GetTokenListUseCaseTest.kt b/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/GetTokenListUseCaseTest.kt index e7dfb90fd8..4336ee54a3 100644 --- a/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/GetTokenListUseCaseTest.kt +++ b/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/GetTokenListUseCaseTest.kt @@ -225,7 +225,7 @@ internal class GetTokenListUseCaseTest { fun `when tokens is empty then not initialized token list should be received`() = runTest { val expectedResult = MockTokenLists.notInitializedTokenList.right() - val useCase = getUseCase(tokens = flowOf(emptySet().right())) + val useCase = getUseCase(tokens = flowOf(emptyList().right())) // When val result = useCase(userWalletId).first() @@ -319,7 +319,7 @@ internal class GetTokenListUseCaseTest { } private fun getUseCase( - tokens: Flow>> = flowOf(MockTokens.tokens.right()), + tokens: Flow>> = flowOf(MockTokens.tokens.right()), quotes: Flow>> = flowOf(MockQuotes.quotes.right()), networks: Either> = MockNetworks.networks.right(), statuses: Flow>> = flowOf(MockNetworks.errorNetworksStatuses.right()), diff --git a/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/mock/MockNetworksGroups.kt b/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/mock/MockNetworksGroups.kt index ebda181faf..ab3078de1d 100644 --- a/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/mock/MockNetworksGroups.kt +++ b/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/mock/MockNetworksGroups.kt @@ -1,7 +1,7 @@ package com.tangem.domain.tokens.mock -import arrow.core.nonEmptySetOf -import arrow.core.toNonEmptySetOrNull +import arrow.core.nonEmptyListOf +import arrow.core.toNonEmptyListOrNull import com.tangem.domain.tokens.model.NetworkGroup @Suppress("MemberVisibilityCanBePrivate") @@ -11,42 +11,42 @@ internal object MockNetworksGroups { network = MockNetworks.network1, currencies = MockTokensStates.failedTokenStates .filter { it.currency.networkId == MockNetworks.network1.id } - .toNonEmptySetOrNull()!!, + .toNonEmptyListOrNull()!!, ) val networkGroup2 = NetworkGroup( network = MockNetworks.network2, currencies = MockTokensStates.failedTokenStates .filter { it.currency.networkId == MockNetworks.network2.id } - .toNonEmptySetOrNull()!!, + .toNonEmptyListOrNull()!!, ) val networkGroup3 = NetworkGroup( network = MockNetworks.network3, currencies = MockTokensStates.failedTokenStates .filter { it.currency.networkId == MockNetworks.network3.id } - .toNonEmptySetOrNull()!!, + .toNonEmptyListOrNull()!!, ) - val failedNetworksGroups = nonEmptySetOf(networkGroup1, networkGroup2, networkGroup3) + val failedNetworksGroups = nonEmptyListOf(networkGroup1, networkGroup2, networkGroup3) val loadedNetworksGroups = failedNetworksGroups.map { group -> group.copy( currencies = MockTokensStates.loadedTokensStates .filter { it.currency.networkId == group.network.id } - .toNonEmptySetOrNull()!!, + .toNonEmptyListOrNull()!!, ) - }.toNonEmptySet() + } val sortedNetworksGroups = loadedNetworksGroups.map { group -> group.copy( currencies = group.currencies .sortedByDescending { it.value.fiatAmount } - .toNonEmptySetOrNull()!!, + .toNonEmptyListOrNull()!!, ) } .sortedByDescending { group -> group.currencies.sumOf { it.value.fiatAmount!! } } - .toNonEmptySetOrNull()!! + .toNonEmptyListOrNull()!! } \ No newline at end of file diff --git a/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/mock/MockTokenLists.kt b/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/mock/MockTokenLists.kt index 403dcb5046..de1f837afd 100644 --- a/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/mock/MockTokenLists.kt +++ b/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/mock/MockTokenLists.kt @@ -1,7 +1,7 @@ package com.tangem.domain.tokens.mock -import arrow.core.NonEmptySet -import arrow.core.toNonEmptySetOrNull +import arrow.core.NonEmptyList +import arrow.core.toNonEmptyListOrNull import com.tangem.domain.tokens.mock.MockNetworksGroups.failedNetworksGroups import com.tangem.domain.tokens.mock.MockNetworksGroups.loadedNetworksGroups import com.tangem.domain.tokens.mock.MockNetworksGroups.sortedNetworksGroups @@ -18,13 +18,13 @@ internal object MockTokenLists { val notInitializedTokenList = TokenList.NotInitialized val emptyGroupedTokenList = TokenList.GroupedByNetwork( - groups = emptySet(), + groups = emptyList(), totalFiatBalance = TokenList.FiatBalance.Failed, sortedBy = TokenList.SortType.NONE, ) val emptyUngroupedTokenList = TokenList.Ungrouped( - currencies = emptySet(), + currencies = emptyList(), totalFiatBalance = TokenList.FiatBalance.Failed, sortedBy = TokenList.SortType.NONE, ) @@ -43,7 +43,7 @@ internal object MockTokenLists { val loadingUngroupedTokenList = with(failedUngroupedTokenList) { copy( - currencies = currencies.map { it.copy(value = CryptoCurrencyStatus.Loading) }.toNonEmptySetOrNull()!!, + currencies = currencies.map { it.copy(value = CryptoCurrencyStatus.Loading) }.toNonEmptyListOrNull()!!, totalFiatBalance = TokenList.FiatBalance.Loading, ) } @@ -55,9 +55,9 @@ internal object MockTokenLists { group.copy( currencies = group.currencies .map { it.copy(value = CryptoCurrencyStatus.Loading) } - .toNonEmptySetOrNull()!!, + .toNonEmptyListOrNull()!!, ) - }.toNonEmptySetOrNull()!!, + }.toNonEmptyListOrNull()!!, ) } @@ -84,7 +84,7 @@ internal object MockTokenLists { sortedBy = TokenList.SortType.NONE, totalFiatBalance = TokenList.FiatBalance.Loaded( amount = groups - .flatMap { it.currencies as NonEmptySet } + .flatMap { it.currencies as NonEmptyList } .sumOf { it.value.fiatAmount ?: BigDecimal.ZERO }, isAllAmountsSummarized = true, ), @@ -95,7 +95,7 @@ internal object MockTokenLists { get() { val tokens = MockTokensStates.loadedTokensStates .sortedByDescending { it.value.fiatAmount } - .toNonEmptySetOrNull()!! + .toNonEmptyListOrNull()!! return unsortedUngroupedTokenList.copy( currencies = tokens, diff --git a/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/mock/MockTokens.kt b/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/mock/MockTokens.kt index b0af037c5e..7ff7ba350a 100644 --- a/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/mock/MockTokens.kt +++ b/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/mock/MockTokens.kt @@ -119,5 +119,5 @@ internal object MockTokens { derivationPath = null, ) - val tokens = setOf(token1, token2, token3, token4, token5, token6, token7, token8, token9, token10) + val tokens = listOf(token1, token2, token3, token4, token5, token6, token7, token8, token9, token10) } \ No newline at end of file diff --git a/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/mock/MockTokensStates.kt b/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/mock/MockTokensStates.kt index 4df0a5d525..93acd79dc2 100644 --- a/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/mock/MockTokensStates.kt +++ b/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/mock/MockTokensStates.kt @@ -1,6 +1,6 @@ package com.tangem.domain.tokens.mock -import arrow.core.nonEmptySetOf +import arrow.core.nonEmptyListOf import com.tangem.domain.tokens.model.CryptoCurrencyStatus import com.tangem.domain.tokens.model.NetworkStatus @@ -57,7 +57,7 @@ internal object MockTokensStates { value = CryptoCurrencyStatus.NoAccount, ) - val failedTokenStates = nonEmptySetOf( + val failedTokenStates = nonEmptyListOf( tokenState1, tokenState2, tokenState3, @@ -86,5 +86,5 @@ internal object MockTokensStates { hasTransactionsInProgress = false, ), ) - }.toNonEmptySet() + } } \ No newline at end of file diff --git a/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/repository/MockCurrenciesRepository.kt b/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/repository/MockCurrenciesRepository.kt index 8ebf978969..9570b02157 100644 --- a/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/repository/MockCurrenciesRepository.kt +++ b/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/repository/MockCurrenciesRepository.kt @@ -11,12 +11,12 @@ import kotlinx.coroutines.flow.map internal class MockCurrenciesRepository( private val sortTokensResult: Either, private val token: Either, - private val tokens: Flow>>, + private val tokens: Flow>>, private val isGrouped: Flow>, private val isSortedByBalance: Flow>, ) : CurrenciesRepository { - var tokensIdsAfterSortingApply: Set? = null + var tokensIdsAfterSortingApply: List? = null private set var isTokensGroupedAfterSortingApply: Boolean? = null @@ -27,7 +27,7 @@ internal class MockCurrenciesRepository( override suspend fun saveTokens( userWalletId: UserWalletId, - currencies: Set, + currencies: List, isGroupedByNetwork: Boolean, isSortedByBalance: Boolean, ) { @@ -45,7 +45,7 @@ internal class MockCurrenciesRepository( override fun getMultiCurrencyWalletCurrencies( userWalletId: UserWalletId, refresh: Boolean, - ): Flow> { + ): Flow> { return tokens.map { it.getOrElse { e -> throw e } } }