Updated on 2026-08-14
This commit is contained in:
parent
950dc41662
commit
dfa6798f07
16 changed files with 220 additions and 82 deletions
|
|
@ -1,39 +1,43 @@
|
|||
package com.tangem.blockchainsdk.converters
|
||||
|
||||
import com.tangem.blockchain.common.Blockchain
|
||||
import com.tangem.blockchain.common.network.providers.ProviderType
|
||||
import com.tangem.blockchainsdk.BlockchainProvidersResponse
|
||||
import com.tangem.blockchainsdk.providers.BlockchainProviderTypes
|
||||
import com.tangem.blockchainsdk.utils.createPrivateProviderType
|
||||
import com.tangem.blockchainsdk.utils.fromNetworkId
|
||||
import com.tangem.datasource.local.config.providers.models.ProviderModel
|
||||
import com.tangem.utils.converter.Converter
|
||||
import com.tangem.blockchainsdk.utils.toNetworkId
|
||||
import com.tangem.utils.converter.TwoWayConverter
|
||||
import timber.log.Timber
|
||||
|
||||
/**
|
||||
* Converts [BlockchainProvidersResponse] to [BlockchainProviderTypes]
|
||||
* Converts [BlockchainProvidersResponse] to [BlockchainProviderTypes] and vice versa
|
||||
*
|
||||
[REDACTED_AUTHOR]
|
||||
*/
|
||||
internal object BlockchainProviderTypesConverter :
|
||||
Converter<BlockchainProvidersResponse, BlockchainProviderTypes> {
|
||||
TwoWayConverter<BlockchainProvidersResponse, BlockchainProviderTypes> {
|
||||
|
||||
override fun convert(value: BlockchainProvidersResponse): BlockchainProviderTypes {
|
||||
return value.mapNotNull { (networkId, blockchainProviders) ->
|
||||
val blockchain = Blockchain.fromNetworkId(networkId) ?: return@mapNotNull null
|
||||
|
||||
val providerTypes = blockchainProviders.mapNotNull { provider ->
|
||||
when (provider) {
|
||||
is ProviderModel.Public -> ProviderType.Public(url = provider.url)
|
||||
is ProviderModel.Private -> createPrivateProviderType(name = provider.name)
|
||||
ProviderModel.UnsupportedType -> {
|
||||
Timber.e("$blockchain provider type is not supported")
|
||||
null
|
||||
}
|
||||
}
|
||||
val providerTypes = ProviderTypeConverter.convertList(input = blockchainProviders)
|
||||
|
||||
providerTypes.forEach {
|
||||
if (it == null) Timber.e("$blockchain provider type is not supported")
|
||||
}
|
||||
|
||||
blockchain to providerTypes
|
||||
blockchain to providerTypes.filterNotNull()
|
||||
}
|
||||
.toMap()
|
||||
}
|
||||
|
||||
override fun convertBack(value: BlockchainProviderTypes): BlockchainProvidersResponse {
|
||||
return value.mapNotNull { (blockchain, providerTypes) ->
|
||||
val networkId = blockchain.toNetworkId()
|
||||
|
||||
val blockchainProviders = ProviderTypeConverter.convertListBack(input = providerTypes)
|
||||
|
||||
networkId to blockchainProviders
|
||||
}
|
||||
.toMap()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,35 @@
|
|||
package com.tangem.blockchainsdk.converters
|
||||
|
||||
import com.tangem.blockchain.common.network.providers.ProviderType
|
||||
import com.tangem.blockchainsdk.providers.ProviderTypeIdMapping
|
||||
import com.tangem.datasource.local.config.providers.models.ProviderModel
|
||||
import com.tangem.utils.converter.TwoWayConverter
|
||||
|
||||
/**
|
||||
* Converts [ProviderModel] to [ProviderType] and vice versa
|
||||
*
|
||||
[REDACTED_AUTHOR]
|
||||
*/
|
||||
internal object ProviderTypeConverter : TwoWayConverter<ProviderModel, ProviderType?> {
|
||||
|
||||
override fun convert(value: ProviderModel): ProviderType? {
|
||||
return when (value) {
|
||||
is ProviderModel.Public -> ProviderType.Public(url = value.url)
|
||||
is ProviderModel.Private -> ProviderTypeIdMapping.entries.firstOrNull { it.id == value.name }?.providerType
|
||||
ProviderModel.UnsupportedType -> null
|
||||
}
|
||||
}
|
||||
|
||||
override fun convertBack(value: ProviderType?): ProviderModel {
|
||||
return when (value) {
|
||||
null -> ProviderModel.UnsupportedType
|
||||
is ProviderType.Public -> ProviderModel.Public(url = value.url)
|
||||
else -> {
|
||||
val id = ProviderTypeIdMapping.entries.firstOrNull { it.providerType == value }?.id
|
||||
?: return ProviderModel.UnsupportedType
|
||||
|
||||
ProviderModel.Private(id)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue