Updated on 2026-08-14

This commit is contained in:
Tangem 2026-02-06 11:13:31 +04:00
parent 8ba59403f4
commit 18d4a01642
7 changed files with 159 additions and 0 deletions

View file

@ -398,4 +398,12 @@ internal object WalletsDomainModule {
fun provideSyncWalletWithRemoteUseCase(walletsRepository: WalletsRepository): SyncWalletWithRemoteUseCase {
return SyncWalletWithRemoteUseCase(walletsRepository = walletsRepository)
}
@Provides
@Singleton
fun provideApplyUserWalletListSortingUseCase(
userWalletsListRepository: UserWalletsListRepository,
): ApplyUserWalletListSortingUseCase {
return ApplyUserWalletListSortingUseCase(userWalletsListRepository = userWalletsListRepository)
}
}

View file

@ -13,6 +13,7 @@ import com.tangem.core.analytics.utils.TrackingContextProxy
import com.tangem.datasource.local.preferences.AppPreferencesStore
import com.tangem.datasource.local.preferences.PreferencesKeys
import com.tangem.datasource.local.preferences.utils.getSyncOrDefault
import com.tangem.domain.common.wallets.UserWalletTransformAction
import com.tangem.domain.common.wallets.UserWalletsListRepository
import com.tangem.domain.common.wallets.UserWalletsListRepository.LockMethod
import com.tangem.domain.common.wallets.error.*
@ -31,6 +32,8 @@ import com.tangem.sdk.api.TangemSdkManager
import com.tangem.tap.domain.userWalletList.model.UserWalletEncryptionKey
import com.tangem.tap.domain.userWalletList.utils.encryptionKey
import com.tangem.tap.domain.userWalletList.utils.lock
import com.tangem.tap.domain.userWalletList.utils.publicInformation
import com.tangem.tap.domain.userWalletList.utils.sensitiveInformation
import com.tangem.tap.domain.userWalletList.utils.toUserWallets
import com.tangem.tap.domain.userWalletList.utils.updateWith
import com.tangem.utils.Provider
@ -38,10 +41,12 @@ import com.tangem.utils.ProviderSuspend
import com.tangem.utils.coroutines.runSuspendCatching
import com.tangem.utils.extensions.addOrReplace
import com.tangem.utils.extensions.indexOfFirstOrNull
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
@Suppress("LongParameterList", "LargeClass")
internal class DefaultUserWalletsListRepository(
@ -384,6 +389,44 @@ internal class DefaultUserWalletsListRepository(
return userWallets.any { it.walletId !in unsecuredWalletIds }
}
override suspend fun transform(action: UserWalletTransformAction) {
val wallets = userWalletsSync()
val walletsMap = wallets.associateBy { it.walletId }
val transformedWallets = action.transform(wallets)
val transformedWalletsMap = transformedWallets.associateBy { it.walletId }
require(walletsMap.keys == transformedWalletsMap.keys) {
"The transformation action must not change the set of wallet IDs." +
"Original IDs: ${walletsMap.keys}, Transformed IDs: ${transformedWalletsMap.keys}"
}
updateWallets { transformedWallets }
withContext(NonCancellable) {
if (savePersistentInformation()) {
publicInformationRepository.transform {
transformedWallets.map { wallet -> wallet.publicInformation }
}
val changedSensitiveInfoWallets = wallets.filter { wallet ->
val transformedWallet = transformedWalletsMap[wallet.walletId]
transformedWallet != null && transformedWallet.sensitiveInformation != wallet.sensitiveInformation
}
changedSensitiveInfoWallets.forEach { wallet ->
if (wallet.isLocked.not()) {
sensitiveInformationRepository.save(wallet, wallet.encryptionKey)
}
checkForUpgradeAndDeleteHotWalletIfNeeded(
newUserWallet = wallet,
oldUserWallet = walletsMap[wallet.walletId] ?: error("This should never happen"),
)
}
}
}
}
private suspend fun checkForUpgradeAndDeleteHotWalletIfNeeded(
newUserWallet: UserWallet,
oldUserWallet: UserWallet,

View file

@ -12,4 +12,8 @@ internal interface UserWalletsPublicInformationRepository {
suspend fun delete(walletIds: List<UserWalletId>): CompletionResult<Unit>
suspend fun clear(): CompletionResult<Unit>
suspend fun transform(
block: (List<UserWalletPublicInformation>?) -> List<UserWalletPublicInformation>?,
): CompletionResult<Unit>
}

View file

@ -77,6 +77,21 @@ internal class DefaultUserWalletsPublicInformationRepository(
}
}
override suspend fun transform(
block: (List<UserWalletPublicInformation>?) -> List<UserWalletPublicInformation>?,
): CompletionResult<Unit> {
return withContext(Dispatchers.IO) {
getAll().flatMap { currentInfo ->
val transformed = block(currentInfo)
if (transformed != null) {
save(transformed)
} else {
CompletionResult.Success(Unit)
}
}
}
}
@JvmName("saveWithPublicInformation")
private suspend fun save(publicInformation: List<UserWalletPublicInformation>): CompletionResult<Unit> = catching {
withContext(Dispatchers.IO) {