BlacKCaT27 / CurrencyEditText

A module designed to encapsulate the use of an Android EditText field for gathering currency information from a user. Supports all ISO-3166 compliant locales/currencies.
Apache License 2.0
339 stars 79 forks source link

Setting new currency does nothing #27

Closed samuelili closed 7 years ago

samuelili commented 7 years ago

I noticed that when calling setCurrency, it'll set it back to the default currency. It's because setCurrency calls init, which calls initCurrency and sets the currency back to the default locale. Basically, trying to set a currency that is not the default currency is impossible because it would set itself back to the default currency. I can't set a locale because I only have a currency.

myCurrencyEditText.setCurrency(myCurrency); // calling this

public void setCurrency(Currency currency) {
  this.currency = currency;

  init(); // goes here
  updateHint();
}

private void init() {
  initCurrency(); // then here
  initCurrencyTextWatcher();
}

private void initCurrency() {
  try {
    currency = Currency.getInstance(locale); // effectively sets it back to the default currency
  } catch (IllegalArgumentException e) {
    currency = Currency.getInstance(defaultLocale);
  }
}
BlacKCaT27 commented 7 years ago

Hm. I'll take a look. I'm planning on doing a deep dive into this code hopefully this weekend. It's been too long since I've made any updates and there's a handful of issues I need to address (as well as refactor all this old code which to current-me seems to have quite a bit of smell in a few places). I'll be sure this makes my to-do list. Thanks for the report.

BlacKCaT27 commented 7 years ago

It's going to take me a bit longer than anticipated as there's quite a bit here that needs to be cleaned up, but this issue specifically I've already resolved. Hope to have a new version out by the end of the week.

samuelili commented 7 years ago

Thanks a lot. Glad to see the project is still being worked on.

BlacKCaT27 commented 7 years ago

Version 2.0 was just released.

This issue is technically 'resolved' in the sense that setCurrency() no longer suffers from this problem because it no longer exists :)

You can now directly set the decimalDigits value used by the formatter by calling setDecimalDigits() on the CurrencyEditText object or by setting the decimal_digits attribute in the XML layout.

Aside from the hint value (which you can set manually), currency was only used for setting the decimal digits value, so exposing the decimal digits value abstracted from the currency class made sense to help hide implementation details. If there's another reason you still want to set the underlying currency class directly, let me know and I'll take a look.

samuelili commented 7 years ago

In my application, I need to have the user to be able to input different currencies, not tied to the users locale. If it's possible, I would still like to be able to set the currency with a simple method.

BlacKCaT27 commented 7 years ago

What features that currency provides are you looking for from the module?

Currently, there are only two things we do with a currency object: obtain the default number of decimal places, and set the hint text with the currencys symbol. Both of things are provided via the API already, and maintaining the various states of "did a caller set their own currency" vs us having to make our own just added needless complexity to the library.

perhaps if you can explain your use case in more detail I can understand better, but there's literally nothing we do with currency besides those two things.