Updated on 2026-08-14
This commit is contained in:
parent
aca7745031
commit
02c11f91fd
2 changed files with 39 additions and 3 deletions
|
|
@ -10,6 +10,7 @@ import com.tangem.data.common.account.WalletAccountsSaver
|
|||
import com.tangem.data.common.api.safeApiCall
|
||||
import com.tangem.data.common.cache.etag.ETagsStore
|
||||
import com.tangem.data.common.currency.UserTokensSaver
|
||||
import com.tangem.data.common.tokens.UserTokensBackwardCompatibility
|
||||
import com.tangem.datasource.api.common.response.ApiResponse
|
||||
import com.tangem.datasource.api.common.response.ApiResponseError.HttpException.Code
|
||||
import com.tangem.datasource.api.common.response.ETAG_HEADER
|
||||
|
|
@ -56,6 +57,8 @@ internal class DefaultWalletAccountsFetcher @Inject constructor(
|
|||
private val mainAccountTokensMigration: DefaultMainAccountTokensMigration,
|
||||
) : WalletAccountsFetcher, WalletAccountsSaver {
|
||||
|
||||
private val userTokensBackwardCompatibility = UserTokensBackwardCompatibility()
|
||||
|
||||
override suspend fun fetch(userWalletId: UserWalletId): GetWalletAccountsResponse {
|
||||
val savedAccountsResponse = getAccountsResponseStore(userWalletId = userWalletId).getSyncOrNull()
|
||||
val fetchResult = fetchWalletAccounts(userWalletId, savedAccountsResponse)
|
||||
|
|
@ -90,7 +93,7 @@ internal class DefaultWalletAccountsFetcher @Inject constructor(
|
|||
override suspend fun store(userWalletId: UserWalletId, response: GetWalletAccountsResponse) {
|
||||
val store = getAccountsResponseStore(userWalletId = userWalletId)
|
||||
|
||||
store.updateData { response }
|
||||
store.updateData { response.applyTokensCompatibility() }
|
||||
}
|
||||
|
||||
override suspend fun update(
|
||||
|
|
@ -99,7 +102,9 @@ internal class DefaultWalletAccountsFetcher @Inject constructor(
|
|||
) {
|
||||
val store = getAccountsResponseStore(userWalletId = userWalletId)
|
||||
|
||||
store.updateData { transform(it) }
|
||||
store.updateData {
|
||||
transform(it).applyTokensCompatibility()
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun push(
|
||||
|
|
@ -272,6 +277,22 @@ internal class DefaultWalletAccountsFetcher @Inject constructor(
|
|||
)
|
||||
}
|
||||
|
||||
private fun GetWalletAccountsResponse?.applyTokensCompatibility(): GetWalletAccountsResponse? {
|
||||
if (this == null) return null
|
||||
|
||||
return copy(
|
||||
accounts = accounts.map { accountDTO ->
|
||||
val tokens = accountDTO.tokens
|
||||
|
||||
if (tokens.isNullOrEmpty()) return@map accountDTO
|
||||
|
||||
accountDTO.copy(
|
||||
tokens = userTokensBackwardCompatibility.applyCompatibilityAndGetUpdated(tokens),
|
||||
)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
private fun getAccountsResponseStore(userWalletId: UserWalletId): AccountsResponseStore {
|
||||
return accountsResponseStoreFactory.create(userWalletId = userWalletId)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ import com.tangem.datasource.api.tangemTech.models.UserTokensResponse
|
|||
* Helper to apply compatibility changes for [UserTokensResponse] to support old saved tokens
|
||||
* in new application with new IDs
|
||||
*/
|
||||
internal class UserTokensBackwardCompatibility {
|
||||
class UserTokensBackwardCompatibility {
|
||||
|
||||
fun applyCompatibilityAndGetUpdated(userTokensResponse: UserTokensResponse): UserTokensResponse {
|
||||
return userTokensResponse.copy(
|
||||
|
|
@ -28,6 +28,21 @@ internal class UserTokensBackwardCompatibility {
|
|||
)
|
||||
}
|
||||
|
||||
fun applyCompatibilityAndGetUpdated(tokens: List<UserTokensResponse.Token>): List<UserTokensResponse.Token> {
|
||||
return tokens.map { token ->
|
||||
val oldSavedId = NETWORKS_TO_OLD_SAVED_IDS[token.networkId]
|
||||
if (oldSavedId != null && token.id == oldSavedId) {
|
||||
Blockchain.fromNetworkId(token.networkId)?.let { blockchain ->
|
||||
token.copy(
|
||||
id = blockchain.toCoinId(),
|
||||
)
|
||||
} ?: token
|
||||
} else {
|
||||
token
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val NETWORKS_TO_OLD_SAVED_IDS = mapOf(
|
||||
"optimistic-ethereum" to "ethereum",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue