Avoid crash due to unsupported currency locale.

- Avoided a crash in ConnectedActivity when trying to obtain the Locale's associated currency, when a Locale does not have a currency. This can happen when the locale is configured for a region like Latin America and not for a specific country.
- Standardized the process to obtain the coingecko supported currency, which will first try to use the current locale's currency and fallback to the default in case the first is not supported.
master
Severiano Jaramillo 2020-01-06 10:08:46 -06:00
parent 20571d49a3
commit 5d09cfa0c2
5 changed files with 48 additions and 9 deletions

View File

@ -121,8 +121,7 @@ abstract class ConnectedActivity : AppCompatActivity() {
// Configure ConnectedActivityViewModel to obtain missing equivalent values
mConnectedActivityViewModel = ViewModelProviders.of(this).get(ConnectedActivityViewModel::class.java)
val currency = Currency.getInstance(Locale.getDefault())
val currencyCode = Helper.getCoingeckoSupportedCurrency(currency.currencyCode)
val currencyCode = Helper.getCoingeckoSupportedCurrency(Locale.getDefault())
Log.d(TAG, "Using currency: ${currencyCode.toUpperCase(Locale.ROOT)}")
mConnectedActivityViewModel.observeMissingEquivalentValuesIn(currencyCode)

View File

@ -159,8 +159,7 @@ class FilterOptionsDialog : DialogFragment(), DatePickerFragment.OnDateSetListen
llEquivalentValue.visibility = if(isChecked) View.GONE else View.VISIBLE }
cbEquivalentValue.isChecked = mFilterOptions.equivalentValueAll
val currency = Currency.getInstance(Locale.getDefault())
val currencyCode = Helper.getCoingeckoSupportedCurrency(currency.currencyCode)
val currencyCode = Helper.getCoingeckoSupportedCurrency(Locale.getDefault())
mCurrency = Currency.getInstance(currencyCode)
val fromEquivalentValue = mFilterOptions.fromEquivalentValue /

View File

@ -57,9 +57,18 @@ object Helper {
}
/**
* If the given currency code is supported, returns it, else returns the default one.
* Verifies that the locale has a valid currency, else uses the default one. Then if
* the given currency code is supported, returns it, else returns the default one.
*/
fun getCoingeckoSupportedCurrency(currencyCode: String): String {
fun getCoingeckoSupportedCurrency(locale: Locale): String {
val currency = try {
Currency.getInstance(locale)
} catch (e: IllegalArgumentException) {
Currency.getInstance(Locale.US)
}
val currencyCode = currency.currencyCode
val supportedCurrencies = setOf("usd", "aed", "ars", "aud", "bdt", "bhd", "bmd", "brl", "cad",
"chf", "clp", "cny", "czk", "dkk", "eur", "gbp", "hkd", "huf", "idr", "ils", "inr", "jpy",
"krw", "kwd", "lkr", "mmk", "mxn", "myr", "nok", "nzd", "php", "pkr", "pln", "rub", "sar",
@ -68,6 +77,6 @@ object Helper {
return if (currencyCode.toLowerCase(Locale.ROOT) in supportedCurrencies)
currencyCode
else
"usd"
"USD"
}
}

View File

@ -39,8 +39,7 @@ class TransactionsViewModel(application: Application) : AndroidViewModel(applica
}
internal fun getFilteredTransactions(userId: String): LiveData<List<TransferDetail>> {
val currency = Currency.getInstance(Locale.getDefault())
val currencyCode = Helper.getCoingeckoSupportedCurrency(currency.currencyCode)
val currencyCode = Helper.getCoingeckoSupportedCurrency(Locale.getDefault())
transactions = mRepository.getAll(userId, currencyCode)
filteredTransactions.addSource(transactions) { transactions ->

View File

@ -0,0 +1,33 @@
package cy.agorise.bitsybitshareswallet.utils
import org.junit.Test
import org.junit.Assert.*
import java.util.*
class HelperTest {
@Test
fun getCoingeckoSupportedCurrency_InvalidInput_ReturnsUSD() {
val locale = Locale("es")
val currencyCode = Helper.getCoingeckoSupportedCurrency(locale)
assertEquals("USD", currencyCode)
}
@Test
fun getCoingeckoSupportedCurrency_UnsupportedInput_ReturnsUSD() {
val locale = Locale("es", "PE")
val currencyCode = Helper.getCoingeckoSupportedCurrency(locale)
assertEquals("USD", currencyCode)
}
@Test
fun getCoingeckoSupportedCurrency_SupportedInput_ReturnsItself() {
val locale = Locale("es", "MX")
val currencyCode = Helper.getCoingeckoSupportedCurrency(locale)
assertEquals("MXN", currencyCode)
}
}