Updated on 2026-08-14

This commit is contained in:
Tangem 2024-03-05 09:12:03 +00:00
commit 3f386274ec
286 changed files with 4852 additions and 2134 deletions

View file

@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tangem.datasource"
xmlns:android="http://schemas.android.com/apk/res/android">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

View file

@ -1,11 +1,15 @@
package com.tangem.datasource.api.tangemTech.models
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
data class CurrenciesResponse(
@Json(name = "currencies") val currencies: List<Currency>,
@Json(name = "imageHost") val imageHost: String? = null,
) {
@JsonClass(generateAdapter = true)
data class Currency(
@Json(name = "id") val id: String,
@Json(name = "code") val code: String, // this is an uppercase id
@ -13,5 +17,7 @@ data class CurrenciesResponse(
@Json(name = "rateBTC") val rateBTC: String,
@Json(name = "unit") val unit: String, // $, €, ₽
@Json(name = "type") val type: String,
val iconSmallUrl: String? = null,
val iconMediumUrl: String? = null,
)
}

View file

@ -21,7 +21,7 @@ internal object AppPreferencesStoreModule {
fun provideAppPreferencesStore(
@ApplicationContext appContext: Context,
dispatchers: CoroutineDispatcherProvider,
@NetworkMoshi moshi: Moshi,
@SdkMoshi moshi: Moshi,
): AppPreferencesStore {
return AppPreferencesStore(
preferencesDataStore = PreferencesDataStore.getInstance(context = appContext, dispatcher = dispatchers.io),

View file

@ -15,8 +15,20 @@ internal class DefaultAvailableAppCurrenciesStore(
}
override suspend fun store(response: CurrenciesResponse) {
val currencies = response.currencies.associateBy(CurrenciesResponse.Currency::code)
val currencies = response.currencies
.map {
it.copy(
iconSmallUrl = response.imageHost?.plus(IMAGE_SMALL)?.format(it.id),
iconMediumUrl = response.imageHost?.plus(IMAGE_MEDIUM)?.format(it.id),
)
}
.associateBy(CurrenciesResponse.Currency::code)
dataStore.store(currencies)
}
private companion object {
const val IMAGE_MEDIUM = "medium/%s.png"
const val IMAGE_SMALL = "small/%s.png"
}
}

View file

@ -45,7 +45,7 @@ class AppPreferencesStore(
* @see getObjectList
* */
inline fun <reified T> MutablePreferences.getObject(key: Preferences.Key<String>): T? {
val adapter = moshi.adapter(T::class.java) // TODO: Support parameterized types
val adapter = moshi.adapter(T::class.java)
return this[key]?.let(adapter::fromJson)
}
@ -55,12 +55,21 @@ class AppPreferencesStore(
return this[key]?.let(adapter::fromJson)
}
/** Get list of data [T] by string [key] or default */
inline fun <reified T> MutablePreferences.getObjectListOrDefault(
key: Preferences.Key<String>,
default: List<T>,
): List<T> {
val adapter = moshi.adapter<List<T>>(Types.newParameterizedType(List::class.java, T::class.java))
return this[key]?.let(adapter::fromJson) ?: default
}
/** Get map with [String] key and value [V] by string [key] from [MutablePreferences] */
inline fun <reified V> MutablePreferences.getObjectMap(key: Preferences.Key<String>): Map<String, V>? {
inline fun <reified V> MutablePreferences.getObjectMap(key: Preferences.Key<String>): Map<String, V> {
val type = Types.newParameterizedType(Map::class.java, String::class.java, V::class.java)
val adapter = moshi.adapter<Map<String, V>>(type)
return this[key]?.let(adapter::fromJson)
return this[key]?.let(adapter::fromJson).orEmpty()
}
/**

View file

@ -3,10 +3,12 @@ package com.tangem.datasource.local.preferences
import androidx.datastore.preferences.core.*
import com.tangem.datasource.local.preferences.PreferencesKeys.APP_LAUNCH_COUNT_KEY
import com.tangem.datasource.local.preferences.PreferencesKeys.FUNDS_FOUND_DATE_KEY
import com.tangem.datasource.local.preferences.PreferencesKeys.IS_TANGEM_TOS_ACCEPTED_KEY
import com.tangem.datasource.local.preferences.PreferencesKeys.SAVE_USER_WALLETS_KEY
import com.tangem.datasource.local.preferences.PreferencesKeys.SHOW_RATING_DIALOG_AT_LAUNCH_COUNT_KEY
import com.tangem.datasource.local.preferences.PreferencesKeys.USED_CARDS_INFO_KEY
import com.tangem.datasource.local.preferences.PreferencesKeys.USER_WAS_INTERACT_WITH_RATING_KEY
import com.tangem.datasource.local.preferences.PreferencesKeys.WAS_TWINS_ONBOARDING_SHOWN
/**
* All preferences keys that DataStore<Preferences> is stored.
@ -54,6 +56,14 @@ object PreferencesKeys {
}
val FEATURE_TOGGLES_KEY by lazy { stringPreferencesKey(name = "featureToggles") }
val WAS_TWINS_ONBOARDING_SHOWN by lazy { booleanPreferencesKey(name = "twinsOnboardingShown") }
val IS_TANGEM_TOS_ACCEPTED_KEY by lazy { booleanPreferencesKey(name = "tangem_tos_accepted") }
val APP_LOGS_KEY by lazy { stringPreferencesKey(name = "app_logs") }
fun getStart2CoinTOSAcceptedKey(region: String?) = booleanPreferencesKey(name = "start2Coin_tos_accepted_$region")
}
/** Preferences keys set that should be migrated from "PreferencesDataSource" to a new DataStore<Preferences> */
@ -65,6 +75,8 @@ internal fun getTapPrefKeysToMigrate(): Set<String> {
FUNDS_FOUND_DATE_KEY,
USER_WAS_INTERACT_WITH_RATING_KEY,
USED_CARDS_INFO_KEY,
WAS_TWINS_ONBOARDING_SHOWN,
IS_TANGEM_TOS_ACCEPTED_KEY,
)
.map(Preferences.Key<*>::name)
.toSet()