From 02ceb0d2ef0eed07c278330e4b9b2af366f5d17f Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Tue, 9 Mar 2021 22:11:29 +0200 Subject: [PATCH] split new transaction item holder in two one for the header row, another for the account rows --- ...> NewTransactionAccountRowItemHolder.java} | 364 +++++------------- .../NewTransactionHeaderItemHolder.java | 347 +++++++++++++++++ .../NewTransactionItemViewHolder.java | 44 +++ .../NewTransactionItemsAdapter.java | 60 ++- .../layout/new_transaction_account_row.xml | 136 +++++++ .../res/layout/new_transaction_header_row.xml | 110 ++++++ .../main/res/layout/new_transaction_row.xml | 235 ----------- 7 files changed, 780 insertions(+), 516 deletions(-) rename app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/{NewTransactionItemHolder.java => NewTransactionAccountRowItemHolder.java} (54%) create mode 100644 app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionHeaderItemHolder.java create mode 100644 app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemViewHolder.java create mode 100644 app/src/main/res/layout/new_transaction_account_row.xml create mode 100644 app/src/main/res/layout/new_transaction_header_row.xml delete mode 100644 app/src/main/res/layout/new_transaction_row.xml diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemHolder.java b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionAccountRowItemHolder.java similarity index 54% rename from app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemHolder.java rename to app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionAccountRowItemHolder.java index 5757c72a..870cf7ee 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemHolder.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionAccountRowItemHolder.java @@ -26,70 +26,47 @@ 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.constraintlayout.widget.ConstraintLayout; -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.databinding.NewTransactionAccountRowBinding; import net.ktnx.mobileledger.db.AccountAutocompleteAdapter; import net.ktnx.mobileledger.model.Currency; import net.ktnx.mobileledger.model.Data; -import net.ktnx.mobileledger.model.MobileLedgerProfile; import net.ktnx.mobileledger.ui.CurrencySelectorFragment; -import net.ktnx.mobileledger.ui.DatePickerFragment; import net.ktnx.mobileledger.ui.TextViewClearHelper; import net.ktnx.mobileledger.utils.DimensionUtils; import net.ktnx.mobileledger.utils.Logger; -import net.ktnx.mobileledger.utils.MLDB; import net.ktnx.mobileledger.utils.Misc; -import net.ktnx.mobileledger.utils.SimpleDate; import java.text.DecimalFormatSymbols; -import java.text.ParseException; -import java.util.Objects; -class NewTransactionItemHolder extends RecyclerView.ViewHolder - implements DatePickerFragment.DatePickedListener, DescriptionSelectedCallback { +class NewTransactionAccountRowItemHolder extends NewTransactionItemViewHolder { private final String decimalDot = "."; - private final MobileLedgerProfile mProfile; - private final NewTransactionRowBinding b; - private final NewTransactionItemsAdapter mAdapter; + private final NewTransactionAccountRowBinding b; private boolean ignoreFocusChanges = false; private String decimalSeparator; private boolean inUpdate = false; private boolean syncingData = false; //TODO multiple amounts with different currencies per posting? - NewTransactionItemHolder(@NonNull NewTransactionRowBinding b, - NewTransactionItemsAdapter adapter) { - super(b.getRoot()); + NewTransactionAccountRowItemHolder(@NonNull NewTransactionAccountRowBinding b, + NewTransactionItemsAdapter adapter) { + super(b.getRoot(), adapter); this.b = b; - this.mAdapter = adapter; new TextViewClearHelper().attachToTextView(b.comment); - b.newTransactionDescription.setNextFocusForwardId(View.NO_ID); b.accountRowAccName.setNextFocusForwardId(View.NO_ID); b.accountRowAccAmounts.setNextFocusForwardId(View.NO_ID); // magic! - b.newTransactionDate.setOnClickListener(v -> pickTransactionDate()); - b.accountCommentButton.setOnClickListener(v -> { b.comment.setVisibility(View.VISIBLE); b.comment.requestFocus(); }); - b.transactionCommentButton.setOnClickListener(v -> { - b.transactionComment.setVisibility(View.VISIBLE); - b.transactionComment.requestFocus(); - }); - - mProfile = Data.getProfile(); - @SuppressLint("DefaultLocale") View.OnFocusChangeListener focusMonitor = (v, hasFocus) -> { final int id = v.getId(); if (hasFocus) { @@ -106,14 +83,8 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder else if (id == R.id.comment) { adapter.noteFocusIsOnComment(pos); } - else if (id == R.id.transaction_comment) { - adapter.noteFocusIsOnTransactionComment(pos); - } - else if (id == R.id.new_transaction_description) { - adapter.noteFocusIsOnDescription(pos); - } else - throw new IllegalStateException("Where is the focus?"); + throw new IllegalStateException("Where is the focus? " + id); } finally { syncingData = wasSyncing; @@ -145,22 +116,15 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder if (id == R.id.comment) { commentFocusChanged(b.comment, hasFocus); } - else if (id == R.id.transaction_comment) { - commentFocusChanged(b.transactionComment, hasFocus); - } }; - b.newTransactionDescription.setOnFocusChangeListener(focusMonitor); b.accountRowAccName.setOnFocusChangeListener(focusMonitor); b.accountRowAccAmounts.setOnFocusChangeListener(focusMonitor); b.comment.setOnFocusChangeListener(focusMonitor); - b.transactionComment.setOnFocusChangeListener(focusMonitor); NewTransactionActivity activity = (NewTransactionActivity) b.getRoot() .getContext(); - MLDB.hookAutocompletionAdapter(activity, b.newTransactionDescription, - MLDB.DESCRIPTION_HISTORY_TABLE, "description", false, activity, mProfile); b.accountRowAccName.setAdapter(new AccountAutocompleteAdapter(b.getRoot() .getContext(), mProfile)); @@ -206,8 +170,6 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder adapter.model.checkTransactionSubmittable(null); } }; - b.newTransactionDescription.addTextChangedListener(tw); - monitorComment(b.transactionComment); b.accountRowAccName.addTextChangedListener(tw); monitorComment(b.comment); b.accountRowAccAmounts.addTextChangedListener(amountWatcher); @@ -220,7 +182,6 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder cpf.show(activity.getSupportFragmentManager(), "currency-selector"); }); - commentFocusChanged(b.transactionComment, false); commentFocusChanged(b.comment, false); adapter.model.getFocusInfo() @@ -249,36 +210,8 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder }); 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); - }); + .observe(activity, show -> b.commentLayout.setVisibility( + show ? View.VISIBLE : View.GONE)); } private void applyFocus(NewTransactionModel.FocusInfo focusInfo) { if (ignoreFocusChanges) { @@ -291,44 +224,22 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder focusInfo.position != getAdapterPosition())) return; - 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; - } - } - 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(); + NewTransactionModel.TransactionAccount acc = getItem().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; - } + if (focused) + Misc.showSoftKeyboard((NewTransactionActivity) b.getRoot() + .getContext()); + break; } } finally { @@ -460,8 +371,6 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder setCurrencyString((currency == null) ? null : currency.getName()); } private void setEditable(Boolean editable) { - b.newTransactionDate.setEnabled(editable); - b.newTransactionDescription.setEnabled(editable); b.accountRowAccName.setEnabled(editable); b.accountRowAccAmounts.setEnabled(editable); } @@ -501,97 +410,66 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder boolean significantChange = false; try { - 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(); + NewTransactionModel.TransactionAccount acc = item.toTransactionAccount(); - // having account name is important - final Editable incomingAccountName = b.accountRowAccName.getText(); - if (TextUtils.isEmpty(acc.getAccountName()) != - TextUtils.isEmpty(incomingAccountName)) - significantChange = true; + // having account name is important + final Editable incomingAccountName = b.accountRowAccName.getText(); + if (TextUtils.isEmpty(acc.getAccountName()) != TextUtils.isEmpty(incomingAccountName)) + significantChange = true; - acc.setAccountName(String.valueOf(incomingAccountName)); - final int accNameSelEnd = b.accountRowAccName.getSelectionEnd(); - final int accNameSelStart = b.accountRowAccName.getSelectionStart(); - acc.setAccountNameCursorPosition(accNameSelEnd); + acc.setAccountName(String.valueOf(incomingAccountName)); + final int accNameSelEnd = b.accountRowAccName.getSelectionEnd(); + final int accNameSelStart = b.accountRowAccName.getSelectionStart(); + acc.setAccountNameCursorPosition(accNameSelEnd); - acc.setComment(String.valueOf(b.comment.getText())); + acc.setComment(String.valueOf(b.comment.getText())); - String amount = String.valueOf(b.accountRowAccAmounts.getText()); - amount = amount.trim(); + String amount = String.valueOf(b.accountRowAccAmounts.getText()); + amount = amount.trim(); - if (amount.isEmpty()) { - if (acc.isAmountSet()) + 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.resetAmount(); + acc.setAmount(parsedAmount); 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); - } - 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); - } - 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 && !Misc.equalStrings(acc.getCurrency(), currValue)) + 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.setCurrency(currValue); + acc.setAmountValid(false); } - } - else { - 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 && !Misc.equalStrings(acc.getCurrency(), currValue)) + significantChange = true; + acc.setCurrency(currValue); } return significantChange; } - catch (ParseException e) { - throw new RuntimeException("Should not happen", e); - } finally { syncingData = false; } } - private void pickTransactionDate() { - DatePickerFragment picker = new DatePickerFragment(); - picker.setFutureDates(mProfile.getFutureDates()); - picker.setOnDatePickedListener(this); - picker.setCurrentDateFromText(b.newTransactionDate.getText()); - picker.show(((NewTransactionActivity) b.getRoot() - .getContext()).getSupportFragmentManager(), null); - } /** * bind * @@ -603,77 +481,47 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder try { syncingData = true; try { - if (item instanceof NewTransactionModel.TransactionHead) { - NewTransactionModel.TransactionHead head = item.toTransactionHead(); - b.newTransactionDate.setText(head.getFormattedDate()); + NewTransactionModel.TransactionAccount acc = item.toTransactionAccount(); + final String incomingAccountName = acc.getAccountName(); + final String presentAccountName = String.valueOf(b.accountRowAccName.getText()); + if (!Misc.equalStrings(incomingAccountName, presentAccountName)) { + Logger.debug("bind", + String.format("Setting account name from '%s' to '%s' (| @ %d)", + presentAccountName, incomingAccountName, + acc.getAccountNameCursorPosition())); // avoid triggering completion pop-up - SimpleCursorAdapter a = - (SimpleCursorAdapter) b.newTransactionDescription.getAdapter(); + AccountAutocompleteAdapter a = + (AccountAutocompleteAdapter) b.accountRowAccName.getAdapter(); try { - b.newTransactionDescription.setAdapter(null); - b.newTransactionDescription.setText(head.getDescription()); + b.accountRowAccName.setAdapter(null); + b.accountRowAccName.setText(incomingAccountName); + b.accountRowAccName.setSelection(acc.getAccountNameCursorPosition()); } finally { - b.newTransactionDescription.setAdapter(a); + b.accountRowAccName.setAdapter(a); } - - b.transactionComment.setText(head.getComment()); - //styleComment(b.transactionComment, head.getComment()); - - b.ntrData.setVisibility(View.VISIBLE); - b.ntrAccount.setVisibility(View.GONE); - setEditable(true); } - else if (item instanceof NewTransactionModel.TransactionAccount) { - NewTransactionModel.TransactionAccount acc = item.toTransactionAccount(); - - final String incomingAccountName = acc.getAccountName(); - final String presentAccountName = String.valueOf(b.accountRowAccName.getText()); - if (!Misc.equalStrings(incomingAccountName, presentAccountName)) { - Logger.debug("bind", - String.format("Setting account name from '%s' to '%s' (| @ %d)", - presentAccountName, incomingAccountName, - acc.getAccountNameCursorPosition())); - // avoid triggering completion pop-up - AccountAutocompleteAdapter a = - (AccountAutocompleteAdapter) b.accountRowAccName.getAdapter(); - try { - b.accountRowAccName.setAdapter(null); - b.accountRowAccName.setText(incomingAccountName); - b.accountRowAccName.setSelection(acc.getAccountNameCursorPosition()); - } - finally { - b.accountRowAccName.setAdapter(a); - } - } - - final String amountHint = acc.getAmountHint(); - if (amountHint == null) { - b.accountRowAccAmounts.setHint(R.string.zero_amount); - } - else { - b.accountRowAccAmounts.setHint(amountHint); - } - b.accountRowAccAmounts.setImeOptions( - acc.isLast() ? EditorInfo.IME_ACTION_DONE : EditorInfo.IME_ACTION_NEXT); + final String amountHint = acc.getAmountHint(); + if (amountHint == null) { + b.accountRowAccAmounts.setHint(R.string.zero_amount); + } + else { + b.accountRowAccAmounts.setHint(amountHint); + } - setCurrencyString(acc.getCurrency()); - b.accountRowAccAmounts.setText( - acc.isAmountSet() ? String.format("%4.2f", acc.getAmount()) : null); - displayAmountValidity(true); + b.accountRowAccAmounts.setImeOptions( + acc.isLast() ? EditorInfo.IME_ACTION_DONE : EditorInfo.IME_ACTION_NEXT); - b.comment.setText(acc.getComment()); + setCurrencyString(acc.getCurrency()); + b.accountRowAccAmounts.setText( + acc.isAmountSet() ? String.format("%4.2f", acc.getAmount()) : null); + displayAmountValidity(true); - b.ntrData.setVisibility(View.GONE); - b.ntrAccount.setVisibility(View.VISIBLE); + b.comment.setText(acc.getComment()); - setEditable(true); - } - else { - throw new RuntimeException("Don't know how to handle " + item); - } + setEditable(true); applyFocus(mAdapter.model.getFocusInfo() .getValue()); @@ -693,26 +541,4 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder ((focusedView != editText) && TextUtils.isEmpty(comment)) ? View.INVISIBLE : View.VISIBLE); } - @Override - public void onDatePicked(int year, int month, int 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); - b.accountRowAccAmounts.requestFocus(View.FOCUS_FORWARD); - } } diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionHeaderItemHolder.java b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionHeaderItemHolder.java new file mode 100644 index 00000000..90c4718f --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionHeaderItemHolder.java @@ -0,0 +1,347 @@ +/* + * Copyright © 2021 Damyan Ivanov. + * This file is part of MoLe. + * MoLe is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * MoLe is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with MoLe. If not, see . + */ + +package net.ktnx.mobileledger.ui.new_transaction; + +import android.annotation.SuppressLint; +import android.graphics.Typeface; +import android.text.Editable; +import android.text.TextUtils; +import android.text.TextWatcher; +import android.view.View; +import android.widget.EditText; +import android.widget.SimpleCursorAdapter; +import android.widget.TextView; + +import androidx.annotation.ColorInt; +import androidx.annotation.NonNull; + +import net.ktnx.mobileledger.R; +import net.ktnx.mobileledger.databinding.NewTransactionHeaderRowBinding; +import net.ktnx.mobileledger.model.Data; +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 net.ktnx.mobileledger.utils.SimpleDate; + +import java.text.DecimalFormatSymbols; +import java.text.ParseException; + +class NewTransactionHeaderItemHolder extends NewTransactionItemViewHolder + implements DatePickerFragment.DatePickedListener { + private final NewTransactionHeaderRowBinding b; + private boolean ignoreFocusChanges = false; + private String decimalSeparator; + private boolean inUpdate = false; + private boolean syncingData = false; + //TODO multiple amounts with different currencies per posting? + NewTransactionHeaderItemHolder(@NonNull NewTransactionHeaderRowBinding b, + NewTransactionItemsAdapter adapter) { + super(b.getRoot(), adapter); + this.b = b; + + b.newTransactionDescription.setNextFocusForwardId(View.NO_ID); + + b.newTransactionDate.setOnClickListener(v -> pickTransactionDate()); + + b.transactionCommentButton.setOnClickListener(v -> { + b.transactionComment.setVisibility(View.VISIBLE); + b.transactionComment.requestFocus(); + }); + + @SuppressLint("DefaultLocale") View.OnFocusChangeListener focusMonitor = (v, hasFocus) -> { + final int id = v.getId(); + if (hasFocus) { + boolean wasSyncing = syncingData; + syncingData = true; + try { + final int pos = getAdapterPosition(); + if (id == R.id.transaction_comment) { + adapter.noteFocusIsOnTransactionComment(pos); + } + else if (id == R.id.new_transaction_description) { + adapter.noteFocusIsOnDescription(pos); + } + else + throw new IllegalStateException("Where is the focus? " + id); + } + finally { + syncingData = wasSyncing; + } + } + + if (id == R.id.transaction_comment) { + commentFocusChanged(b.transactionComment, hasFocus); + } + }; + + b.newTransactionDescription.setOnFocusChangeListener(focusMonitor); + b.transactionComment.setOnFocusChangeListener(focusMonitor); + + NewTransactionActivity activity = (NewTransactionActivity) b.getRoot() + .getContext(); + + MLDB.hookAutocompletionAdapter(activity, b.newTransactionDescription, + MLDB.DESCRIPTION_HISTORY_TABLE, "description", false, activity, mProfile); + + decimalSeparator = ""; + Data.locale.observe(activity, locale -> decimalSeparator = String.valueOf( + DecimalFormatSymbols.getInstance(locale) + .getMonetaryDecimalSeparator())); + + final TextWatcher tw = 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) { +// debug("input", "text changed"); + if (inUpdate) + return; + + Logger.debug("textWatcher", "calling syncData()"); + if (syncData()) { + Logger.debug("textWatcher", + "syncData() returned, checking if transaction is submittable"); + adapter.model.checkTransactionSubmittable(null); + } + Logger.debug("textWatcher", "done"); + } + }; + b.newTransactionDescription.addTextChangedListener(tw); + monitorComment(b.transactionComment); + + commentFocusChanged(b.transactionComment, false); + + adapter.model.getFocusInfo() + .observe(activity, this::applyFocus); + + adapter.model.getShowComments() + .observe(activity, show -> 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; + + NewTransactionModel.Item head = getItem().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; + } + } + finally { + ignoreFocusChanges = false; + } + } + private void monitorComment(EditText editText) { + editText.addTextChangedListener(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) { +// debug("input", "text changed"); + if (inUpdate) + return; + + 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; + textColor = b.dummyText.getTextColors() + .getDefaultColor(); + if (hasFocus) { + textView.setTypeface(null, Typeface.NORMAL); + textView.setHint(R.string.transaction_account_comment_hint); + } + else { + int alpha = (textColor >> 24 & 0xff); + alpha = 3 * alpha / 4; + textColor = (alpha << 24) | (0x00ffffff & textColor); + textView.setTypeface(null, Typeface.ITALIC); + textView.setHint(""); + if (TextUtils.isEmpty(textView.getText())) { + textView.setVisibility(View.INVISIBLE); + } + } + textView.setTextColor(textColor); + + } + private void setEditable(Boolean editable) { + b.newTransactionDate.setEnabled(editable); + b.newTransactionDescription.setEnabled(editable); + } + private void beginUpdates() { + if (inUpdate) + throw new RuntimeException("Already in update mode"); + inUpdate = true; + } + private void endUpdates() { + if (!inUpdate) + throw new RuntimeException("Not in update mode"); + inUpdate = false; + } + /** + * syncData() + *

+ * Stores the data from the UI elements into the model item + * Returns true if there were changes made that suggest transaction has to be + * checked for being submittable + */ + private boolean syncData() { + if (syncingData) { + Logger.debug("new-trans", "skipping syncData() loop"); + return false; + } + + if (getAdapterPosition() < 0) { + // probably the row was swiped out + Logger.debug("new-trans", "Ignoring request to suncData(): adapter position negative"); + return false; + } + + + boolean significantChange = false; + + syncingData = true; + try { + NewTransactionModel.TransactionHead head = getItem().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())); + + return significantChange; + } + catch (ParseException e) { + throw new RuntimeException("Should not happen", e); + } + finally { + syncingData = false; + } + } + private void pickTransactionDate() { + DatePickerFragment picker = new DatePickerFragment(); + picker.setFutureDates(mProfile.getFutureDates()); + picker.setOnDatePickedListener(this); + picker.setCurrentDateFromText(b.newTransactionDate.getText()); + picker.show(((NewTransactionActivity) b.getRoot() + .getContext()).getSupportFragmentManager(), null); + } + /** + * bind + * + * @param item updates the UI elements with the data from the model item + */ + @SuppressLint("DefaultLocale") + public void bind(@NonNull NewTransactionModel.Item item) { + beginUpdates(); + try { + syncingData = true; + try { + 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()); + + setEditable(true); + + 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) { + 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()); + + } +} diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemViewHolder.java b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemViewHolder.java new file mode 100644 index 00000000..38c2796a --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemViewHolder.java @@ -0,0 +1,44 @@ +/* + * Copyright © 2021 Damyan Ivanov. + * This file is part of MoLe. + * MoLe is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * MoLe is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with MoLe. If not, see . + */ + +package net.ktnx.mobileledger.ui.new_transaction; + +import android.view.View; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import net.ktnx.mobileledger.model.Data; +import net.ktnx.mobileledger.model.MobileLedgerProfile; + +abstract class NewTransactionItemViewHolder extends RecyclerView.ViewHolder { + final NewTransactionItemsAdapter mAdapter; + final MobileLedgerProfile mProfile; + public NewTransactionItemViewHolder(@NonNull View itemView, + NewTransactionItemsAdapter adapter) { + super(itemView); + mAdapter = adapter; + mProfile = Data.getProfile(); + } + NewTransactionModel.Item getItem() { + return mAdapter.getItem(getAdapterPosition()); +// return Objects.requireNonNull(mAdapter.model.getItems() +// .getValue()) +// .get(getAdapterPosition()); + } + abstract public void bind(NewTransactionModel.Item item); +} diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemsAdapter.java b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemsAdapter.java index b1844c8e..3760d70f 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemsAdapter.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemsAdapter.java @@ -26,7 +26,8 @@ import androidx.recyclerview.widget.DiffUtil; import androidx.recyclerview.widget.ItemTouchHelper; import androidx.recyclerview.widget.RecyclerView; -import net.ktnx.mobileledger.databinding.NewTransactionRowBinding; +import net.ktnx.mobileledger.databinding.NewTransactionAccountRowBinding; +import net.ktnx.mobileledger.databinding.NewTransactionHeaderRowBinding; import net.ktnx.mobileledger.model.MobileLedgerProfile; import net.ktnx.mobileledger.utils.Logger; @@ -34,7 +35,9 @@ import java.util.List; import java.util.Locale; import java.util.Objects; -class NewTransactionItemsAdapter extends RecyclerView.Adapter { +class NewTransactionItemsAdapter extends RecyclerView.Adapter { + private static final int ITEM_VIEW_TYPE_HEADER = 1; + private static final int ITEM_VIEW_TYPE_ACCOUNT = 2; final NewTransactionModel model; private final ItemTouchHelper touchHelper; private final AsyncListDiffer differ = @@ -117,6 +120,20 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/new_transaction_header_row.xml b/app/src/main/res/layout/new_transaction_header_row.xml new file mode 100644 index 00000000..9f0ff194 --- /dev/null +++ b/app/src/main/res/layout/new_transaction_header_row.xml @@ -0,0 +1,110 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/new_transaction_row.xml b/app/src/main/res/layout/new_transaction_row.xml deleted file mode 100644 index d9c31083..00000000 --- a/app/src/main/res/layout/new_transaction_row.xml +++ /dev/null @@ -1,235 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file -- 2.39.2