Fixes to the equivalent values
- Only inserting an equivalent value into the database in case the value is equal non-negative (negative values are a result of errors) - Using USD as fallback equivalent value currency
This commit is contained in:
parent
21e600069e
commit
1422ed699e
2 changed files with 17 additions and 8 deletions
|
@ -127,7 +127,8 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection {
|
||||||
// Configure ConnectedActivityViewModel to obtain missing equivalent values
|
// Configure ConnectedActivityViewModel to obtain missing equivalent values
|
||||||
mConnectedActivityViewModel = ViewModelProviders.of(this).get(ConnectedActivityViewModel::class.java)
|
mConnectedActivityViewModel = ViewModelProviders.of(this).get(ConnectedActivityViewModel::class.java)
|
||||||
|
|
||||||
mConnectedActivityViewModel.observeMissingEquivalentValuesIn("usd") //TODO: Obtain this from shared preferences?
|
val currency = Currency.getInstance(Locale.getDefault())
|
||||||
|
mConnectedActivityViewModel.observeMissingEquivalentValuesIn(currency.currencyCode) //TODO: Obtain this from shared preferences?
|
||||||
|
|
||||||
// Configure UserAccountViewModel to obtain the missing account ids
|
// Configure UserAccountViewModel to obtain the missing account ids
|
||||||
mUserAccountViewModel = ViewModelProviders.of(this).get(UserAccountViewModel::class.java)
|
mUserAccountViewModel = ViewModelProviders.of(this).get(UserAccountViewModel::class.java)
|
||||||
|
|
|
@ -82,7 +82,7 @@ class TransferRepository internal constructor(context: Context) {
|
||||||
.map { transfer -> obtainFiatValue(transfer, symbol) }
|
.map { transfer -> obtainFiatValue(transfer, symbol) }
|
||||||
.subscribe({
|
.subscribe({
|
||||||
Log.d(TAG,"Got equivalent value: $it")
|
Log.d(TAG,"Got equivalent value: $it")
|
||||||
mEquivalentValuesDao.insert(it)
|
if(it.value >= 0) mEquivalentValuesDao.insert(it)
|
||||||
},{
|
},{
|
||||||
Log.e(TAG,"Error while trying to create a new equivalent value. Msg: ${it.message}")
|
Log.e(TAG,"Error while trying to create a new equivalent value. Msg: ${it.message}")
|
||||||
for(element in it.stackTrace){
|
for(element in it.stackTrace){
|
||||||
|
@ -110,12 +110,20 @@ class TransferRepository internal constructor(context: Context) {
|
||||||
?.execute()
|
?.execute()
|
||||||
var equivalentFiatValue = -1L
|
var equivalentFiatValue = -1L
|
||||||
if(response?.isSuccessful == true){
|
if(response?.isSuccessful == true){
|
||||||
val price: Double = response.body()?.market_data?.current_price?.get(symbol) ?: -1.0
|
// We try to use the locale-selected currency first
|
||||||
// The equivalent value is obtained by:
|
var selectedCurrency = symbol
|
||||||
// 1- Dividing the base value by 100000 (BTS native precision)
|
// Checking that the provided currency is supported by the Coingecko API
|
||||||
// 2- Multiplying that BTS value by the unit price in the chosen fiat
|
val isCurrencySupported = response?.body()?.market_data?.current_price?.keys?.contains(symbol.toLowerCase())
|
||||||
// 3- Multiplying the resulting value by 100 in order to express it in cents
|
// In case it is not, we fallback to USD
|
||||||
equivalentFiatValue = Math.round(transfer.btsValue?.div(1e5)?.times(price)?.times(100) ?: -1.0)
|
if(isCurrencySupported == false) selectedCurrency = "usd"
|
||||||
|
val price: Double = response.body()?.market_data?.current_price?.get(selectedCurrency.toLowerCase()) ?: -1.0
|
||||||
|
if(price > 0){
|
||||||
|
// The equivalent value is obtained by:
|
||||||
|
// 1- Dividing the base value by 100000 (BTS native precision)
|
||||||
|
// 2- Multiplying that BTS value by the unit price in the chosen fiat
|
||||||
|
// 3- Multiplying the resulting value by 100 in order to express it in cents
|
||||||
|
equivalentFiatValue = Math.round(transfer.btsValue?.toFloat()?.div(1e5)?.times(price)?.times(100) ?: -1.0)
|
||||||
|
}
|
||||||
}else{
|
}else{
|
||||||
Log.w(TAG,"Request was not successful. code: ${response?.code()}")
|
Log.w(TAG,"Request was not successful. code: ${response?.code()}")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue