Introducing a supported currencies cache for the equivalent values feature

- In case the device is without connectivity, we cannot check in real time what the list of supported currencies is from the coingecko API. For this special corner cases we store a cache of this currency list.
master
Nelson R. Perez 2019-11-07 16:36:49 -05:00
parent 2ef589526d
commit f8b7a11ba9
2 changed files with 21 additions and 0 deletions

View File

@ -1,7 +1,9 @@
package cy.agorise.bitsybitshareswallet.repositories
import android.content.Context
import android.content.SharedPreferences
import android.os.AsyncTask
import android.preference.PreferenceManager
import android.util.Log
import androidx.lifecycle.LiveData
import cy.agorise.bitsybitshareswallet.database.BitsyDatabase
@ -26,11 +28,13 @@ class TransferRepository internal constructor(context: Context) {
private val mTransferDao: TransferDao
private val mEquivalentValuesDao: EquivalentValueDao
private val compositeDisposable = CompositeDisposable()
private val mPreferences: SharedPreferences
init {
val db = BitsyDatabase.getDatabase(context)
mTransferDao = db!!.transferDao()
mEquivalentValuesDao = db.equivalentValueDao()
mPreferences = PreferenceManager.getDefaultSharedPreferences(context)
}
fun insertAll(transfers: List<Transfer>) {
@ -177,10 +181,24 @@ class TransferRepository internal constructor(context: Context) {
val response = sg.getService(CoingeckoService::class.java)
?.getSupportedCurrencies()
?.execute()
// Updating the supported currencies cache
mPreferences.edit()
.putStringSet(Constants.KEY_COINGECKO_CURRENCIES_CACHE, response?.body()?.toMutableSet() ?: setOf())
.apply()
if(response?.body()?.indexOf(symbol.toLowerCase()) == -1)
"usd"
else
it
}
.onErrorReturn {
// Error caused potentially by the lack of connectivity. If this happens we just
// retrieve the value from the cache
val currencies = mPreferences.getStringSet(Constants.KEY_COINGECKO_CURRENCIES_CACHE, setOf())
var selectedCurrency = "usd"
if(currencies.contains(symbol)) {
selectedCurrency = symbol
}
selectedCurrency
}
}
}

View File

@ -48,6 +48,9 @@ object Constants {
/** Coingecko's API URL */
const val COINGECKO_URL = "https://api.coingecko.com"
/** Key used to store and retrieve a cache of the coingecko supported currencies */
const val KEY_COINGECKO_CURRENCIES_CACHE = "key_coingecko_currencies_cache"
/** The fee to send in every transfer (0.01%) */
const val FEE_PERCENTAGE = 0.0001