Updated on 2026-08-14

This commit is contained in:
Tangem 2023-07-26 12:54:28 +03:00
parent 3e1a45dbad
commit 6016933480
9 changed files with 21 additions and 23 deletions

View file

@ -12,10 +12,10 @@ internal class RuntimeUserWalletsStore(
private val walletsStateHolder: WalletsStateHolder,
) : UserWalletsStore {
override suspend fun getSync(userWalletId: UserWalletId): UserWallet? {
override suspend fun getOrNull(userWalletId: UserWalletId): UserWallet? {
return walletsStateHolder.userWalletsListManager
?.userWallets
?.firstOrNull()
?.firstOrNull { it.walletId == userWalletId }
?.singleOrNull { it.walletId == userWalletId }
}
}

View file

@ -4,7 +4,7 @@ import com.tangem.datasource.local.cache.model.CacheKey
interface CacheKeysStore {
fun get(id: String): CacheKey?
fun getOrNull(id: String): CacheKey?
fun addOrReplace(key: CacheKey)

View file

@ -7,8 +7,8 @@ internal class RuntimeCacheKeysStore : CacheKeysStore {
private val store = RuntimeDataStore(keyProvider = CacheKey::id)
override fun get(id: String): CacheKey? {
return store.getSync { it.id == id }.firstOrNull()
override fun getOrNull(id: String): CacheKey? {
return store.getSync { it.id == id }.singleOrNull()
}
override fun addOrReplace(key: CacheKey) {

View file

@ -3,12 +3,11 @@ package com.tangem.datasource.local.datastore
import com.squareup.moshi.JsonAdapter
import com.tangem.datasource.files.FileReader
import com.tangem.datasource.local.datastore.model.WriteTrigger
import com.tangem.domain.core.error.DataError
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.*
import java.io.FileNotFoundException
import timber.log.Timber
internal class FileDataStore<Data>(
internal class FileDataStore<Data : Any>(
private val fileNameProvider: (key: String) -> String,
private val fileReader: FileReader,
private val adapter: JsonAdapter<Data>,
@ -26,32 +25,31 @@ internal class FileDataStore<Data>(
.filterNotNull()
}
fun getSync(key: String): Data? {
fun getSyncOrNull(key: String): Data? {
return getInternal(fileNameProvider(key))
}
fun store(key: String, content: Data) {
val fileName = fileNameProvider(key)
try {
val json = adapter.toJson(content)
fileReader.rewriteFile(json, fileNameProvider(key))
fileReader.rewriteFile(json, fileName)
writeTrigger.tryEmit(WriteTrigger)
} catch (e: Throwable) {
throw DataError.PersistenceError.UnableToWriteFile(e)
Timber.e(e, "Unable to write file: $fileName")
}
}
private fun getInternal(fileName: String): Data? {
return try {
val json = try {
fileReader.readFile(fileName)
} catch (e: FileNotFoundException) {
return null
}
val json = fileReader.readFile(fileName)
adapter.fromJson(json)
} catch (e: Throwable) {
throw DataError.PersistenceError.UnableToReadFile(e)
Timber.e(e, "Unable to read file: $fileName")
null
}
}
}

View file

@ -5,7 +5,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.update
internal class RuntimeDataStore<Key, Data>(private val keyProvider: (Data) -> Key) {
internal class RuntimeDataStore<Key : Any, Data : Any>(private val keyProvider: (Data) -> Key) {
private val store = MutableStateFlow<HashMap<Key, Data>>(hashMapOf())

View file

@ -18,8 +18,8 @@ internal class FileUserTokensStore(fileReader: FileReader, moshi: Moshi) : UserT
return store.get(userWalletId)
}
override suspend fun getSync(userWalletId: String): UserTokensResponse? {
return store.getSync(userWalletId)
override suspend fun getSyncOrNull(userWalletId: String): UserTokensResponse? {
return store.getSyncOrNull(userWalletId)
}
override suspend fun store(userWalletId: String, tokens: UserTokensResponse) {

View file

@ -7,7 +7,7 @@ interface UserTokensStore {
fun get(userWalletId: String): Flow<UserTokensResponse>
suspend fun getSync(userWalletId: String): UserTokensResponse?
suspend fun getSyncOrNull(userWalletId: String): UserTokensResponse?
suspend fun store(userWalletId: String, tokens: UserTokensResponse)
}

View file

@ -5,5 +5,5 @@ import com.tangem.domain.wallets.models.UserWalletId
interface UserWalletsStore {
suspend fun getSync(userWalletId: UserWalletId): UserWallet?
suspend fun getOrNull(userWalletId: UserWalletId): UserWallet?
}

View file

@ -10,7 +10,7 @@ internal class DefaultCacheRegistry(
) : CacheRegistry {
override suspend fun isExpired(key: String): Boolean {
val cacheKey = cacheKeysStore.get(key) ?: return true
val cacheKey = cacheKeysStore.getOrNull(key) ?: return true
return cacheKey.updatedAt
.plus(cacheKey.expiresIn)