Updated on 2026-08-14

This commit is contained in:
Tangem 2025-07-30 18:10:31 +03:00
parent 1e4e932954
commit aa54b0f842

View file

@ -4,26 +4,35 @@ import com.tangem.common.services.secure.SecureStorage
import com.tangem.domain.wallets.models.UserWalletId
import com.tangem.tap.domain.userWalletList.repository.SelectedUserWalletRepository
import com.tangem.utils.coroutines.CoroutineDispatcherProvider
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
internal class DefaultSelectedUserWalletRepository(
private val secureStorage: SecureStorage,
private val dispatchers: CoroutineDispatcherProvider,
) : SelectedUserWalletRepository {
private val mutex = Mutex()
override suspend fun get(): UserWalletId? = withContext(dispatchers.io) {
secureStorage.get(StorageKey.SelectedWalletId.name)
?.decodeToString(throwOnInvalidSequence = true)
?.let { UserWalletId(it) }
mutex.withLock {
secureStorage.get(StorageKey.SelectedWalletId.name)
?.decodeToString(throwOnInvalidSequence = true)
?.let { UserWalletId(it) }
}
}
override suspend fun set(walletId: UserWalletId?) = withContext(dispatchers.io) {
if (walletId == null) {
secureStorage.delete(StorageKey.SelectedWalletId.name)
} else {
secureStorage.store(
data = walletId.stringValue.encodeToByteArray(throwOnInvalidSequence = true),
account = StorageKey.SelectedWalletId.name,
)
mutex.withLock {
if (walletId == null) {
secureStorage.delete(StorageKey.SelectedWalletId.name)
} else {
secureStorage.store(
data = walletId.stringValue.encodeToByteArray(throwOnInvalidSequence = true),
account = StorageKey.SelectedWalletId.name,
)
}
}
}