From 593d0307aeb6e151c4f693697d7746a3f4c929e8 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Wed, 13 Nov 2019 23:36:52 +0200 Subject: [PATCH] new transaction: locale-aware number formatting (broken in RecyclerView migration) --- .../ui/activity/NewTransactionItemHolder.java | 8 ++++++-- .../ui/activity/NewTransactionModel.java | 15 ++++++++------- app/src/main/res/layout/new_transaction_row.xml | 5 ++--- app/src/main/res/values-bg/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java index a5a4541f..123b91e9 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java @@ -17,6 +17,7 @@ package net.ktnx.mobileledger.ui.activity; +import android.annotation.SuppressLint; import android.text.Editable; import android.text.TextWatcher; import android.view.View; @@ -149,6 +150,9 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder return; syncingData = true; try { + if (hint == null) + hint = tvAmount.getResources() + .getString(R.string.zero_amount); tvAmount.setHint(hint); } finally { @@ -289,6 +293,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder * * @param item updates the UI elements with the data from the model item */ + @SuppressLint("DefaultLocale") public void setData(NewTransactionModel.Item item) { beginUpdates(); try { @@ -318,8 +323,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder LedgerTransactionAccount acc = item.getAccount(); tvAccount.setText(acc.getAccountName()); tvAmount.setText( - acc.isAmountSet() ? String.format(Locale.US, "%1.2f", acc.getAmount()) - : ""); + acc.isAmountSet() ? String.format("%1.2f", acc.getAmount()) : ""); lHead.setVisibility(View.GONE); lAccount.setVisibility(View.VISIBLE); lPadding.setVisibility(View.GONE); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java index bbfedcbc..914a478b 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java @@ -17,6 +17,8 @@ package net.ktnx.mobileledger.ui.activity; +import android.annotation.SuppressLint; + import androidx.annotation.NonNull; import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; @@ -43,7 +45,6 @@ public class NewTransactionModel extends ViewModel { static final Pattern reYMD = Pattern.compile("^\\s*(\\d+)\\d*/\\s*(\\d+)\\s*/\\s*(\\d+)\\s*$"); static final Pattern reMD = Pattern.compile("^\\s*(\\d+)\\s*/\\s*(\\d+)\\s*$"); static final Pattern reD = Pattern.compile("\\s*(\\d+)\\s*$"); - private static final String ZERO_AMOUNT_HINT = "0.00"; private final Item header = new Item(this, null, ""); private final Item trailer = new Item(this); private final ArrayList items = new ArrayList<>(); @@ -129,6 +130,7 @@ public class NewTransactionModel extends ViewModel { // 3a) there must be exactly one empty amount // 4) empty accounts with empty amounts are ignored // 5) a row with an empty account name or empty amount is guaranteed to exist + @SuppressLint("DefaultLocale") public void checkTransactionSubmittable(NewTransactionItemsAdapter adapter) { int accounts = 0; int accounts_with_values = 0; @@ -191,18 +193,17 @@ public class NewTransactionModel extends ViewModel { if (single_empty_amount) { if (item.equals(empty_amount)) { - empty_amount.setAmountHint(String.format(Locale.US, "%1.2f", - Misc.isZero(running_total) ? 0f : -running_total)); + empty_amount.setAmountHint(Misc.isZero(running_total) ? null + : String.format( + "%1.2f", + -running_total)); continue; } } else { // no single empty account and this account's amount is not set // => hint should be '0.00' - String hint = item.getAmountHint(); - if ((hint == null) || !hint.equals(ZERO_AMOUNT_HINT)) { - item.setAmountHint(ZERO_AMOUNT_HINT); - } + item.setAmountHint(null); } } diff --git a/app/src/main/res/layout/new_transaction_row.xml b/app/src/main/res/layout/new_transaction_row.xml index da151420..707c7b0d 100644 --- a/app/src/main/res/layout/new_transaction_row.xml +++ b/app/src/main/res/layout/new_transaction_row.xml @@ -99,12 +99,11 @@ android:width="0dp" android:foregroundGravity="bottom" android:gravity="bottom|end" - android:hint="0.00" - android:inputType="numberSigned|numberDecimal" + android:hint="@string/zero_amount" + android:inputType="numberSigned|numberDecimal|number" android:minWidth="70sp" android:textAlignment="viewEnd" android:selectAllOnFocus="true" - tools:ignore="HardcodedText" android:imeOptions="actionNext"/> diff --git a/app/src/main/res/values-bg/strings.xml b/app/src/main/res/values-bg/strings.xml index f73bd94d..be9004e3 100644 --- a/app/src/main/res/values-bg/strings.xml +++ b/app/src/main/res/values-bg/strings.xml @@ -112,5 +112,6 @@ Грешен или непълен адрес Бутон за избор на цвят ВНИМАНИЕ: Използване на удостоверяване с несигурна схема на достъп + 0,00 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index bdfbf3e7..837daf77 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -154,4 +154,5 @@ Invalid URL Color picker button WARNING: Insecure http used with authentication + 0.00 -- 2.39.5