From d1c77b00cf6ce99ee0a06b4b3f7b383f6803d39a Mon Sep 17 00:00:00 2001 From: Tangem Date: Tue, 26 Sep 2023 18:26:31 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../tokens/ToggleTokenListGroupingUseCase.kt | 14 +++++--- .../tokens/ToggleTokenListSortingUseCase.kt | 14 +++++--- .../tangem/domain/tokens/model/TokenList.kt | 10 ++++-- .../tokens/operations/TokenListOperations.kt | 2 +- .../operations/TokenListSortingOperations.kt | 2 +- .../domain/tokens/GetTokenListUseCaseTest.kt | 2 +- .../tokens/ToggleTokenListGroupingTest.kt | 30 +---------------- .../ToggleTokenListSortingUseCaseTest.kt | 32 ++----------------- .../domain/tokens/mock/MockTokenLists.kt | 2 +- .../utils/CryptoCurrenciesIdsResolver.kt | 2 +- .../utils/common/TokenListOperations.kt | 2 +- .../items/TokenListToListStateConverter.kt | 2 +- .../utils/TokenListToContentItemsConverter.kt | 2 +- .../wallet/viewmodels/WalletViewModel.kt | 13 ++++---- 14 files changed, 44 insertions(+), 85 deletions(-) diff --git a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/ToggleTokenListGroupingUseCase.kt b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/ToggleTokenListGroupingUseCase.kt index 6328868257..20f1ae77ef 100644 --- a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/ToggleTokenListGroupingUseCase.kt +++ b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/ToggleTokenListGroupingUseCase.kt @@ -19,20 +19,20 @@ class ToggleTokenListGroupingUseCase( suspend operator fun invoke(tokenList: TokenList): Either { return withContext(dispatchers.default) { either { - ensure(tokenList.totalFiatBalance !is TokenList.FiatBalance.Loading) { - TokenListSortingError.TokenListIsLoading - } - when (tokenList) { is TokenList.GroupedByNetwork -> ungroupTokens(tokenList) is TokenList.Ungrouped -> groupTokens(tokenList) - is TokenList.NotInitialized -> raise(TokenListSortingError.TokenListIsEmpty) + is TokenList.Empty -> raise(TokenListSortingError.TokenListIsEmpty) } } } } private fun Raise.groupTokens(tokenList: TokenList.Ungrouped): TokenList.GroupedByNetwork { + ensure(tokenList.totalFiatBalance !is TokenList.FiatBalance.Loading) { + TokenListSortingError.TokenListIsLoading + } + val sortingOperations = TokenListSortingOperations(tokenList) return TokenList.GroupedByNetwork( @@ -47,6 +47,10 @@ class ToggleTokenListGroupingUseCase( private fun Raise.ungroupTokens( tokenList: TokenList.GroupedByNetwork, ): TokenList.Ungrouped { + ensure(tokenList.totalFiatBalance !is TokenList.FiatBalance.Loading) { + TokenListSortingError.TokenListIsLoading + } + val sortingOperations = TokenListSortingOperations(tokenList) return TokenList.Ungrouped( diff --git a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/ToggleTokenListSortingUseCase.kt b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/ToggleTokenListSortingUseCase.kt index d1ae3d9b74..482940452a 100644 --- a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/ToggleTokenListSortingUseCase.kt +++ b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/ToggleTokenListSortingUseCase.kt @@ -19,14 +19,10 @@ class ToggleTokenListSortingUseCase( suspend operator fun invoke(tokenList: TokenList): Either { return withContext(dispatchers.default) { either { - ensure(tokenList.totalFiatBalance !is TokenList.FiatBalance.Loading) { - TokenListSortingError.TokenListIsLoading - } - when (tokenList) { is TokenList.GroupedByNetwork -> sortGroupedTokenList(tokenList) is TokenList.Ungrouped -> sortUngroupedTokenList(tokenList) - is TokenList.NotInitialized -> raise(TokenListSortingError.TokenListIsEmpty) + is TokenList.Empty -> raise(TokenListSortingError.TokenListIsEmpty) } } } @@ -35,6 +31,10 @@ class ToggleTokenListSortingUseCase( private fun Raise.sortGroupedTokenList( tokenList: TokenList.GroupedByNetwork, ): TokenList.GroupedByNetwork { + ensure(tokenList.totalFiatBalance !is TokenList.FiatBalance.Loading) { + TokenListSortingError.TokenListIsLoading + } + val operations = getSortingOperations(tokenList) return tokenList.copy( @@ -48,6 +48,10 @@ class ToggleTokenListSortingUseCase( private fun Raise.sortUngroupedTokenList( tokenList: TokenList.Ungrouped, ): TokenList.Ungrouped { + ensure(tokenList.totalFiatBalance !is TokenList.FiatBalance.Loading) { + TokenListSortingError.TokenListIsLoading + } + val operations = getSortingOperations(tokenList) return tokenList.copy( 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 c447c3ac8d..089bbdefde 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 @@ -41,8 +41,14 @@ sealed class TokenList { override val sortedBy: SortType, ) : TokenList() - /** Represents a state where the token list is not initialized. */ - object NotInitialized : TokenList() + /** Represents a state where the token list is empty. */ + object Empty : TokenList() { + + override val totalFiatBalance: FiatBalance = FiatBalance.Loaded( + amount = BigDecimal.ZERO, + isAllAmountsSummarized = true, + ) + } /** Defines the possible sorting criteria for the tokens. */ enum class SortType { 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 984cd6ddfc..1a03353ee5 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 @@ -41,7 +41,7 @@ internal class TokenListOperations( private fun Raise.createTokenList(isGrouped: Boolean, isSortedByBalance: Boolean): TokenList { val nonEmptyCurrencies = tokens.toNonEmptyListOrNull() - ?: return TokenList.NotInitialized + ?: return TokenList.Empty val isAnyTokenLoading = nonEmptyCurrencies.any { it.value is CryptoCurrencyStatus.Loading } val fiatBalanceOperations = TokenListFiatBalanceOperations(nonEmptyCurrencies, isAnyTokenLoading) 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 47bc1054bb..2833be87eb 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 @@ -27,7 +27,7 @@ internal class TokenListSortingOperations( currencies = when (tokenList) { is TokenList.GroupedByNetwork -> tokenList.groups.flatMap { it.currencies } is TokenList.Ungrouped -> tokenList.currencies - is TokenList.NotInitialized -> emptyList() + is TokenList.Empty -> emptyList() }, isAnyTokenLoading = isAnyTokenLoading, sortByBalance = sortByBalance, 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 90d8481285..7736efaf5c 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 @@ -205,7 +205,7 @@ internal class GetTokenListUseCaseTest { @Test fun `when tokens is empty then not initialized token list should be received`() = runTest { - val expectedResult = MockTokenLists.notInitializedTokenList.right() + val expectedResult = MockTokenLists.emptyTokenList.right() val useCase = getUseCase(tokens = flowOf(emptyList().right())) diff --git a/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/ToggleTokenListGroupingTest.kt b/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/ToggleTokenListGroupingTest.kt index 299b44a0d0..c34710cd97 100644 --- a/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/ToggleTokenListGroupingTest.kt +++ b/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/ToggleTokenListGroupingTest.kt @@ -12,21 +12,7 @@ import org.junit.Test internal class ToggleTokenListGroupingTest { @Test - fun `when grouped list is empty then error should be received`() = runTest { - // Given - val expectedResult = TokenListSortingError.TokenListIsEmpty.left() - - val useCase = getUseCase() - - // When - val result = useCase(MockTokenLists.emptyGroupedTokenList) - - // Then - assertEquals(expectedResult, result) - } - - @Test - fun `when ungrouped list is empty then error should be received`() = runTest { + fun `when list is empty then error should be received`() = runTest { // Given val expectedResult = TokenListSortingError.TokenListIsEmpty.left() @@ -67,20 +53,6 @@ internal class ToggleTokenListGroupingTest { assertEquals(expectedResult, result) } - @Test - fun `when list is not initialized then error should be received`() = runTest { - // Given - val expectedResult = TokenListSortingError.TokenListIsLoading.left() - - val useCase = getUseCase() - - // When - val result = useCase(MockTokenLists.notInitializedTokenList) - - // Then - assertEquals(expectedResult, result) - } - @Test fun `when list is ungrouped and sorted then sorted grouped list should be received`() = runTest { // Given diff --git a/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/ToggleTokenListSortingUseCaseTest.kt b/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/ToggleTokenListSortingUseCaseTest.kt index f8070694f7..ebbc935479 100644 --- a/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/ToggleTokenListSortingUseCaseTest.kt +++ b/domain/tokens/src/test/kotlin/com/tangem/domain/tokens/ToggleTokenListSortingUseCaseTest.kt @@ -12,42 +12,14 @@ import org.junit.Test internal class ToggleTokenListSortingUseCaseTest { @Test - fun `when grouped list is not initialized then error should be received`() = runTest { - // Given - val expectedResult = TokenListSortingError.TokenListIsLoading.left() - - val useCase = getUseCase() - - // When - val result = useCase(MockTokenLists.notInitializedTokenList) - - // Then - assertEquals(expectedResult, result) - } - - @Test - fun `when grouped list is empty then error should be received`() = runTest { + fun `when list is empty then error should be received`() = runTest { // Given val expectedResult = TokenListSortingError.TokenListIsEmpty.left() val useCase = getUseCase() // When - val result = useCase(MockTokenLists.emptyGroupedTokenList) - - // Then - assertEquals(expectedResult, result) - } - - @Test - fun `when ungrouped list is empty then error should be received`() = runTest { - // Given - val expectedResult = TokenListSortingError.TokenListIsEmpty.left() - - val useCase = getUseCase() - - // When - val result = useCase(MockTokenLists.emptyUngroupedTokenList) + val result = useCase(MockTokenLists.emptyTokenList) // Then assertEquals(expectedResult, result) 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 cbfd65a56f..fb41b5aa56 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 @@ -15,7 +15,7 @@ internal object MockTokenLists { const val isGrouped = false const val isSortedByBalance = false - val notInitializedTokenList = TokenList.NotInitialized + val emptyTokenList = TokenList.Empty val emptyGroupedTokenList = TokenList.GroupedByNetwork( groups = emptyList(), diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/organizetokens/utils/CryptoCurrenciesIdsResolver.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/organizetokens/utils/CryptoCurrenciesIdsResolver.kt index 4fa74e5171..60af8fee15 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/organizetokens/utils/CryptoCurrenciesIdsResolver.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/organizetokens/utils/CryptoCurrenciesIdsResolver.kt @@ -16,7 +16,7 @@ internal class CryptoCurrenciesIdsResolver { val currenciesStatuses = when (tokenList) { is TokenList.GroupedByNetwork -> tokenList.groups.flatMap { it.currencies } is TokenList.Ungrouped -> tokenList.currencies - is TokenList.NotInitialized, + is TokenList.Empty, null, -> return emptyList() } diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/organizetokens/utils/common/TokenListOperations.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/organizetokens/utils/common/TokenListOperations.kt index 4e4250f5e9..38b17ef15f 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/organizetokens/utils/common/TokenListOperations.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/organizetokens/utils/common/TokenListOperations.kt @@ -7,6 +7,6 @@ internal fun TokenList.disableSortingByBalance(): TokenList { return when (this) { is TokenList.GroupedByNetwork -> this.copy(sortedBy = SortType.NONE) is TokenList.Ungrouped -> this.copy(sortedBy = SortType.NONE) - is TokenList.NotInitialized -> this + is TokenList.Empty -> this } } \ No newline at end of file diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/organizetokens/utils/converter/items/TokenListToListStateConverter.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/organizetokens/utils/converter/items/TokenListToListStateConverter.kt index dd82c1672b..455e896da9 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/organizetokens/utils/converter/items/TokenListToListStateConverter.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/organizetokens/utils/converter/items/TokenListToListStateConverter.kt @@ -17,7 +17,7 @@ internal class TokenListToListStateConverter( return when (value) { is TokenList.GroupedByNetwork -> createListState(value) is TokenList.Ungrouped -> createListState(value) - is TokenList.NotInitialized -> createEmptyListState() + is TokenList.Empty -> createEmptyListState() } } diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/utils/TokenListToContentItemsConverter.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/utils/TokenListToContentItemsConverter.kt index 24d808bd72..3ed9fb061b 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/utils/TokenListToContentItemsConverter.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/utils/TokenListToContentItemsConverter.kt @@ -29,7 +29,7 @@ internal class TokenListToContentItemsConverter( override fun convert(value: TokenList): WalletTokensListState { return when (value) { - is TokenList.NotInitialized -> WalletTokensListState.Loading() + is TokenList.Empty -> WalletTokensListState.Empty is TokenList.GroupedByNetwork -> WalletTokensListState.Content( items = value.mapToMultiCurrencyItems(), organizeTokensButton = value.mapToOrganizeTokensButtonState(), diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModel.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModel.kt index 809cc098e1..a76a16ffe8 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModel.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModel.kt @@ -241,7 +241,7 @@ internal class WalletViewModel @Inject constructor( if (cacheState != null) { uiState = stateFactory.getStateWithoutDeletedWallet(cacheState, action) - if (cacheState.isLoadingState()) { + if (cacheState.isLoadingOrEmptyState()) { uiState = stateFactory.getStateAndTriggerEvent( state = uiState, event = WalletEvent.ChangeWallet(action.selectedWalletIndex), @@ -509,7 +509,7 @@ internal class WalletViewModel @Inject constructor( pullToRefreshConfig = cacheState.pullToRefreshConfig.copy(isRefreshing = false), ) - if (cacheState.isLoadingState()) { + if (cacheState.isLoadingOrEmptyState()) { getContentItemsUpdates(index) } } else { @@ -904,7 +904,7 @@ internal class WalletViewModel @Inject constructor( .hasNonZeroWallets() } is TokenList.Ungrouped -> tokenList.currencies.hasNonZeroWallets() - TokenList.NotInitialized -> false + is TokenList.Empty -> false } if (hasNonZeroWallets) { @@ -984,7 +984,7 @@ internal class WalletViewModel @Inject constructor( .flatMap(NetworkGroup::currencies) } is TokenList.Ungrouped -> tokenList.currencies - is TokenList.NotInitialized -> emptyList() + is TokenList.Empty -> emptyList() } } else { listOfNotNull(singleWalletCryptoCurrencyStatus) @@ -1033,7 +1033,7 @@ internal class WalletViewModel @Inject constructor( ) } - private fun WalletState.isLoadingState(): Boolean { + private fun WalletState.isLoadingOrEmptyState(): Boolean { // Check the base components if (this is WalletState.ContentState && walletsListConfig.wallets[walletsListConfig.selectedWalletIndex] is WalletCardState.Loading @@ -1044,12 +1044,13 @@ internal class WalletViewModel @Inject constructor( // Check the special components return when (this) { is WalletMultiCurrencyState -> { + val isTokensEmpty = tokensListState is WalletTokensListState.Empty val hasLoadingTokens = tokensListState is WalletTokensListState.ContentState && (tokensListState as WalletTokensListState.ContentState).items .filterIsInstance() .any { it.state is TokenItemState.Loading } - tokensListState is WalletTokensListState.Loading || hasLoadingTokens + isTokensEmpty || tokensListState is WalletTokensListState.Loading || hasLoadingTokens } is WalletSingleCurrencyState -> { this is WalletSingleCurrencyState.Content && marketPriceBlockState is MarketPriceBlockState.Loading