Updated on 2026-08-14
This commit is contained in:
parent
bfdac6a9a8
commit
47fe92dc94
7 changed files with 61 additions and 12 deletions
|
|
@ -40,4 +40,13 @@ internal object TokensDomainModule {
|
|||
fun provideToggleTokenListSortingUseCase(dispatchers: CoroutineDispatcherProvider): ToggleTokenListSortingUseCase {
|
||||
return ToggleTokenListSortingUseCase(dispatchers)
|
||||
}
|
||||
|
||||
@Provides
|
||||
@ViewModelScoped
|
||||
fun provideApplyTokenListSortingUseCase(
|
||||
tokensRepository: TokensRepository,
|
||||
dispatchers: CoroutineDispatcherProvider,
|
||||
): ApplyTokenListSortingUseCase {
|
||||
return ApplyTokenListSortingUseCase(tokensRepository, dispatchers)
|
||||
}
|
||||
}
|
||||
|
|
@ -11,7 +11,7 @@ internal class MockTokensRepository : TokensRepository {
|
|||
|
||||
override suspend fun sortTokens(
|
||||
userWalletId: UserWalletId,
|
||||
sortedTokensIds: Set<Token.ID>,
|
||||
sortedTokens: Set<Token>,
|
||||
isGrouped: Boolean,
|
||||
isSortedByBalance: Boolean,
|
||||
) = Unit
|
||||
|
|
|
|||
|
|
@ -0,0 +1,49 @@
|
|||
package com.tangem.domain.tokens
|
||||
|
||||
import arrow.core.Either
|
||||
import arrow.core.raise.Raise
|
||||
import arrow.core.raise.catch
|
||||
import arrow.core.raise.either
|
||||
import arrow.core.raise.ensureNotNull
|
||||
import arrow.core.toNonEmptySetOrNull
|
||||
import com.tangem.domain.tokens.error.TokenListSortingError
|
||||
import com.tangem.domain.tokens.model.Token
|
||||
import com.tangem.domain.tokens.repository.TokensRepository
|
||||
import com.tangem.domain.wallets.models.UserWalletId
|
||||
import com.tangem.utils.coroutines.CoroutineDispatcherProvider
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
class ApplyTokenListSortingUseCase(
|
||||
private val tokensRepository: TokensRepository,
|
||||
private val dispatchers: CoroutineDispatcherProvider,
|
||||
) {
|
||||
|
||||
suspend operator fun invoke(
|
||||
userWalletId: UserWalletId,
|
||||
sortedTokens: Set<Token>,
|
||||
isGrouped: Boolean,
|
||||
isSortedByBalance: Boolean,
|
||||
): Either<TokenListSortingError, Unit> {
|
||||
return withContext(dispatchers.default) {
|
||||
either {
|
||||
val nonEmptyTokens = ensureNotNull(sortedTokens.toNonEmptySetOrNull()) {
|
||||
TokenListSortingError.TokenListIsEmpty
|
||||
}
|
||||
|
||||
applySorting(userWalletId, nonEmptyTokens, isGrouped, isSortedByBalance)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun Raise<TokenListSortingError>.applySorting(
|
||||
userWalletId: UserWalletId,
|
||||
tokens: Set<Token>,
|
||||
isGrouped: Boolean,
|
||||
isSortedByBalance: Boolean,
|
||||
) = withContext(dispatchers.io) {
|
||||
catch(
|
||||
block = { tokensRepository.sortTokens(userWalletId, tokens, isGrouped, isSortedByBalance) },
|
||||
catch = { raise(TokenListSortingError.DataError(it)) },
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -6,9 +6,6 @@ import com.tangem.domain.tokens.model.Token
|
|||
import com.tangem.domain.wallets.models.UserWalletId
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
// FIXME: Use Raise as context instead of Effect when context receivers become stable
|
||||
// [REDACTED_JIRA]
|
||||
|
||||
/**
|
||||
* Repository for everything related to the blockchain networks
|
||||
* */
|
||||
|
|
|
|||
|
|
@ -4,9 +4,6 @@ import com.tangem.domain.tokens.model.Quote
|
|||
import com.tangem.domain.tokens.model.Token
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
// FIXME: Use Raise as context instead of Effect when context receivers become stable
|
||||
// [REDACTED_JIRA]
|
||||
|
||||
/**
|
||||
* Repository for everything related to the quotes of tokens
|
||||
* */
|
||||
|
|
|
|||
|
|
@ -4,9 +4,6 @@ import com.tangem.domain.tokens.model.Token
|
|||
import com.tangem.domain.wallets.models.UserWalletId
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
// FIXME: Use Raise as context instead of Effect when context receivers become stable
|
||||
// [REDACTED_JIRA]
|
||||
|
||||
/**
|
||||
* Repository for everything related to the tokens of user wallet
|
||||
* */
|
||||
|
|
@ -14,7 +11,7 @@ interface TokensRepository {
|
|||
|
||||
suspend fun sortTokens(
|
||||
userWalletId: UserWalletId,
|
||||
sortedTokensIds: Set<Token.ID>,
|
||||
sortedTokens: Set<Token>,
|
||||
isGrouped: Boolean,
|
||||
isSortedByBalance: Boolean,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ internal class MockTokensRepository(
|
|||
|
||||
override suspend fun sortTokens(
|
||||
userWalletId: UserWalletId,
|
||||
sortedTokensIds: Set<Token.ID>,
|
||||
sortedTokens: Set<Token>,
|
||||
isGrouped: Boolean,
|
||||
isSortedByBalance: Boolean,
|
||||
) = Unit
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue