Updated on 2026-08-14

This commit is contained in:
Tangem 2022-12-30 11:18:15 +03:00
parent 1474d83756
commit 725705ecf2
9 changed files with 64 additions and 36 deletions

View file

@ -0,0 +1,20 @@
package com.tangem.tap.features.wallet.data
import com.tangem.datasource.api.tangemTech.TangemTechApi
import com.tangem.datasource.api.tangemTech.models.CurrenciesResponse
import com.tangem.tap.features.wallet.domain.WalletRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
/**
* Implementation of repository for Wallet feature
*
* @property tangemTechApi API for server requests
*/
class WalletRepositoryImpl(private val tangemTechApi: TangemTechApi) : WalletRepository {
override suspend fun getCurrencyList(): CurrenciesResponse =
withContext(Dispatchers.IO) { // TODO("After adding DI") replace with CoroutineDispatcherProvider
tangemTechApi.getCurrencyList()
}
}

View file

@ -0,0 +1,10 @@
package com.tangem.tap.features.wallet.domain
import com.tangem.datasource.api.tangemTech.models.CurrenciesResponse
/** Repository for Wallet feature */
interface WalletRepository {
/** Get list of currency */
suspend fun getCurrencyList(): CurrenciesResponse
}

View file

@ -1,8 +1,6 @@
package com.tangem.tap.features.wallet.redux.middlewares
import com.tangem.common.services.Result
import com.tangem.datasource.api.tangemTech.CurrenciesResponse
import com.tangem.datasource.api.tangemTech.TangemTechService
import com.tangem.datasource.api.tangemTech.models.Currency
import com.tangem.tap.common.analytics.Analytics
import com.tangem.tap.common.analytics.events.AnalyticsParam
import com.tangem.tap.common.analytics.events.MainScreen
@ -11,6 +9,7 @@ import com.tangem.tap.common.extensions.dispatchDialogShow
import com.tangem.tap.common.redux.global.GlobalAction
import com.tangem.tap.domain.TapWalletManager
import com.tangem.tap.features.details.redux.DetailsAction
import com.tangem.tap.features.wallet.domain.WalletRepository
import com.tangem.tap.features.wallet.redux.WalletAction
import com.tangem.tap.features.wallet.redux.models.WalletDialog
import com.tangem.tap.features.walletSelector.redux.WalletSelectorAction
@ -21,7 +20,7 @@ import com.tangem.tap.userWalletsListManager
import kotlinx.coroutines.launch
class AppCurrencyMiddleware(
private val tangemTechService: TangemTechService,
private val walletRepository: WalletRepository,
private val tapWalletManager: TapWalletManager,
private val fiatCurrenciesPrefStorage: FiatCurrenciesPrefStorage,
private val appCurrencyProvider: () -> FiatCurrency,
@ -45,12 +44,10 @@ class AppCurrencyMiddleware(
}
scope.launch {
when (val result = tangemTechService.currencies()) {
is Result.Success -> {
val currenciesList = result.data.currencies
if (currenciesList.isNotEmpty() &&
currenciesList.toSet() != storedFiatCurrencies.toSet()
) {
runCatching { walletRepository.getCurrencyList() }
.onSuccess {
val currenciesList = it.currencies
if (currenciesList.isNotEmpty() && !currenciesList.toSet().equals(storedFiatCurrencies.toSet())) {
fiatCurrenciesPrefStorage.save(currenciesList)
store.dispatchDialogShow(
WalletDialog.CurrencySelectionDialog(
@ -60,8 +57,6 @@ class AppCurrencyMiddleware(
)
}
}
is Result.Failure -> {}
}
}
}
@ -82,7 +77,7 @@ class AppCurrencyMiddleware(
}
}
private fun List<CurrenciesResponse.Currency>.mapToUiModel(): List<FiatCurrency> {
private fun List<Currency>.mapToUiModel(): List<FiatCurrency> {
return this.map {
FiatCurrency(
code = it.code,

View file

@ -36,6 +36,7 @@ import com.tangem.tap.domain.model.WalletStoreModel
import com.tangem.tap.features.demo.DemoHelper
import com.tangem.tap.features.home.redux.HomeAction
import com.tangem.tap.features.send.redux.PrepareSendScreen
import com.tangem.tap.features.wallet.data.WalletRepositoryImpl
import com.tangem.tap.features.wallet.models.Currency
import com.tangem.tap.features.wallet.models.PendingTransactionType
import com.tangem.tap.features.wallet.models.filterByCoin
@ -72,7 +73,8 @@ class WalletMiddleware {
private val walletDialogMiddleware = WalletDialogsMiddleware()
private val appCurrencyMiddleware by lazy(mode = LazyThreadSafetyMode.NONE) {
AppCurrencyMiddleware(
tangemTechService = store.state.domainNetworks.tangemTechService,
// TODO("After adding DI") get dependencies by DI
walletRepository = WalletRepositoryImpl(store.state.domainNetworks.tangemTechService.api),
tapWalletManager = store.state.globalState.tapWalletManager,
fiatCurrenciesPrefStorage = preferencesStorage.fiatCurrenciesPrefStorage,
appCurrencyProvider = { store.state.globalState.appCurrency },

View file

@ -3,7 +3,7 @@ package com.tangem.tap.persistence
import android.content.SharedPreferences
import androidx.core.content.edit
import com.tangem.common.json.MoshiJsonConverter
import com.tangem.datasource.api.tangemTech.CurrenciesResponse
import com.tangem.datasource.api.tangemTech.models.Currency
import com.tangem.tap.common.entities.FiatCurrency
/**
@ -32,14 +32,14 @@ class FiatCurrenciesPrefStorage(
preferences.edit { putString(APP_CURRENCY_KEY, json) }
}
fun save(currencies: List<CurrenciesResponse.Currency>) {
fun save(currencies: List<Currency>) {
val json: String = converter.toJson(currencies)
return preferences.edit().putString(FIAT_CURRENCIES_KEY, json).apply()
}
fun restore(): List<CurrenciesResponse.Currency> {
fun restore(): List<Currency> {
val json = preferences.getString(FIAT_CURRENCIES_KEY, "")
val type = converter.typedList(CurrenciesResponse.Currency::class.java)
val type = converter.typedList(Currency::class.java)
if (json.isNullOrBlank()) return emptyList()
return converter.fromJson(json, type) ?: emptyList()

View file

@ -34,18 +34,6 @@ data class CoinsResponse(
//rates.keys = networkId's
data class RatesResponse(val rates: Map<String, Double>) : TangemTechResponse
data class CurrenciesResponse(val currencies: List<Currency>) {
data class Currency(
val id: String,
val code: String, // this is an uppercase id
val name: String,
val rateBTC: String,
val unit: String, // $, €, ₽
val type: String,
) : TangemTechResponse
}
data class GeoResponse(
val code: String,
) : TangemTechResponse

View file

@ -1,5 +1,6 @@
package com.tangem.datasource.api.tangemTech
import com.tangem.datasource.api.tangemTech.models.CurrenciesResponse
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.PUT
@ -29,7 +30,7 @@ interface TangemTechApi {
): RatesResponse
@GET("currencies")
suspend fun currencies(): CurrenciesResponse
suspend fun getCurrencyList(): CurrenciesResponse
@GET("geo")
suspend fun geo(): GeoResponse

View file

@ -19,7 +19,7 @@ class TangemTechService(
CacheControlHttpInterceptor(cacheMaxAge),
)
private var api: TangemTechApi = createApi()
var api: TangemTechApi = createApi()
suspend fun coins(
contractAddress: String? = null,
@ -54,10 +54,6 @@ class TangemTechService(
performRequest { api.geo() }
}
suspend fun currencies(): Result<CurrenciesResponse> = withContext(Dispatchers.IO) {
performRequest { api.currencies() }
}
suspend fun getUserTokens(userId: String): Result<UserTokensResponse> = withContext(Dispatchers.IO) {
performRequest { api.getUserTokens(userId) }
}

View file

@ -0,0 +1,16 @@
package com.tangem.datasource.api.tangemTech.models
import com.squareup.moshi.Json
data class CurrenciesResponse(
@Json(name = "currencies") val currencies: List<Currency>
)
data class Currency(
@Json(name = "id") val id: String,
@Json(name = "code") val code: String, // this is an uppercase id
@Json(name = "name") val name: String,
@Json(name = "rateBTC") val rateBTC: String,
@Json(name = "unit") val unit: String, // $, €, ₽
@Json(name = "type") val type: String
)