Updated on 2026-08-14
This commit is contained in:
parent
03dce384bd
commit
c4603c4cbd
13 changed files with 171 additions and 39 deletions
|
|
@ -2,16 +2,15 @@ package com.tangem.domain.wallets.usecase
|
|||
|
||||
import arrow.core.Either
|
||||
import com.tangem.domain.notifications.models.ApplicationId
|
||||
import com.tangem.domain.wallets.legacy.UserWalletsListManager
|
||||
import com.tangem.domain.wallets.models.UserWallet
|
||||
import com.tangem.domain.wallets.repository.WalletsRepository
|
||||
|
||||
class AssociateWalletsWithApplicationIdUseCase(
|
||||
private val userWalletsListManager: UserWalletsListManager,
|
||||
private val walletsRepository: WalletsRepository,
|
||||
) {
|
||||
|
||||
suspend operator fun invoke(applicationId: ApplicationId): Either<Throwable, Unit> = Either.catch {
|
||||
val wallets = userWalletsListManager.userWalletsSync
|
||||
walletsRepository.associateWallets(applicationId.value, wallets)
|
||||
}
|
||||
suspend operator fun invoke(applicationId: ApplicationId, wallets: List<UserWallet>): Either<Throwable, Unit> =
|
||||
Either.catch {
|
||||
walletsRepository.associateWallets(applicationId.value, wallets)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
package com.tangem.domain.wallets.usecase
|
||||
|
||||
import com.tangem.domain.wallets.legacy.UserWalletsListManager
|
||||
import com.tangem.domain.wallets.legacy.asLockable
|
||||
import com.tangem.domain.wallets.legacy.isLockedSync
|
||||
import com.tangem.domain.wallets.models.UserWallet
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.filter
|
||||
|
||||
class GetSavedWalletChangesUseCase(
|
||||
private val userWalletsListManager: UserWalletsListManager,
|
||||
) {
|
||||
|
||||
operator fun invoke(): Flow<List<UserWallet>> {
|
||||
return userWalletsListManager.userWallets
|
||||
.filter {
|
||||
userWalletsListManager.asLockable() ?: return@filter false
|
||||
return@filter if (userWalletsListManager.isLockedSync.not()) {
|
||||
it.isNotEmpty()
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
.distinctUntilChanged()
|
||||
}
|
||||
}
|
||||
|
|
@ -1,10 +1,8 @@
|
|||
package com.tangem.domain.wallets.usecase
|
||||
|
||||
import arrow.core.Either
|
||||
import arrow.core.raise.either
|
||||
import com.tangem.domain.notifications.models.ApplicationId
|
||||
import com.tangem.domain.wallets.delegate.UserWalletsSyncDelegate
|
||||
import com.tangem.domain.wallets.models.UpdateWalletError
|
||||
import com.tangem.domain.wallets.repository.WalletsRepository
|
||||
|
||||
class UpdateRemoteWalletsInfoUseCase(
|
||||
|
|
@ -12,8 +10,8 @@ class UpdateRemoteWalletsInfoUseCase(
|
|||
private val userWalletsSyncDelegate: UserWalletsSyncDelegate,
|
||||
) {
|
||||
|
||||
suspend operator fun invoke(applicationId: ApplicationId): Either<UpdateWalletError, Unit> = either {
|
||||
suspend operator fun invoke(applicationId: ApplicationId): Either<Throwable, Unit> = Either.catch {
|
||||
val walletsInfo = walletsRepository.getWalletsInfo(applicationId.value)
|
||||
userWalletsSyncDelegate.syncWallets(walletsInfo).bind()
|
||||
userWalletsSyncDelegate.syncWallets(walletsInfo)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
package com.tangem.domain.wallets.usecase
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import com.tangem.domain.wallets.legacy.UserWalletsListManager
|
||||
import com.tangem.domain.wallets.legacy.asLockable
|
||||
import com.tangem.domain.wallets.legacy.isLockedSync
|
||||
import com.tangem.domain.wallets.models.UserWallet
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.mockkStatic
|
||||
import kotlinx.coroutines.flow.firstOrNull
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
|
||||
class GetSavedWalletChangesUseCaseTest {
|
||||
|
||||
private lateinit var useCase: GetSavedWalletChangesUseCase
|
||||
private lateinit var userWalletsListManager: UserWalletsListManager
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
userWalletsListManager = mockk()
|
||||
useCase = GetSavedWalletChangesUseCase(userWalletsListManager)
|
||||
mockkStatic("com.tangem.domain.wallets.legacy.UserWalletsListManagerExtensionsKt")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN manager is not lockable WHEN invoke THEN return empty list`() = runTest {
|
||||
// GIVEN
|
||||
every { userWalletsListManager.isLockable } returns false
|
||||
every { userWalletsListManager.userWallets } returns flowOf(emptyList())
|
||||
every { userWalletsListManager.isLockedSync } returns false
|
||||
every { userWalletsListManager.asLockable() } returns null
|
||||
|
||||
// WHEN
|
||||
val result = useCase().firstOrNull()
|
||||
|
||||
// THEN
|
||||
assertThat(result).isNull()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN manager is locked WHEN invoke THEN return empty list`() = runTest {
|
||||
// GIVEN
|
||||
val mockLockable = mockk<UserWalletsListManager.Lockable>()
|
||||
every { userWalletsListManager.isLockable } returns true
|
||||
every { userWalletsListManager.userWallets } returns flowOf(emptyList())
|
||||
every { userWalletsListManager.isLockedSync } returns true
|
||||
every { userWalletsListManager.asLockable() } returns mockLockable
|
||||
|
||||
// WHEN
|
||||
val result = useCase().firstOrNull()
|
||||
|
||||
// THEN
|
||||
assertThat(result).isNull()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN manager is not locked and has wallets WHEN invoke THEN return wallets list`() = runTest {
|
||||
// GIVEN
|
||||
val mockLockable = mockk<UserWalletsListManager.Lockable>()
|
||||
val wallets = listOf(mockk<UserWallet>(), mockk<UserWallet>())
|
||||
every { userWalletsListManager.isLockable } returns true
|
||||
every { userWalletsListManager.userWallets } returns flowOf(wallets)
|
||||
every { userWalletsListManager.isLockedSync } returns false
|
||||
every { userWalletsListManager.asLockable() } returns mockLockable
|
||||
|
||||
// WHEN
|
||||
val result = useCase().firstOrNull()
|
||||
|
||||
// THEN
|
||||
assertThat(result).isEqualTo(wallets)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue