Updated on 2026-08-14

This commit is contained in:
Tangem 2022-11-03 17:45:42 +05:00
parent e8ed347498
commit d2d725c4e9
9 changed files with 195 additions and 49 deletions

View file

@ -16,6 +16,7 @@ class PreferencesStorage(applicationContext: Application) {
val usedCardsPrefStorage: UsedCardsPrefStorage
val fiatCurrenciesPrefStorage: FiatCurrenciesPrefStorage
val disclaimerPrefStorage: DisclaimerPrefStorage
val toppedUpWalletStorage: ToppedUpWalletStorage
init {
incrementLaunchCounter()
@ -25,6 +26,7 @@ class PreferencesStorage(applicationContext: Application) {
fiatCurrenciesPrefStorage = FiatCurrenciesPrefStorage(preferences, MoshiJsonConverter.INSTANCE)
fiatCurrenciesPrefStorage.migrate()
disclaimerPrefStorage = DisclaimerPrefStorage(preferences)
toppedUpWalletStorage = ToppedUpWalletStorage(preferences, MoshiJsonConverter.INSTANCE)
}
var chatFirstLaunchTime: Long?

View file

@ -0,0 +1,57 @@
package com.tangem.tap.persistence
import android.content.SharedPreferences
import androidx.core.content.edit
import com.tangem.common.json.MoshiJsonConverter
import com.tangem.tap.common.analytics.filters.BasicTopUpFilter
import timber.log.Timber
/**
[REDACTED_AUTHOR]
*/
class ToppedUpWalletStorage(
private val preferences: SharedPreferences,
private val jsonConverter: MoshiJsonConverter,
) {
private val walletList: MutableSet<BasicTopUpFilter.Data> = mutableSetOf()
init {
walletList.addAll(restore())
}
fun save(userWalletInfo: BasicTopUpFilter.Data): Boolean {
walletList.removeAll { it.walletId == userWalletInfo.walletId }
walletList.add(userWalletInfo)
return save(walletList)
}
fun restore(walletId: String): BasicTopUpFilter.Data? {
return walletList.firstOrNull { it.walletId == walletId }
}
private fun save(userWallets: MutableSet<BasicTopUpFilter.Data>): Boolean {
return try {
val json = jsonConverter.toJson(userWallets)
preferences.edit(true) { putString(KEY, json) }
true
} catch (ex: Exception) {
Timber.e(ex)
false
}
}
private fun restore(): MutableSet<BasicTopUpFilter.Data> {
val json = preferences.getString(KEY, null) ?: return mutableSetOf()
return try {
jsonConverter.fromJson(json, jsonConverter.typedList(BasicTopUpFilter.Data::class.java))!!
} catch (ex: Exception) {
preferences.edit(true) { remove(KEY) }
mutableSetOf()
}
}
companion object {
private const val KEY = "userWalletsInfo"
}
}