Updated on 2026-08-14
This commit is contained in:
parent
1e4e932954
commit
aa54b0f842
1 changed files with 19 additions and 10 deletions
|
|
@ -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,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue