package net.ktnx.mobileledger.ui.activity;
+import android.annotation.SuppressLint;
+import android.os.Build;
import android.text.Editable;
import android.text.TextWatcher;
+import android.text.method.DigitsKeyListener;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;
import androidx.annotation.NonNull;
-import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.RecyclerView;
import net.ktnx.mobileledger.model.Data;
import net.ktnx.mobileledger.model.LedgerTransactionAccount;
import net.ktnx.mobileledger.model.MobileLedgerProfile;
+import net.ktnx.mobileledger.ui.AutoCompleteTextViewWithClear;
import net.ktnx.mobileledger.ui.DatePickerFragment;
import net.ktnx.mobileledger.utils.Logger;
import net.ktnx.mobileledger.utils.MLDB;
import net.ktnx.mobileledger.utils.Misc;
+import java.text.DecimalFormatSymbols;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
class NewTransactionItemHolder extends RecyclerView.ViewHolder
implements DatePickerFragment.DatePickedListener, DescriptionSelectedCallback {
+ private final String decimalSeparator;
+ private final String decimalDot;
private NewTransactionModel.Item item;
private TextView tvDate;
private AutoCompleteTextView tvDescription;
private AutoCompleteTextView tvAccount;
private TextView tvAmount;
- private ConstraintLayout lHead;
+ private LinearLayout lHead;
private LinearLayout lAccount;
private FrameLayout lPadding;
private MobileLedgerProfile mProfile;
tvAccount.setNextFocusForwardId(View.NO_ID);
tvAmount.setNextFocusForwardId(View.NO_ID); // magic!
- tvDate.setOnFocusChangeListener((v, hasFocus) -> {
- if (hasFocus) pickTransactionDate();
- });
tvDate.setOnClickListener(v -> pickTransactionDate());
mProfile = Data.profile.getValue();
- if (mProfile == null) throw new AssertionError();
+ if (mProfile == null)
+ throw new AssertionError();
+
+ View.OnFocusChangeListener focusMonitor = (v, hasFocus) -> {
+ if (hasFocus) {
+ boolean wasSyncing = syncingData;
+ syncingData = true;
+ try {
+ final int pos = getAdapterPosition();
+ adapter.updateFocusedItem(pos);
+ if (v instanceof AutoCompleteTextViewWithClear) {
+ adapter.noteFocusIsOnAccount(pos);
+ }
+ else {
+ adapter.noteFocusIsOnAmount(pos);
+ }
+ }
+ finally {
+ syncingData = wasSyncing;
+ }
+ }
+ };
+
+ tvDescription.setOnFocusChangeListener(focusMonitor);
+ tvAccount.setOnFocusChangeListener(focusMonitor);
+ tvAmount.setOnFocusChangeListener(focusMonitor);
MLDB.hookAutocompletionAdapter(tvDescription.getContext(), tvDescription,
MLDB.DESCRIPTION_HISTORY_TABLE, "description", false, adapter, mProfile);
MLDB.hookAutocompletionAdapter(tvAccount.getContext(), tvAccount, MLDB.ACCOUNTS_TABLE,
"name", true, this, mProfile);
+ // FIXME: react on configuration (locale) changes
+ decimalSeparator = String.valueOf(DecimalFormatSymbols.getInstance()
+ .getMonetaryDecimalSeparator());
+ decimalDot = ".";
+
final TextWatcher tw = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
@Override
public void afterTextChanged(Editable s) {
// debug("input", "text changed");
- if (inUpdate) return;
+ if (inUpdate)
+ return;
Logger.debug("textWatcher", "calling syncData()");
syncData();
Logger.debug("textWatcher", "done");
}
};
+ final TextWatcher amountWatcher = new TextWatcher() {
+ @Override
+ public void beforeTextChanged(CharSequence s, int start, int count, int after) {
+ }
+ @Override
+ public void onTextChanged(CharSequence s, int start, int before, int count) {
+
+ }
+ @Override
+ public void afterTextChanged(Editable s) {
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
+ // only one decimal separator is allowed
+ // plus and minus are allowed only at the beginning
+ String val = s.toString();
+ if (val.isEmpty())
+ tvAmount.setKeyListener(DigitsKeyListener.getInstance(
+ "0123456789+-" + decimalSeparator + decimalDot));
+ else if (val.contains(decimalSeparator) || val.contains(decimalDot))
+ tvAmount.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
+ else
+ tvAmount.setKeyListener(DigitsKeyListener.getInstance(
+ "0123456789" + decimalSeparator + decimalDot));
+
+ syncData();
+ adapter.model.checkTransactionSubmittable(adapter);
+ }
+ }
+ };
tvDescription.addTextChangedListener(tw);
tvAccount.addTextChangedListener(tw);
- tvAmount.addTextChangedListener(tw);
+ tvAmount.addTextChangedListener(amountWatcher);
+
+ // FIXME: react on locale changes
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
+ tvAmount.setKeyListener(DigitsKeyListener.getInstance(Locale.getDefault(), true, true));
+ else
+ tvAmount.setKeyListener(
+ DigitsKeyListener.getInstance("0123456789+-" + decimalSeparator + decimalDot));
dateObserver = date -> {
- if (syncingData) return;
+ if (syncingData)
+ return;
syncingData = true;
try {
tvDate.setText(item.getFormattedDate());
}
};
descriptionObserver = description -> {
- if (syncingData) return;
+ if (syncingData)
+ return;
syncingData = true;
try {
tvDescription.setText(description);
}
};
hintObserver = hint -> {
- if (syncingData) return;
+ if (syncingData)
+ return;
syncingData = true;
try {
- tvAmount.setHint(hint);
+ if (hint == null)
+ tvAmount.setHint(R.string.zero_amount);
+ else
+ tvAmount.setHint(hint);
}
finally {
syncingData = false;
//if (!tvDate.hasFocus()) tvDate.requestFocus();
boolean focused = tvDescription.requestFocus();
tvDescription.dismissDropDown();
- if (focused) Misc.showSoftKeyboard(
- (NewTransactionActivity) tvDescription.getContext());
+ if (focused)
+ Misc.showSoftKeyboard(
+ (NewTransactionActivity) tvDescription.getContext());
break;
case transactionRow:
- focused = tvAccount.requestFocus();
- tvAccount.dismissDropDown();
- if (focused)
- Misc.showSoftKeyboard((NewTransactionActivity) tvAccount.getContext());
+ // do nothing if a row element already has the focus
+ if (!itemView.hasFocus()) {
+ if (item.focusIsOnAmount()) {
+ tvAmount.requestFocus();
+ }
+ else {
+ focused = tvAccount.requestFocus();
+ tvAccount.dismissDropDown();
+ if (focused)
+ Misc.showSoftKeyboard((NewTransactionActivity) tvAccount.getContext());
+ }
+ }
break;
}
item.getAccount()
.getAmount())
: "unset") : "")));
- if (adapterPosition == count) tvAmount.setImeOptions(EditorInfo.IME_ACTION_DONE);
- else tvAmount.setImeOptions(EditorInfo.IME_ACTION_NEXT);
+ if (adapterPosition == count)
+ tvAmount.setImeOptions(EditorInfo.IME_ACTION_DONE);
+ else
+ tvAmount.setImeOptions(EditorInfo.IME_ACTION_NEXT);
};
}
private void setEditable(Boolean editable) {
tvAmount.setEnabled(editable);
}
private void beginUpdates() {
- if (inUpdate) throw new RuntimeException("Already in update mode");
+ if (inUpdate)
+ throw new RuntimeException("Already in update mode");
inUpdate = true;
}
private void endUpdates() {
- if (!inUpdate) throw new RuntimeException("Not in update mode");
+ if (!inUpdate)
+ throw new RuntimeException("Not in update mode");
inUpdate = false;
}
/**
* Stores the data from the UI elements into the model item
*/
private void syncData() {
- if (item == null) return;
+ if (item == null)
+ return;
if (syncingData) {
Logger.debug("new-trans", "skipping syncData() loop");
String amount = String.valueOf(tvAmount.getText());
amount = amount.trim();
- if (!amount.isEmpty()) {
+ if (amount.isEmpty()) {
+ item.getAccount()
+ .resetAmount();
+ }
+ else {
try {
+ amount = amount.replace(decimalSeparator, decimalDot);
item.getAccount()
.setAmount(Float.parseFloat(amount));
}
catch (NumberFormatException e) {
Logger.debug("new-trans", String.format(
"assuming amount is not set due to number format exception. " +
- "input was '%s'",
- amount));
+ "input was '%s'", amount));
item.getAccount()
.resetAmount();
}
}
- else item.getAccount()
- .resetAmount();
break;
case bottomFiller:
*
* @param item updates the UI elements with the data from the model item
*/
+ @SuppressLint("DefaultLocale")
public void setData(NewTransactionModel.Item item) {
beginUpdates();
try {
case transactionRow:
LedgerTransactionAccount acc = item.getAccount();
tvAccount.setText(acc.getAccountName());
- tvAmount.setText(
- acc.isAmountSet() ? String.format(Locale.US, "%1.2f", acc.getAmount())
- : "");
+ if (acc.isAmountSet()) {
+ tvAmount.setText(String.format("%1.2f", acc.getAmount()));
+ }
+ else {
+ tvAmount.setText("");
+// tvAmount.setHint(R.string.zero_amount);
+ }
+ tvAmount.setHint(item.getAmountHint());
lHead.setVisibility(View.GONE);
lAccount.setVisibility(View.VISIBLE);
lPadding.setVisibility(View.GONE);
c.set(year, month, day);
item.setDate(c.getTime());
boolean focused = tvDescription.requestFocus();
- if (focused) Misc.showSoftKeyboard((NewTransactionActivity) tvAccount.getContext());
+ if (focused)
+ Misc.showSoftKeyboard((NewTransactionActivity) tvAccount.getContext());
}
@Override