Updated on 2026-08-14
This commit is contained in:
parent
90fea21f17
commit
1841d77c4c
13 changed files with 57 additions and 347 deletions
|
|
@ -1,57 +0,0 @@
|
|||
package com.tangem.datasource.di
|
||||
|
||||
import android.content.Context
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.core.DataStoreFactory
|
||||
import androidx.datastore.dataStoreFile
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.tangem.datasource.local.datastore.RuntimeDataStore
|
||||
import com.tangem.datasource.local.network.DefaultNetworksStatusesStore
|
||||
import com.tangem.datasource.local.network.NetworksStatusesStore
|
||||
import com.tangem.datasource.local.network.entity.NetworkStatusDM
|
||||
import com.tangem.datasource.utils.MoshiDataStoreSerializer
|
||||
import com.tangem.datasource.utils.mapWithStringKeyTypes
|
||||
import com.tangem.datasource.utils.setTypes
|
||||
import com.tangem.utils.coroutines.CoroutineDispatcherProvider
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
internal object NetworksStatusesStoreModule {
|
||||
|
||||
@Singleton
|
||||
@Provides
|
||||
fun providePersistenceNetworksStatusesStore(
|
||||
@NetworkMoshi moshi: Moshi,
|
||||
@ApplicationContext context: Context,
|
||||
dispatchers: CoroutineDispatcherProvider,
|
||||
): DataStore<Map<String, Set<NetworkStatusDM>>> {
|
||||
return DataStoreFactory.create(
|
||||
serializer = MoshiDataStoreSerializer(
|
||||
moshi = moshi,
|
||||
types = mapWithStringKeyTypes(valueTypes = setTypes<NetworkStatusDM>()),
|
||||
defaultValue = emptyMap(),
|
||||
),
|
||||
produceFile = { context.dataStoreFile(fileName = "networks_statuses") },
|
||||
scope = CoroutineScope(context = dispatchers.io + SupervisorJob()),
|
||||
)
|
||||
}
|
||||
|
||||
@Singleton
|
||||
@Provides
|
||||
fun provideNetworksStatusesStore(
|
||||
persistenceNetworksStatusesStore: DataStore<Map<String, Set<NetworkStatusDM>>>,
|
||||
): NetworksStatusesStore {
|
||||
return DefaultNetworksStatusesStore(
|
||||
runtimeDataStore = RuntimeDataStore(),
|
||||
persistenceDataStore = persistenceNetworksStatusesStore,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,201 +0,0 @@
|
|||
package com.tangem.datasource.local.network
|
||||
|
||||
import androidx.datastore.core.DataStore
|
||||
import com.tangem.datasource.local.datastore.RuntimeDataStore
|
||||
import com.tangem.datasource.local.network.converter.NetworkDerivationPathConverter
|
||||
import com.tangem.datasource.local.network.converter.NetworkStatusConverter
|
||||
import com.tangem.datasource.local.network.converter.NetworkStatusDataModelConverter
|
||||
import com.tangem.datasource.local.network.entity.NetworkStatusDM
|
||||
import com.tangem.domain.models.StatusSource
|
||||
import com.tangem.domain.tokens.model.Network
|
||||
import com.tangem.domain.tokens.model.NetworkStatus
|
||||
import com.tangem.domain.wallets.models.UserWalletId
|
||||
import com.tangem.utils.extensions.addOrReplace
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.flow.*
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
|
||||
private typealias NetworkStatusesByWalletId = Map<String, Set<NetworkStatusDM>>
|
||||
|
||||
internal class DefaultNetworksStatusesStore(
|
||||
private val runtimeDataStore: RuntimeDataStore<Set<NetworkStatus>>,
|
||||
private val persistenceDataStore: DataStore<NetworkStatusesByWalletId>,
|
||||
) : NetworksStatusesStore {
|
||||
|
||||
private val mutex = Mutex()
|
||||
|
||||
override fun get(key: UserWalletId): Flow<Set<NetworkStatus>> {
|
||||
return runtimeDataStore.get(provideStringKey(key))
|
||||
}
|
||||
|
||||
override fun get(key: UserWalletId, networks: Set<Network>): Flow<Set<NetworkStatus>> = channelFlow {
|
||||
val cachedStatuses = persistenceDataStore.data.firstOrNull()
|
||||
?.get(key.stringValue)
|
||||
?.mapNotNullTo(mutableSetOf()) { cached ->
|
||||
val network = networks.firstOrNull {
|
||||
it.id == cached.networkId &&
|
||||
it.derivationPath == NetworkDerivationPathConverter.convert(cached.derivationPath)
|
||||
}
|
||||
?: return@mapNotNullTo null
|
||||
|
||||
NetworkStatusConverter(network = network, isCached = true).convert(value = cached)
|
||||
}
|
||||
.orEmpty()
|
||||
|
||||
if (cachedStatuses.isNotEmpty()) {
|
||||
send(cachedStatuses)
|
||||
}
|
||||
|
||||
runtimeDataStore.get(provideStringKey(key))
|
||||
.onEach { runtimeStatuses ->
|
||||
val mergedStatuses = mergeStatuses(
|
||||
networks = networks,
|
||||
cachedStatuses = cachedStatuses,
|
||||
runtimeStatuses = runtimeStatuses,
|
||||
)
|
||||
|
||||
send(mergedStatuses)
|
||||
}
|
||||
.launchIn(scope = this)
|
||||
}
|
||||
|
||||
override suspend fun getSyncOrNull(key: UserWalletId): Set<NetworkStatus>? {
|
||||
val runtimeStatuses = runtimeDataStore.getSyncOrNull(key = provideStringKey(key)) ?: return null
|
||||
|
||||
val networks = runtimeStatuses.map(NetworkStatus::network).toSet()
|
||||
|
||||
val cachedStatuses = persistenceDataStore.data.firstOrNull()
|
||||
?.get(key.stringValue)
|
||||
?.mapNotNullTo(mutableSetOf()) { cached ->
|
||||
val network = networks.firstOrNull {
|
||||
it.id == cached.networkId &&
|
||||
it.derivationPath == NetworkDerivationPathConverter.convert(cached.derivationPath)
|
||||
}
|
||||
?: return@mapNotNullTo null
|
||||
|
||||
NetworkStatusConverter(network = network, isCached = true).convert(value = cached)
|
||||
}
|
||||
.orEmpty()
|
||||
|
||||
return mergeStatuses(
|
||||
networks = networks,
|
||||
cachedStatuses = cachedStatuses,
|
||||
runtimeStatuses = runtimeStatuses,
|
||||
)
|
||||
}
|
||||
|
||||
override suspend fun store(key: UserWalletId, value: NetworkStatus) {
|
||||
storeAll(key = key, values = setOf(value))
|
||||
}
|
||||
|
||||
override suspend fun storeAll(key: UserWalletId, values: Set<NetworkStatus>) {
|
||||
mutex.withLock {
|
||||
coroutineScope {
|
||||
launch { storeInRuntimeStore(key = key, statuses = values) }
|
||||
launch { storeInPersistenceStore(userWalletId = key, statuses = values) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun refresh(key: UserWalletId, networks: Set<Network>) {
|
||||
mutex.withLock {
|
||||
val currentStatuses = getSyncOrNull(key).orEmpty()
|
||||
|
||||
storeInRuntimeStore(
|
||||
key = key,
|
||||
statuses = networks.mapNotNullTo(hashSetOf()) { network ->
|
||||
val status = currentStatuses.firstOrNull {
|
||||
it.network.id == network.id && it.network.derivationPath == network.derivationPath
|
||||
} ?: return@mapNotNullTo null
|
||||
|
||||
status.copy(
|
||||
value = status.value.copySealed(source = StatusSource.CACHE),
|
||||
)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge [cachedStatuses] with [runtimeStatuses]
|
||||
* The resulting set contains statuses from both sets.
|
||||
* If a status with the same network is in both sets, the status from [runtimeStatuses] is used.
|
||||
*/
|
||||
private fun mergeStatuses(
|
||||
networks: Set<Network>,
|
||||
cachedStatuses: Set<NetworkStatus>,
|
||||
runtimeStatuses: Set<NetworkStatus>,
|
||||
): Set<NetworkStatus> {
|
||||
return networks.mapNotNullTo(hashSetOf()) { network ->
|
||||
val runtimeStatus = runtimeStatuses.firstOrNull { it.network == network }
|
||||
|
||||
if (runtimeStatus == null) {
|
||||
getCachedStatusIfPossible(
|
||||
cachedStatuses = cachedStatuses,
|
||||
network = network,
|
||||
source = StatusSource.CACHE,
|
||||
)
|
||||
} else if (runtimeStatus.value is NetworkStatus.Unreachable) {
|
||||
getCachedStatusIfPossible(
|
||||
cachedStatuses = cachedStatuses,
|
||||
network = network,
|
||||
source = StatusSource.ONLY_CACHE,
|
||||
)
|
||||
?: runtimeStatus
|
||||
} else {
|
||||
runtimeStatus
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getCachedStatusIfPossible(
|
||||
cachedStatuses: Set<NetworkStatus>,
|
||||
network: Network,
|
||||
source: StatusSource,
|
||||
): NetworkStatus? {
|
||||
val cached = cachedStatuses.firstOrNull { it.network == network } ?: return null
|
||||
|
||||
val updatedCachedStatus = when (val status = cached.value) {
|
||||
is NetworkStatus.NoAccount -> status.copy(source = source)
|
||||
is NetworkStatus.Verified -> status.copy(source = source)
|
||||
is NetworkStatus.Unreachable,
|
||||
is NetworkStatus.MissedDerivation,
|
||||
-> null
|
||||
}
|
||||
|
||||
return if (updatedCachedStatus != null) {
|
||||
cached.copy(value = updatedCachedStatus)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun storeInRuntimeStore(key: UserWalletId, statuses: Set<NetworkStatus>) {
|
||||
val updatedValues = getSyncOrNull(key).orEmpty()
|
||||
.addOrReplace(items = statuses) { prev, new -> prev.network == new.network }
|
||||
|
||||
runtimeDataStore.store(key = provideStringKey(key), value = updatedValues)
|
||||
}
|
||||
|
||||
private suspend fun storeInPersistenceStore(userWalletId: UserWalletId, statuses: Set<NetworkStatus>) {
|
||||
// Converter will return null if the network status is not supported
|
||||
val newStatuses = NetworkStatusDataModelConverter.convertSet(input = statuses).filterNotNull().toSet()
|
||||
|
||||
persistenceDataStore.updateData { storedStatuses ->
|
||||
storedStatuses.toMutableMap().apply {
|
||||
val updatedValues = this[userWalletId.stringValue].orEmpty()
|
||||
.addOrReplace(newStatuses) { prev, new ->
|
||||
prev.networkId == new.networkId && prev.derivationPath == new.derivationPath
|
||||
}
|
||||
|
||||
this[userWalletId.stringValue] = updatedValues
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun provideStringKey(key: UserWalletId): String {
|
||||
return "network_statuses_${key.stringValue}"
|
||||
}
|
||||
}
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
package com.tangem.datasource.local.network
|
||||
|
||||
import com.tangem.domain.tokens.model.Network
|
||||
import com.tangem.domain.tokens.model.NetworkStatus
|
||||
import com.tangem.domain.wallets.models.UserWalletId
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface NetworksStatusesStore {
|
||||
|
||||
fun get(key: UserWalletId): Flow<Set<NetworkStatus>>
|
||||
|
||||
fun get(key: UserWalletId, networks: Set<Network>): Flow<Set<NetworkStatus>>
|
||||
|
||||
suspend fun getSyncOrNull(key: UserWalletId): Set<NetworkStatus>?
|
||||
|
||||
suspend fun store(key: UserWalletId, value: NetworkStatus)
|
||||
|
||||
suspend fun storeAll(key: UserWalletId, values: Set<NetworkStatus>)
|
||||
|
||||
suspend fun refresh(key: UserWalletId, networks: Set<Network>)
|
||||
}
|
||||
|
|
@ -1,60 +0,0 @@
|
|||
package com.tangem.datasource.local.network.converter
|
||||
|
||||
import com.tangem.datasource.local.network.entity.NetworkStatusDM
|
||||
import com.tangem.domain.tokens.model.NetworkAddress
|
||||
import com.tangem.utils.converter.TwoWayConverter
|
||||
import timber.log.Timber
|
||||
|
||||
/**
|
||||
* Converter from [Set<NetworkStatusDM.Address>] to [NetworkAddress] and vice versa
|
||||
*
|
||||
[REDACTED_AUTHOR]
|
||||
*/
|
||||
class NetworkAddressConverter(
|
||||
private val selectedAddress: String,
|
||||
) : TwoWayConverter<Set<NetworkStatusDM.Address>, NetworkAddress> {
|
||||
|
||||
override fun convert(value: Set<NetworkStatusDM.Address>): NetworkAddress {
|
||||
val defaultAddress = value
|
||||
.firstOrNull { it.value == selectedAddress }
|
||||
?.let(::toNetworkAddress)
|
||||
|
||||
requireNotNull(defaultAddress) { "Selected address must not be null" }
|
||||
|
||||
return if (value.size != 1) {
|
||||
NetworkAddress.Selectable(
|
||||
defaultAddress = defaultAddress,
|
||||
availableAddresses = value.mapTo(destination = hashSetOf(), transform = ::toNetworkAddress),
|
||||
)
|
||||
} else {
|
||||
NetworkAddress.Single(defaultAddress = defaultAddress)
|
||||
}
|
||||
}
|
||||
|
||||
override fun convertBack(value: NetworkAddress): Set<NetworkStatusDM.Address> {
|
||||
return value.availableAddresses
|
||||
.map { address ->
|
||||
NetworkStatusDM.Address(
|
||||
value = address.value,
|
||||
type = when (address.type) {
|
||||
NetworkAddress.Address.Type.Primary -> NetworkStatusDM.Address.Type.Primary
|
||||
NetworkAddress.Address.Type.Secondary -> NetworkStatusDM.Address.Type.Secondary
|
||||
},
|
||||
)
|
||||
}
|
||||
.toSet()
|
||||
}
|
||||
|
||||
private fun toNetworkAddress(address: NetworkStatusDM.Address): NetworkAddress.Address {
|
||||
val type = when (address.type) {
|
||||
NetworkStatusDM.Address.Type.Primary -> NetworkAddress.Address.Type.Primary
|
||||
NetworkStatusDM.Address.Type.Secondary -> NetworkAddress.Address.Type.Secondary
|
||||
}
|
||||
|
||||
if (address.value.isBlank()) {
|
||||
Timber.w("Address value is blank")
|
||||
}
|
||||
|
||||
return NetworkAddress.Address(value = address.value, type = type)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,35 +0,0 @@
|
|||
package com.tangem.datasource.local.network.converter
|
||||
|
||||
import com.tangem.common.extensions.mapNotNullValues
|
||||
import com.tangem.domain.tokens.model.CryptoCurrency
|
||||
import com.tangem.domain.tokens.model.CryptoCurrencyAmountStatus
|
||||
import com.tangem.utils.converter.TwoWayConverter
|
||||
import java.math.BigDecimal
|
||||
|
||||
private typealias AmountsDataModel = Map<String, BigDecimal>
|
||||
private typealias AmountsDomainModel = Map<CryptoCurrency.ID, CryptoCurrencyAmountStatus>
|
||||
|
||||
/**
|
||||
* Converter from [AmountsDataModel] to [AmountsDomainModel] and vice versa
|
||||
*
|
||||
[REDACTED_AUTHOR]
|
||||
*/
|
||||
object NetworkAmountsConverter : TwoWayConverter<AmountsDataModel, AmountsDomainModel> {
|
||||
|
||||
override fun convert(value: AmountsDataModel): AmountsDomainModel {
|
||||
return value
|
||||
.mapKeys { CryptoCurrency.ID.fromValue(value = it.key) }
|
||||
.mapValues { (_, amount) -> CryptoCurrencyAmountStatus.Loaded(value = amount) }
|
||||
}
|
||||
|
||||
override fun convertBack(value: AmountsDomainModel): AmountsDataModel {
|
||||
return value
|
||||
.mapKeys { (id, _) -> id.value }
|
||||
.mapNotNullValues { (_, amount) ->
|
||||
when (amount) {
|
||||
is CryptoCurrencyAmountStatus.Loaded -> amount.value
|
||||
is CryptoCurrencyAmountStatus.NotFound -> null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
package com.tangem.datasource.local.network.converter
|
||||
|
||||
import com.tangem.datasource.local.network.entity.NetworkStatusDM
|
||||
import com.tangem.datasource.local.network.entity.NetworkStatusDM.DerivationPath.Type
|
||||
import com.tangem.domain.tokens.model.Network
|
||||
import com.tangem.utils.converter.TwoWayConverter
|
||||
|
||||
/**
|
||||
* Converter from [NetworkStatusDM.DerivationPath] to [Network.DerivationPath] and vice versa
|
||||
*
|
||||
[REDACTED_AUTHOR]
|
||||
*/
|
||||
object NetworkDerivationPathConverter :
|
||||
TwoWayConverter<NetworkStatusDM.DerivationPath, Network.DerivationPath> {
|
||||
|
||||
override fun convert(value: NetworkStatusDM.DerivationPath): Network.DerivationPath {
|
||||
return when (value.type) {
|
||||
Type.CARD -> Network.DerivationPath.Card(value.value)
|
||||
Type.CUSTOM -> Network.DerivationPath.Custom(value.value)
|
||||
Type.NONE -> Network.DerivationPath.None
|
||||
}
|
||||
}
|
||||
|
||||
override fun convertBack(value: Network.DerivationPath): NetworkStatusDM.DerivationPath {
|
||||
return NetworkStatusDM.DerivationPath(
|
||||
value = value.value.orEmpty(),
|
||||
type = when (value) {
|
||||
is Network.DerivationPath.Card -> Type.CARD
|
||||
is Network.DerivationPath.Custom -> Type.CUSTOM
|
||||
Network.DerivationPath.None -> Type.NONE
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,47 +0,0 @@
|
|||
package com.tangem.datasource.local.network.converter
|
||||
|
||||
import com.tangem.datasource.local.network.entity.NetworkStatusDM
|
||||
import com.tangem.domain.models.StatusSource
|
||||
import com.tangem.domain.tokens.model.Network
|
||||
import com.tangem.domain.tokens.model.NetworkStatus
|
||||
import com.tangem.utils.converter.Converter
|
||||
|
||||
/**
|
||||
* Converter from [NetworkStatusDM] to [NetworkStatus]
|
||||
*
|
||||
* @property network network
|
||||
* @property isCached flag that determines whether the status is a cache
|
||||
*
|
||||
[REDACTED_AUTHOR]
|
||||
*/
|
||||
internal class NetworkStatusConverter(
|
||||
private val network: Network,
|
||||
private val isCached: Boolean,
|
||||
) : Converter<NetworkStatusDM, NetworkStatus> {
|
||||
|
||||
override fun convert(value: NetworkStatusDM): NetworkStatus {
|
||||
val address = NetworkAddressConverter(selectedAddress = value.selectedAddress)
|
||||
.convert(value = value.availableAddresses)
|
||||
|
||||
val status = when (value) {
|
||||
is NetworkStatusDM.Verified -> {
|
||||
NetworkStatus.Verified(
|
||||
address = address,
|
||||
amounts = NetworkAmountsConverter.convert(value = value.amounts),
|
||||
pendingTransactions = mapOf(),
|
||||
source = if (isCached) StatusSource.CACHE else StatusSource.ACTUAL,
|
||||
)
|
||||
}
|
||||
is NetworkStatusDM.NoAccount -> {
|
||||
NetworkStatus.NoAccount(
|
||||
address = address,
|
||||
amountToCreateAccount = value.amountToCreateAccount,
|
||||
errorMessage = value.errorMessage,
|
||||
source = if (isCached) StatusSource.CACHE else StatusSource.ACTUAL,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return NetworkStatus(network = network, value = status)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,40 +0,0 @@
|
|||
package com.tangem.datasource.local.network.converter
|
||||
|
||||
import com.tangem.datasource.local.network.entity.NetworkStatusDM
|
||||
import com.tangem.domain.tokens.model.NetworkStatus
|
||||
import com.tangem.utils.converter.Converter
|
||||
|
||||
/**
|
||||
* Converter from [NetworkStatus] to [NetworkStatusDM]
|
||||
*
|
||||
[REDACTED_AUTHOR]
|
||||
*/
|
||||
object NetworkStatusDataModelConverter : Converter<NetworkStatus, NetworkStatusDM?> {
|
||||
|
||||
override fun convert(value: NetworkStatus): NetworkStatusDM? {
|
||||
return when (val status = value.value) {
|
||||
is NetworkStatus.Verified -> {
|
||||
NetworkStatusDM.Verified(
|
||||
networkId = value.network.id,
|
||||
derivationPath = NetworkDerivationPathConverter.convertBack(value = value.network.derivationPath),
|
||||
selectedAddress = status.address.defaultAddress.value,
|
||||
availableAddresses = NetworkAddressConverter(selectedAddress = status.address.defaultAddress.value)
|
||||
.convertBack(value = status.address),
|
||||
amounts = NetworkAmountsConverter.convertBack(value = status.amounts),
|
||||
)
|
||||
}
|
||||
is NetworkStatus.NoAccount -> {
|
||||
NetworkStatusDM.NoAccount(
|
||||
networkId = value.network.id,
|
||||
derivationPath = NetworkDerivationPathConverter.convertBack(value = value.network.derivationPath),
|
||||
selectedAddress = status.address.defaultAddress.value,
|
||||
availableAddresses = NetworkAddressConverter(selectedAddress = status.address.defaultAddress.value)
|
||||
.convertBack(value = status.address),
|
||||
amountToCreateAccount = status.amountToCreateAccount,
|
||||
errorMessage = status.errorMessage,
|
||||
)
|
||||
}
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue