package net.ktnx.mobileledger.ui.activity;
-import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.EditorInfo;
-import android.view.inputmethod.InputMethodManager;
import android.widget.AutoCompleteTextView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
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.util.Calendar;
import java.util.Date;
private Observer<String> hintObserver;
private Observer<Integer> focusedAccountObserver;
private Observer<Integer> accountCountObserver;
+ private Observer<Boolean> editableObserver;
private boolean inUpdate = false;
private boolean syncingData = false;
NewTransactionItemHolder(@NonNull View itemView, NewTransactionItemsAdapter adapter) {
tvAmount.setNextFocusForwardId(View.NO_ID); // magic!
tvDate.setOnFocusChangeListener((v, hasFocus) -> {
- if (hasFocus) pickTransactionDate();
+ if (hasFocus)
+ pickTransactionDate();
});
tvDate.setOnClickListener(v -> pickTransactionDate());
mProfile = Data.profile.getValue();
- if (mProfile == null) throw new AssertionError();
+ if (mProfile == null)
+ throw new AssertionError();
MLDB.hookAutocompletionAdapter(tvDescription.getContext(), tvDescription,
MLDB.DESCRIPTION_HISTORY_TABLE, "description", false, adapter, mProfile);
@Override
public void afterTextChanged(Editable s) {
// debug("input", "text changed");
- if (inUpdate) return;
+ if (inUpdate)
+ return;
Logger.debug("textWatcher", "calling syncData()");
syncData();
tvAmount.addTextChangedListener(tw);
dateObserver = date -> {
- if (syncingData) return;
- tvDate.setText(item.getFormattedDate());
+ if (syncingData)
+ return;
+ syncingData = true;
+ try {
+ tvDate.setText(item.getFormattedDate());
+ }
+ finally {
+ syncingData = false;
+ }
};
descriptionObserver = description -> {
- if (syncingData) return;
- tvDescription.setText(description);
+ if (syncingData)
+ return;
+ syncingData = true;
+ try {
+ tvDescription.setText(description);
+ }
+ finally {
+ syncingData = false;
+ }
};
hintObserver = hint -> {
- if (syncingData) return;
- tvAmount.setHint(hint);
+ if (syncingData)
+ return;
+ syncingData = true;
+ try {
+ tvAmount.setHint(hint);
+ }
+ finally {
+ syncingData = false;
+ }
};
+ editableObserver = this::setEditable;
focusedAccountObserver = index -> {
if ((index != null) && index.equals(getAdapterPosition())) {
switch (item.getType()) {
case generalData:
- tvDate.requestFocus();
+ // bad idea - double pop-up, and not really necessary.
+ // the user can tap the input to get the calendar
+ //if (!tvDate.hasFocus()) tvDate.requestFocus();
+ boolean focused = tvDescription.requestFocus();
+ tvDescription.dismissDropDown();
+ if (focused)
+ Misc.showSoftKeyboard(
+ (NewTransactionActivity) tvDescription.getContext());
break;
case transactionRow:
- tvAccount.requestFocus();
+ focused = tvAccount.requestFocus();
tvAccount.dismissDropDown();
- tvAccount.selectAll();
+ if (focused)
+ Misc.showSoftKeyboard((NewTransactionActivity) tvAccount.getContext());
+
break;
}
}
};
accountCountObserver = count -> {
- if (getAdapterPosition() == count) tvAmount.setImeOptions(EditorInfo.IME_ACTION_DONE);
- else tvAmount.setImeOptions(EditorInfo.IME_ACTION_NEXT);
+ final int adapterPosition = getAdapterPosition();
+ final int layoutPosition = getLayoutPosition();
+ Logger.debug("holder",
+ String.format(Locale.US, "count=%d; pos=%d, layoutPos=%d [%s]", count,
+ adapterPosition, layoutPosition, item.getType()
+ .toString()
+ .concat(item.getType() ==
+ NewTransactionModel.ItemType.transactionRow
+ ? String.format(Locale.US,
+ "'%s'=%s",
+ item.getAccount()
+ .getAccountName(),
+ item.getAccount()
+ .isAmountSet()
+ ? String.format(Locale.US,
+ "%.2f",
+ item.getAccount()
+ .getAmount())
+ : "unset") : "")));
+ if (adapterPosition == count)
+ tvAmount.setImeOptions(EditorInfo.IME_ACTION_DONE);
+ else
+ tvAmount.setImeOptions(EditorInfo.IME_ACTION_NEXT);
};
}
+ private void setEditable(Boolean editable) {
+ tvDate.setEnabled(editable);
+ tvDescription.setEnabled(editable);
+ tvAccount.setEnabled(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()) item.getAccount()
- .setAmount(Float.parseFloat(amount));
- else item.getAccount()
- .resetAmount();
+ if (!amount.isEmpty()) {
+ try {
+ 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));
+ item.getAccount()
+ .resetAmount();
+ }
+ }
+ else
+ item.getAccount()
+ .resetAmount();
break;
case bottomFiller:
this.item.stopObservingDate(dateObserver);
this.item.stopObservingDescription(descriptionObserver);
this.item.stopObservingAmountHint(hintObserver);
+ this.item.stopObservingEditableFlag(editableObserver);
this.item.getModel()
.stopObservingFocusedItem(focusedAccountObserver);
this.item.getModel()
lHead.setVisibility(View.VISIBLE);
lAccount.setVisibility(View.GONE);
lPadding.setVisibility(View.GONE);
+ setEditable(true);
break;
case transactionRow:
LedgerTransactionAccount acc = item.getAccount();
lHead.setVisibility(View.GONE);
lAccount.setVisibility(View.VISIBLE);
lPadding.setVisibility(View.GONE);
+ setEditable(true);
break;
case bottomFiller:
lHead.setVisibility(View.GONE);
lAccount.setVisibility(View.GONE);
lPadding.setVisibility(View.VISIBLE);
+ setEditable(false);
break;
}
item.observeDate(activity, dateObserver);
item.observeDescription(activity, descriptionObserver);
item.observeAmountHint(activity, hintObserver);
+ item.observeEditableFlag(activity, editableObserver);
item.getModel()
.observeFocusedItem(activity, focusedAccountObserver);
item.getModel()
final Calendar c = GregorianCalendar.getInstance();
c.set(year, month, day);
item.setDate(c.getTime());
- boolean tookFocus = tvDescription.requestFocus();
- if (tookFocus) {
- // make the keyboard appear
- InputMethodManager imm = (InputMethodManager) tvDate.getContext()
- .getSystemService(
- Context.INPUT_METHOD_SERVICE);
- imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
- }
+ boolean focused = tvDescription.requestFocus();
+ if (focused)
+ Misc.showSoftKeyboard((NewTransactionActivity) tvAccount.getContext());
+
}
@Override
public void descriptionSelected(String description) {