Updated on 2026-08-14

This commit is contained in:
Tangem 2025-05-20 16:37:36 +04:00
parent c5560c3c6a
commit ea3cbd0648
8 changed files with 20 additions and 54 deletions

View file

@ -40,12 +40,6 @@ internal object SimpleNetworkStatusConverter : Converter<NetworkStatusDM, Simple
}
}
return SimpleNetworkStatus(
id = SimpleNetworkStatus.Id(
networkId = value.networkId,
derivationPath = NetworkDerivationPathConverter.convert(value = value.derivationPath),
),
value = status,
)
return SimpleNetworkStatus(id = value.networkId, value = status)
}
}

View file

@ -9,19 +9,8 @@ import com.tangem.domain.tokens.model.NetworkStatus
* @property id identifier
* @property value value
*/
internal data class SimpleNetworkStatus(val id: Id, val value: NetworkStatus.Value) {
internal data class SimpleNetworkStatus(val id: Network.ID, val value: NetworkStatus.Value) {
/**
* Identifier of [SimpleNetworkStatus]
*
* @property networkId network id
* @property derivationPath derivation path
*/
data class Id(val networkId: Network.ID, val derivationPath: Network.DerivationPath) {
constructor(network: Network) : this(
networkId = network.id,
derivationPath = network.derivationPath,
)
}
/** Constructs [SimpleNetworkStatus] from [NetworkStatus] */
constructor(status: NetworkStatus) : this(id = status.network.id, value = status.value)
}

View file

@ -46,7 +46,7 @@ internal class DefaultMultiNetworkStatusProducer @AssistedInject constructor(
statuses.mapNotNullTo(hashSetOf()) { status ->
val network = getNetwork(
networkId = status.id.networkId,
networkId = status.id,
derivationPath = status.id.derivationPath,
scanResponse = userWallet.scanResponse,
excludedBlockchains = excludedBlockchains,

View file

@ -56,16 +56,14 @@ internal class DefaultNetworksStatusesStoreV2(
}
override suspend fun getSyncOrNull(userWalletId: UserWalletId, network: Network): SimpleNetworkStatus? {
val simpleStatusId = SimpleNetworkStatus.Id(network = network)
return runtimeStore.getSyncOrNull()?.get(userWalletId.stringValue)?.firstOrNull { it.id == simpleStatusId }
return runtimeStore.getSyncOrNull()?.get(userWalletId.stringValue)?.firstOrNull { it.id == network.id }
}
override suspend fun updateStatusSource(
userWalletId: UserWalletId,
network: Network,
source: StatusSource,
ifNotFound: (SimpleNetworkStatus.Id) -> SimpleNetworkStatus?,
ifNotFound: (Network.ID) -> SimpleNetworkStatus?,
) {
updateStatusSource(
userWalletId = userWalletId,
@ -79,7 +77,7 @@ internal class DefaultNetworksStatusesStoreV2(
userWalletId: UserWalletId,
networks: Set<Network>,
source: StatusSource,
ifNotFound: (SimpleNetworkStatus.Id) -> SimpleNetworkStatus?,
ifNotFound: (Network.ID) -> SimpleNetworkStatus?,
) {
if (networks.isEmpty()) {
Timber.d("Nothing to update: networks is empty")
@ -101,18 +99,16 @@ internal class DefaultNetworksStatusesStoreV2(
private suspend fun updateInRuntime(
userWalletId: UserWalletId,
networks: Set<Network>,
ifNotFound: (SimpleNetworkStatus.Id) -> SimpleNetworkStatus? = { null },
ifNotFound: (Network.ID) -> SimpleNetworkStatus? = { null },
update: (SimpleNetworkStatus) -> SimpleNetworkStatus,
) {
val simpleStatusIds = networks.map(SimpleNetworkStatus::Id)
runtimeStore.update(default = emptyMap()) { stored ->
stored.toMutableMap().apply {
val storedStatuses = this[userWalletId.stringValue].orEmpty()
val statuses = simpleStatusIds.mapNotNullTo(hashSetOf()) { simpleStatusId ->
val status = storedStatuses.firstOrNull { it.id == simpleStatusId }
?: ifNotFound(simpleStatusId)
val statuses = networks.mapNotNullTo(hashSetOf()) { network ->
val status = storedStatuses.firstOrNull { it.id == network.id }
?: ifNotFound(network.id)
?: return@mapNotNullTo null
update(status)
@ -126,14 +122,12 @@ internal class DefaultNetworksStatusesStoreV2(
}
private suspend fun storeInRuntime(userWalletId: UserWalletId, status: NetworkStatus) {
val simpleStatusId = SimpleNetworkStatus.Id(network = status.network)
runtimeStore.update(default = emptyMap()) { stored ->
stored.toMutableMap().apply {
val simpleStatus = SimpleNetworkStatus(id = simpleStatusId, value = status.value)
val simpleStatus = SimpleNetworkStatus(status = status)
val updatedStatuses = this[userWalletId.stringValue].orEmpty()
.addOrReplace(simpleStatus) { it.id == simpleStatusId }
.addOrReplace(simpleStatus) { it.id == simpleStatus.id }
put(key = userWalletId.stringValue, value = updatedStatuses)
}

View file

@ -99,6 +99,6 @@ internal suspend fun NetworksStatusesStoreV2.setSourceAsOnlyCache(userWalletId:
)
}
private fun createUnreachableStatus(id: SimpleNetworkStatus.Id): SimpleNetworkStatus {
private fun createUnreachableStatus(id: Network.ID): SimpleNetworkStatus {
return SimpleNetworkStatus(id = id, value = NetworkStatus.Unreachable(address = null))
}

View file

@ -24,7 +24,7 @@ internal interface NetworksStatusesStoreV2 {
userWalletId: UserWalletId,
network: Network,
source: StatusSource,
ifNotFound: (SimpleNetworkStatus.Id) -> SimpleNetworkStatus? = { null },
ifNotFound: (Network.ID) -> SimpleNetworkStatus? = { null },
)
/** Update [source] of [networks] by [userWalletId]. If the status is not found, create a new one by [ifNotFound] */
@ -32,7 +32,7 @@ internal interface NetworksStatusesStoreV2 {
userWalletId: UserWalletId,
networks: Set<Network>,
source: StatusSource,
ifNotFound: (SimpleNetworkStatus.Id) -> SimpleNetworkStatus? = { null },
ifNotFound: (Network.ID) -> SimpleNetworkStatus? = { null },
)
/**

View file

@ -5,12 +5,7 @@ import com.tangem.data.networks.models.SimpleNetworkStatus
import com.tangem.datasource.local.network.entity.NetworkStatusDM
import com.tangem.domain.tokens.model.NetworkStatus
internal fun NetworkStatus.toSimple(): SimpleNetworkStatus {
return SimpleNetworkStatus(
id = SimpleNetworkStatus.Id(network = network),
value = value,
)
}
internal fun NetworkStatus.toSimple() = SimpleNetworkStatus(status = this)
internal fun NetworkStatus.toDataModel(): NetworkStatusDM? {
return NetworkStatusDataModelConverter.convert(value = this)

View file

@ -70,10 +70,7 @@ internal class SimpleNetworkStatusConverterTest {
),
),
expected = SimpleNetworkStatus(
id = SimpleNetworkStatus.Id(
networkId = network.id,
derivationPath = Network.DerivationPath.Card(value = "card"),
),
id = network.id,
value = NetworkStatus.Verified(
address = NetworkAddress.Selectable(
defaultAddress = NetworkAddress.Address(
@ -133,10 +130,7 @@ internal class SimpleNetworkStatusConverterTest {
errorMessage = "errorMessage",
),
expected = SimpleNetworkStatus(
id = SimpleNetworkStatus.Id(
networkId = network.id,
derivationPath = Network.DerivationPath.Card(value = "card"),
),
id = network.id,
value = NetworkStatus.NoAccount(
address = NetworkAddress.Selectable(
defaultAddress = NetworkAddress.Address(