import android.view.Gravity;
import android.view.View;
import android.view.inputmethod.EditorInfo;
+import android.widget.EditText;
+import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
-import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
-import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.RecyclerView;
import net.ktnx.mobileledger.R;
import net.ktnx.mobileledger.async.DescriptionSelectedCallback;
import net.ktnx.mobileledger.databinding.NewTransactionRowBinding;
+import net.ktnx.mobileledger.db.AccountAutocompleteAdapter;
import net.ktnx.mobileledger.model.Currency;
import net.ktnx.mobileledger.model.Data;
-import net.ktnx.mobileledger.model.LedgerTransactionAccount;
import net.ktnx.mobileledger.model.MobileLedgerProfile;
import net.ktnx.mobileledger.ui.CurrencySelectorFragment;
import net.ktnx.mobileledger.ui.DatePickerFragment;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
-import java.util.Date;
-import java.util.Locale;
-
-import static net.ktnx.mobileledger.ui.new_transaction.NewTransactionModel.ItemType;
+import java.util.Objects;
class NewTransactionItemHolder extends RecyclerView.ViewHolder
implements DatePickerFragment.DatePickedListener, DescriptionSelectedCallback {
- private final String decimalDot;
- private final Observer<Boolean> showCommentsObserver;
+ private final String decimalDot = ".";
private final MobileLedgerProfile mProfile;
- private final Observer<SimpleDate> dateObserver;
- private final Observer<String> descriptionObserver;
- private final Observer<String> transactionCommentObserver;
- private final Observer<String> hintObserver;
- private final Observer<Integer> focusedAccountObserver;
- private final Observer<Integer> accountCountObserver;
- private final Observer<Boolean> editableObserver;
- private final Observer<Currency.Position> currencyPositionObserver;
- private final Observer<Boolean> currencyGapObserver;
- private final Observer<Locale> localeObserver;
- private final Observer<Currency> currencyObserver;
- private final Observer<Boolean> showCurrencyObserver;
- private final Observer<String> commentObserver;
- private final Observer<Boolean> amountValidityObserver;
private final NewTransactionRowBinding b;
+ private final NewTransactionItemsAdapter mAdapter;
+ private boolean ignoreFocusChanges = false;
private String decimalSeparator;
- private NewTransactionModel.Item item;
- private Date date;
private boolean inUpdate = false;
private boolean syncingData = false;
- //TODO multiple amounts with different currencies per posting
+ //TODO multiple amounts with different currencies per posting?
NewTransactionItemHolder(@NonNull NewTransactionRowBinding b,
NewTransactionItemsAdapter adapter) {
super(b.getRoot());
this.b = b;
+ this.mAdapter = adapter;
new TextViewClearHelper().attachToTextView(b.comment);
b.newTransactionDescription.setNextFocusForwardId(View.NO_ID);
mProfile = Data.getProfile();
- View.OnFocusChangeListener focusMonitor = (v, hasFocus) -> {
+ @SuppressLint("DefaultLocale") View.OnFocusChangeListener focusMonitor = (v, hasFocus) -> {
final int id = v.getId();
if (hasFocus) {
boolean wasSyncing = syncingData;
syncingData = true;
try {
final int pos = getAdapterPosition();
- adapter.updateFocusedItem(pos);
if (id == R.id.account_row_acc_name) {
adapter.noteFocusIsOnAccount(pos);
}
else if (id == R.id.new_transaction_description) {
adapter.noteFocusIsOnDescription(pos);
}
+ else
+ throw new IllegalStateException("Where is the focus?");
}
finally {
syncingData = wasSyncing;
}
}
+ else { // lost focus
+ if (id == R.id.account_row_acc_amounts) {
+ try {
+ String input = String.valueOf(b.accountRowAccAmounts.getText());
+ input = input.replace(decimalSeparator, decimalDot);
+ b.accountRowAccAmounts.setText(
+ String.format("%4.2f", Float.parseFloat(input)));
+ }
+ catch (NumberFormatException ex) {
+ // ignored
+ }
+ }
+ }
if (id == R.id.comment) {
commentFocusChanged(b.comment, hasFocus);
b.comment.setOnFocusChangeListener(focusMonitor);
b.transactionComment.setOnFocusChangeListener(focusMonitor);
- MLDB.hookAutocompletionAdapter(b.getRoot()
- .getContext(), b.newTransactionDescription,
- MLDB.DESCRIPTION_HISTORY_TABLE, "description", false, adapter, mProfile);
- MLDB.hookAutocompletionAdapter(b.getRoot()
- .getContext(), b.accountRowAccName, MLDB.ACCOUNTS_TABLE,
- "name", true, this, mProfile);
+ NewTransactionActivity activity = (NewTransactionActivity) b.getRoot()
+ .getContext();
- decimalSeparator = String.valueOf(DecimalFormatSymbols.getInstance()
- .getMonetaryDecimalSeparator());
- localeObserver = locale -> decimalSeparator = String.valueOf(
- DecimalFormatSymbols.getInstance(locale)
- .getMonetaryDecimalSeparator());
+ MLDB.hookAutocompletionAdapter(activity, b.newTransactionDescription,
+ MLDB.DESCRIPTION_HISTORY_TABLE, "description", false, activity, mProfile);
+ b.accountRowAccName.setAdapter(new AccountAutocompleteAdapter(b.getRoot()
+ .getContext(), mProfile));
- decimalDot = ".";
+ decimalSeparator = "";
+ Data.locale.observe(activity, locale -> decimalSeparator = String.valueOf(
+ DecimalFormatSymbols.getInstance(locale)
+ .getMonetaryDecimalSeparator()));
final TextWatcher tw = new TextWatcher() {
@Override
return;
Logger.debug("textWatcher", "calling syncData()");
- syncData();
- Logger.debug("textWatcher",
- "syncData() returned, checking if transaction is submittable");
- adapter.checkTransactionSubmittable();
+ if (syncData()) {
+ Logger.debug("textWatcher",
+ "syncData() returned, checking if transaction is submittable");
+ adapter.model.checkTransactionSubmittable(null);
+ }
Logger.debug("textWatcher", "done");
}
};
final TextWatcher amountWatcher = new TextWatcher() {
@Override
- public void beforeTextChanged(CharSequence s, int start, int count, int after) {
- Logger.debug("num",
- String.format(Locale.US, "beforeTextChanged: start=%d, count=%d, after=%d",
- start, count, after));
- }
+ 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) {
+ checkAmountValid(s.toString());
if (syncData())
- adapter.checkTransactionSubmittable();
+ adapter.model.checkTransactionSubmittable(null);
}
};
b.newTransactionDescription.addTextChangedListener(tw);
- b.transactionComment.addTextChangedListener(tw);
+ monitorComment(b.transactionComment);
b.accountRowAccName.addTextChangedListener(tw);
- b.comment.addTextChangedListener(tw);
+ monitorComment(b.comment);
b.accountRowAccAmounts.addTextChangedListener(amountWatcher);
b.currencyButton.setOnClickListener(v -> {
CurrencySelectorFragment cpf = new CurrencySelectorFragment();
cpf.showPositionAndPadding();
- cpf.setOnCurrencySelectedListener(c -> item.setCurrency(c));
- final AppCompatActivity activity = (AppCompatActivity) v.getContext();
+ cpf.setOnCurrencySelectedListener(
+ c -> adapter.setItemCurrency(getAdapterPosition(), c.getName()));
cpf.show(activity.getSupportFragmentManager(), "currency-selector");
});
- dateObserver = date -> {
- if (syncingData)
- return;
- syncingData = true;
- try {
- b.newTransactionDate.setText(item.getFormattedDate());
- }
- finally {
- syncingData = false;
- }
- };
- descriptionObserver = description -> {
- if (syncingData)
- return;
- syncingData = true;
- try {
- b.newTransactionDescription.setText(description);
- }
- finally {
- syncingData = false;
- }
- };
- transactionCommentObserver = transactionComment -> {
- final View focusedView = b.transactionComment.findFocus();
- b.transactionComment.setTypeface(null,
- (focusedView == b.transactionComment) ? Typeface.NORMAL : Typeface.ITALIC);
- b.transactionComment.setVisibility(
- ((focusedView != b.transactionComment) && TextUtils.isEmpty(transactionComment))
- ? View.INVISIBLE : View.VISIBLE);
-
- };
- hintObserver = hint -> {
- if (syncingData)
- return;
- syncingData = true;
- try {
- if (hint == null)
- b.accountRowAccAmounts.setHint(R.string.zero_amount);
- else
- b.accountRowAccAmounts.setHint(hint);
- }
- finally {
- syncingData = false;
- }
- };
- editableObserver = this::setEditable;
commentFocusChanged(b.transactionComment, false);
commentFocusChanged(b.comment, false);
- focusedAccountObserver = index -> {
- if ((index == null) || !index.equals(getAdapterPosition()) || itemView.hasFocus())
+
+ adapter.model.getFocusInfo()
+ .observe(activity, this::applyFocus);
+
+ Data.currencyGap.observe(activity,
+ hasGap -> updateCurrencyPositionAndPadding(Data.currencySymbolPosition.getValue(),
+ hasGap));
+
+ Data.currencySymbolPosition.observe(activity,
+ position -> updateCurrencyPositionAndPadding(position,
+ Data.currencyGap.getValue()));
+
+ adapter.model.getShowCurrency()
+ .observe(activity, showCurrency -> {
+ if (showCurrency) {
+ b.currency.setVisibility(View.VISIBLE);
+ b.currencyButton.setVisibility(View.VISIBLE);
+ b.currency.setText(mProfile.getDefaultCommodity());
+ }
+ else {
+ b.currency.setVisibility(View.GONE);
+ b.currencyButton.setVisibility(View.GONE);
+ b.currency.setText(null);
+ }
+ });
+
+ adapter.model.getShowComments()
+ .observe(activity, show -> {
+ ConstraintLayout.LayoutParams amountLayoutParams =
+ (ConstraintLayout.LayoutParams) b.amountLayout.getLayoutParams();
+ ConstraintLayout.LayoutParams accountParams =
+ (ConstraintLayout.LayoutParams) b.accountRowAccName.getLayoutParams();
+
+ if (show) {
+ accountParams.endToStart = ConstraintLayout.LayoutParams.UNSET;
+ accountParams.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
+
+ amountLayoutParams.topToTop = ConstraintLayout.LayoutParams.UNSET;
+ amountLayoutParams.topToBottom = b.accountRowAccName.getId();
+
+ b.commentLayout.setVisibility(View.VISIBLE);
+ }
+ else {
+ accountParams.endToStart = b.amountLayout.getId();
+ accountParams.endToEnd = ConstraintLayout.LayoutParams.UNSET;
+
+ amountLayoutParams.topToBottom = ConstraintLayout.LayoutParams.UNSET;
+ amountLayoutParams.topToTop = ConstraintLayout.LayoutParams.PARENT_ID;
+
+ b.commentLayout.setVisibility(View.GONE);
+ }
+
+ b.accountRowAccName.setLayoutParams(accountParams);
+ b.amountLayout.setLayoutParams(amountLayoutParams);
+
+ b.transactionCommentLayout.setVisibility(show ? View.VISIBLE : View.GONE);
+ });
+ }
+ private void applyFocus(NewTransactionModel.FocusInfo focusInfo) {
+ if (ignoreFocusChanges) {
+ Logger.debug("new-trans", "Ignoring focus change");
+ return;
+ }
+ ignoreFocusChanges = true;
+ try {
+ if (((focusInfo == null) || (focusInfo.element == null) ||
+ focusInfo.position != getAdapterPosition()))
return;
- switch (item.getType()) {
- case generalData:
- // bad idea - double pop-up, and not really necessary.
- // the user can tap the input to get the calendar
- //if (!tvDate.hasFocus()) tvDate.requestFocus();
- switch (item.getFocusedElement()) {
- case TransactionComment:
- b.transactionComment.setVisibility(View.VISIBLE);
- b.transactionComment.requestFocus();
- break;
- case Description:
- boolean focused = b.newTransactionDescription.requestFocus();
+ NewTransactionModel.Item item = getItem();
+ if (item instanceof NewTransactionModel.TransactionHead) {
+ NewTransactionModel.TransactionHead head = item.toTransactionHead();
+ // bad idea - double pop-up, and not really necessary.
+ // the user can tap the input to get the calendar
+ //if (!tvDate.hasFocus()) tvDate.requestFocus();
+ switch (focusInfo.element) {
+ case TransactionComment:
+ b.transactionComment.setVisibility(View.VISIBLE);
+ b.transactionComment.requestFocus();
+ break;
+ case Description:
+ boolean focused = b.newTransactionDescription.requestFocus();
// tvDescription.dismissDropDown();
- if (focused)
- Misc.showSoftKeyboard((NewTransactionActivity) b.getRoot()
- .getContext());
- break;
- }
- break;
- case transactionRow:
- switch (item.getFocusedElement()) {
- case Amount:
- b.accountRowAccAmounts.requestFocus();
- break;
- case Comment:
- b.comment.setVisibility(View.VISIBLE);
- b.comment.requestFocus();
- break;
- case Account:
- boolean focused = b.accountRowAccName.requestFocus();
- b.accountRowAccName.dismissDropDown();
- if (focused)
- Misc.showSoftKeyboard((NewTransactionActivity) b.getRoot()
- .getContext());
- break;
- }
-
- break;
+ if (focused)
+ Misc.showSoftKeyboard((NewTransactionActivity) b.getRoot()
+ .getContext());
+ break;
+ }
}
- };
- accountCountObserver = count -> {
- 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() ==
- 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)
- b.accountRowAccAmounts.setImeOptions(EditorInfo.IME_ACTION_DONE);
- else
- b.accountRowAccAmounts.setImeOptions(EditorInfo.IME_ACTION_NEXT);
- };
-
- currencyObserver = currency -> {
- setCurrency(currency);
- adapter.checkTransactionSubmittable();
- };
-
- currencyGapObserver =
- hasGap -> updateCurrencyPositionAndPadding(Data.currencySymbolPosition.getValue(),
- hasGap);
-
- currencyPositionObserver =
- position -> updateCurrencyPositionAndPadding(position, Data.currencyGap.getValue());
-
- showCurrencyObserver = showCurrency -> {
- if (showCurrency) {
- b.currency.setVisibility(View.VISIBLE);
- b.currencyButton.setVisibility(View.VISIBLE);
- String defaultCommodity = mProfile.getDefaultCommodity();
- item.setCurrency(
- (defaultCommodity == null) ? null : Currency.loadByName(defaultCommodity));
+ else if (item instanceof NewTransactionModel.TransactionAccount) {
+ NewTransactionModel.TransactionAccount acc = item.toTransactionAccount();
+ switch (focusInfo.element) {
+ case Amount:
+ b.accountRowAccAmounts.requestFocus();
+ break;
+ case Comment:
+ b.comment.setVisibility(View.VISIBLE);
+ b.comment.requestFocus();
+ break;
+ case Account:
+ boolean focused = b.accountRowAccName.requestFocus();
+// b.accountRowAccName.dismissDropDown();
+ if (focused)
+ Misc.showSoftKeyboard((NewTransactionActivity) b.getRoot()
+ .getContext());
+ break;
+ }
}
- else {
- b.currency.setVisibility(View.GONE);
- b.currencyButton.setVisibility(View.GONE);
- item.setCurrency(null);
+ }
+ finally {
+ ignoreFocusChanges = false;
+ }
+ }
+ public void checkAmountValid(String s) {
+ boolean valid = true;
+ try {
+ if (s.length() > 0) {
+ float ignored = Float.parseFloat(s.replace(decimalSeparator, decimalDot));
}
- };
-
- commentObserver = comment -> {
- final View focusedView = b.comment.findFocus();
- b.comment.setTypeface(null,
- (focusedView == b.comment) ? Typeface.NORMAL : Typeface.ITALIC);
- b.comment.setVisibility(
- ((focusedView != b.comment) && TextUtils.isEmpty(comment)) ? View.INVISIBLE
- : View.VISIBLE);
- };
-
- showCommentsObserver = show -> {
- ConstraintLayout.LayoutParams amountLayoutParams =
- (ConstraintLayout.LayoutParams) b.amountLayout.getLayoutParams();
- ConstraintLayout.LayoutParams accountParams =
- (ConstraintLayout.LayoutParams) b.accountRowAccName.getLayoutParams();
- if (show) {
- accountParams.endToStart = ConstraintLayout.LayoutParams.UNSET;
- accountParams.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
-
- amountLayoutParams.topToTop = ConstraintLayout.LayoutParams.UNSET;
- amountLayoutParams.topToBottom = b.accountRowAccName.getId();
+ }
+ catch (NumberFormatException ex) {
+ valid = false;
+ }
- b.commentLayout.setVisibility(View.VISIBLE);
+ displayAmountValidity(valid);
+ }
+ private void displayAmountValidity(boolean valid) {
+ b.accountRowAccAmounts.setCompoundDrawablesRelativeWithIntrinsicBounds(
+ valid ? 0 : R.drawable.ic_error_outline_black_24dp, 0, 0, 0);
+ b.accountRowAccAmounts.setMinEms(valid ? 4 : 5);
+ }
+ private void monitorComment(EditText editText) {
+ editText.addTextChangedListener(new TextWatcher() {
+ @Override
+ public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
- else {
- accountParams.endToStart = b.amountLayout.getId();
- accountParams.endToEnd = ConstraintLayout.LayoutParams.UNSET;
-
- amountLayoutParams.topToBottom = ConstraintLayout.LayoutParams.UNSET;
- amountLayoutParams.topToTop = ConstraintLayout.LayoutParams.PARENT_ID;
-
- b.commentLayout.setVisibility(View.GONE);
+ @Override
+ public void onTextChanged(CharSequence s, int start, int before, int count) {
}
+ @Override
+ public void afterTextChanged(Editable s) {
+// debug("input", "text changed");
+ if (inUpdate)
+ return;
- b.accountRowAccName.setLayoutParams(accountParams);
- b.amountLayout.setLayoutParams(amountLayoutParams);
-
- b.transactionCommentLayout.setVisibility(show ? View.VISIBLE : View.GONE);
- };
-
- amountValidityObserver = valid -> {
- b.accountRowAccAmounts.setCompoundDrawablesRelativeWithIntrinsicBounds(
- valid ? 0 : R.drawable.ic_error_outline_black_24dp, 0, 0, 0);
- b.accountRowAccAmounts.setMinEms(valid ? 4 : 5);
- };
+ Logger.debug("textWatcher", "calling syncData()");
+ syncData();
+ Logger.debug("textWatcher",
+ "syncData() returned, checking if transaction is submittable");
+ styleComment(editText, s.toString());
+ Logger.debug("textWatcher", "done");
+ }
+ });
}
private void commentFocusChanged(TextView textView, boolean hasFocus) {
@ColorInt int textColor;
* checked for being submittable
*/
private boolean syncData() {
- if (item == null)
- return false;
-
if (syncingData) {
Logger.debug("new-trans", "skipping syncData() loop");
return false;
}
+ NewTransactionModel.Item item = getItem();
+
syncingData = true;
+ boolean significantChange = false;
+
try {
- switch (item.getType()) {
- case generalData:
- item.setDate(String.valueOf(b.newTransactionDate.getText()));
- item.setDescription(String.valueOf(b.newTransactionDescription.getText()));
- item.setTransactionComment(String.valueOf(b.transactionComment.getText()));
- break;
- case transactionRow:
- final LedgerTransactionAccount account = item.getAccount();
- account.setAccountName(String.valueOf(b.accountRowAccName.getText()));
-
- item.setComment(String.valueOf(b.comment.getText()));
-
- String amount = String.valueOf(b.accountRowAccAmounts.getText());
- amount = amount.trim();
-
- if (amount.isEmpty()) {
- account.resetAmount();
- item.validateAmount();
+ if (item instanceof NewTransactionModel.TransactionHead) {
+ NewTransactionModel.TransactionHead head = item.toTransactionHead();
+
+ head.setDate(String.valueOf(b.newTransactionDate.getText()));
+
+ // transaction description is required
+ if (TextUtils.isEmpty(head.getDescription()) !=
+ TextUtils.isEmpty(b.newTransactionDescription.getText()))
+ significantChange = true;
+
+ head.setDescription(String.valueOf(b.newTransactionDescription.getText()));
+ head.setComment(String.valueOf(b.transactionComment.getText()));
+ }
+ else if (item instanceof NewTransactionModel.TransactionAccount) {
+ NewTransactionModel.TransactionAccount acc = item.toTransactionAccount();
+
+ // having account name is important
+ if (TextUtils.isEmpty(acc.getAccountName()) !=
+ TextUtils.isEmpty(b.accountRowAccName.getText()))
+ significantChange = true;
+
+ acc.setAccountName(String.valueOf(b.accountRowAccName.getText()));
+
+ acc.setComment(String.valueOf(b.comment.getText()));
+
+ String amount = String.valueOf(b.accountRowAccAmounts.getText());
+ amount = amount.trim();
+
+ if (amount.isEmpty()) {
+ if (acc.isAmountSet())
+ significantChange = true;
+ acc.resetAmount();
+ acc.setAmountValid(true);
+ }
+ else {
+ try {
+ amount = amount.replace(decimalSeparator, decimalDot);
+ final float parsedAmount = Float.parseFloat(amount);
+ if (!acc.isAmountSet() || !Misc.equalFloats(parsedAmount, acc.getAmount()))
+ significantChange = true;
+ acc.setAmount(parsedAmount);
+ acc.setAmountValid(true);
}
- else {
- try {
- amount = amount.replace(decimalSeparator, decimalDot);
- account.setAmount(Float.parseFloat(amount));
- item.validateAmount();
- }
- catch (NumberFormatException e) {
- Logger.debug("new-trans", String.format(
- "assuming amount is not set due to number format exception. " +
- "input was '%s'", amount));
- account.invalidateAmount();
- item.invalidateAmount();
- }
- final String curr = String.valueOf(b.currency.getText());
- if (curr.equals(b.currency.getContext()
- .getResources()
- .getString(R.string.currency_symbol)) ||
- curr.isEmpty())
- account.setCurrency(null);
- else
- account.setCurrency(curr);
+ catch (NumberFormatException e) {
+ Logger.debug("new-trans", String.format(
+ "assuming amount is not set due to number format exception. " +
+ "input was '%s'", amount));
+ if (acc.isAmountValid())
+ significantChange = true;
+ acc.setAmountValid(false);
}
-
- break;
- case bottomFiller:
- throw new RuntimeException("Should not happen");
+ final String curr = String.valueOf(b.currency.getText());
+ final String currValue;
+ if (curr.equals(b.currency.getContext()
+ .getResources()
+ .getString(R.string.currency_symbol)) ||
+ curr.isEmpty())
+ currValue = null;
+ else
+ currValue = curr;
+
+ if (!significantChange && !TextUtils.equals(acc.getCurrency(), currValue))
+ significantChange = true;
+ acc.setCurrency(currValue);
+ }
+ }
+ else {
+ throw new RuntimeException("Should not happen");
}
- return true;
+ return significantChange;
}
catch (ParseException e) {
throw new RuntimeException("Should not happen", e);
.getContext()).getSupportFragmentManager(), null);
}
/**
- * setData
+ * bind
*
* @param item updates the UI elements with the data from the model item
*/
@SuppressLint("DefaultLocale")
- public void setData(NewTransactionModel.Item item) {
+ public void bind(@NonNull NewTransactionModel.Item item) {
beginUpdates();
try {
- if (this.item != null && !this.item.equals(item)) {
- this.item.stopObservingDate(dateObserver);
- this.item.stopObservingDescription(descriptionObserver);
- this.item.stopObservingTransactionComment(transactionCommentObserver);
- this.item.stopObservingAmountHint(hintObserver);
- this.item.stopObservingEditableFlag(editableObserver);
- this.item.getModel()
- .stopObservingFocusedItem(focusedAccountObserver);
- this.item.getModel()
- .stopObservingAccountCount(accountCountObserver);
- Data.currencySymbolPosition.removeObserver(currencyPositionObserver);
- Data.currencyGap.removeObserver(currencyGapObserver);
- Data.locale.removeObserver(localeObserver);
- this.item.stopObservingCurrency(currencyObserver);
- this.item.getModel().showCurrency.removeObserver(showCurrencyObserver);
- this.item.stopObservingComment(commentObserver);
- this.item.getModel().showComments.removeObserver(showCommentsObserver);
- this.item.stopObservingAmountValidity(amountValidityObserver);
-
- this.item = null;
- }
+ syncingData = true;
+ try {
+ if (item instanceof NewTransactionModel.TransactionHead) {
+ NewTransactionModel.TransactionHead head = item.toTransactionHead();
+ b.newTransactionDate.setText(head.getFormattedDate());
+
+ // avoid triggering completion pop-up
+ SimpleCursorAdapter a =
+ (SimpleCursorAdapter) b.newTransactionDescription.getAdapter();
+ try {
+ b.newTransactionDescription.setAdapter(null);
+ b.newTransactionDescription.setText(head.getDescription());
+ }
+ finally {
+ b.newTransactionDescription.setAdapter(a);
+ }
+
+ b.transactionComment.setText(head.getComment());
+ //styleComment(b.transactionComment, head.getComment());
- switch (item.getType()) {
- case generalData:
- b.newTransactionDate.setText(item.getFormattedDate());
- b.newTransactionDescription.setText(item.getDescription());
- b.transactionComment.setText(item.getTransactionComment());
b.ntrData.setVisibility(View.VISIBLE);
b.ntrAccount.setVisibility(View.GONE);
b.ntrPadding.setVisibility(View.GONE);
setEditable(true);
- break;
- case transactionRow:
- LedgerTransactionAccount acc = item.getAccount();
- b.accountRowAccName.setText(acc.getAccountName());
- b.comment.setText(acc.getComment());
- if (acc.isAmountSet()) {
- b.accountRowAccAmounts.setText(String.format("%1.2f", acc.getAmount()));
+ }
+ else if (item instanceof NewTransactionModel.TransactionAccount) {
+ NewTransactionModel.TransactionAccount acc = item.toTransactionAccount();
+
+ // avoid triggering completion pop-up
+ AccountAutocompleteAdapter a =
+ (AccountAutocompleteAdapter) b.accountRowAccName.getAdapter();
+ try {
+ b.accountRowAccName.setAdapter(null);
+ b.accountRowAccName.setText(acc.getAccountName());
+ }
+ finally {
+ b.accountRowAccName.setAdapter(a);
+ }
+
+ final String amountHint = acc.getAmountHint();
+ if (amountHint == null) {
+ b.accountRowAccAmounts.setHint(R.string.zero_amount);
}
else {
- b.accountRowAccAmounts.setText("");
-// tvAmount.setHint(R.string.zero_amount);
+ b.accountRowAccAmounts.setHint(amountHint);
}
- b.accountRowAccAmounts.setHint(item.getAmountHint());
+
+ b.accountRowAccAmounts.setImeOptions(
+ acc.isLast() ? EditorInfo.IME_ACTION_DONE : EditorInfo.IME_ACTION_NEXT);
+
setCurrencyString(acc.getCurrency());
+ b.accountRowAccAmounts.setText(
+ acc.isAmountSet() ? String.format("%4.2f", acc.getAmount()) : null);
+ displayAmountValidity(true);
+
+ b.comment.setText(acc.getComment());
+
b.ntrData.setVisibility(View.GONE);
b.ntrAccount.setVisibility(View.VISIBLE);
b.ntrPadding.setVisibility(View.GONE);
+
setEditable(true);
- break;
- case bottomFiller:
- b.ntrData.setVisibility(View.GONE);
- b.ntrAccount.setVisibility(View.GONE);
- b.ntrPadding.setVisibility(View.VISIBLE);
- setEditable(false);
- break;
- }
- if (this.item == null) { // was null or has changed
- this.item = item;
- final NewTransactionActivity activity = (NewTransactionActivity) b.getRoot()
- .getContext();
-
- if (!item.isBottomFiller()) {
- item.observeEditableFlag(activity, editableObserver);
- item.getModel()
- .observeFocusedItem(activity, focusedAccountObserver);
- item.getModel()
- .observeShowComments(activity, showCommentsObserver);
}
- switch (item.getType()) {
- case generalData:
- item.observeDate(activity, dateObserver);
- item.observeDescription(activity, descriptionObserver);
- item.observeTransactionComment(activity, transactionCommentObserver);
- break;
- case transactionRow:
- item.observeAmountHint(activity, hintObserver);
- Data.currencySymbolPosition.observe(activity, currencyPositionObserver);
- Data.currencyGap.observe(activity, currencyGapObserver);
- Data.locale.observe(activity, localeObserver);
- item.observeCurrency(activity, currencyObserver);
- item.getModel().showCurrency.observe(activity, showCurrencyObserver);
- item.observeComment(activity, commentObserver);
- item.getModel()
- .observeAccountCount(activity, accountCountObserver);
- item.observeAmountValidity(activity, amountValidityObserver);
- break;
+ else {
+ throw new RuntimeException("Don't know how to handle " + item);
}
+
+ applyFocus(mAdapter.model.getFocusInfo()
+ .getValue());
+ }
+ finally {
+ syncingData = false;
}
}
finally {
endUpdates();
}
}
+ private void styleComment(EditText editText, String comment) {
+ final View focusedView = editText.findFocus();
+ editText.setTypeface(null, (focusedView == editText) ? Typeface.NORMAL : Typeface.ITALIC);
+ editText.setVisibility(
+ ((focusedView != editText) && TextUtils.isEmpty(comment)) ? View.INVISIBLE
+ : View.VISIBLE);
+ }
@Override
public void onDatePicked(int year, int month, int day) {
- item.setDate(new SimpleDate(year, month + 1, day));
+ final NewTransactionModel.TransactionHead head = getItem().toTransactionHead();
+ head.setDate(new SimpleDate(year, month + 1, day));
+ b.newTransactionDate.setText(head.getFormattedDate());
+
boolean focused = b.newTransactionDescription.requestFocus();
if (focused)
Misc.showSoftKeyboard((NewTransactionActivity) b.getRoot()
.getContext());
}
+ private NewTransactionModel.Item getItem() {
+ return Objects.requireNonNull(mAdapter.model.getItems()
+ .getValue())
+ .get(getAdapterPosition());
+ }
@Override
public void descriptionSelected(String description) {
b.accountRowAccName.setText(description);