From 301b3c61d29d5bfb27a5ed4b98f6d24f4d2fcfc6 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sat, 29 Feb 2020 17:35:39 +0100 Subject: [PATCH 01/77] Fixes #876 - Take into account only splitTypeSwitch to compute Split Balance --- .../ui/transaction/SplitEditorFragment.java | 57 ++++++++++++++----- 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java index 1494b16c7..b56daf566 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java @@ -422,30 +422,61 @@ public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { @Override public void afterTextChanged(Editable editable) { + + // + // Compute Split balance + // + BigDecimal imbalance = BigDecimal.ZERO; for (View splitItem : mSplitItemViewList) { SplitViewHolder viewHolder = (SplitViewHolder) splitItem.getTag(); - BigDecimal amount = viewHolder.getAmountValue().abs(); + + // Get the absolute value of the amount + BigDecimal absAmount = viewHolder.getAmountValue() + .abs(); + long accountId = viewHolder.accountsSpinner.getSelectedItemId(); - boolean hasDebitNormalBalance = AccountsDbAdapter.getInstance() - .getAccountType(accountId).hasDebitNormalBalance(); + + // #876 May be usefull for debug +// String accountFullName = AccountsDbAdapter.getInstance() +// .getAccountFullName(AccountsDbAdapter.getInstance() +// .getUID(accountId)); + + // #876 +// boolean hasDebitNormalBalance = AccountsDbAdapter.getInstance() +// .getAccountType(accountId) +// .hasDebitNormalBalance(); if (viewHolder.splitTypeSwitch.isChecked()) { - if (hasDebitNormalBalance) - imbalance = imbalance.add(amount); - else - imbalance = imbalance.subtract(amount); + // Switch is CREDIT + + // #876 +// if (hasDebitNormalBalance) { +// imbalance = imbalance.add(absAmount); +// } else { +// imbalance = imbalance.subtract(absAmount); +// } + imbalance = imbalance.add(absAmount); + } else { - if (hasDebitNormalBalance) - imbalance = imbalance.subtract(amount); - else - imbalance = imbalance.add(amount); + // Switch is DEBIT + + // #876 +// if (hasDebitNormalBalance) { +// imbalance = imbalance.subtract(absAmount); +// } else { +// imbalance = imbalance.add(absAmount); +// } + imbalance = imbalance.subtract(absAmount); + } - } + } // for - TransactionsActivity.displayBalance(mImbalanceTextView, new Money(imbalance, mCommodity)); + TransactionsActivity.displayBalance(mImbalanceTextView, + new Money(imbalance, + mCommodity)); } } From fdea65bf0622503daa8dcb516129becbf5186d1f Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Mon, 2 Mar 2020 01:50:58 +0100 Subject: [PATCH 02/77] #110 - Formatting and Comments --- .../gnucash/android/model/AccountType.java | 18 ++++- .../java/org/gnucash/android/model/Split.java | 2 +- .../ui/account/AccountsListFragment.java | 2 + .../ui/transaction/SplitEditorFragment.java | 67 ++++++++++++----- .../transaction/TransactionFormFragment.java | 71 +++++++++++++------ .../ui/transaction/TransactionsActivity.java | 23 ++++-- 6 files changed, 134 insertions(+), 49 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/model/AccountType.java b/app/src/main/java/org/gnucash/android/model/AccountType.java index 171156cdc..9f1494210 100644 --- a/app/src/main/java/org/gnucash/android/model/AccountType.java +++ b/app/src/main/java/org/gnucash/android/model/AccountType.java @@ -6,9 +6,21 @@ * they are currently not used except for exporting */ public enum AccountType { - CASH(TransactionType.DEBIT), BANK(TransactionType.DEBIT), CREDIT, ASSET(TransactionType.DEBIT), LIABILITY, - INCOME, EXPENSE(TransactionType.DEBIT), PAYABLE, RECEIVABLE(TransactionType.DEBIT), EQUITY, CURRENCY, - STOCK(TransactionType.DEBIT), MUTUAL(TransactionType.DEBIT), TRADING, ROOT; + CASH(TransactionType.DEBIT), + BANK(TransactionType.DEBIT), + CREDIT, + ASSET(TransactionType.DEBIT), + LIABILITY, + INCOME, + EXPENSE(TransactionType.DEBIT), + PAYABLE, + RECEIVABLE(TransactionType.DEBIT), + EQUITY, + CURRENCY, + STOCK(TransactionType.DEBIT), + MUTUAL(TransactionType.DEBIT), + TRADING, + ROOT; /** * Indicates that this type of normal balance the account type has diff --git a/app/src/main/java/org/gnucash/android/model/Split.java b/app/src/main/java/org/gnucash/android/model/Split.java index 27f9fb7df..c659cd1e4 100644 --- a/app/src/main/java/org/gnucash/android/model/Split.java +++ b/app/src/main/java/org/gnucash/android/model/Split.java @@ -41,7 +41,7 @@ public class Split extends BaseModel implements Parcelable{ /** - * Amount value of this split which is in the currency of the transaction + * Amount absolute value of this split which is in the currency of the transaction */ private Money mValue; diff --git a/app/src/main/java/org/gnucash/android/ui/account/AccountsListFragment.java b/app/src/main/java/org/gnucash/android/ui/account/AccountsListFragment.java index a83ab10bc..62b67a641 100644 --- a/app/src/main/java/org/gnucash/android/ui/account/AccountsListFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/account/AccountsListFragment.java @@ -581,7 +581,9 @@ class AccountViewHolder extends RecyclerView.ViewHolder implements PopupMenu.OnM long accoundId; public AccountViewHolder(View itemView) { + super(itemView); + ButterKnife.bind(this, itemView); optionsMenu.setOnClickListener(new View.OnClickListener() { diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java index b56daf566..5e65dc9bc 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java @@ -239,11 +239,22 @@ class SplitViewHolder implements OnTransferFundsListener{ View splitView; Money quantity; - public SplitViewHolder(View splitView, Split split){ - ButterKnife.bind(this, splitView); + public SplitViewHolder(View splitView, + Split split) { + + ButterKnife.bind(this, + splitView); + this.splitView = splitView; - if (split != null && !split.getQuantity().equals(split.getValue())) + + if (split != null && !split.getQuantity() + .equals(split.getValue())) { this.quantity = split.getQuantity(); + } + + // #876 Set the splitTypeSwitch according to split type + this.splitTypeSwitch.setChecked(split.getType()); + setListeners(split); } @@ -381,27 +392,49 @@ private void saveSplits() { * Extracts the input from the views and builds {@link org.gnucash.android.model.Split}s to correspond to the input. * @return List of {@link org.gnucash.android.model.Split}s represented in the view */ - private ArrayList extractSplitsFromView(){ + private ArrayList extractSplitsFromView() { + ArrayList splitList = new ArrayList<>(); + for (View splitView : mSplitItemViewList) { + SplitViewHolder viewHolder = (SplitViewHolder) splitView.getTag(); - if (viewHolder.splitAmountEditText.getValue() == null) + + if (viewHolder.splitAmountEditText.getValue() == null) { + // + continue; - BigDecimal amountBigDecimal = viewHolder.splitAmountEditText.getValue(); + } else { + // - String currencyCode = mAccountsDbAdapter.getCurrencyCode(mAccountUID); - Money valueAmount = new Money(amountBigDecimal.abs(), Commodity.getInstance(currencyCode)); + BigDecimal amountBigDecimal = viewHolder.splitAmountEditText.getValue(); + + String currencyCode = mAccountsDbAdapter.getCurrencyCode(mAccountUID); + + Money valueAmount = new Money(amountBigDecimal.abs(), + Commodity.getInstance(currencyCode)); + + String accountUID = mAccountsDbAdapter.getUID(viewHolder.accountsSpinner.getSelectedItemId()); + + Split split = new Split(valueAmount, + accountUID); + + split.setMemo(viewHolder.splitMemoEditText.getText() + .toString()); + split.setType(viewHolder.splitTypeSwitch.getTransactionType()); + split.setUID(viewHolder.splitUidTextView.getText() + .toString() + .trim()); + if (viewHolder.quantity != null) { + split.setQuantity(viewHolder.quantity.abs()); + } + + splitList.add(split); + } + + } // for - String accountUID = mAccountsDbAdapter.getUID(viewHolder.accountsSpinner.getSelectedItemId()); - Split split = new Split(valueAmount, accountUID); - split.setMemo(viewHolder.splitMemoEditText.getText().toString()); - split.setType(viewHolder.splitTypeSwitch.getTransactionType()); - split.setUID(viewHolder.splitUidTextView.getText().toString().trim()); - if (viewHolder.quantity != null) - split.setQuantity(viewHolder.quantity.abs()); - splitList.add(split); - } return splitList; } diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java index ad9f36e03..c01b71d6f 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java @@ -602,33 +602,60 @@ private void updateTransferAccountsList(){ /** * Opens the split editor dialog */ - private void openSplitEditor(){ - if (mAmountEditText.getValue() == null){ - Toast.makeText(getActivity(), R.string.toast_enter_amount_to_split, Toast.LENGTH_SHORT).show(); - return; - } + private void openSplitEditor() { + + if (mAmountEditText.getValue() == null) { - String baseAmountString; + Toast.makeText(getActivity(), + R.string.toast_enter_amount_to_split, + Toast.LENGTH_SHORT) + .show(); - if (mTransaction == null){ //if we are creating a new transaction (not editing an existing one) - BigDecimal enteredAmount = mAmountEditText.getValue(); - baseAmountString = enteredAmount.toPlainString(); } else { - Money biggestAmount = Money.createZeroInstance(mTransaction.getCurrencyCode()); - for (Split split : mTransaction.getSplits()) { - if (split.getValue().asBigDecimal().compareTo(biggestAmount.asBigDecimal()) > 0) - biggestAmount = split.getValue(); - } - baseAmountString = biggestAmount.toPlainString(); - } - Intent intent = new Intent(getActivity(), FormActivity.class); - intent.putExtra(UxArgument.FORM_TYPE, FormActivity.FormType.SPLIT_EDITOR.name()); - intent.putExtra(UxArgument.SELECTED_ACCOUNT_UID, mAccountUID); - intent.putExtra(UxArgument.AMOUNT_STRING, baseAmountString); - intent.putParcelableArrayListExtra(UxArgument.SPLIT_LIST, (ArrayList) extractSplitsFromView()); + String baseAmountString; + + if (mTransaction == null) { + // we are creating a new transaction (not editing an existing one) + + BigDecimal enteredAmount = mAmountEditText.getValue(); + + baseAmountString = enteredAmount.toPlainString(); - startActivityForResult(intent, REQUEST_SPLIT_EDITOR); + } else { + // we are editing an existing transaction + + // + // Find splits biggest amount (in absolute value) + // + + Money biggestAmount = Money.createZeroInstance(mTransaction.getCurrencyCode()); + + for (Split split : mTransaction.getSplits()) { + if (split.getValue() + .asBigDecimal() + .compareTo(biggestAmount.asBigDecimal()) > 0) { + biggestAmount = split.getValue(); + } + } // for + + baseAmountString = biggestAmount.toPlainString(); + } + + Intent intent = new Intent(getActivity(), + FormActivity.class); + intent.putExtra(UxArgument.FORM_TYPE, + FormActivity.FormType.SPLIT_EDITOR.name()); + intent.putExtra(UxArgument.SELECTED_ACCOUNT_UID, + mAccountUID); + intent.putExtra(UxArgument.AMOUNT_STRING, + baseAmountString); + intent.putParcelableArrayListExtra(UxArgument.SPLIT_LIST, + (ArrayList) extractSplitsFromView()); + + startActivityForResult(intent, + REQUEST_SPLIT_EDITOR); + } } /** diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java index fb4ba91ef..ac2b5d871 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java @@ -481,14 +481,25 @@ public String getCurrentAccountUID(){ * @param balanceTextView {@link android.widget.TextView} where balance is to be displayed * @param balance {@link org.gnucash.android.model.Money} balance to display */ - public static void displayBalance(TextView balanceTextView, Money balance){ + public static void displayBalance(TextView balanceTextView, + Money balance) { + balanceTextView.setText(balance.formattedString()); + Context context = GnuCashApplication.getAppContext(); - int fontColor = balance.isNegative() ? - context.getResources().getColor(R.color.debit_red) : - context.getResources().getColor(R.color.credit_green); - if (balance.asBigDecimal().compareTo(BigDecimal.ZERO) == 0) - fontColor = context.getResources().getColor(android.R.color.black); + + int fontColor = balance.isNegative() + ? context.getResources() + .getColor(R.color.debit_red) + : context.getResources() + .getColor(R.color.credit_green); + + if (balance.asBigDecimal() + .compareTo(BigDecimal.ZERO) == 0) { + fontColor = context.getResources() + .getColor(android.R.color.black); + } + balanceTextView.setTextColor(fontColor); } From 931bde4d4d667e3ae1072345925811aa2884e1d8 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Mon, 2 Mar 2020 01:55:11 +0100 Subject: [PATCH 03/77] #876 - French Translation --- app/src/main/res/values-fr/strings.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 9ab498501..9f4333014 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -197,7 +197,7 @@ Augmenter Revenu Remboursement - Frais + Dépense Facture Facture d\'achat Achat From 5367a269960e37ba66532359b6c8f7a5fe6a8228 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Mon, 2 Mar 2020 01:59:44 +0100 Subject: [PATCH 04/77] #876 - Correct errors in debit/credit labels vs GnuCash Windows. isChecked() : true == CREDIT. CREDIT => red, DEBIT => green --- .../ui/util/widget/TransactionTypeSwitch.java | 129 ++++++++++++------ 1 file changed, 85 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java b/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java index 9a06fe3ad..ca3a64b74 100644 --- a/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java +++ b/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java @@ -33,7 +33,7 @@ import java.util.List; /** - * A special type of {@link android.widget.ToggleButton} which displays the appropriate CREDIT/DEBIT labels for the + * A special type of {@link android.widget.ToggleButton} which displays the appropriate DEBIT/CREDIT labels for the * different account types. * @author Ngewi Fet */ @@ -59,49 +59,55 @@ public void setAccountType(AccountType accountType){ Context context = getContext().getApplicationContext(); switch (mAccountType) { case CASH: - setTextOn(context.getString(R.string.label_spend)); - setTextOff(context.getString(R.string.label_receive)); + setTextOff(context.getString(R.string.label_receive)); // DEBIT + setTextOn(context.getString(R.string.label_spend)); // CREDIT break; case BANK: - setTextOn(context.getString(R.string.label_withdrawal)); - setTextOff(context.getString(R.string.label_deposit)); + setTextOff(context.getString(R.string.label_deposit)); // DEBIT + setTextOn(context.getString(R.string.label_withdrawal)); // CREDIT break; case CREDIT: - setTextOn(context.getString(R.string.label_payment)); - setTextOff(context.getString(R.string.label_charge)); + // #876 Change according to GnuCash on Windows + setTextOff(context.getString(R.string.label_payment)); // DEBIT + setTextOn(context.getString(R.string.label_charge)); // CREDIT break; case ASSET: case EQUITY: case LIABILITY: - setTextOn(context.getString(R.string.label_decrease)); - setTextOff(context.getString(R.string.label_increase)); + // #876 Change according to GnuCash on Windows + setTextOff(context.getString(R.string.label_decrease)); // DEBIT + setTextOn(context.getString(R.string.label_increase)); // CREDIT break; case INCOME: - setTextOn(context.getString(R.string.label_charge)); - setTextOff(context.getString(R.string.label_income)); + // #876 Change according to GnuCash on Windows + setTextOff(context.getString(R.string.label_charge)); // DEBIT + setTextOn(context.getString(R.string.label_income)); // CREDIT break; case EXPENSE: - setTextOn(context.getString(R.string.label_rebate)); - setTextOff(context.getString(R.string.label_expense)); + setTextOff(context.getString(R.string.label_expense)); // DEBIT + setTextOn(context.getString(R.string.label_rebate)); // CREDIT break; case PAYABLE: - setTextOn(context.getString(R.string.label_payment)); - setTextOff(context.getString(R.string.label_bill)); + // #876 Change according to GnuCash on Windows + setTextOff(context.getString(R.string.label_payment)); // DEBIT + setTextOn(context.getString(R.string.label_bill)); // CREDIT break; case RECEIVABLE: - setTextOn(context.getString(R.string.label_payment)); - setTextOff(context.getString(R.string.label_invoice)); + setTextOff(context.getString(R.string.label_invoice)); // DEBIT + setTextOn(context.getString(R.string.label_payment)); // CREDIT break; case STOCK: case MUTUAL: - setTextOn(context.getString(R.string.label_buy)); - setTextOff(context.getString(R.string.label_sell)); + // #876 Change according to GnuCash on Windows + setTextOff(context.getString(R.string.label_buy)); // DEBIT + setTextOn(context.getString(R.string.label_sell)); // CREDIT break; case CURRENCY: case ROOT: default: - setTextOn(context.getString(R.string.label_debit)); - setTextOff(context.getString(R.string.label_credit)); + // #876 Change according to GnuCash on Windows + setTextOff(context.getString(R.string.label_debit)); // DEBIT + setTextOn(context.getString(R.string.label_credit)); // CREDIT break; } setText(isChecked() ? getTextOn() : getTextOff()); @@ -130,7 +136,9 @@ public void addOnCheckedChangeListener(OnCheckedChangeListener checkedChangeList * @param transactionType {@link org.gnucash.android.model.TransactionType} of the split */ public void setChecked(TransactionType transactionType){ - setChecked(Transaction.shouldDecreaseBalance(mAccountType, transactionType)); + // #876 +// setChecked(Transaction.shouldDecreaseBalance(mAccountType, transactionType)); + setChecked(TransactionType.CREDIT.equals(transactionType)); } /** @@ -141,12 +149,24 @@ public AccountType getAccountType(){ return mAccountType; } - public TransactionType getTransactionType(){ - if (mAccountType.hasDebitNormalBalance()){ - return isChecked() ? TransactionType.CREDIT : TransactionType.DEBIT; - } else { - return isChecked() ? TransactionType.DEBIT : TransactionType.CREDIT; - } + public TransactionType getTransactionType() { + + // #876 +// if (mAccountType.hasDebitNormalBalance()) { +// +// return isChecked() +// ? TransactionType.CREDIT +// : TransactionType.DEBIT; +// +// } else { +// +// return isChecked() +// ? TransactionType.DEBIT +// : TransactionType.CREDIT; +// } + return isChecked() + ? TransactionType.CREDIT + : TransactionType.DEBIT; } private class OnTypeChangedListener implements OnCheckedChangeListener{ @@ -163,32 +183,53 @@ public OnTypeChangedListener(CalculatorEditText amountEditText, TextView currenc } @Override - public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { - setText(isChecked ? getTextOn() : getTextOff()); - if (isChecked){ - int red = ContextCompat.getColor(getContext(), R.color.debit_red); - TransactionTypeSwitch.this.setTextColor(red); - mAmountEditText.setTextColor(red); - mCurrencyTextView.setTextColor(red); - } - else { - int green = ContextCompat.getColor(getContext(), R.color.credit_green); - TransactionTypeSwitch.this.setTextColor(green); - mAmountEditText.setTextColor(green); - mCurrencyTextView.setTextColor(green); + public void onCheckedChanged(CompoundButton compoundButton, + boolean isChecked) { + + setText(isChecked + ? getTextOn() // CREDIT + : getTextOff() // DEBIT + ); + + if (isChecked) { + // CREDIT + + // RED + int red = ContextCompat.getColor(getContext(), + R.color.debit_red); + setTextColor(red); + + } else { + // DEBIT + + // GREEN + int green = ContextCompat.getColor(getContext(), + R.color.credit_green); + setTextColor(green); } + BigDecimal amount = mAmountEditText.getValue(); - if (amount != null){ + + if (amount != null) { if ((isChecked && amount.signum() > 0) //we switched to debit but the amount is +ve - || (!isChecked && amount.signum() < 0)){ //credit but amount is -ve + || (!isChecked && amount.signum() < 0)) { //credit but amount is -ve + mAmountEditText.setValue(amount.negate()); } } for (OnCheckedChangeListener listener : mOnCheckedChangeListeners) { - listener.onCheckedChanged(compoundButton, isChecked); + listener.onCheckedChanged(compoundButton, + isChecked); } } + + private void setTextColor(final int color) { + + TransactionTypeSwitch.this.setTextColor(color); + mAmountEditText.setTextColor(color); + mCurrencyTextView.setTextColor(color); + } } } From b2ea8a0adde01038e6085b26d28df6c4fdde0bad Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Mon, 2 Mar 2020 02:01:03 +0100 Subject: [PATCH 05/77] #876 - Computing Balance : Debit => +, Credit => - --- .../gnucash/android/model/Transaction.java | 75 ++++++++++++++----- 1 file changed, 57 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/model/Transaction.java b/app/src/main/java/org/gnucash/android/model/Transaction.java index 21ba696aa..c6d41dac6 100644 --- a/app/src/main/java/org/gnucash/android/model/Transaction.java +++ b/app/src/main/java/org/gnucash/android/model/Transaction.java @@ -290,36 +290,64 @@ private Money getImbalance(){ * @return Money list of splits */ public static Money computeBalance(String accountUID, List splitList) { + AccountsDbAdapter accountsDbAdapter = AccountsDbAdapter.getInstance(); AccountType accountType = accountsDbAdapter.getAccountType(accountUID); String accountCurrencyCode = accountsDbAdapter.getAccountCurrencyCode(accountUID); - boolean isDebitAccount = accountType.hasDebitNormalBalance(); +// boolean isDebitAccount = accountType.hasDebitNormalBalance(); + Money balance = Money.createZeroInstance(accountCurrencyCode); + for (Split split : splitList) { + if (!split.getAccountUID().equals(accountUID)) continue; + + // + // Get Amount absolute value + // + Money amount; if (split.getValue().getCommodity().getCurrencyCode().equals(accountCurrencyCode)){ amount = split.getValue(); } else { //if this split belongs to the account, then either its value or quantity is in the account currency amount = split.getQuantity(); } + + // + // Compute balance + // + boolean isDebitSplit = split.getType() == TransactionType.DEBIT; - if (isDebitAccount) { - if (isDebitSplit) { - balance = balance.add(amount); - } else { - balance = balance.subtract(amount); - } + + // #876 +// if (isDebitAccount) { +// if (isDebitSplit) { +// balance = balance.add(amount); +// } else { +// balance = balance.subtract(amount); +// } +// } else { +// if (isDebitSplit) { +// balance = balance.subtract(amount); +// } else { +// balance = balance.add(amount); +// } +// } + if (isDebitSplit) { + // DEBIT + + balance = balance.add(amount); + } else { - if (isDebitSplit) { - balance = balance.subtract(amount); - } else { - balance = balance.add(amount); - } + // CREDIT + + balance = balance.subtract(amount); } - } + + } // for + return balance; } @@ -420,16 +448,27 @@ public static TransactionType getTypeForBalance(AccountType accountType, boolean return type; } + // #876 /** * Returns true if the transaction type represents a decrease for the account balance for the accountType, false otherwise * @return true if the amount represents a decrease in the account balance, false otherwise * @see #getTypeForBalance(AccountType, boolean) */ - public static boolean shouldDecreaseBalance(AccountType accountType, TransactionType transactionType) { - if (accountType.hasDebitNormalBalance()) { - return transactionType == TransactionType.CREDIT; - } else - return transactionType == TransactionType.DEBIT; + public static boolean wouldDecreaseBalance(AccountType accountType, + TransactionType transactionType) { + + // TODO TW M 2020-03-02 : TBC +// if (accountType.hasDebitNormalBalance()) { +// // Account usually with DEBIT > CREDIT +// +// return transactionType == TransactionType.CREDIT; +// +// } else { +// // Account usually with DEBIT < CREDIT +// +// return transactionType == TransactionType.DEBIT; +// } + return transactionType == TransactionType.CREDIT; } /** From 66154580f08c89e36e6b6734b0c260b6c35ac6d0 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Mon, 2 Mar 2020 02:02:00 +0100 Subject: [PATCH 06/77] #876 - hasDebitNormalBalance should not invert amount signum --- .../android/db/adapter/SplitsDbAdapter.java | 101 +++++++++++++----- 1 file changed, 72 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/db/adapter/SplitsDbAdapter.java b/app/src/main/java/org/gnucash/android/db/adapter/SplitsDbAdapter.java index 9750504de..2f66a5081 100644 --- a/app/src/main/java/org/gnucash/android/db/adapter/SplitsDbAdapter.java +++ b/app/src/main/java/org/gnucash/android/db/adapter/SplitsDbAdapter.java @@ -179,20 +179,35 @@ public Money computeSplitBalance(List accountUIDList, String currencyCod } - private Money calculateSplitBalance(List accountUIDList, String currencyCode, boolean hasDebitNormalBalance, - long startTimestamp, long endTimestamp){ - if (accountUIDList.size() == 0){ - return new Money("0", currencyCode); + private Money calculateSplitBalance(List accountUIDList, + String currencyCode, + boolean hasDebitNormalBalance, + long startTimestamp, + long endTimestamp) { + + if (accountUIDList.size() == 0) { + return new Money("0", + currencyCode); } - Cursor cursor; + Cursor cursor; String[] selectionArgs = null; - String selection = DatabaseSchema.AccountEntry.TABLE_NAME + "_" + DatabaseSchema.CommonColumns.COLUMN_UID + " in ( '" + TextUtils.join("' , '", accountUIDList) + "' ) AND " + - TransactionEntry.TABLE_NAME + "_" + TransactionEntry.COLUMN_TEMPLATE + " = 0"; + String selection = DatabaseSchema.AccountEntry.TABLE_NAME + + "_" + + DatabaseSchema.CommonColumns.COLUMN_UID + + " in ( '" + + TextUtils.join("' , '", + accountUIDList) + + "' ) AND " + + TransactionEntry.TABLE_NAME + + "_" + + TransactionEntry.COLUMN_TEMPLATE + + " = 0"; if (startTimestamp != -1 && endTimestamp != -1) { selection += " AND " + TransactionEntry.TABLE_NAME + "_" + TransactionEntry.COLUMN_TIMESTAMP + " BETWEEN ? AND ? "; - selectionArgs = new String[]{String.valueOf(startTimestamp), String.valueOf(endTimestamp)}; + selectionArgs = new String[]{String.valueOf(startTimestamp), + String.valueOf(endTimestamp)}; } else if (startTimestamp == -1 && endTimestamp != -1) { selection += " AND " + TransactionEntry.TABLE_NAME + "_" + TransactionEntry.COLUMN_TIMESTAMP + " <= ?"; selectionArgs = new String[]{String.valueOf(endTimestamp)}; @@ -202,34 +217,57 @@ private Money calculateSplitBalance(List accountUIDList, String currency } cursor = mDb.query("trans_split_acct", - new String[]{"TOTAL ( CASE WHEN " + SplitEntry.TABLE_NAME + "_" + SplitEntry.COLUMN_TYPE + " = 'DEBIT' THEN " + - SplitEntry.TABLE_NAME + "_" + SplitEntry.COLUMN_QUANTITY_NUM + " ELSE - " + - SplitEntry.TABLE_NAME + "_" + SplitEntry.COLUMN_QUANTITY_NUM + " END )", - SplitEntry.TABLE_NAME + "_" + SplitEntry.COLUMN_QUANTITY_DENOM, - DatabaseSchema.AccountEntry.TABLE_NAME + "_" + DatabaseSchema.AccountEntry.COLUMN_CURRENCY}, - selection, selectionArgs, DatabaseSchema.AccountEntry.TABLE_NAME + "_" + DatabaseSchema.AccountEntry.COLUMN_CURRENCY, null, null); + new String[]{"TOTAL ( CASE WHEN " + + SplitEntry.TABLE_NAME + + "_" + + SplitEntry.COLUMN_TYPE + + " = 'DEBIT' THEN " + + SplitEntry.TABLE_NAME + + "_" + + SplitEntry.COLUMN_QUANTITY_NUM + + " ELSE - " + + SplitEntry.TABLE_NAME + + "_" + + SplitEntry.COLUMN_QUANTITY_NUM + + " END )", + SplitEntry.TABLE_NAME + "_" + SplitEntry.COLUMN_QUANTITY_DENOM, + DatabaseSchema.AccountEntry.TABLE_NAME + + "_" + + DatabaseSchema.AccountEntry.COLUMN_CURRENCY}, + selection, + selectionArgs, + DatabaseSchema.AccountEntry.TABLE_NAME + "_" + DatabaseSchema.AccountEntry.COLUMN_CURRENCY, + null, + null); try { - Money total = Money.createZeroInstance(currencyCode); + Money total = Money.createZeroInstance(currencyCode); CommoditiesDbAdapter commoditiesDbAdapter = null; - PricesDbAdapter pricesDbAdapter = null; - Commodity commodity = null; - String currencyUID = null; + PricesDbAdapter pricesDbAdapter = null; + Commodity commodity = null; + String currencyUID = null; + while (cursor.moveToNext()) { - long amount_num = cursor.getLong(0); - long amount_denom = cursor.getLong(1); + long amount_num = cursor.getLong(0); + long amount_denom = cursor.getLong(1); String commodityCode = cursor.getString(2); + //Log.d(getClass().getName(), commodity + " " + amount_num + "/" + amount_denom); if (commodityCode.equals("XXX") || amount_num == 0) { // ignore custom currency continue; } - if (!hasDebitNormalBalance) { - amount_num = -amount_num; - } + + // #876 +// if (!hasDebitNormalBalance) { +// amount_num = -amount_num; +// } + if (commodityCode.equals(currencyCode)) { // currency matches - total = total.add(new Money(amount_num, amount_denom, currencyCode)); + total = total.add(new Money(amount_num, + amount_denom, + currencyCode)); //Log.d(getClass().getName(), "currency " + commodity + " sub - total " + total); } else { // there is a second currency involved @@ -241,18 +279,23 @@ private Money calculateSplitBalance(List accountUIDList, String currency } // get price String commodityUID = commoditiesDbAdapter.getCommodityUID(commodityCode); - Pair price = pricesDbAdapter.getPrice(commodityUID, currencyUID); + Pair price = pricesDbAdapter.getPrice(commodityUID, + currencyUID); if (price.first <= 0 || price.second <= 0) { // no price exists, just ignore it continue; } - BigDecimal amount = Money.getBigDecimal(amount_num, amount_denom); + BigDecimal amount = Money.getBigDecimal(amount_num, + amount_denom); BigDecimal amountConverted = amount.multiply(new BigDecimal(price.first)) - .divide(new BigDecimal(price.second), commodity.getSmallestFractionDigits(), BigDecimal.ROUND_HALF_EVEN); - total = total.add(new Money(amountConverted, commodity)); + .divide(new BigDecimal(price.second), + commodity.getSmallestFractionDigits(), + BigDecimal.ROUND_HALF_EVEN); + total = total.add(new Money(amountConverted, + commodity)); //Log.d(getClass().getName(), "currency " + commodity + " sub - total " + total); } - } + } // while return total; } finally { cursor.close(); From 4c84125c06f8888a449585d41b4f7e992c54f621 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Tue, 3 Mar 2020 23:25:08 +0100 Subject: [PATCH 07/77] #876 - Create AccountUtils --- .../gnucash/android/ui/util/AccountUtils.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 app/src/main/java/org/gnucash/android/ui/util/AccountUtils.java diff --git a/app/src/main/java/org/gnucash/android/ui/util/AccountUtils.java b/app/src/main/java/org/gnucash/android/ui/util/AccountUtils.java new file mode 100644 index 000000000..a72e813c2 --- /dev/null +++ b/app/src/main/java/org/gnucash/android/ui/util/AccountUtils.java @@ -0,0 +1,44 @@ +package org.gnucash.android.ui.util; + +import org.gnucash.android.db.DatabaseSchema; +import org.gnucash.android.model.AccountType; + +/** + * Utilities for Accounts UI + * + * @author JeanGarf + */ +public class AccountUtils { + + /** + * Build the where clause to select Accounts allowed for Transfer + * for the given accountUID + * + * @param accountUID + * The account UID for which we want to collect account allowed for transfer + * May be null (to allow all non special accounts) + * + * @return + * the where clause + * + * @author JeanGarf + */ + public static String getTransfertAccountWhereClause(final String accountUID) { + + return "(" + + DatabaseSchema.AccountEntry.COLUMN_UID + + " != '" + + ((accountUID != null) ? accountUID : "") + + "' AND " + + DatabaseSchema.AccountEntry.COLUMN_TYPE + + " != '" + + AccountType.ROOT.name() + + "' AND " + + DatabaseSchema.AccountEntry.COLUMN_PLACEHOLDER + + " = 0" + + " AND " + + DatabaseSchema.AccountEntry.COLUMN_HIDDEN + + " = 0" + + ")"; + } +} From 9e1951fc57b2944923844831a3b52eba37b21782 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Tue, 3 Mar 2020 23:26:03 +0100 Subject: [PATCH 08/77] #876 - Enhance Code Quality --- .../android/db/adapter/AccountsDbAdapter.java | 2 +- .../android/db/adapter/SplitsDbAdapter.java | 34 +- .../gnucash/android/model/AccountType.java | 13 +- .../android/model/TransactionType.java | 1 + .../ui/transaction/SplitEditorFragment.java | 410 ++++++++++++------ .../transaction/TransactionFormFragment.java | 4 +- 6 files changed, 322 insertions(+), 142 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/db/adapter/AccountsDbAdapter.java b/app/src/main/java/org/gnucash/android/db/adapter/AccountsDbAdapter.java index e19138306..2a67184e0 100644 --- a/app/src/main/java/org/gnucash/android/db/adapter/AccountsDbAdapter.java +++ b/app/src/main/java/org/gnucash/android/db/adapter/AccountsDbAdapter.java @@ -766,7 +766,7 @@ public Cursor fetchAccountsOrderedByFavoriteAndFullName(String where, String[] w * @return Account Balance of an account including sub-accounts */ public Money getAccountBalance(String accountUID){ - return computeBalance(accountUID, -1, -1); + return getAccountBalance(accountUID, -1, -1); } /** diff --git a/app/src/main/java/org/gnucash/android/db/adapter/SplitsDbAdapter.java b/app/src/main/java/org/gnucash/android/db/adapter/SplitsDbAdapter.java index 2f66a5081..3bfb32aca 100644 --- a/app/src/main/java/org/gnucash/android/db/adapter/SplitsDbAdapter.java +++ b/app/src/main/java/org/gnucash/android/db/adapter/SplitsDbAdapter.java @@ -158,8 +158,15 @@ public Split buildModelInstance(@NonNull final Cursor cursor){ * @param hasDebitNormalBalance Does the final balance has normal debit credit meaning * @return Balance of the splits for this account */ - public Money computeSplitBalance(List accountUIDList, String currencyCode, boolean hasDebitNormalBalance){ - return calculateSplitBalance(accountUIDList, currencyCode, hasDebitNormalBalance, -1, -1); + public Money computeSplitBalance(List accountUIDList, + String currencyCode, + boolean hasDebitNormalBalance) { + + return computeSplitBalance(accountUIDList, + currencyCode, + hasDebitNormalBalance, + -1, + -1); } /** @@ -173,9 +180,17 @@ public Money computeSplitBalance(List accountUIDList, String currencyCod * @param endTimestamp the end timestamp of the time range * @return Balance of the splits for this account within the specified time range */ - public Money computeSplitBalance(List accountUIDList, String currencyCode, boolean hasDebitNormalBalance, - long startTimestamp, long endTimestamp){ - return calculateSplitBalance(accountUIDList, currencyCode, hasDebitNormalBalance, startTimestamp, endTimestamp); + public Money computeSplitBalance(List accountUIDList, + String currencyCode, + boolean hasDebitNormalBalance, + long startTimestamp, + long endTimestamp) { + + return calculateSplitBalance(accountUIDList, + currencyCode, + hasDebitNormalBalance, + startTimestamp, + endTimestamp); } @@ -260,23 +275,30 @@ private Money calculateSplitBalance(List accountUIDList, // #876 // if (!hasDebitNormalBalance) { +// // The account has usually DEBIT < CREDIT +// +// // Invert signum to get a positive amount // amount_num = -amount_num; // } if (commodityCode.equals(currencyCode)) { // currency matches + total = total.add(new Money(amount_num, amount_denom, currencyCode)); //Log.d(getClass().getName(), "currency " + commodity + " sub - total " + total); + } else { // there is a second currency involved + if (commoditiesDbAdapter == null) { commoditiesDbAdapter = new CommoditiesDbAdapter(mDb); pricesDbAdapter = new PricesDbAdapter(mDb); commodity = commoditiesDbAdapter.getCommodity(currencyCode); currencyUID = commoditiesDbAdapter.getCommodityUID(currencyCode); } + // get price String commodityUID = commoditiesDbAdapter.getCommodityUID(commodityCode); Pair price = pricesDbAdapter.getPrice(commodityUID, @@ -296,7 +318,9 @@ private Money calculateSplitBalance(List accountUIDList, //Log.d(getClass().getName(), "currency " + commodity + " sub - total " + total); } } // while + return total; + } finally { cursor.close(); } diff --git a/app/src/main/java/org/gnucash/android/model/AccountType.java b/app/src/main/java/org/gnucash/android/model/AccountType.java index 9f1494210..a0837f477 100644 --- a/app/src/main/java/org/gnucash/android/model/AccountType.java +++ b/app/src/main/java/org/gnucash/android/model/AccountType.java @@ -37,7 +37,8 @@ public enum AccountType { //nothing to see here, move along } - public boolean hasDebitNormalBalance(){ + public boolean hasDebitNormalBalance() { + return mNormalBalance == TransactionType.DEBIT; } @@ -45,7 +46,15 @@ public boolean hasDebitNormalBalance(){ * Returns the type of normal balance this account possesses * @return TransactionType balance of the account type */ - public TransactionType getNormalBalanceType(){ + public TransactionType getNormalBalanceType() { + return mNormalBalance; } + + // TODO TW C 2020-03-03 : A renommer en anglais + public boolean isResultAccount() { + + return EXPENSE.equals(this) || INCOME.equals(this); + } + } diff --git a/app/src/main/java/org/gnucash/android/model/TransactionType.java b/app/src/main/java/org/gnucash/android/model/TransactionType.java index 0306a4c06..0fccc6166 100644 --- a/app/src/main/java/org/gnucash/android/model/TransactionType.java +++ b/app/src/main/java/org/gnucash/android/model/TransactionType.java @@ -22,6 +22,7 @@ * @author Ngewi Fet * @author Jesse Shieh */ +// TODO TW C 2020-03-03 : A renommer SplitType (AC) public enum TransactionType { DEBIT, CREDIT; diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java index 5e65dc9bc..e4b0f5d15 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java @@ -60,6 +60,7 @@ import org.gnucash.android.ui.common.FormActivity; import org.gnucash.android.ui.common.UxArgument; import org.gnucash.android.ui.transaction.dialog.TransferFundsDialogFragment; +import org.gnucash.android.ui.util.AccountUtils; import org.gnucash.android.ui.util.widget.CalculatorEditText; import org.gnucash.android.ui.util.widget.CalculatorKeyboard; import org.gnucash.android.ui.util.widget.TransactionTypeSwitch; @@ -79,6 +80,10 @@ */ public class SplitEditorFragment extends Fragment { + // + // SplitEditorFragment + // + @BindView(R.id.split_list_layout) LinearLayout mSplitsLinearLayout; @BindView(R.id.calculator_keyboard) KeyboardView mKeyboardView; @BindView(R.id.imbalance_textview) TextView mImbalanceTextView; @@ -197,13 +202,23 @@ public boolean onOptionsItemSelected(MenuItem item) { * @return Returns the split view which was added */ private View addSplitView(Split split){ + LayoutInflater layoutInflater = getActivity().getLayoutInflater(); - View splitView = layoutInflater.inflate(R.layout.item_split_entry, mSplitsLinearLayout, false); - mSplitsLinearLayout.addView(splitView,0); - SplitViewHolder viewHolder = new SplitViewHolder(splitView, split); - splitView.setTag(viewHolder); - mSplitItemViewList.add(splitView); - return splitView; + + View splitEntryView = layoutInflater.inflate(R.layout.item_split_entry, + mSplitsLinearLayout, + false); + + // Respect sort list order + mSplitsLinearLayout.addView(splitEntryView); + + SplitViewHolder viewHolder = new SplitViewHolder(splitEntryView, + split); + splitEntryView.setTag(viewHolder); + + mSplitItemViewList.add(splitEntryView); + + return splitEntryView; } /** @@ -216,122 +231,14 @@ private void initArgs() { mAccountUID = ((FormActivity) getActivity()).getCurrentAccountUID(); mBaseAmount = new BigDecimal(args.getString(UxArgument.AMOUNT_STRING)); - String conditions = "(" + // Get account list that are not hidden nor placeholder, and sort them with Favorites first + String where = "(" + DatabaseSchema.AccountEntry.COLUMN_HIDDEN + " = 0 AND " + DatabaseSchema.AccountEntry.COLUMN_PLACEHOLDER + " = 0" + ")"; - mCursor = mAccountsDbAdapter.fetchAccountsOrderedByFullName(conditions, null); - mCommodity = CommoditiesDbAdapter.getInstance().getCommodity(mAccountsDbAdapter.getCurrencyCode(mAccountUID)); - } - - /** - * Holds a split item view and binds the items in it - */ - class SplitViewHolder implements OnTransferFundsListener{ - @BindView(R.id.input_split_memo) EditText splitMemoEditText; - @BindView(R.id.input_split_amount) CalculatorEditText splitAmountEditText; - @BindView(R.id.btn_remove_split) ImageView removeSplitButton; - @BindView(R.id.input_accounts_spinner) Spinner accountsSpinner; - @BindView(R.id.split_currency_symbol) TextView splitCurrencyTextView; - @BindView(R.id.split_uid) TextView splitUidTextView; - @BindView(R.id.btn_split_type) TransactionTypeSwitch splitTypeSwitch; - - View splitView; - Money quantity; - - public SplitViewHolder(View splitView, - Split split) { - - ButterKnife.bind(this, - splitView); - - this.splitView = splitView; - - if (split != null && !split.getQuantity() - .equals(split.getValue())) { - this.quantity = split.getQuantity(); - } - - // #876 Set the splitTypeSwitch according to split type - this.splitTypeSwitch.setChecked(split.getType()); - - setListeners(split); - } - - @Override - public void transferComplete(Money amount) { - quantity = amount; - } - - private void setListeners(Split split){ - splitAmountEditText.bindListeners(mCalculatorKeyboard); - - removeSplitButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - mSplitsLinearLayout.removeView(splitView); - mSplitItemViewList.remove(splitView); - mImbalanceWatcher.afterTextChanged(null); - } - }); - - updateTransferAccountsList(accountsSpinner); - - splitCurrencyTextView.setText(mCommodity.getSymbol()); - splitTypeSwitch.setAmountFormattingListener(splitAmountEditText, splitCurrencyTextView); - splitTypeSwitch.setChecked(mBaseAmount.signum() > 0); - splitUidTextView.setText(BaseModel.generateUID()); - - if (split != null) { - splitAmountEditText.setCommodity(split.getValue().getCommodity()); - splitAmountEditText.setValue(split.getFormattedValue().asBigDecimal()); - splitCurrencyTextView.setText(split.getValue().getCommodity().getSymbol()); - splitMemoEditText.setText(split.getMemo()); - splitUidTextView.setText(split.getUID()); - String splitAccountUID = split.getAccountUID(); - setSelectedTransferAccount(mAccountsDbAdapter.getID(splitAccountUID), accountsSpinner); - splitTypeSwitch.setAccountType(mAccountsDbAdapter.getAccountType(splitAccountUID)); - splitTypeSwitch.setChecked(split.getType()); - } + mCursor = mAccountsDbAdapter.fetchAccountsOrderedByFavoriteAndFullName(where, null); - accountsSpinner.setOnItemSelectedListener(new SplitAccountListener(splitTypeSwitch, this)); - splitTypeSwitch.addOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { - @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - mImbalanceWatcher.afterTextChanged(null); - } - }); - splitAmountEditText.addTextChangedListener(mImbalanceWatcher); - } - - /** - * Returns the value of the amount in the splitAmountEditText field without setting the value to the view - *

If the expression in the view is currently incomplete or invalid, null is returned. - * This method is used primarily for computing the imbalance

- * @return Value in the split item amount field, or {@link BigDecimal#ZERO} if the expression is empty or invalid - */ - public BigDecimal getAmountValue(){ - String amountString = splitAmountEditText.getCleanString(); - if (amountString.isEmpty()) - return BigDecimal.ZERO; - - ExpressionBuilder expressionBuilder = new ExpressionBuilder(amountString); - Expression expression; - - try { - expression = expressionBuilder.build(); - } catch (RuntimeException e) { - return BigDecimal.ZERO; - } - - if (expression != null && expression.validate().isValid()) { - return new BigDecimal(expression.evaluate()); - } else { - Log.v(SplitEditorFragment.this.getClass().getSimpleName(), - "Incomplete expression for updating imbalance: " + expression); - return BigDecimal.ZERO; - } - } + mCommodity = CommoditiesDbAdapter.getInstance().getCommodity(mAccountsDbAdapter.getCurrencyCode(mAccountUID)); } /** @@ -346,6 +253,7 @@ private void setSelectedTransferAccount(long accountId, final Spinner accountsSp } } } + /** * Updates the list of possible transfer accounts. * Only accounts with the same currency can be transferred to @@ -438,18 +346,226 @@ private ArrayList extractSplitsFromView() { return splitList; } + // + // SplitViewHolder + // + + /** + * Holds a split item view and binds the items in it + */ + class SplitViewHolder + implements OnTransferFundsListener { + + @BindView(R.id.split_currency_symbol) + TextView splitCurrencyTextView; + @BindView(R.id.input_split_amount) + CalculatorEditText splitAmountEditText; + @BindView(R.id.btn_split_type) + TransactionTypeSwitch splitTypeSwitch; + @BindView(R.id.btn_remove_split) + ImageView removeSplitButton; + @BindView(R.id.input_accounts_spinner) + Spinner accountsSpinner; + @BindView(R.id.input_split_memo) + EditText splitMemoEditText; + @BindView(R.id.split_uid) + TextView splitUidTextView; + + private View splitView; + private Money quantity; + + public SplitViewHolder(View splitView, + Split split) { + + ButterKnife.bind(this, + splitView); + + this.splitView = splitView; + + // Set Listeners + setListeners(); + + if (split != null && !split.getQuantity() + .equals(split.getValue())) { + this.quantity = split.getQuantity(); + } + + // Init Views from split + initViews(split); + } + + private void setListeners() { + + // + // Listeners on splitAmountEditText + // + + splitAmountEditText.bindListeners(mCalculatorKeyboard); + + splitAmountEditText.addTextChangedListener(mImbalanceWatcher); + + // + // Listeners on removeSplitButton + // + + removeSplitButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + + mSplitsLinearLayout.removeView(splitView); + mSplitItemViewList.remove(splitView); + mImbalanceWatcher.afterTextChanged(null); + } + }); + + // + // Listeners on accountsSpinner + // + + accountsSpinner.setOnItemSelectedListener(new SplitTransferAccountSelectedListener(splitTypeSwitch, + this)); + + // + // Listeners on splitTypeSwitch + // + + // Set a ColorizeOnTransactionTypeChange listener + splitTypeSwitch.setColorizeOnCheckedChangeListener(splitAmountEditText, + splitCurrencyTextView); + + splitTypeSwitch.addOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, + boolean isChecked) { + + mImbalanceWatcher.afterTextChanged(null); + } + }); + } + + private void initViews(final Split split) { + + // + // splitTypeSwitch + // + + // TODO TW C 2020-03-03 : A enlever ou mettre dans un else ? + // Switch on/off according to amount signum + splitTypeSwitch.setChecked(mBaseAmount.signum() > 0); + + // + // Fill spinner + // + + updateTransferAccountsList(accountsSpinner); + + // + // Display Currency + // + + splitCurrencyTextView.setText(mCommodity.getSymbol()); + + // + // uid + // + + splitUidTextView.setText(BaseModel.generateUID()); + + // + // Handle split + // + + if (split != null) { + // There is a valid Split + + splitAmountEditText.setCommodity(split.getValue() + .getCommodity()); + splitAmountEditText.setValue(split.getFormattedValue() + .asBigDecimal()); + + splitCurrencyTextView.setText(split.getValue() + .getCommodity() + .getSymbol()); + + splitMemoEditText.setText(split.getMemo()); + + splitUidTextView.setText(split.getUID()); + + String splitAccountUID = split.getAccountUID(); + setSelectedTransferAccount(mAccountsDbAdapter.getID(splitAccountUID), + accountsSpinner); + + splitTypeSwitch.setAccountType(mAccountsDbAdapter.getAccountType(splitAccountUID)); + + splitTypeSwitch.setChecked(split.getType()); + } + } + + /** + * Returns the value of the amount in the splitAmountEditText field without setting the value to the view + *

If the expression in the view is currently incomplete or invalid, null is returned. + * This method is used primarily for computing the imbalance

+ * + * @return Value in the split item amount field, or {@link BigDecimal#ZERO} if the expression is empty or invalid + */ + public BigDecimal getAmountValue() { + + String amountString = splitAmountEditText.getCleanString(); + if (amountString.isEmpty()) { + return BigDecimal.ZERO; + } + + ExpressionBuilder expressionBuilder = new ExpressionBuilder(amountString); + Expression expression; + + try { + expression = expressionBuilder.build(); + } catch (RuntimeException e) { + return BigDecimal.ZERO; + } + + if (expression != null && expression.validate() + .isValid()) { + return new BigDecimal(expression.evaluate()); + } else { + Log.v(SplitEditorFragment.this.getClass() + .getSimpleName(), + "Incomplete expression for updating imbalance: " + expression); + return BigDecimal.ZERO; + } + } + + @Override + public void transferComplete(Money amount) { + + quantity = amount; + } + + } + + // + // BalanceTextWatcher + // + /** * Updates the displayed balance of the accounts when the amount of a split is changed */ - private class BalanceTextWatcher implements TextWatcher { + private class BalanceTextWatcher + implements TextWatcher { @Override - public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { + public void beforeTextChanged(CharSequence charSequence, + int i, + int i2, + int i3) { //nothing to see here, move along } @Override - public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { + public void onTextChanged(CharSequence charSequence, + int i, + int i2, + int i3) { //nothing to see here, move along } @@ -513,49 +629,77 @@ public void afterTextChanged(Editable editable) { } } + // + // SplitTransferAccountSelectedListener + // + /** * Listens to changes in the transfer account and updates the currency symbol, the label of the * transaction type and if neccessary */ - private class SplitAccountListener implements AdapterView.OnItemSelectedListener { - TransactionTypeSwitch mTypeToggleButton; - SplitViewHolder mSplitViewHolder; + private class SplitTransferAccountSelectedListener + implements AdapterView.OnItemSelectedListener { + + private TransactionTypeSwitch mTypeToggleButton; + private SplitViewHolder mSplitViewHolder; /** * Flag to know when account spinner callback is due to user interaction or layout of components */ boolean userInteraction = false; - public SplitAccountListener(TransactionTypeSwitch typeToggleButton, SplitViewHolder viewHolder){ + public SplitTransferAccountSelectedListener(TransactionTypeSwitch typeToggleButton, + SplitViewHolder viewHolder) { + this.mTypeToggleButton = typeToggleButton; this.mSplitViewHolder = viewHolder; } + /** + * Called when user has chosen a new Account for the split + * using the spinner + * + * @param parentView + * @param selectedItemView + * @param position + * @param id + */ @Override - public void onItemSelected(AdapterView parentView, View selectedItemView, int position, long id) { + public void onItemSelected(AdapterView parentView, + View selectedItemView, + int position, + long id) { + AccountType accountType = mAccountsDbAdapter.getAccountType(id); + + // TODO TW C 2020-03-03 : A renommer mTransactionTypeSwitch ou mSplitTypeSwitch mTypeToggleButton.setAccountType(accountType); //refresh the imbalance amount if we change the account mImbalanceWatcher.afterTextChanged(null); - String fromCurrencyCode = mAccountsDbAdapter.getCurrencyCode(mAccountUID); + String fromCurrencyCode = mAccountsDbAdapter.getCurrencyCode(mAccountUID); String targetCurrencyCode = mAccountsDbAdapter.getCurrencyCode(mAccountsDbAdapter.getUID(id)); - if (!userInteraction || fromCurrencyCode.equals(targetCurrencyCode)){ + if (!userInteraction || fromCurrencyCode.equals(targetCurrencyCode)) { //first call is on layout, subsequent calls will be true and transfer will work as usual userInteraction = true; return; } BigDecimal amountBigD = mSplitViewHolder.splitAmountEditText.getValue(); - if (amountBigD == null) + if (amountBigD == null) { return; + } + + Money amount = new Money(amountBigD, + Commodity.getInstance(fromCurrencyCode)); - Money amount = new Money(amountBigD, Commodity.getInstance(fromCurrencyCode)); - TransferFundsDialogFragment fragment - = TransferFundsDialogFragment.getInstance(amount, targetCurrencyCode, mSplitViewHolder); - fragment.show(getFragmentManager(), "tranfer_funds_editor"); + TransferFundsDialogFragment fragment = TransferFundsDialogFragment.getInstance(amount, + targetCurrencyCode, + mSplitViewHolder); + fragment.show(getFragmentManager(), + "tranfer_funds_editor"); } @Override diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java index c01b71d6f..dda65f596 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java @@ -662,7 +662,9 @@ private void openSplitEditor() { * Sets click listeners for the dialog buttons */ private void setListeners() { - mTransactionTypeSwitch.setAmountFormattingListener(mAmountEditText, mCurrencyTextView); + + mTransactionTypeSwitch.setColorizeOnCheckedChangeListener(mAmountEditText, + mCurrencyTextView); mDateTextView.setOnClickListener(new View.OnClickListener() { From c329455b7cd71773314a734288f3f81843ffcad7 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Tue, 3 Mar 2020 23:27:18 +0100 Subject: [PATCH 09/77] #876 - Set red/green color according to decrease/increase balance (and not more debit/credit) --- .../ui/util/widget/TransactionTypeSwitch.java | 136 ++++++++++++++---- 1 file changed, 106 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java b/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java index ca3a64b74..dd9fcafb5 100644 --- a/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java +++ b/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java @@ -25,7 +25,6 @@ import org.gnucash.android.R; import org.gnucash.android.model.AccountType; -import org.gnucash.android.model.Transaction; import org.gnucash.android.model.TransactionType; import java.math.BigDecimal; @@ -37,26 +36,47 @@ * different account types. * @author Ngewi Fet */ +// TODO TW C 2020-03-03 : A renommer SplitTypeSwitch (AC) public class TransactionTypeSwitch extends SwitchCompat { + private AccountType mAccountType = AccountType.EXPENSE; + // Listeners to call in case of change in Transaction Type switch List mOnCheckedChangeListeners = new ArrayList<>(); - public TransactionTypeSwitch(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); + public TransactionTypeSwitch(Context context, + AttributeSet attrs, + int defStyle) { + + super(context, + attrs, + defStyle); } - public TransactionTypeSwitch(Context context, AttributeSet attrs) { - super(context, attrs); + public TransactionTypeSwitch(Context context, + AttributeSet attrs) { + + super(context, + attrs); } public TransactionTypeSwitch(Context context) { + super(context); } - public void setAccountType(AccountType accountType){ + /** + * Store the accountType + * and define switch on/off texts accordingly + * + * @param accountType + */ + public void setAccountType(AccountType accountType) { + this.mAccountType = accountType; + Context context = getContext().getApplicationContext(); + switch (mAccountType) { case CASH: setTextOff(context.getString(R.string.label_receive)); // DEBIT @@ -110,17 +130,31 @@ public void setAccountType(AccountType accountType){ setTextOn(context.getString(R.string.label_credit)); // CREDIT break; } + + // + // Set switch text and color + // + + // TODO TW C 2020-03-03 : A remettre setText(isChecked() ? getTextOn() : getTextOff()); +// setSwitchTextAndColor(isChecked()); + invalidate(); } /** - * Set a checked change listener to monitor the amount view and currency views and update the display (color & balance accordingly) + * Bind a ColorizeOnTransactionTypeChangeListener as a switch checked change listener + * to update the signum of amount view, colorize amount, + * currency and switch views accordingly + * * @param amoutView Amount string {@link android.widget.EditText} * @param currencyTextView Currency symbol text view */ - public void setAmountFormattingListener(CalculatorEditText amoutView, TextView currencyTextView){ - setOnCheckedChangeListener(new OnTypeChangedListener(amoutView, currencyTextView)); + public void setColorizeOnCheckedChangeListener(CalculatorEditText amoutView, + TextView currencyTextView) { + + setOnCheckedChangeListener(new ColorizeOnTransactionTypeChangeListener(amoutView, + currencyTextView)); } /** @@ -145,7 +179,8 @@ public void setChecked(TransactionType transactionType){ * Returns the account type associated with this button * @return Type of account */ - public AccountType getAccountType(){ + public AccountType getAccountType() { + return mAccountType; } @@ -169,15 +204,28 @@ public TransactionType getTransactionType() { : TransactionType.DEBIT; } - private class OnTypeChangedListener implements OnCheckedChangeListener{ + // + // Inner Class OnTypeChangedListener + // + + /** + * Listener on change on Transaction Type (DEBIT turned to CREDIT or vice-versa) + * which update displayed amount signum and colorize, accordingly + */ + private class ColorizeOnTransactionTypeChangeListener + implements OnCheckedChangeListener{ + + // View to update and colorize private CalculatorEditText mAmountEditText; private TextView mCurrencyTextView; + /** * Constructor with the amount view * @param amountEditText EditText displaying the amount value * @param currencyTextView Currency symbol text view */ - public OnTypeChangedListener(CalculatorEditText amountEditText, TextView currencyTextView){ + public ColorizeOnTransactionTypeChangeListener(CalculatorEditText amountEditText, TextView currencyTextView){ + this.mAmountEditText = amountEditText; this.mCurrencyTextView = currencyTextView; } @@ -186,12 +234,56 @@ public OnTypeChangedListener(CalculatorEditText amountEditText, TextView currenc public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { - setText(isChecked + final boolean isCredit = isChecked; + + // + // Set switch text and color + // + + setSwitchTextAndColor(isCredit); + + // + // Change signum if needed + // + + BigDecimal amount = mAmountEditText.getValue(); + + if (amount != null) { + if ((isCredit && amount.signum() > 0) //we switched to debit but the amount is +ve + || (!isCredit && amount.signum() < 0)) { //credit but amount is -ve + + mAmountEditText.setValue(amount.negate()); + } + + } + + // + // Call other listeners + // + + for (OnCheckedChangeListener listener : mOnCheckedChangeListeners) { + listener.onCheckedChanged(compoundButton, + isChecked); + } // for + } + + public void setSwitchTextAndColor(final boolean isCredit) { + + // + // Set switch text + // + + setText(isCredit ? getTextOn() // CREDIT : getTextOff() // DEBIT ); - if (isChecked) { + // + // Set text color + // + + // TODO TW C 2020-03-02 : A renommer et commenter + if ((mAccountType.isResultAccount() && !isCredit) || (!mAccountType.isResultAccount() && isCredit)) { // CREDIT // RED @@ -207,22 +299,6 @@ public void onCheckedChanged(CompoundButton compoundButton, R.color.credit_green); setTextColor(green); } - - BigDecimal amount = mAmountEditText.getValue(); - - if (amount != null) { - if ((isChecked && amount.signum() > 0) //we switched to debit but the amount is +ve - || (!isChecked && amount.signum() < 0)) { //credit but amount is -ve - - mAmountEditText.setValue(amount.negate()); - } - - } - - for (OnCheckedChangeListener listener : mOnCheckedChangeListeners) { - listener.onCheckedChanged(compoundButton, - isChecked); - } } private void setTextColor(final int color) { From 02e6a024f0f4986ed04bf6d824b6a2b7f272a8b3 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Tue, 3 Mar 2020 23:37:35 +0100 Subject: [PATCH 10/77] #876 - Move attributes from ColorizeOnTransactionTypeChangeListener to TransactionTypeSwitch --- .../ui/util/widget/TransactionTypeSwitch.java | 97 +++++++++---------- 1 file changed, 48 insertions(+), 49 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java b/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java index dd9fcafb5..4aba5b00a 100644 --- a/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java +++ b/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java @@ -41,6 +41,10 @@ public class TransactionTypeSwitch extends SwitchCompat { private AccountType mAccountType = AccountType.EXPENSE; + // View to update and colorize + private CalculatorEditText mAmountEditText; + private TextView mCurrencyTextView; + // Listeners to call in case of change in Transaction Type switch List mOnCheckedChangeListeners = new ArrayList<>(); @@ -135,9 +139,7 @@ public void setAccountType(AccountType accountType) { // Set switch text and color // - // TODO TW C 2020-03-03 : A remettre - setText(isChecked() ? getTextOn() : getTextOff()); -// setSwitchTextAndColor(isChecked()); + setSwitchTextAndColor(isChecked()); invalidate(); } @@ -204,6 +206,47 @@ public TransactionType getTransactionType() { : TransactionType.DEBIT; } + private void setSwitchTextAndColor(final boolean isCredit) { + + // + // Set switch text + // + + setText(isCredit + ? getTextOn() // CREDIT + : getTextOff() // DEBIT + ); + + // + // Set text color + // + + // TODO TW C 2020-03-02 : A renommer et commenter + if ((mAccountType.isResultAccount() && !isCredit) || (!mAccountType.isResultAccount() && isCredit)) { + // CREDIT + + // RED + int red = ContextCompat.getColor(getContext(), + R.color.debit_red); + setAllTextsColor(red); + + } else { + // DEBIT + + // GREEN + int green = ContextCompat.getColor(getContext(), + R.color.credit_green); + setAllTextsColor(green); + } + } + + private void setAllTextsColor(final int color) { + + TransactionTypeSwitch.this.setTextColor(color); + mAmountEditText.setTextColor(color); + mCurrencyTextView.setTextColor(color); + } + // // Inner Class OnTypeChangedListener // @@ -215,10 +258,6 @@ public TransactionType getTransactionType() { private class ColorizeOnTransactionTypeChangeListener implements OnCheckedChangeListener{ - // View to update and colorize - private CalculatorEditText mAmountEditText; - private TextView mCurrencyTextView; - /** * Constructor with the amount view * @param amountEditText EditText displaying the amount value @@ -226,8 +265,8 @@ private class ColorizeOnTransactionTypeChangeListener */ public ColorizeOnTransactionTypeChangeListener(CalculatorEditText amountEditText, TextView currencyTextView){ - this.mAmountEditText = amountEditText; - this.mCurrencyTextView = currencyTextView; + mAmountEditText = amountEditText; + mCurrencyTextView = currencyTextView; } @Override @@ -267,45 +306,5 @@ public void onCheckedChanged(CompoundButton compoundButton, } // for } - public void setSwitchTextAndColor(final boolean isCredit) { - - // - // Set switch text - // - - setText(isCredit - ? getTextOn() // CREDIT - : getTextOff() // DEBIT - ); - - // - // Set text color - // - - // TODO TW C 2020-03-02 : A renommer et commenter - if ((mAccountType.isResultAccount() && !isCredit) || (!mAccountType.isResultAccount() && isCredit)) { - // CREDIT - - // RED - int red = ContextCompat.getColor(getContext(), - R.color.debit_red); - setTextColor(red); - - } else { - // DEBIT - - // GREEN - int green = ContextCompat.getColor(getContext(), - R.color.credit_green); - setTextColor(green); - } - } - - private void setTextColor(final int color) { - - TransactionTypeSwitch.this.setTextColor(color); - mAmountEditText.setTextColor(color); - mCurrencyTextView.setTextColor(color); - } } } From e7f8d6f6f8475e616b28d4ac3ea47ec354c898b0 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Wed, 4 Mar 2020 00:21:16 +0100 Subject: [PATCH 11/77] #876 - Create setViewsToColorize(...) to initialize views to colorize in red/green --- .../ui/util/widget/TransactionTypeSwitch.java | 40 ++++++++++++------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java b/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java index 4aba5b00a..24af9367f 100644 --- a/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java +++ b/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java @@ -33,7 +33,11 @@ /** * A special type of {@link android.widget.ToggleButton} which displays the appropriate DEBIT/CREDIT labels for the - * different account types. + * linked account type + * + * checked means CREDIT + * unchecked means DEBIT + * * @author Ngewi Fet */ // TODO TW C 2020-03-03 : A renommer SplitTypeSwitch (AC) @@ -69,6 +73,23 @@ public TransactionTypeSwitch(Context context) { super(context); } + /** + * Store views to colorize (green/red) accordingly to TransactionType and AccountType + * in addition to the current switch button + * + * @param amountEditText + * EditText displaying the amount value + * + * @param currencyTextView + * Currency symbol text view + */ + public void setViewsToColorize(CalculatorEditText amountEditText, + TextView currencyTextView) { + + mAmountEditText = amountEditText; + mCurrencyTextView = currencyTextView; + } + /** * Store the accountType * and define switch on/off texts accordingly @@ -77,7 +98,7 @@ public TransactionTypeSwitch(Context context) { */ public void setAccountType(AccountType accountType) { - this.mAccountType = accountType; + mAccountType = accountType; Context context = getContext().getApplicationContext(); @@ -148,15 +169,10 @@ public void setAccountType(AccountType accountType) { * Bind a ColorizeOnTransactionTypeChangeListener as a switch checked change listener * to update the signum of amount view, colorize amount, * currency and switch views accordingly - * - * @param amoutView Amount string {@link android.widget.EditText} - * @param currencyTextView Currency symbol text view */ - public void setColorizeOnCheckedChangeListener(CalculatorEditText amoutView, - TextView currencyTextView) { + public void setColorizeOnCheckedChangeListener() { - setOnCheckedChangeListener(new ColorizeOnTransactionTypeChangeListener(amoutView, - currencyTextView)); + setOnCheckedChangeListener(new ColorizeOnTransactionTypeChangeListener()); } /** @@ -260,13 +276,9 @@ private class ColorizeOnTransactionTypeChangeListener /** * Constructor with the amount view - * @param amountEditText EditText displaying the amount value - * @param currencyTextView Currency symbol text view */ - public ColorizeOnTransactionTypeChangeListener(CalculatorEditText amountEditText, TextView currencyTextView){ + public ColorizeOnTransactionTypeChangeListener(){ - mAmountEditText = amountEditText; - mCurrencyTextView = currencyTextView; } @Override From f507e59624ab33b7f5540616dc724068e912b44e Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Wed, 4 Mar 2020 00:21:39 +0100 Subject: [PATCH 12/77] #876 - Use setViewsToColorize(...) to initialize views to colorize in red/green --- .../android/ui/transaction/SplitEditorFragment.java | 12 ++++++++---- .../ui/transaction/TransactionFormFragment.java | 10 ++++++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java index e4b0f5d15..a1bebab63 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java @@ -60,7 +60,6 @@ import org.gnucash.android.ui.common.FormActivity; import org.gnucash.android.ui.common.UxArgument; import org.gnucash.android.ui.transaction.dialog.TransferFundsDialogFragment; -import org.gnucash.android.ui.util.AccountUtils; import org.gnucash.android.ui.util.widget.CalculatorEditText; import org.gnucash.android.ui.util.widget.CalculatorKeyboard; import org.gnucash.android.ui.util.widget.TransactionTypeSwitch; @@ -430,8 +429,7 @@ public void onClick(View view) { // // Set a ColorizeOnTransactionTypeChange listener - splitTypeSwitch.setColorizeOnCheckedChangeListener(splitAmountEditText, - splitCurrencyTextView); + splitTypeSwitch.setColorizeOnCheckedChangeListener(); splitTypeSwitch.addOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override @@ -449,6 +447,9 @@ private void initViews(final Split split) { // splitTypeSwitch // + splitTypeSwitch.setViewsToColorize(splitAmountEditText, + splitCurrencyTextView); + // TODO TW C 2020-03-03 : A enlever ou mettre dans un else ? // Switch on/off according to amount signum splitTypeSwitch.setChecked(mBaseAmount.signum() > 0); @@ -651,8 +652,11 @@ private class SplitTransferAccountSelectedListener public SplitTransferAccountSelectedListener(TransactionTypeSwitch typeToggleButton, SplitViewHolder viewHolder) { - this.mTypeToggleButton = typeToggleButton; this.mSplitViewHolder = viewHolder; + + this.mTypeToggleButton = typeToggleButton; + this.mTypeToggleButton.setViewsToColorize(mSplitViewHolder.splitAmountEditText, + mSplitViewHolder.splitCurrencyTextView); } /** diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java index dda65f596..9101d20ec 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java @@ -465,6 +465,9 @@ private void initializeViewsWithTransaction(){ mDescriptionEditText.setText(mTransaction.getDescription()); mDescriptionEditText.setSelection(mDescriptionEditText.getText().length()); + mTransactionTypeSwitch.setViewsToColorize(mAmountEditText, + mCurrencyTextView); + mTransactionTypeSwitch.setAccountType(mAccountType); mTransactionTypeSwitch.setChecked(mTransaction.getBalance(mAccountUID).isNegative()); @@ -552,7 +555,11 @@ private void initalizeViews() { mTimeTextView.setText(TIME_FORMATTER.format(time)); mTime = mDate = Calendar.getInstance(); + mTransactionTypeSwitch.setViewsToColorize(mAmountEditText, + mCurrencyTextView); + mTransactionTypeSwitch.setAccountType(mAccountType); + String typePref = PreferenceActivity.getActiveBookSharedPreferences().getString(getString(R.string.key_default_transaction_type), "DEBIT"); mTransactionTypeSwitch.setChecked(TransactionType.valueOf(typePref)); @@ -663,8 +670,7 @@ private void openSplitEditor() { */ private void setListeners() { - mTransactionTypeSwitch.setColorizeOnCheckedChangeListener(mAmountEditText, - mCurrencyTextView); + mTransactionTypeSwitch.setColorizeOnCheckedChangeListener(); mDateTextView.setOnClickListener(new View.OnClickListener() { From 6ed4930e9252fad4c1e0ae72ed9044a451dc6b6d Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Wed, 4 Mar 2020 00:22:00 +0100 Subject: [PATCH 13/77] #876 - In transaction form, put the split button to the right, like for transaction without splits --- .../main/res/layout/fragment_transaction_form.xml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/src/main/res/layout/fragment_transaction_form.xml b/app/src/main/res/layout/fragment_transaction_form.xml index a011a6df4..43398d898 100644 --- a/app/src/main/res/layout/fragment_transaction_form.xml +++ b/app/src/main/res/layout/fragment_transaction_form.xml @@ -75,12 +75,6 @@ android:textIsSelectable="true" gnucash:keyboardKeysLayout="@xml/calculator_keyboard"/> - - + + Date: Wed, 4 Mar 2020 00:30:33 +0100 Subject: [PATCH 14/77] #876 - Renamings and TODO --- .../org/gnucash/android/model/TransactionType.java | 2 +- .../android/ui/transaction/SplitEditorFragment.java | 13 ++++++------- .../ui/util/widget/TransactionTypeSwitch.java | 2 +- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/model/TransactionType.java b/app/src/main/java/org/gnucash/android/model/TransactionType.java index 0fccc6166..83a58690d 100644 --- a/app/src/main/java/org/gnucash/android/model/TransactionType.java +++ b/app/src/main/java/org/gnucash/android/model/TransactionType.java @@ -22,7 +22,7 @@ * @author Ngewi Fet * @author Jesse Shieh */ -// TODO TW C 2020-03-03 : A renommer SplitType (AC) +// TODO TW m 2020-03-03 : Should be named SplitType public enum TransactionType { DEBIT, CREDIT; diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java index a1bebab63..18ad5a27b 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java @@ -641,7 +641,7 @@ public void afterTextChanged(Editable editable) { private class SplitTransferAccountSelectedListener implements AdapterView.OnItemSelectedListener { - private TransactionTypeSwitch mTypeToggleButton; + private TransactionTypeSwitch mTransactionTypeSwitch; private SplitViewHolder mSplitViewHolder; /** @@ -649,14 +649,14 @@ private class SplitTransferAccountSelectedListener */ boolean userInteraction = false; - public SplitTransferAccountSelectedListener(TransactionTypeSwitch typeToggleButton, + public SplitTransferAccountSelectedListener(TransactionTypeSwitch transactionTypeSwitch, SplitViewHolder viewHolder) { this.mSplitViewHolder = viewHolder; - this.mTypeToggleButton = typeToggleButton; - this.mTypeToggleButton.setViewsToColorize(mSplitViewHolder.splitAmountEditText, - mSplitViewHolder.splitCurrencyTextView); + this.mTransactionTypeSwitch = transactionTypeSwitch; + this.mTransactionTypeSwitch.setViewsToColorize(mSplitViewHolder.splitAmountEditText, + mSplitViewHolder.splitCurrencyTextView); } /** @@ -676,8 +676,7 @@ public void onItemSelected(AdapterView parentView, AccountType accountType = mAccountsDbAdapter.getAccountType(id); - // TODO TW C 2020-03-03 : A renommer mTransactionTypeSwitch ou mSplitTypeSwitch - mTypeToggleButton.setAccountType(accountType); + mTransactionTypeSwitch.setAccountType(accountType); //refresh the imbalance amount if we change the account mImbalanceWatcher.afterTextChanged(null); diff --git a/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java b/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java index 24af9367f..8b83c3cac 100644 --- a/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java +++ b/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java @@ -40,7 +40,7 @@ * * @author Ngewi Fet */ -// TODO TW C 2020-03-03 : A renommer SplitTypeSwitch (AC) +// TODO TW m 2020-03-03 : Should be named SplitTypeToggleButton (or SplitTypeSwitch) instead of TransactionTypeSwitch public class TransactionTypeSwitch extends SwitchCompat { private AccountType mAccountType = AccountType.EXPENSE; From b7c042f59c88d8adece6c2b988e8cacaf02e951c Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Fri, 6 Mar 2020 20:42:35 +0100 Subject: [PATCH 15/77] #876 - Add AccountType parameter in displayBalance(...) --- .../ui/transaction/TransactionsActivity.java | 140 +++++++++++++----- 1 file changed, 100 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java index ac2b5d871..348e137a7 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java @@ -24,6 +24,7 @@ import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; +import android.support.annotation.ColorInt; import android.support.annotation.NonNull; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.TabLayout; @@ -50,6 +51,7 @@ import org.gnucash.android.db.adapter.AccountsDbAdapter; import org.gnucash.android.db.adapter.TransactionsDbAdapter; import org.gnucash.android.model.Account; +import org.gnucash.android.model.AccountType; import org.gnucash.android.model.Money; import org.gnucash.android.ui.account.AccountsActivity; import org.gnucash.android.ui.account.AccountsListFragment; @@ -78,7 +80,7 @@ public class TransactionsActivity extends BaseDrawerActivity implements /** * Logging tag */ - protected static final String TAG = "TransactionsActivity"; + protected static final String LOG_TAG = "TransactionsActivity"; /** * ViewPager index for sub-accounts fragment @@ -120,7 +122,7 @@ public class TransactionsActivity extends BaseDrawerActivity implements private SparseArray mFragmentPageReferenceMap = new SparseArray<>(); /** - * Flag for determining is the currently displayed account is a placeholder account or not. + * Flag for determining if the currently displayed account is a placeholder account or not. * This will determine if the transactions tab is displayed or not */ private boolean mIsPlaceholderAccount; @@ -128,10 +130,18 @@ public class TransactionsActivity extends BaseDrawerActivity implements private AdapterView.OnItemSelectedListener mTransactionListNavigationListener = new AdapterView.OnItemSelectedListener() { @Override - public void onItemSelected(AdapterView parent, View view, int position, long id) { + public void onItemSelected(AdapterView parent, View spinnerSelectedItemView, int position, long id) { + mAccountUID = mAccountsDbAdapter.getUID(id); - getIntent().putExtra(UxArgument.SELECTED_ACCOUNT_UID, mAccountUID); //update the intent in case the account gets rotated - mIsPlaceholderAccount = mAccountsDbAdapter.isPlaceholderAccount(mAccountUID); + getIntent().putExtra(UxArgument.SELECTED_ACCOUNT_UID, + getCurrentAccountUID()); //update the intent in case the account gets rotated + + // + // Show Transaction Page if not a PlaceHolder, hide otherwise + // + + mIsPlaceholderAccount = mAccountsDbAdapter.isPlaceholderAccount(getCurrentAccountUID()); + if (mIsPlaceholderAccount){ if (mTabLayout.getTabCount() > 1) { mPagerAdapter.notifyDataSetChanged(); @@ -143,9 +153,9 @@ public void onItemSelected(AdapterView parent, View view, int position, long mTabLayout.addTab(mTabLayout.newTab().setText(R.string.section_header_transactions)); } } - if (view != null) { + if (spinnerSelectedItemView != null) { // Hide the favorite icon of the selected account to avoid clutter - ((TextView) view).setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); + ((TextView) spinnerSelectedItemView).setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); } //refresh any fragments in the tab with the new account UID refresh(); @@ -229,7 +239,8 @@ public int getCount() { private AccountsListFragment prepareSubAccountsListFragment(){ AccountsListFragment subAccountsListFragment = new AccountsListFragment(); Bundle args = new Bundle(); - args.putString(UxArgument.PARENT_ACCOUNT_UID, mAccountUID); + args.putString(UxArgument.PARENT_ACCOUNT_UID, + getCurrentAccountUID()); subAccountsListFragment.setArguments(args); return subAccountsListFragment; } @@ -238,12 +249,17 @@ private AccountsListFragment prepareSubAccountsListFragment(){ * Creates and initializes fragment for displaying transactions * @return {@link TransactionsListFragment} initialized with the current account transactions */ - private TransactionsListFragment prepareTransactionsListFragment(){ + private TransactionsListFragment prepareTransactionsListFragment() { + TransactionsListFragment transactionsListFragment = new TransactionsListFragment(); - Bundle args = new Bundle(); - args.putString(UxArgument.SELECTED_ACCOUNT_UID, mAccountUID); + Bundle args = new Bundle(); + args.putString(UxArgument.SELECTED_ACCOUNT_UID, + getCurrentAccountUID()); transactionsListFragment.setArguments(args); - Log.i(TAG, "Opening transactions for account: " + mAccountUID); + + Log.i(LOG_TAG, + "Opening transactions for account: " + getCurrentAccountUID()); + return transactionsListFragment; } } @@ -260,13 +276,15 @@ public void refresh(String accountUID) { if (mPagerAdapter != null) mPagerAdapter.notifyDataSetChanged(); - new AccountBalanceTask(mSumTextView).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, mAccountUID); + new AccountBalanceTask(mSumTextView).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, + getCurrentAccountUID()); } @Override public void refresh(){ - refresh(mAccountUID); + + refresh(getCurrentAccountUID()); setTitleIndicatorColor(); } @@ -282,6 +300,7 @@ public int getTitleRes() { @Override protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); getSupportActionBar().setDisplayShowTitleEnabled(false); @@ -289,7 +308,11 @@ protected void onCreate(Bundle savedInstanceState) { mAccountUID = getIntent().getStringExtra(UxArgument.SELECTED_ACCOUNT_UID); mAccountsDbAdapter = AccountsDbAdapter.getInstance(); - mIsPlaceholderAccount = mAccountsDbAdapter.isPlaceholderAccount(mAccountUID); + // + // Add Tranbsaction Page + // + + mIsPlaceholderAccount = mAccountsDbAdapter.isPlaceholderAccount(getCurrentAccountUID()); mTabLayout.addTab(mTabLayout.newTab().setText(R.string.section_header_subaccounts)); if (!mIsPlaceholderAccount) { @@ -320,8 +343,9 @@ public void onTabReselected(TabLayout.Tab tab) { }); //if there are no transactions, and there are sub-accounts, show the sub-accounts - if (TransactionsDbAdapter.getInstance().getTransactionsCount(mAccountUID) == 0 - && mAccountsDbAdapter.getSubAccountCount(mAccountUID) > 0){ + if (TransactionsDbAdapter.getInstance() + .getTransactionsCount(getCurrentAccountUID()) == 0 + && mAccountsDbAdapter.getSubAccountCount(getCurrentAccountUID()) > 0) { mViewPager.setCurrentItem(INDEX_SUB_ACCOUNTS_FRAGMENT); } else { mViewPager.setCurrentItem(INDEX_TRANSACTIONS_FRAGMENT); @@ -335,13 +359,14 @@ public void onClick(View v) { Intent addAccountIntent = new Intent(TransactionsActivity.this, FormActivity.class); addAccountIntent.setAction(Intent.ACTION_INSERT_OR_EDIT); addAccountIntent.putExtra(UxArgument.FORM_TYPE, FormActivity.FormType.ACCOUNT.name()); - addAccountIntent.putExtra(UxArgument.PARENT_ACCOUNT_UID, mAccountUID); + addAccountIntent.putExtra(UxArgument.PARENT_ACCOUNT_UID, + getCurrentAccountUID()); startActivityForResult(addAccountIntent, AccountsActivity.REQUEST_EDIT_ACCOUNT); ; break; case INDEX_TRANSACTIONS_FRAGMENT: - createNewTransaction(mAccountUID); + createNewTransaction(getCurrentAccountUID()); break; } @@ -359,7 +384,8 @@ protected void onResume() { * Sets the color for the ViewPager title indicator to match the account color */ private void setTitleIndicatorColor() { - int iColor = AccountsDbAdapter.getActiveAccountColorResource(mAccountUID); + + int iColor = AccountsDbAdapter.getActiveAccountColorResource(getCurrentAccountUID()); mTabLayout.setBackgroundColor(iColor); @@ -374,7 +400,11 @@ private void setTitleIndicatorColor() { * Set up action bar navigation list and listener callbacks */ private void setupActionBarNavigation() { + + // // set up spinner adapter for navigation list + // + if (mAccountsCursor != null) { mAccountsCursor.close(); } @@ -387,17 +417,17 @@ private void setupActionBarNavigation() { mToolbarSpinner.setOnItemSelectedListener(mTransactionListNavigationListener); getSupportActionBar().setDisplayHomeAsUpEnabled(true); - updateNavigationSelection(); + selectCurrentAccountInToolbarSpinner(); } /** * Updates the action bar navigation list selection to that of the current account * whose transactions are being displayed/manipulated */ - public void updateNavigationSelection() { - // set the selected item in the spinner - int i = 0; - Cursor accountsCursor = mAccountsDbAdapter.fetchAllRecordsOrderedByFullName(); + public void selectCurrentAccountInToolbarSpinner() { + // set the selected item in the spinner + int i = 0; + Cursor accountsCursor = mAccountsDbAdapter.fetchAllRecordsOrderedByFullName(); while (accountsCursor.moveToNext()) { String uid = accountsCursor.getString(accountsCursor.getColumnIndexOrThrow(DatabaseSchema.AccountEntry.COLUMN_UID)); if (mAccountUID.equals(uid)) { @@ -407,7 +437,7 @@ public void updateNavigationSelection() { ++i; } accountsCursor.close(); - } + } @Override public boolean onPrepareOptionsMenu(Menu menu) { @@ -416,7 +446,8 @@ public boolean onPrepareOptionsMenu(Menu menu) { if (favoriteAccountMenuItem == null) //when the activity is used to edit a transaction return super.onPrepareOptionsMenu(menu); - boolean isFavoriteAccount = AccountsDbAdapter.getInstance().isFavoriteAccount(mAccountUID); + boolean isFavoriteAccount = AccountsDbAdapter.getInstance() + .isFavoriteAccount(getCurrentAccountUID()); int favoriteIcon = isFavoriteAccount ? R.drawable.ic_star_white_24dp : R.drawable.ic_star_border_white_24dp; favoriteAccountMenuItem.setIcon(favoriteIcon); @@ -432,8 +463,8 @@ public boolean onOptionsItemSelected(MenuItem item) { case R.id.menu_favorite_account: AccountsDbAdapter accountsDbAdapter = AccountsDbAdapter.getInstance(); - long accountId = accountsDbAdapter.getID(mAccountUID); - boolean isFavorite = accountsDbAdapter.isFavoriteAccount(mAccountUID); + long accountId = accountsDbAdapter.getID(getCurrentAccountUID()); + boolean isFavorite = accountsDbAdapter.isFavoriteAccount(getCurrentAccountUID()); //toggle favorite preference accountsDbAdapter.updateAccount(accountId, DatabaseSchema.AccountEntry.COLUMN_FAVORITE, isFavorite ? "0" : "1"); supportInvalidateOptionsMenu(); @@ -442,7 +473,8 @@ public boolean onOptionsItemSelected(MenuItem item) { case R.id.menu_edit_account: Intent editAccountIntent = new Intent(this, FormActivity.class); editAccountIntent.setAction(Intent.ACTION_INSERT_OR_EDIT); - editAccountIntent.putExtra(UxArgument.SELECTED_ACCOUNT_UID, mAccountUID); + editAccountIntent.putExtra(UxArgument.SELECTED_ACCOUNT_UID, + getCurrentAccountUID()); editAccountIntent.putExtra(UxArgument.FORM_TYPE, FormActivity.FormType.ACCOUNT.name()); startActivityForResult(editAccountIntent, AccountsActivity.REQUEST_EDIT_ACCOUNT); return true; @@ -465,7 +497,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { @Override protected void onDestroy() { super.onDestroy(); - mAccountsCursor.close(); + getAccountsCursor().close(); } /** @@ -476,28 +508,55 @@ public String getCurrentAccountUID(){ return mAccountUID; } + public Cursor getAccountsCursor() { + + return mAccountsCursor; + } + + // TODO TW C 2020-03-06 : A mettre ailleurs, car c'est pas spécifique de cette Activité /** * Display the balance of a transaction in a text view and format the text color to match the sign of the amount * @param balanceTextView {@link android.widget.TextView} where balance is to be displayed * @param balance {@link org.gnucash.android.model.Money} balance to display */ - public static void displayBalance(TextView balanceTextView, - Money balance) { + public static void displayBalance(final TextView balanceTextView, + final Money balance, + final AccountType accountType) { + + // + // Display amount + // + // TODO TW C 2020-03-06 : Déterminer qui doit dire s'il faut afficher un nombre négatif ou sa valeur absolue balanceTextView.setText(balance.formattedString()); - Context context = GnuCashApplication.getAppContext(); + // + // Define amount color + // - int fontColor = balance.isNegative() - ? context.getResources() - .getColor(R.color.debit_red) - : context.getResources() - .getColor(R.color.credit_green); + @ColorInt int fontColor; if (balance.asBigDecimal() .compareTo(BigDecimal.ZERO) == 0) { + // balance is null + + Context context = GnuCashApplication.getAppContext(); + fontColor = context.getResources() .getColor(android.R.color.black); + + } else { + // balance is not null + + final boolean isCredit = balance.isNegative(); + +// fontColor = isCredit +// ? context.getResources() +// .getColor(R.color.debit_red) +// : context.getResources() +// .getColor(R.color.credit_green); + fontColor = AccountType.getAmountColor(isCredit, + accountType); } balanceTextView.setTextColor(fontColor); @@ -539,7 +598,8 @@ public void createNewTransaction(String accountUID) { public void editTransaction(String transactionUID){ Intent createTransactionIntent = new Intent(this.getApplicationContext(), FormActivity.class); createTransactionIntent.setAction(Intent.ACTION_INSERT_OR_EDIT); - createTransactionIntent.putExtra(UxArgument.SELECTED_ACCOUNT_UID, mAccountUID); + createTransactionIntent.putExtra(UxArgument.SELECTED_ACCOUNT_UID, + getCurrentAccountUID()); createTransactionIntent.putExtra(UxArgument.SELECTED_TRANSACTION_UID, transactionUID); createTransactionIntent.putExtra(UxArgument.FORM_TYPE, FormActivity.FormType.TRANSACTION.name()); startActivity(createTransactionIntent); From e178e3a84779599ea35ddc8c65fcabd7fcfad67e Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Fri, 6 Mar 2020 20:43:52 +0100 Subject: [PATCH 16/77] #876 - use displayBalance(...) with AccountType --- .../ui/report/sheet/BalanceSheetFragment.java | 24 +++++++++++++++---- .../ui/transaction/SplitEditorFragment.java | 10 ++++++-- .../transaction/TransactionsListFragment.java | 7 ++++-- .../dialog/TransferFundsDialogFragment.java | 7 +++++- .../android/ui/util/AccountBalanceTask.java | 20 ++++++++++++---- 5 files changed, 54 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/report/sheet/BalanceSheetFragment.java b/app/src/main/java/org/gnucash/android/ui/report/sheet/BalanceSheetFragment.java index 4ca91569d..e234f126c 100644 --- a/app/src/main/java/org/gnucash/android/ui/report/sheet/BalanceSheetFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/report/sheet/BalanceSheetFragment.java @@ -78,6 +78,8 @@ public ReportType getReportType() { @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); + + // TODO TW C 2020-03-06 : A mettre dans AccountType sous formes de constantes mAssetAccountTypes = new ArrayList<>(); mAssetAccountTypes.add(AccountType.ASSET); mAssetAccountTypes.add(AccountType.CASH); @@ -109,11 +111,15 @@ protected void generateReport() { @Override protected void displayReport() { + loadAccountViews(mAssetAccountTypes, mAssetsTableLayout); loadAccountViews(mLiabilityAccountTypes, mLiabilitiesTableLayout); loadAccountViews(mEquityAccountTypes, mEquityTableLayout); - TransactionsActivity.displayBalance(mNetWorth, mAssetsBalance.subtract(mLiabilitiesBalance)); + TransactionsActivity.displayBalance(mNetWorth, + // #8xx + mAssetsBalance.add(mLiabilitiesBalance), + null); } @Override @@ -128,6 +134,7 @@ public void onPrepareOptionsMenu(Menu menu) { * @param tableLayout Table layout into which to load the rows */ private void loadAccountViews(List accountTypes, TableLayout tableLayout){ + LayoutInflater inflater = LayoutInflater.from(getActivity()); Cursor cursor = mAccountsDbAdapter.fetchAccounts(DatabaseSchema.AccountEntry.COLUMN_TYPE @@ -135,14 +142,19 @@ private void loadAccountViews(List accountTypes, TableLayout tableL + DatabaseSchema.AccountEntry.COLUMN_PLACEHOLDER + " = 0", null, DatabaseSchema.AccountEntry.COLUMN_FULL_NAME + " ASC"); + AccountType accountType = null; + while (cursor.moveToNext()){ String accountUID = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseSchema.AccountEntry.COLUMN_UID)); String name = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseSchema.AccountEntry.COLUMN_NAME)); Money balance = mAccountsDbAdapter.getAccountBalance(accountUID); View view = inflater.inflate(R.layout.row_balance_sheet, tableLayout, false); ((TextView)view.findViewById(R.id.account_name)).setText(name); - TextView balanceTextView = (TextView) view.findViewById(R.id.account_balance); - TransactionsActivity.displayBalance(balanceTextView, balance); + TextView balanceTextView = (TextView) view.findViewById(R.id.account_balance); + accountType = AccountType.valueOf(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseSchema.AccountEntry.COLUMN_TYPE))); + TransactionsActivity.displayBalance(balanceTextView, + balance, + accountType); tableLayout.addView(view); } @@ -157,7 +169,11 @@ private void loadAccountViews(List accountTypes, TableLayout tableL TextView accountBalance = (TextView) totalView.findViewById(R.id.account_balance); accountBalance.setTextSize(16); accountBalance.setTypeface(null, Typeface.BOLD); - TransactionsActivity.displayBalance(accountBalance, mAccountsDbAdapter.getAccountBalance(accountTypes, -1, System.currentTimeMillis())); + TransactionsActivity.displayBalance(accountBalance, + mAccountsDbAdapter.getAccountBalance(accountTypes, + -1, + System.currentTimeMillis()), + accountType); tableLayout.addView(totalView); } diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java index 18ad5a27b..297f5f919 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java @@ -151,7 +151,11 @@ public void onActivityCreated(Bundle savedInstanceState) { View view = addSplitView(split); view.findViewById(R.id.input_accounts_spinner).setEnabled(false); view.findViewById(R.id.btn_remove_split).setVisibility(View.GONE); - TransactionsActivity.displayBalance(mImbalanceTextView, new Money(mBaseAmount.negate(), mCommodity)); + + TransactionsActivity.displayBalance(mImbalanceTextView, + new Money(mBaseAmount.negate(), + mCommodity), + accountType); } } @@ -580,6 +584,7 @@ public void afterTextChanged(Editable editable) { BigDecimal imbalance = BigDecimal.ZERO; for (View splitItem : mSplitItemViewList) { + SplitViewHolder viewHolder = (SplitViewHolder) splitItem.getTag(); // Get the absolute value of the amount @@ -626,7 +631,8 @@ public void afterTextChanged(Editable editable) { TransactionsActivity.displayBalance(mImbalanceTextView, new Money(imbalance, - mCommodity)); + mCommodity), + null); } } diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsListFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsListFragment.java index 9ea0847e9..13dbde277 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsListFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsListFragment.java @@ -165,7 +165,7 @@ public void refresh(){ @Override public void onResume() { super.onResume(); - ((TransactionsActivity)getActivity()).updateNavigationSelection(); + ((TransactionsActivity)getActivity()).selectCurrentAccountInToolbarSpinner(); refresh(); } @@ -275,7 +275,10 @@ public void onBindViewHolderCursor(ViewHolder holder, Cursor cursor) { final String transactionUID = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseSchema.TransactionEntry.COLUMN_UID)); Money amount = mTransactionsDbAdapter.getBalance(transactionUID, mAccountUID); - TransactionsActivity.displayBalance(holder.transactionAmount, amount); + TransactionsActivity.displayBalance(holder.transactionAmount, + amount, + GnuCashApplication.getAccountsDbAdapter() + .getAccountType(mAccountUID)); long dateMillis = cursor.getLong(cursor.getColumnIndexOrThrow(DatabaseSchema.TransactionEntry.COLUMN_TIMESTAMP)); String dateText = TransactionsActivity.getPrettyDateFormat(getActivity(), dateMillis); diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/dialog/TransferFundsDialogFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/dialog/TransferFundsDialogFragment.java index 55bccd9c8..9db08fa47 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/dialog/TransferFundsDialogFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/dialog/TransferFundsDialogFragment.java @@ -35,6 +35,7 @@ import android.widget.TextView; import org.gnucash.android.R; +import org.gnucash.android.app.GnuCashApplication; import org.gnucash.android.db.adapter.CommoditiesDbAdapter; import org.gnucash.android.db.adapter.PricesDbAdapter; import org.gnucash.android.model.Commodity; @@ -97,7 +98,11 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa View view = inflater.inflate(R.layout.dialog_transfer_funds, container, false); ButterKnife.bind(this, view); - TransactionsActivity.displayBalance(mStartAmountLabel, mOriginAmount); + TransactionsActivity.displayBalance(mStartAmountLabel, + mOriginAmount, + // TODO TW C 2020-03-05 : A vérifier + null); + String fromCurrencyCode = mOriginAmount.getCommodity().getCurrencyCode(); mFromCurrencyLabel.setText(fromCurrencyCode); mToCurrencyLabel.setText(mTargetCommodity.getCurrencyCode()); diff --git a/app/src/main/java/org/gnucash/android/ui/util/AccountBalanceTask.java b/app/src/main/java/org/gnucash/android/ui/util/AccountBalanceTask.java index 8bef261af..7192927c7 100644 --- a/app/src/main/java/org/gnucash/android/ui/util/AccountBalanceTask.java +++ b/app/src/main/java/org/gnucash/android/ui/util/AccountBalanceTask.java @@ -38,7 +38,8 @@ public class AccountBalanceTask extends AsyncTask { public static final String LOG_TAG = AccountBalanceTask.class.getName(); private final WeakReference accountBalanceTextViewReference; - private final AccountsDbAdapter accountsDbAdapter; + private final AccountsDbAdapter accountsDbAdapter; + private String mAccountUID; public AccountBalanceTask(TextView balanceTextView){ accountBalanceTextViewReference = new WeakReference<>(balanceTextView); @@ -55,7 +56,10 @@ protected Money doInBackground(String... params) { Money balance = Money.getZeroInstance(); try { - balance = accountsDbAdapter.getAccountBalance(params[0], -1, -1); + mAccountUID = params[0]; + balance = accountsDbAdapter.getAccountBalance(mAccountUID, + -1, + -1); } catch (Exception ex) { Log.e(LOG_TAG, "Error computing account balance ", ex); Crashlytics.logException(ex); @@ -65,10 +69,16 @@ protected Money doInBackground(String... params) { @Override protected void onPostExecute(Money balance) { - if (accountBalanceTextViewReference.get() != null && balance != null){ + + if (accountBalanceTextViewReference.get() != null && balance != null) { + final TextView balanceTextView = accountBalanceTextViewReference.get(); - if (balanceTextView != null){ - TransactionsActivity.displayBalance(balanceTextView, balance); + + if (balanceTextView != null) { + + TransactionsActivity.displayBalance(balanceTextView, + balance, + accountsDbAdapter.getAccountType(mAccountUID)); } } } From 2f824368d15bee5c92084e2294068c7401e5e93f Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Fri, 6 Mar 2020 20:49:32 +0100 Subject: [PATCH 17/77] #876 - Correct balance amount computation and factorize into getAmountColor() --- .../gnucash/android/model/AccountType.java | 58 ++++++++++ .../java/org/gnucash/android/model/Split.java | 90 +++++++++------ .../ui/report/ReportsOverviewFragment.java | 7 +- .../TransactionDetailActivity.java | 103 +++++++++++++----- .../ui/util/widget/TransactionTypeSwitch.java | 26 +---- 5 files changed, 202 insertions(+), 82 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/model/AccountType.java b/app/src/main/java/org/gnucash/android/model/AccountType.java index a0837f477..9b856ef90 100644 --- a/app/src/main/java/org/gnucash/android/model/AccountType.java +++ b/app/src/main/java/org/gnucash/android/model/AccountType.java @@ -1,5 +1,11 @@ package org.gnucash.android.model; +import android.support.annotation.ColorInt; +import android.support.annotation.ColorRes; + +import org.gnucash.android.R; +import org.gnucash.android.app.GnuCashApplication; + /** * The type of account * This are the different types specified by the OFX format and @@ -37,6 +43,48 @@ public enum AccountType { //nothing to see here, move along } + // TODO TW C 2020-03-06 : Enlever le static + + /** + * Compute red/green color according to accountType and isCredit + * + * @param isCredit + * @param accountType + * + * @return + */ + @ColorInt + public static int getAmountColor(final boolean isCredit, + final AccountType accountType) { + + AccountType tmpAccountType = ((accountType != null) + ? accountType + : AccountType.ASSET); + + @ColorRes final int colorRes; + + // TODO TW C 2020-03-06 : Trouver un meilleur nom + final boolean specialAccountType = tmpAccountType.isEquityAccount() || tmpAccountType.isResultAccount(); + + if ((!specialAccountType && isCredit) || (specialAccountType && !isCredit)) { + // TODO TW C 2020-03-02 : commenter + // CREDIT + + // RED + colorRes = R.color.debit_red; + + } else { + // DEBIT + + // GREEN + colorRes = R.color.credit_green; + } + + return GnuCashApplication.getAppContext() + .getResources() + .getColor(colorRes); + } + public boolean hasDebitNormalBalance() { return mNormalBalance == TransactionType.DEBIT; @@ -51,6 +99,16 @@ public TransactionType getNormalBalanceType() { return mNormalBalance; } + public boolean isAssetAccount() { + + return ASSET.equals(this) || BANK.equals(this) || CASH.equals(this); + } + + public boolean isEquityAccount() { + + return EQUITY.equals(this); + } + // TODO TW C 2020-03-03 : A renommer en anglais public boolean isResultAccount() { diff --git a/app/src/main/java/org/gnucash/android/model/Split.java b/app/src/main/java/org/gnucash/android/model/Split.java index c659cd1e4..261b10a40 100644 --- a/app/src/main/java/org/gnucash/android/model/Split.java +++ b/app/src/main/java/org/gnucash/android/model/Split.java @@ -301,37 +301,6 @@ public Money getFormattedQuantity(){ return getFormattedAmount(mQuantity, mAccountUID, mSplitType); } - /** - * Splits are saved as absolute values to the database, with no negative numbers. - * The type of movement the split causes to the balance of an account determines - * its sign, and that depends on the split type and the account type - * @param amount Money amount to format - * @param accountUID GUID of the account - * @param splitType Transaction type of the split - * @return -{@code amount} if the amount would reduce the balance of - * {@code account}, otherwise +{@code amount} - */ - private static Money getFormattedAmount(Money amount, String accountUID, TransactionType - splitType){ - boolean isDebitAccount = AccountsDbAdapter.getInstance().getAccountType(accountUID).hasDebitNormalBalance(); - Money absAmount = amount.abs(); - - boolean isDebitSplit = splitType == TransactionType.DEBIT; - if (isDebitAccount) { - if (isDebitSplit) { - return absAmount; - } else { - return absAmount.negate(); - } - } else { - if (isDebitSplit) { - return absAmount.negate(); - } else { - return absAmount; - } - } - } - /** * Return the reconciled state of this split *

@@ -351,6 +320,54 @@ public char getReconcileState() { return mReconcileState; } + /** + * Splits are saved as absolute values to the database, with no negative numbers. + * The type of movement the split causes to the balance of an account determines + * its sign, and that depends on the split type and the account type + * @param amount Money amount to format + * @param accountUID GUID of the account + * @param splitType Transaction type of the split + * @return -{@code amount} if the amount would reduce the balance of + * {@code account}, otherwise +{@code amount} + */ + private static Money getFormattedAmount(Money amount, + String accountUID, + TransactionType splitType) { + +// boolean isDebitAccount = AccountsDbAdapter.getInstance() +// .getAccountType(accountUID) +// .hasDebitNormalBalance(); + + Money absAmount = amount.abs(); + + boolean isDebitSplit = splitType == TransactionType.DEBIT; + +// if (isDebitAccount) { +// if (isDebitSplit) { +// return absAmount; +// } else { +// return absAmount.negate(); +// } +// } else { +// if (isDebitSplit) { +// return absAmount.negate(); +// } else { +// return absAmount; +// } +// } + + if (isDebitSplit) { + // It is a Debit Split + + return absAmount; + + } else { + // It is not a Debit Split + + return absAmount.negate(); + } + } + /** * Check if this split is reconciled * @return {@code true} if the split is reconciled, {@code false} otherwise @@ -394,7 +411,16 @@ public void setReconcileDate(Timestamp reconcileDate) { @Override public String toString() { - return mSplitType.name() + " of " + mValue.toString() + " in account: " + mAccountUID; + + return mSplitType.name() + + " of " + + mValue.toString() + + " in account: " + + mAccountUID + + " (" + + AccountsDbAdapter.getInstance() + .getAccountFullName(mAccountUID) + + ")"; } /** diff --git a/app/src/main/java/org/gnucash/android/ui/report/ReportsOverviewFragment.java b/app/src/main/java/org/gnucash/android/ui/report/ReportsOverviewFragment.java index 9b81fc5b6..6d99db6fd 100644 --- a/app/src/main/java/org/gnucash/android/ui/report/ReportsOverviewFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/report/ReportsOverviewFragment.java @@ -213,9 +213,10 @@ protected void displayReport() { mChart.highlightValues(null); mChart.invalidate(); - TransactionsActivity.displayBalance(mTotalAssets, mAssetsBalance); - TransactionsActivity.displayBalance(mTotalLiabilities, mLiabilitiesBalance); - TransactionsActivity.displayBalance(mNetWorth, mAssetsBalance.subtract(mLiabilitiesBalance)); + TransactionsActivity.displayBalance(mTotalAssets, mAssetsBalance, AccountType.ASSET); + TransactionsActivity.displayBalance(mTotalLiabilities, mLiabilitiesBalance, AccountType.LIABILITY); + // #8xx + TransactionsActivity.displayBalance(mNetWorth, mAssetsBalance.add(mLiabilitiesBalance), null); } /** diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java index 44771f872..eb22e5352 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java @@ -22,9 +22,11 @@ import org.gnucash.android.model.ScheduledAction; import org.gnucash.android.model.Split; import org.gnucash.android.model.Transaction; +import org.gnucash.android.model.TransactionType; import org.gnucash.android.ui.common.FormActivity; import org.gnucash.android.ui.common.UxArgument; import org.gnucash.android.ui.passcode.PasscodeLockActivity; +import org.gnucash.android.ui.util.AccountUtils; import java.text.DateFormat; import java.util.Date; @@ -40,14 +42,55 @@ */ public class TransactionDetailActivity extends PasscodeLockActivity { - @BindView(R.id.trn_description) TextView mTransactionDescription; - @BindView(R.id.trn_time_and_date) TextView mTimeAndDate; - @BindView(R.id.trn_recurrence) TextView mRecurrence; - @BindView(R.id.trn_notes) TextView mNotes; + class SplitAmountViewHolder { + @BindView(R.id.split_account_name) TextView accountName; + @BindView(R.id.split_debit) TextView splitDebitView; + @BindView(R.id.split_credit) TextView splitCreditView; + + View itemView; + + public SplitAmountViewHolder(View view, + Split split) { + + itemView = view; + + ButterKnife.bind(this, + view); + + AccountsDbAdapter accountsDbAdapter = AccountsDbAdapter.getInstance(); + + final String accountUID = split.getAccountUID(); + + accountName.setText(accountsDbAdapter.getAccountFullName(accountUID)); + + // quantity (positive or negative number) + Money quantity = split.getFormattedQuantity(); + + // Define debit or credit view + // #8xx +// TextView balanceView = quantity.isNegative() +// ? splitDebitView +// : splitCreditView; + TextView balanceView = TransactionType.CREDIT.equals(split.getType()) + ? splitCreditView + : splitDebitView; + + TransactionsActivity.displayBalance(balanceView, + quantity, + AccountsDbAdapter.getInstance() + .getAccountType(split.getAccountUID())); + } + + } // Class SplitAmountViewHolder + @BindView(R.id.toolbar) Toolbar mToolBar; + @BindView(R.id.trn_description) TextView mTransactionDescription; @BindView(R.id.transaction_account) TextView mTransactionAccount; @BindView(R.id.balance_debit) TextView mDebitBalance; @BindView(R.id.balance_credit) TextView mCreditBalance; + @BindView(R.id.trn_time_and_date) TextView mTimeAndDate; + @BindView(R.id.trn_recurrence) TextView mRecurrence; + @BindView(R.id.trn_notes) TextView mNotes; @BindView(R.id.fragment_transaction_details) TableLayout mDetailTableLayout; @@ -92,25 +135,6 @@ protected void onCreate(Bundle savedInstanceState) { } - class SplitAmountViewHolder { - @BindView(R.id.split_account_name) TextView accountName; - @BindView(R.id.split_debit) TextView splitDebit; - @BindView(R.id.split_credit) TextView splitCredit; - - View itemView; - - public SplitAmountViewHolder(View view, Split split){ - itemView = view; - ButterKnife.bind(this, view); - - AccountsDbAdapter accountsDbAdapter = AccountsDbAdapter.getInstance(); - accountName.setText(accountsDbAdapter.getAccountFullName(split.getAccountUID())); - Money quantity = split.getFormattedQuantity(); - TextView balanceView = quantity.isNegative() ? splitDebit : splitCredit; - TransactionsActivity.displayBalance(balanceView, quantity); - } - } - /** * Reads the transaction information from the database and binds it to the views */ @@ -118,35 +142,64 @@ private void bindViews(){ TransactionsDbAdapter transactionsDbAdapter = TransactionsDbAdapter.getInstance(); Transaction transaction = transactionsDbAdapter.getRecord(mTransactionUID); + // Transaction description mTransactionDescription.setText(transaction.getDescription()); + + // Account Full Name mTransactionAccount.setText(getString(R.string.label_inside_account_with_name, AccountsDbAdapter.getInstance().getAccountFullName(mAccountUID))); + // + // Account balance + // + AccountsDbAdapter accountsDbAdapter = AccountsDbAdapter.getInstance(); + // Compute balance at Transaction time Money accountBalance = accountsDbAdapter.getAccountBalance(mAccountUID, -1, transaction.getTimeMillis()); - TextView balanceTextView = accountBalance.isNegative() ? mDebitBalance : mCreditBalance; - TransactionsActivity.displayBalance(balanceTextView, accountBalance); + + // #8xx + // Define in which field (Debit or Credit) the balance shall be displayed + TextView balanceTextView = accountBalance.isNegative() ? mCreditBalance : mDebitBalance ; + + TransactionsActivity.displayBalance(balanceTextView, + accountBalance, + accountsDbAdapter.getAccountType(mAccountUID)); + + // + // Détails + // mDetailTableRows = mDetailTableLayout.getChildCount(); + boolean useDoubleEntry = GnuCashApplication.isDoubleEntryEnabled(); LayoutInflater inflater = LayoutInflater.from(this); int index = 0; + for (Split split : transaction.getSplits()) { + if (!useDoubleEntry && split.getAccountUID().equals( accountsDbAdapter.getImbalanceAccountUID(split.getValue().getCommodity()))) { //do now show imbalance accounts for single entry use case continue; } + View view = inflater.inflate(R.layout.item_split_amount_info, mDetailTableLayout, false); SplitAmountViewHolder viewHolder = new SplitAmountViewHolder(view, split); mDetailTableLayout.addView(viewHolder.itemView, index++); } + // + // Date + // Date trnDate = new Date(transaction.getTimeMillis()); String timeAndDate = DateFormat.getDateInstance(DateFormat.FULL).format(trnDate); mTimeAndDate.setText(timeAndDate); + // + // + // + if (transaction.getScheduledActionUID() != null){ ScheduledAction scheduledAction = ScheduledActionDbAdapter.getInstance().getRecord(transaction.getScheduledActionUID()); mRecurrence.setText(scheduledAction.getRepeatString()); diff --git a/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java b/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java index 8b83c3cac..66f249c71 100644 --- a/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java +++ b/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java @@ -17,7 +17,7 @@ package org.gnucash.android.ui.util.widget; import android.content.Context; -import android.support.v4.content.ContextCompat; +import android.support.annotation.ColorInt; import android.support.v7.widget.SwitchCompat; import android.util.AttributeSet; import android.widget.CompoundButton; @@ -234,29 +234,11 @@ private void setSwitchTextAndColor(final boolean isCredit) { ); // - // Set text color + // Set text color of views // - // TODO TW C 2020-03-02 : A renommer et commenter - if ((mAccountType.isResultAccount() && !isCredit) || (!mAccountType.isResultAccount() && isCredit)) { - // CREDIT - - // RED - int red = ContextCompat.getColor(getContext(), - R.color.debit_red); - setAllTextsColor(red); - - } else { - // DEBIT - - // GREEN - int green = ContextCompat.getColor(getContext(), - R.color.credit_green); - setAllTextsColor(green); - } - } - - private void setAllTextsColor(final int color) { + @ColorInt final int color = AccountType.getAmountColor(isCredit, + getAccountType()); TransactionTypeSwitch.this.setTextColor(color); mAmountEditText.setTextColor(color); From 52f59ef7229078a3bfd4d1a0ce051f0b4a87af81 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Fri, 6 Mar 2020 20:52:36 +0100 Subject: [PATCH 18/77] #876 - Explain that balance in DetailTransactionActivity is computed at the date indicated below --- app/src/main/res/values-fr/strings.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 9f4333014..4767b5317 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -109,7 +109,7 @@ Email par défaut pour les exports. Vous pourrez toujours le changer lors de votre prochain export. Toutes les transactions seront un transfert d’un compte à un autre Activer la Double entrée - Solde + Solde à la date indiquée ci-dessous Entrer un nom de compte pour créer un compte Monnaie Compte parent diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4ef7ae17e..9348efdae 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -109,7 +109,7 @@ The default email address to send exports to. You can still change this when you export. All transactions will be a transfer from one account to another Activate Double Entry - Balance + Balance at date below Enter an account name to create an account Currency Parent account From b31daadf9009fa1a3a2dccdab010cc72d66bd829 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Fri, 6 Mar 2020 20:52:55 +0100 Subject: [PATCH 19/77] #876 - Enhance Code Quality --- .../java/org/gnucash/android/db/adapter/SplitsDbAdapter.java | 5 +++++ app/src/main/java/org/gnucash/android/model/Money.java | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/gnucash/android/db/adapter/SplitsDbAdapter.java b/app/src/main/java/org/gnucash/android/db/adapter/SplitsDbAdapter.java index 3bfb32aca..c74a32c51 100644 --- a/app/src/main/java/org/gnucash/android/db/adapter/SplitsDbAdapter.java +++ b/app/src/main/java/org/gnucash/android/db/adapter/SplitsDbAdapter.java @@ -220,13 +220,18 @@ private Money calculateSplitBalance(List accountUIDList, + " = 0"; if (startTimestamp != -1 && endTimestamp != -1) { + selection += " AND " + TransactionEntry.TABLE_NAME + "_" + TransactionEntry.COLUMN_TIMESTAMP + " BETWEEN ? AND ? "; selectionArgs = new String[]{String.valueOf(startTimestamp), String.valueOf(endTimestamp)}; + } else if (startTimestamp == -1 && endTimestamp != -1) { + selection += " AND " + TransactionEntry.TABLE_NAME + "_" + TransactionEntry.COLUMN_TIMESTAMP + " <= ?"; selectionArgs = new String[]{String.valueOf(endTimestamp)}; + } else if (startTimestamp != -1/* && endTimestamp == -1*/) { + selection += " AND " + TransactionEntry.TABLE_NAME + "_" + TransactionEntry.COLUMN_TIMESTAMP + " >= ?"; selectionArgs = new String[]{String.valueOf(startTimestamp)}; } diff --git a/app/src/main/java/org/gnucash/android/model/Money.java b/app/src/main/java/org/gnucash/android/model/Money.java index eedc67ac1..0c3ddd145 100644 --- a/app/src/main/java/org/gnucash/android/model/Money.java +++ b/app/src/main/java/org/gnucash/android/model/Money.java @@ -257,7 +257,8 @@ public double asDouble(){ public String asString(){ return toPlainString(); } - + + // TODO TW C 2020-03-06 : A factoriser avec Split.getFormattedAmount /** * Returns a string representation of the Money object formatted according to * the locale and includes the currency symbol. From bf4948222925d8bf27571666bc1995661993a01d Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sat, 7 Mar 2020 12:31:04 +0100 Subject: [PATCH 20/77] #876 - Replace mAssetAccountTypes with ASSET_ACCOUNT_TYPES from AccountType --- .../gnucash/android/model/AccountType.java | 22 ++++++++++++-- .../ui/report/sheet/BalanceSheetFragment.java | 30 ++++++------------- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/model/AccountType.java b/app/src/main/java/org/gnucash/android/model/AccountType.java index 9b856ef90..fd5dccb94 100644 --- a/app/src/main/java/org/gnucash/android/model/AccountType.java +++ b/app/src/main/java/org/gnucash/android/model/AccountType.java @@ -6,12 +6,17 @@ import org.gnucash.android.R; import org.gnucash.android.app.GnuCashApplication; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + /** * The type of account * This are the different types specified by the OFX format and * they are currently not used except for exporting */ public enum AccountType { + CASH(TransactionType.DEBIT), BANK(TransactionType.DEBIT), CREDIT, @@ -28,19 +33,30 @@ public enum AccountType { TRADING, ROOT; + public final static List ASSET_ACCOUNT_TYPES = new ArrayList(Arrays.asList(AccountType.ASSET, + AccountType.CASH, + AccountType.BANK)); + + public final static List LIABLITY_ACCOUNT_TYPES = new ArrayList(Arrays.asList(AccountType.LIABILITY, + AccountType.CREDIT)); + + public final static List EQUITY_ACCOUNT_TYPES = new ArrayList(Arrays.asList(AccountType.EQUITY)); + + /** * Indicates that this type of normal balance the account type has *

To increase the value of an account with normal balance of credit, one would credit the account. * To increase the value of an account with normal balance of debit, one would likewise debit the account.

*/ - private TransactionType mNormalBalance = TransactionType.CREDIT; + private TransactionType mNormalBalance; + + AccountType(TransactionType normalBalance) { - AccountType(TransactionType normalBalance){ this.mNormalBalance = normalBalance; } AccountType() { - //nothing to see here, move along + this(TransactionType.CREDIT); } // TODO TW C 2020-03-06 : Enlever le static diff --git a/app/src/main/java/org/gnucash/android/ui/report/sheet/BalanceSheetFragment.java b/app/src/main/java/org/gnucash/android/ui/report/sheet/BalanceSheetFragment.java index e234f126c..2de5f32d3 100644 --- a/app/src/main/java/org/gnucash/android/ui/report/sheet/BalanceSheetFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/report/sheet/BalanceSheetFragment.java @@ -40,6 +40,10 @@ import butterknife.BindView; +import static org.gnucash.android.model.AccountType.ASSET_ACCOUNT_TYPES; +import static org.gnucash.android.model.AccountType.EQUITY_ACCOUNT_TYPES; +import static org.gnucash.android.model.AccountType.LIABLITY_ACCOUNT_TYPES; + /** * Balance sheet report fragment * @author Ngewi Fet @@ -56,9 +60,6 @@ public class BalanceSheetFragment extends BaseReportFragment { private Money mAssetsBalance; private Money mLiabilitiesBalance; - private List mAssetAccountTypes; - private List mLiabilityAccountTypes; - private List mEquityAccountTypes; @Override public int getLayoutResource() { @@ -78,19 +79,6 @@ public ReportType getReportType() { @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - - // TODO TW C 2020-03-06 : A mettre dans AccountType sous formes de constantes - mAssetAccountTypes = new ArrayList<>(); - mAssetAccountTypes.add(AccountType.ASSET); - mAssetAccountTypes.add(AccountType.CASH); - mAssetAccountTypes.add(AccountType.BANK); - - mLiabilityAccountTypes = new ArrayList<>(); - mLiabilityAccountTypes.add(AccountType.LIABILITY); - mLiabilityAccountTypes.add(AccountType.CREDIT); - - mEquityAccountTypes = new ArrayList<>(); - mEquityAccountTypes.add(AccountType.EQUITY); } @Override @@ -105,16 +93,16 @@ public boolean requiresTimeRangeOptions() { @Override protected void generateReport() { - mAssetsBalance = mAccountsDbAdapter.getAccountBalance(mAssetAccountTypes, -1, System.currentTimeMillis()); - mLiabilitiesBalance = mAccountsDbAdapter.getAccountBalance(mLiabilityAccountTypes, -1, System.currentTimeMillis()); + mAssetsBalance = mAccountsDbAdapter.getAccountBalance(ASSET_ACCOUNT_TYPES, -1, System.currentTimeMillis()); + mLiabilitiesBalance = mAccountsDbAdapter.getAccountBalance(LIABLITY_ACCOUNT_TYPES, -1, System.currentTimeMillis()); } @Override protected void displayReport() { - loadAccountViews(mAssetAccountTypes, mAssetsTableLayout); - loadAccountViews(mLiabilityAccountTypes, mLiabilitiesTableLayout); - loadAccountViews(mEquityAccountTypes, mEquityTableLayout); + loadAccountViews(ASSET_ACCOUNT_TYPES, mAssetsTableLayout); + loadAccountViews(LIABLITY_ACCOUNT_TYPES, mLiabilitiesTableLayout); + loadAccountViews(EQUITY_ACCOUNT_TYPES, mEquityTableLayout); TransactionsActivity.displayBalance(mNetWorth, // #8xx From 8e04ba52f7cb3949c45dc09cf75d69024b115222 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sat, 7 Mar 2020 13:58:23 +0100 Subject: [PATCH 21/77] #876 - Code quality --- .../gnucash/android/model/AccountType.java | 21 ++++++++----------- .../ui/transaction/SplitEditorFragment.java | 1 - 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/model/AccountType.java b/app/src/main/java/org/gnucash/android/model/AccountType.java index fd5dccb94..09977aee5 100644 --- a/app/src/main/java/org/gnucash/android/model/AccountType.java +++ b/app/src/main/java/org/gnucash/android/model/AccountType.java @@ -60,17 +60,16 @@ public enum AccountType { } // TODO TW C 2020-03-06 : Enlever le static - /** - * Compute red/green color according to accountType and isCredit + * Compute red/green color according to accountType and isCreditAmount * - * @param isCredit + * @param isCreditAmount * @param accountType * * @return */ @ColorInt - public static int getAmountColor(final boolean isCredit, + public static int getAmountColor(final boolean isCreditAmount, final AccountType accountType) { AccountType tmpAccountType = ((accountType != null) @@ -79,18 +78,17 @@ public static int getAmountColor(final boolean isCredit, @ColorRes final int colorRes; - // TODO TW C 2020-03-06 : Trouver un meilleur nom - final boolean specialAccountType = tmpAccountType.isEquityAccount() || tmpAccountType.isResultAccount(); + // Accounts for which + final boolean debitCreditInvertedColorAccountType = tmpAccountType.isExpenseOrIncomeAccount() || tmpAccountType.isEquityAccount(); - if ((!specialAccountType && isCredit) || (specialAccountType && !isCredit)) { - // TODO TW C 2020-03-02 : commenter - // CREDIT + if ((isCreditAmount && !debitCreditInvertedColorAccountType) || (!isCreditAmount && debitCreditInvertedColorAccountType)) { + // Credit amount and account like Assets, Bank, Cash..., or Debit amount and account like Expense/Income // RED colorRes = R.color.debit_red; } else { - // DEBIT + // Credit amount and account like Expense/Income, or Debit amount and account like Assets, Bank, Cash...) // GREEN colorRes = R.color.credit_green; @@ -125,8 +123,7 @@ public boolean isEquityAccount() { return EQUITY.equals(this); } - // TODO TW C 2020-03-03 : A renommer en anglais - public boolean isResultAccount() { + public boolean isExpenseOrIncomeAccount() { return EXPENSE.equals(this) || INCOME.equals(this); } diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java index 297f5f919..7ca854708 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java @@ -454,7 +454,6 @@ private void initViews(final Split split) { splitTypeSwitch.setViewsToColorize(splitAmountEditText, splitCurrencyTextView); - // TODO TW C 2020-03-03 : A enlever ou mettre dans un else ? // Switch on/off according to amount signum splitTypeSwitch.setChecked(mBaseAmount.signum() > 0); From 535e3d5d5f1dcd1280f295ae6f02a6226d0b1a47 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sat, 7 Mar 2020 14:11:12 +0100 Subject: [PATCH 22/77] #876 - Transfer displayBalance(...) into AccountType and make methods non static --- .../gnucash/android/model/AccountType.java | 92 ++++++++++++++----- .../ui/transaction/TransactionsActivity.java | 49 ---------- 2 files changed, 69 insertions(+), 72 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/model/AccountType.java b/app/src/main/java/org/gnucash/android/model/AccountType.java index 09977aee5..edc2e1a1d 100644 --- a/app/src/main/java/org/gnucash/android/model/AccountType.java +++ b/app/src/main/java/org/gnucash/android/model/AccountType.java @@ -1,11 +1,14 @@ package org.gnucash.android.model; +import android.content.Context; import android.support.annotation.ColorInt; import android.support.annotation.ColorRes; +import android.widget.TextView; import org.gnucash.android.R; import org.gnucash.android.app.GnuCashApplication; +import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -59,27 +62,66 @@ public enum AccountType { this(TransactionType.CREDIT); } - // TODO TW C 2020-03-06 : Enlever le static + /** + * Display the balance of a transaction in a text view and format the text color to match the sign of the amount + * @param balanceTextView {@link android.widget.TextView} where balance is to be displayed + * @param balance {@link org.gnucash.android.model.Money} balance to display + */ + public void displayBalance(final TextView balanceTextView, + final Money balance) { + + // + // Display amount + // + + // TODO TW C 2020-03-06 : Déterminer qui doit dire s'il faut afficher un nombre négatif ou sa valeur absolue + balanceTextView.setText(balance.formattedString()); + + // + // Define amount color + // + + @ColorInt int fontColor; + + if (balance.asBigDecimal() + .compareTo(BigDecimal.ZERO) == 0) { + // balance is null + + Context context = GnuCashApplication.getAppContext(); + + fontColor = context.getResources() + .getColor(android.R.color.black); + + } else { + // balance is not null + + final boolean isCreditBalance = balance.isNegative(); + +// fontColor = isCreditBalance +// ? context.getResources() +// .getColor(R.color.debit_red) +// : context.getResources() +// .getColor(R.color.credit_green); + fontColor = getAmountColor(isCreditBalance); + } + + balanceTextView.setTextColor(fontColor); + } + /** * Compute red/green color according to accountType and isCreditAmount * * @param isCreditAmount - * @param accountType * * @return */ @ColorInt - public static int getAmountColor(final boolean isCreditAmount, - final AccountType accountType) { - - AccountType tmpAccountType = ((accountType != null) - ? accountType - : AccountType.ASSET); + public int getAmountColor(final boolean isCreditAmount) { @ColorRes final int colorRes; // Accounts for which - final boolean debitCreditInvertedColorAccountType = tmpAccountType.isExpenseOrIncomeAccount() || tmpAccountType.isEquityAccount(); + final boolean debitCreditInvertedColorAccountType = isExpenseOrIncomeAccount() || isEquityAccount(); if ((isCreditAmount && !debitCreditInvertedColorAccountType) || (!isCreditAmount && debitCreditInvertedColorAccountType)) { // Credit amount and account like Assets, Bank, Cash..., or Debit amount and account like Expense/Income @@ -99,20 +141,6 @@ public static int getAmountColor(final boolean isCreditAmount, .getColor(colorRes); } - public boolean hasDebitNormalBalance() { - - return mNormalBalance == TransactionType.DEBIT; - } - - /** - * Returns the type of normal balance this account possesses - * @return TransactionType balance of the account type - */ - public TransactionType getNormalBalanceType() { - - return mNormalBalance; - } - public boolean isAssetAccount() { return ASSET.equals(this) || BANK.equals(this) || CASH.equals(this); @@ -128,4 +156,22 @@ public boolean isExpenseOrIncomeAccount() { return EXPENSE.equals(this) || INCOME.equals(this); } + public boolean hasDebitNormalBalance() { + + return mNormalBalance == TransactionType.DEBIT; + } + + // + // Getters/Setters + // + + /** + * Returns the type of normal balance this account possesses + * @return TransactionType balance of the account type + */ + public TransactionType getNormalBalanceType() { + + return mNormalBalance; + } + } diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java index 348e137a7..565395a33 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java @@ -513,55 +513,6 @@ public Cursor getAccountsCursor() { return mAccountsCursor; } - // TODO TW C 2020-03-06 : A mettre ailleurs, car c'est pas spécifique de cette Activité - /** - * Display the balance of a transaction in a text view and format the text color to match the sign of the amount - * @param balanceTextView {@link android.widget.TextView} where balance is to be displayed - * @param balance {@link org.gnucash.android.model.Money} balance to display - */ - public static void displayBalance(final TextView balanceTextView, - final Money balance, - final AccountType accountType) { - - // - // Display amount - // - - // TODO TW C 2020-03-06 : Déterminer qui doit dire s'il faut afficher un nombre négatif ou sa valeur absolue - balanceTextView.setText(balance.formattedString()); - - // - // Define amount color - // - - @ColorInt int fontColor; - - if (balance.asBigDecimal() - .compareTo(BigDecimal.ZERO) == 0) { - // balance is null - - Context context = GnuCashApplication.getAppContext(); - - fontColor = context.getResources() - .getColor(android.R.color.black); - - } else { - // balance is not null - - final boolean isCredit = balance.isNegative(); - -// fontColor = isCredit -// ? context.getResources() -// .getColor(R.color.debit_red) -// : context.getResources() -// .getColor(R.color.credit_green); - fontColor = AccountType.getAmountColor(isCredit, - accountType); - } - - balanceTextView.setTextColor(fontColor); - } - /** * Formats the date to show the the day of the week if the {@code dateMillis} is within 7 days * of today. Else it shows the actual date formatted as short string.
From 88440026067f99bc1b85bd28ab58200c1645c4b9 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sat, 7 Mar 2020 14:20:07 +0100 Subject: [PATCH 23/77] #876 - Use AccountType.displayBalance(...) --- .../ui/report/ReportsOverviewFragment.java | 6 ++--- .../ui/report/sheet/BalanceSheetFragment.java | 23 ++++++++----------- .../ui/transaction/SplitEditorFragment.java | 14 +++++------ .../TransactionDetailActivity.java | 18 ++++++++------- .../transaction/TransactionsListFragment.java | 11 +++++---- .../dialog/TransferFundsDialogFragment.java | 10 ++++---- .../android/ui/util/AccountBalanceTask.java | 9 ++++---- .../ui/util/widget/TransactionTypeSwitch.java | 3 +-- 8 files changed, 45 insertions(+), 49 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/report/ReportsOverviewFragment.java b/app/src/main/java/org/gnucash/android/ui/report/ReportsOverviewFragment.java index 6d99db6fd..df5d58e62 100644 --- a/app/src/main/java/org/gnucash/android/ui/report/ReportsOverviewFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/report/ReportsOverviewFragment.java @@ -213,10 +213,10 @@ protected void displayReport() { mChart.highlightValues(null); mChart.invalidate(); - TransactionsActivity.displayBalance(mTotalAssets, mAssetsBalance, AccountType.ASSET); - TransactionsActivity.displayBalance(mTotalLiabilities, mLiabilitiesBalance, AccountType.LIABILITY); + AccountType.ASSET.displayBalance(mTotalAssets, mAssetsBalance); + AccountType.LIABILITY.displayBalance(mTotalLiabilities, mLiabilitiesBalance); // #8xx - TransactionsActivity.displayBalance(mNetWorth, mAssetsBalance.add(mLiabilitiesBalance), null); + AccountType.ASSET.displayBalance(mNetWorth, mAssetsBalance.add(mLiabilitiesBalance)); } /** diff --git a/app/src/main/java/org/gnucash/android/ui/report/sheet/BalanceSheetFragment.java b/app/src/main/java/org/gnucash/android/ui/report/sheet/BalanceSheetFragment.java index 2de5f32d3..2ce902d97 100644 --- a/app/src/main/java/org/gnucash/android/ui/report/sheet/BalanceSheetFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/report/sheet/BalanceSheetFragment.java @@ -33,9 +33,7 @@ import org.gnucash.android.model.Money; import org.gnucash.android.ui.report.BaseReportFragment; import org.gnucash.android.ui.report.ReportType; -import org.gnucash.android.ui.transaction.TransactionsActivity; -import java.util.ArrayList; import java.util.List; import butterknife.BindView; @@ -104,10 +102,9 @@ protected void displayReport() { loadAccountViews(LIABLITY_ACCOUNT_TYPES, mLiabilitiesTableLayout); loadAccountViews(EQUITY_ACCOUNT_TYPES, mEquityTableLayout); - TransactionsActivity.displayBalance(mNetWorth, - // #8xx - mAssetsBalance.add(mLiabilitiesBalance), - null); + AccountType.ASSET.displayBalance(mNetWorth, + // #8xx + mAssetsBalance.add(mLiabilitiesBalance)); } @Override @@ -140,9 +137,8 @@ private void loadAccountViews(List accountTypes, TableLayout tableL ((TextView)view.findViewById(R.id.account_name)).setText(name); TextView balanceTextView = (TextView) view.findViewById(R.id.account_balance); accountType = AccountType.valueOf(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseSchema.AccountEntry.COLUMN_TYPE))); - TransactionsActivity.displayBalance(balanceTextView, - balance, - accountType); + accountType.displayBalance(balanceTextView, + balance); tableLayout.addView(view); } @@ -157,11 +153,10 @@ private void loadAccountViews(List accountTypes, TableLayout tableL TextView accountBalance = (TextView) totalView.findViewById(R.id.account_balance); accountBalance.setTextSize(16); accountBalance.setTypeface(null, Typeface.BOLD); - TransactionsActivity.displayBalance(accountBalance, - mAccountsDbAdapter.getAccountBalance(accountTypes, - -1, - System.currentTimeMillis()), - accountType); + accountType.displayBalance(accountBalance, + mAccountsDbAdapter.getAccountBalance(accountTypes, + -1, + System.currentTimeMillis())); tableLayout.addView(totalView); } diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java index 7ca854708..8de1cb932 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java @@ -152,10 +152,9 @@ public void onActivityCreated(Bundle savedInstanceState) { view.findViewById(R.id.input_accounts_spinner).setEnabled(false); view.findViewById(R.id.btn_remove_split).setVisibility(View.GONE); - TransactionsActivity.displayBalance(mImbalanceTextView, - new Money(mBaseAmount.negate(), - mCommodity), - accountType); + accountType.displayBalance(mImbalanceTextView, + new Money(mBaseAmount.negate(), + mCommodity)); } } @@ -628,10 +627,9 @@ public void afterTextChanged(Editable editable) { } // for - TransactionsActivity.displayBalance(mImbalanceTextView, - new Money(imbalance, - mCommodity), - null); + AccountType.ASSET.displayBalance(mImbalanceTextView, + new Money(imbalance, + mCommodity)); } } diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java index eb22e5352..4ce39ef0a 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java @@ -18,6 +18,7 @@ import org.gnucash.android.db.adapter.AccountsDbAdapter; import org.gnucash.android.db.adapter.ScheduledActionDbAdapter; import org.gnucash.android.db.adapter.TransactionsDbAdapter; +import org.gnucash.android.model.AccountType; import org.gnucash.android.model.Money; import org.gnucash.android.model.ScheduledAction; import org.gnucash.android.model.Split; @@ -26,7 +27,6 @@ import org.gnucash.android.ui.common.FormActivity; import org.gnucash.android.ui.common.UxArgument; import org.gnucash.android.ui.passcode.PasscodeLockActivity; -import org.gnucash.android.ui.util.AccountUtils; import java.text.DateFormat; import java.util.Date; @@ -75,10 +75,11 @@ public SplitAmountViewHolder(View view, ? splitCreditView : splitDebitView; - TransactionsActivity.displayBalance(balanceView, - quantity, - AccountsDbAdapter.getInstance() - .getAccountType(split.getAccountUID())); + final AccountType accountType = AccountsDbAdapter.getInstance() + .getAccountType(split.getAccountUID()); + + accountType.displayBalance(balanceView, + quantity); } } // Class SplitAmountViewHolder @@ -161,9 +162,10 @@ private void bindViews(){ // Define in which field (Debit or Credit) the balance shall be displayed TextView balanceTextView = accountBalance.isNegative() ? mCreditBalance : mDebitBalance ; - TransactionsActivity.displayBalance(balanceTextView, - accountBalance, - accountsDbAdapter.getAccountType(mAccountUID)); + final AccountType accountType = accountsDbAdapter.getAccountType(mAccountUID); + + accountType.displayBalance(balanceTextView, + accountBalance); // // Détails diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsListFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsListFragment.java index 13dbde277..37e8c5dd6 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsListFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsListFragment.java @@ -49,6 +49,7 @@ import org.gnucash.android.db.adapter.DatabaseAdapter; import org.gnucash.android.db.adapter.SplitsDbAdapter; import org.gnucash.android.db.adapter.TransactionsDbAdapter; +import org.gnucash.android.model.AccountType; import org.gnucash.android.model.Money; import org.gnucash.android.model.Split; import org.gnucash.android.model.Transaction; @@ -275,10 +276,12 @@ public void onBindViewHolderCursor(ViewHolder holder, Cursor cursor) { final String transactionUID = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseSchema.TransactionEntry.COLUMN_UID)); Money amount = mTransactionsDbAdapter.getBalance(transactionUID, mAccountUID); - TransactionsActivity.displayBalance(holder.transactionAmount, - amount, - GnuCashApplication.getAccountsDbAdapter() - .getAccountType(mAccountUID)); + + final AccountType accountType = GnuCashApplication.getAccountsDbAdapter() + .getAccountType(mAccountUID); + + accountType.displayBalance(holder.transactionAmount, + amount); long dateMillis = cursor.getLong(cursor.getColumnIndexOrThrow(DatabaseSchema.TransactionEntry.COLUMN_TIMESTAMP)); String dateText = TransactionsActivity.getPrettyDateFormat(getActivity(), dateMillis); diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/dialog/TransferFundsDialogFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/dialog/TransferFundsDialogFragment.java index 9db08fa47..aa2eb94a1 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/dialog/TransferFundsDialogFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/dialog/TransferFundsDialogFragment.java @@ -35,14 +35,13 @@ import android.widget.TextView; import org.gnucash.android.R; -import org.gnucash.android.app.GnuCashApplication; import org.gnucash.android.db.adapter.CommoditiesDbAdapter; import org.gnucash.android.db.adapter.PricesDbAdapter; +import org.gnucash.android.model.AccountType; import org.gnucash.android.model.Commodity; import org.gnucash.android.model.Money; import org.gnucash.android.model.Price; import org.gnucash.android.ui.transaction.OnTransferFundsListener; -import org.gnucash.android.ui.transaction.TransactionsActivity; import org.gnucash.android.util.AmountParser; import java.math.BigDecimal; @@ -98,10 +97,9 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa View view = inflater.inflate(R.layout.dialog_transfer_funds, container, false); ButterKnife.bind(this, view); - TransactionsActivity.displayBalance(mStartAmountLabel, - mOriginAmount, - // TODO TW C 2020-03-05 : A vérifier - null); + // TODO TW C 2020-03-05 : A vérifier + AccountType.ASSET.displayBalance(mStartAmountLabel, + mOriginAmount); String fromCurrencyCode = mOriginAmount.getCommodity().getCurrencyCode(); mFromCurrencyLabel.setText(fromCurrencyCode); diff --git a/app/src/main/java/org/gnucash/android/ui/util/AccountBalanceTask.java b/app/src/main/java/org/gnucash/android/ui/util/AccountBalanceTask.java index 7192927c7..d9b68acd2 100644 --- a/app/src/main/java/org/gnucash/android/ui/util/AccountBalanceTask.java +++ b/app/src/main/java/org/gnucash/android/ui/util/AccountBalanceTask.java @@ -24,8 +24,8 @@ import com.crashlytics.android.Crashlytics; import org.gnucash.android.db.adapter.AccountsDbAdapter; +import org.gnucash.android.model.AccountType; import org.gnucash.android.model.Money; -import org.gnucash.android.ui.transaction.TransactionsActivity; import java.lang.ref.WeakReference; @@ -76,9 +76,10 @@ protected void onPostExecute(Money balance) { if (balanceTextView != null) { - TransactionsActivity.displayBalance(balanceTextView, - balance, - accountsDbAdapter.getAccountType(mAccountUID)); + final AccountType accountType = accountsDbAdapter.getAccountType(mAccountUID); + + accountType.displayBalance(balanceTextView, + balance); } } } diff --git a/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java b/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java index 66f249c71..b94276a87 100644 --- a/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java +++ b/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java @@ -237,8 +237,7 @@ private void setSwitchTextAndColor(final boolean isCredit) { // Set text color of views // - @ColorInt final int color = AccountType.getAmountColor(isCredit, - getAccountType()); + @ColorInt final int color = getAccountType().getAmountColor(isCredit); TransactionTypeSwitch.this.setTextColor(color); mAmountEditText.setTextColor(color); From cd0916bc0c1a10ffad3c7b3e6067babd2d5176de Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sat, 7 Mar 2020 16:01:39 +0100 Subject: [PATCH 24/77] #876 - Enhance Code Quality --- .../export/csv/CsvTransactionsExporter.java | 1 - .../gnucash/android/model/AccountType.java | 7 +- .../java/org/gnucash/android/model/Money.java | 2 +- .../ui/transaction/SplitEditorFragment.java | 1 + .../layout/activity_transaction_detail.xml | 172 ++++++++++-------- 5 files changed, 99 insertions(+), 84 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/export/csv/CsvTransactionsExporter.java b/app/src/main/java/org/gnucash/android/export/csv/CsvTransactionsExporter.java index f298cd201..25a8e704c 100644 --- a/app/src/main/java/org/gnucash/android/export/csv/CsvTransactionsExporter.java +++ b/app/src/main/java/org/gnucash/android/export/csv/CsvTransactionsExporter.java @@ -26,7 +26,6 @@ import org.gnucash.android.R; import org.gnucash.android.export.ExportParams; import org.gnucash.android.export.Exporter; -import org.gnucash.android.model.Account; import org.gnucash.android.model.Split; import org.gnucash.android.model.Transaction; import org.gnucash.android.model.TransactionType; diff --git a/app/src/main/java/org/gnucash/android/model/AccountType.java b/app/src/main/java/org/gnucash/android/model/AccountType.java index edc2e1a1d..245819b1f 100644 --- a/app/src/main/java/org/gnucash/android/model/AccountType.java +++ b/app/src/main/java/org/gnucash/android/model/AccountType.java @@ -65,7 +65,7 @@ public enum AccountType { /** * Display the balance of a transaction in a text view and format the text color to match the sign of the amount * @param balanceTextView {@link android.widget.TextView} where balance is to be displayed - * @param balance {@link org.gnucash.android.model.Money} balance to display + * @param balance {@link org.gnucash.android.model.Money} balance (>0 or <0) to display */ public void displayBalance(final TextView balanceTextView, final Money balance) { @@ -97,11 +97,6 @@ public void displayBalance(final TextView balanceTextView, final boolean isCreditBalance = balance.isNegative(); -// fontColor = isCreditBalance -// ? context.getResources() -// .getColor(R.color.debit_red) -// : context.getResources() -// .getColor(R.color.credit_green); fontColor = getAmountColor(isCreditBalance); } diff --git a/app/src/main/java/org/gnucash/android/model/Money.java b/app/src/main/java/org/gnucash/android/model/Money.java index 0c3ddd145..2df937a44 100644 --- a/app/src/main/java/org/gnucash/android/model/Money.java +++ b/app/src/main/java/org/gnucash/android/model/Money.java @@ -50,7 +50,7 @@ public final class Money implements Comparable{ private Commodity mCommodity; /** - * Amount value held by this object + * Amount value held by this object (can be > 0 or < 0) */ private BigDecimal mAmount; diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java index 8de1cb932..6cf6116ff 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java @@ -483,6 +483,7 @@ private void initViews(final Split split) { splitAmountEditText.setCommodity(split.getValue() .getCommodity()); + splitAmountEditText.setValue(split.getFormattedValue() .asBigDecimal()); diff --git a/app/src/main/res/layout/activity_transaction_detail.xml b/app/src/main/res/layout/activity_transaction_detail.xml index f0a2d3739..aac29c9f7 100644 --- a/app/src/main/res/layout/activity_transaction_detail.xml +++ b/app/src/main/res/layout/activity_transaction_detail.xml @@ -25,6 +25,7 @@ android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - + android:stretchColumns="1" + android:orientation="vertical"> - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + Date: Sat, 7 Mar 2020 21:53:34 +0100 Subject: [PATCH 25/77] #876 - Rename mValue to mAmountAbsValue and use setter to ensure storing of absolute values in splits --- .../java/org/gnucash/android/model/Split.java | 282 +++++++++++------- 1 file changed, 180 insertions(+), 102 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/model/Split.java b/app/src/main/java/org/gnucash/android/model/Split.java index 261b10a40..708d775e5 100644 --- a/app/src/main/java/org/gnucash/android/model/Split.java +++ b/app/src/main/java/org/gnucash/android/model/Split.java @@ -43,12 +43,12 @@ public class Split extends BaseModel implements Parcelable{ /** * Amount absolute value of this split which is in the currency of the transaction */ - private Money mValue; + private Money mAmountAbsValue; /** * Amount of the split in the currency of the account to which the split belongs */ - private Money mQuantity; + private Money mQuantityAbsValue; /** * Transaction UID which this split belongs to @@ -88,8 +88,11 @@ public class Split extends BaseModel implements Parcelable{ * @param accountUID String UID of transfer account */ public Split(@NonNull Money value, @NonNull Money quantity, String accountUID){ - setQuantity(quantity); + + // Store absolute value setValue(value); + setQuantity(quantity); + setAccountUID(accountUID); } @@ -110,6 +113,7 @@ public Split(@NonNull Money amount, String accountUID){ /** * Clones the sourceSplit to create a new instance with same fields + * * @param sourceSplit Split to be cloned * @param generateUID Determines if the clone should have a new UID or should * maintain the one from source @@ -119,8 +123,8 @@ public Split(Split sourceSplit, boolean generateUID){ this.mAccountUID = sourceSplit.mAccountUID; this.mSplitType = sourceSplit.mSplitType; this.mTransactionUID = sourceSplit.mTransactionUID; - this.mValue = new Money(sourceSplit.mValue); - this.mQuantity = new Money(sourceSplit.mQuantity); + setValue(new Money(sourceSplit.mAmountAbsValue)); + setQuantity(new Money(sourceSplit.mQuantityAbsValue)); //todo: clone reconciled status if (generateUID){ @@ -131,12 +135,32 @@ public Split(Split sourceSplit, boolean generateUID){ } /** - * Returns the value amount of the split + * Returns the value (= signed amount) of the split + * + * @return Money amount of the split with the currency of the transaction + * + * @see #getQuantity() + */ + public Money getValueWithSignum() { + + // splitAmount (positive or negative number) + Money signedValue = TransactionType.DEBIT.equals(getType()) + ? getValue() + : getValue().negate(); + + return signedValue; + } + + + /** + * Returns the absolute value of the amount of the split + * * @return Money amount of the split with the currency of the transaction * @see #getQuantity() */ public Money getValue() { - return mValue; + + return mAmountAbsValue; } /** @@ -145,11 +169,12 @@ public Money getValue() { *

The value is in the currency of the containing transaction. * It's stored unsigned.

* - * @param value Money value of this split + * @param amountValue Money value of this split * @see #setQuantity(Money) */ - public void setValue(Money value) { - mValue = value.abs(); + public void setValue(Money amountValue) { + + mAmountAbsValue = amountValue.abs(); } /** @@ -159,7 +184,8 @@ public void setValue(Money value) { * @see #getValue() */ public Money getQuantity() { - return mQuantity; + + return mQuantityAbsValue; } /** @@ -168,11 +194,12 @@ public Money getQuantity() { *

The quantity is in the currency of the owning account. * It will be stored unsigned.

* - * @param quantity Money quantity amount + * @param quantityAbsValue Money quantity amount * @see #setValue(Money) */ - public void setQuantity(Money quantity) { - this.mQuantity = quantity.abs(); + public void setQuantity(Money quantityAbsValue) { + + this.mQuantityAbsValue = quantityAbsValue.abs(); } /** @@ -247,12 +274,14 @@ public void setMemo(String memo) { * @return New split pair of current split * @see TransactionType#invert() */ - public Split createPair(String accountUID){ - Split pair = new Split(mValue, accountUID); + public Split createPair(String accountUID) { + + Split pair = new Split(mAmountAbsValue, + accountUID); pair.setType(mSplitType.invert()); pair.setMemo(mMemo); pair.setTransactionUID(mTransactionUID); - pair.setQuantity(mQuantity); + pair.setQuantity(mQuantityAbsValue); return pair; } @@ -262,12 +291,13 @@ public Split createPair(String accountUID){ */ protected Split clone() throws CloneNotSupportedException { super.clone(); - Split split = new Split(mValue, mAccountUID); + Split split = new Split(mAmountAbsValue, + mAccountUID); split.setUID(getUID()); split.setType(mSplitType); split.setMemo(mMemo); split.setTransactionUID(mTransactionUID); - split.setQuantity(mQuantity); + split.setQuantity(mQuantityAbsValue); return split; } @@ -279,27 +309,35 @@ protected Split clone() throws CloneNotSupportedException { * @return whether the two splits are a pair */ public boolean isPairOf(Split other) { - return mValue.equals(other.mValue) - && mSplitType.invert().equals(other.mSplitType); - } - - /** - * Returns the formatted amount (with or without negation sign) for the split value - * @return Money amount of value - * @see #getFormattedAmount(Money, String, TransactionType) - */ - public Money getFormattedValue(){ - return getFormattedAmount(mValue, mAccountUID, mSplitType); - } - /** - * Returns the formatted amount (with or without negation sign) for the quantity - * @return Money amount of quantity - * @see #getFormattedAmount(Money, String, TransactionType) - */ - public Money getFormattedQuantity(){ - return getFormattedAmount(mQuantity, mAccountUID, mSplitType); - } + return mAmountAbsValue.equals(other.mAmountAbsValue) && mSplitType.invert() + .equals(other.mSplitType); + } + + // TODO TW m 2020-03-07 : To clean +// /** +// * Returns the formatted amount (with or without negation sign) for the split value +// * @return Money amount of value +// * @see #getFormattedAmount(Money, String, TransactionType) +// */ +// public Money getFormattedAmount() { +// +// return getFormattedAmount(mValue, +// mAccountUID, +// mSplitType); +// } +// +// /** +// * Returns the formatted amount (with or without negation sign) for the quantity +// * @return Money amount of quantity +// * @see #getFormattedAmount(Money, String, TransactionType) +// */ +// public Money getFormattedQuantity(){ +// +// return getFormattedAmount(mQuantity, +// mAccountUID, +// mSplitType); +// } /** * Return the reconciled state of this split @@ -320,53 +358,64 @@ public char getReconcileState() { return mReconcileState; } - /** - * Splits are saved as absolute values to the database, with no negative numbers. - * The type of movement the split causes to the balance of an account determines - * its sign, and that depends on the split type and the account type - * @param amount Money amount to format - * @param accountUID GUID of the account - * @param splitType Transaction type of the split - * @return -{@code amount} if the amount would reduce the balance of - * {@code account}, otherwise +{@code amount} - */ - private static Money getFormattedAmount(Money amount, - String accountUID, - TransactionType splitType) { - -// boolean isDebitAccount = AccountsDbAdapter.getInstance() -// .getAccountType(accountUID) -// .hasDebitNormalBalance(); - - Money absAmount = amount.abs(); - - boolean isDebitSplit = splitType == TransactionType.DEBIT; - -// if (isDebitAccount) { -// if (isDebitSplit) { -// return absAmount; -// } else { -// return absAmount.negate(); -// } + // TODO TW C 2020-03-07 : To Clean +// /** +// * Splits are saved as absolute values to the database, with no negative numbers. +// * +// * The type of movement the split causes to the balance of an account determines +// * its sign, and that depends on the split type and the account type +// * +// * @return -{@code amount} if the amount would reduce the balance of +// * {@code account}, otherwise +{@code amount} +// */ +// public Money getFormattedValue() { +// +// // Get absolute value of the split amount +// Money amount = getValue(); +// +// // TODO TW M 2020-03-07 : Utiliser une Préférence pour afficher ou pas les nombres négatifs dans les Splits +// if (true) { +// // Preference tells to display only positive amounts in Splits +// +// // NTD +// // } else { +// // Preference tells to display positive or négative amounts in Splits +// +// boolean isDebitSplit = mSplitType == TransactionType.DEBIT; +// +//// boolean isDebitAccount = AccountsDbAdapter.getInstance() +//// .getAccountType(mAccountUID) +//// .hasDebitNormalBalance(); +//// +//// if (isDebitAccount) { +//// if (isDebitSplit) { +//// return amount; +//// } else { +//// return amount.negate(); +//// } +//// } else { +//// if (isDebitSplit) { +//// return amount.negate(); +//// } else { +//// return amount; +//// } +//// } +// // if (isDebitSplit) { -// return absAmount.negate(); +// // DEBIT +// +// // NTD +// // } else { -// return absAmount; +// // CREDIT +// +// amount = amount.negate(); // } // } - - if (isDebitSplit) { - // It is a Debit Split - - return absAmount; - - } else { - // It is not a Debit Split - - return absAmount.negate(); - } - } +// +// return amount; +// } /** * Check if this split is reconciled @@ -412,9 +461,7 @@ public void setReconcileDate(Timestamp reconcileDate) { @Override public String toString() { - return mSplitType.name() - + " of " - + mValue.toString() + return mSplitType.name() + " of " + mAmountAbsValue.toString() + " in account: " + mAccountUID + " (" @@ -438,10 +485,27 @@ public String toString() { public String toCsv(){ String sep = ";"; //TODO: add reconciled state and date - String splitString = getUID() + sep + mValue.getNumerator() + sep + mValue.getDenominator() - + sep + mValue.getCommodity().getCurrencyCode() + sep + mQuantity.getNumerator() - + sep + mQuantity.getDenominator() + sep + mQuantity.getCommodity().getCurrencyCode() - + sep + mTransactionUID + sep + mAccountUID + sep + mSplitType.name(); + String splitString = getUID() + + sep + + mAmountAbsValue.getNumerator() + + sep + + mAmountAbsValue.getDenominator() + + sep + + mAmountAbsValue.getCommodity() + .getCurrencyCode() + + sep + + mQuantityAbsValue.getNumerator() + + sep + + mQuantityAbsValue.getDenominator() + + sep + + mQuantityAbsValue.getCommodity() + .getCurrencyCode() + + sep + + mTransactionUID + + sep + + mAccountUID + + sep + + mSplitType.name(); if (mMemo != null){ splitString = splitString + sep + mMemo; } @@ -515,8 +579,12 @@ public boolean isEquivalentTo(Split split){ if (super.equals(split)) return true; if (mReconcileState != split.mReconcileState) return false; - if (!mValue.equals(split.mValue)) return false; - if (!mQuantity.equals(split.mQuantity)) return false; + if (!mAmountAbsValue.equals(split.mAmountAbsValue)) { + return false; + } + if (!mQuantityAbsValue.equals(split.mQuantityAbsValue)) { + return false; + } if (!mTransactionUID.equals(split.mTransactionUID)) return false; if (!mAccountUID.equals(split.mAccountUID)) return false; if (mSplitType != split.mSplitType) return false; @@ -540,8 +608,12 @@ public boolean equals(Object o) { Split split = (Split) o; if (mReconcileState != split.mReconcileState) return false; - if (!mValue.equals(split.mValue)) return false; - if (!mQuantity.equals(split.mQuantity)) return false; + if (!mAmountAbsValue.equals(split.mAmountAbsValue)) { + return false; + } + if (!mQuantityAbsValue.equals(split.mQuantityAbsValue)) { + return false; + } if (!mTransactionUID.equals(split.mTransactionUID)) return false; if (!mAccountUID.equals(split.mAccountUID)) return false; if (mSplitType != split.mSplitType) return false; @@ -551,8 +623,8 @@ public boolean equals(Object o) { @Override public int hashCode() { int result = super.hashCode(); - result = 31 * result + mValue.hashCode(); - result = 31 * result + mQuantity.hashCode(); + result = 31 * result + mAmountAbsValue.hashCode(); + result = 31 * result + mQuantityAbsValue.hashCode(); result = 31 * result + mTransactionUID.hashCode(); result = 31 * result + mAccountUID.hashCode(); result = 31 * result + mSplitType.hashCode(); @@ -573,13 +645,15 @@ public void writeToParcel(Parcel dest, int flags) { dest.writeString(mTransactionUID); dest.writeString(mSplitType.name()); - dest.writeLong(mValue.getNumerator()); - dest.writeLong(mValue.getDenominator()); - dest.writeString(mValue.getCommodity().getCurrencyCode()); + dest.writeLong(mAmountAbsValue.getNumerator()); + dest.writeLong(mAmountAbsValue.getDenominator()); + dest.writeString(mAmountAbsValue.getCommodity() + .getCurrencyCode()); - dest.writeLong(mQuantity.getNumerator()); - dest.writeLong(mQuantity.getDenominator()); - dest.writeString(mQuantity.getCommodity().getCurrencyCode()); + dest.writeLong(mQuantityAbsValue.getNumerator()); + dest.writeLong(mQuantityAbsValue.getDenominator()); + dest.writeString(mQuantityAbsValue.getCommodity() + .getCurrencyCode()); dest.writeString(mMemo == null ? "" : mMemo); dest.writeString(String.valueOf(mReconcileState)); @@ -600,12 +674,16 @@ private Split(Parcel source){ long valueNum = source.readLong(); long valueDenom = source.readLong(); String valueCurrency = source.readString(); - mValue = new Money(valueNum, valueDenom, valueCurrency).abs(); + setValue(new Money(valueNum, + valueDenom, + valueCurrency)); long qtyNum = source.readLong(); long qtyDenom = source.readLong(); String qtyCurrency = source.readString(); - mQuantity = new Money(qtyNum, qtyDenom, qtyCurrency).abs(); + setQuantity(new Money(qtyNum, + qtyDenom, + qtyCurrency)); String memo = source.readString(); mMemo = memo.isEmpty() ? null : memo; From 23790c87ba375b0152bbf38c997b074dc7aaa993 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sat, 7 Mar 2020 21:54:32 +0100 Subject: [PATCH 26/77] #876 - Add shallDisplayAbsValue parameter in displayBalance(...) --- .../gnucash/android/model/AccountType.java | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/model/AccountType.java b/app/src/main/java/org/gnucash/android/model/AccountType.java index 245819b1f..130b1d83a 100644 --- a/app/src/main/java/org/gnucash/android/model/AccountType.java +++ b/app/src/main/java/org/gnucash/android/model/AccountType.java @@ -59,23 +59,28 @@ public enum AccountType { } AccountType() { + this(TransactionType.CREDIT); } /** * Display the balance of a transaction in a text view and format the text color to match the sign of the amount + * * @param balanceTextView {@link android.widget.TextView} where balance is to be displayed * @param balance {@link org.gnucash.android.model.Money} balance (>0 or <0) to display */ public void displayBalance(final TextView balanceTextView, - final Money balance) { + final Money balance, + final boolean shallDisplayAbsValue) { // // Display amount // - // TODO TW C 2020-03-06 : Déterminer qui doit dire s'il faut afficher un nombre négatif ou sa valeur absolue - balanceTextView.setText(balance.formattedString()); + balanceTextView.setText(shallDisplayAbsValue + ? balance.abs() + .formattedString() + : balance.formattedString()); // // Define amount color @@ -103,6 +108,21 @@ public void displayBalance(final TextView balanceTextView, balanceTextView.setTextColor(fontColor); } + /** + * Display the balance of a transaction in a text view and format the text color to match the sign of the amount + * + * @param balanceTextView + * {@link android.widget.TextView} where balance is to be displayed + * @param balance + * {@link org.gnucash.android.model.Money} balance (>0 or <0) to display + */ + public void displayBalance(final TextView balanceTextView, + final Money balance) { + + displayBalance(balanceTextView, + balance, + false); + } /** * Compute red/green color according to accountType and isCreditAmount * From c0bf48d69472e5e326d3f5a52ad79edaec44548a Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sat, 7 Mar 2020 22:01:02 +0100 Subject: [PATCH 27/77] #876 - TransactionTypeSwitch does not change amount signum anymore --- .../ui/util/widget/TransactionTypeSwitch.java | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java b/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java index b94276a87..24853ce2a 100644 --- a/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java +++ b/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java @@ -33,14 +33,13 @@ /** * A special type of {@link android.widget.ToggleButton} which displays the appropriate DEBIT/CREDIT labels for the - * linked account type + * linked account type and update the color of the amount and currency fields as well * * checked means CREDIT * unchecked means DEBIT * * @author Ngewi Fet */ -// TODO TW m 2020-03-03 : Should be named SplitTypeToggleButton (or SplitTypeSwitch) instead of TransactionTypeSwitch public class TransactionTypeSwitch extends SwitchCompat { private AccountType mAccountType = AccountType.EXPENSE; @@ -233,6 +232,21 @@ private void setSwitchTextAndColor(final boolean isCredit) { : getTextOff() // DEBIT ); + // + // Change signum if needed + // + +// BigDecimal amount = mAmountEditText.getValue(); +// +// if (amount != null) { +// if ((isCredit && amount.signum() > 0) //we switched to debit but the amount is +ve +// || (!isCredit && amount.signum() < 0)) { //credit but amount is -ve +// +// mAmountEditText.setValue(amount.negate()); +// } +// +// } + // // Set text color of views // @@ -274,21 +288,6 @@ public void onCheckedChanged(CompoundButton compoundButton, setSwitchTextAndColor(isCredit); - // - // Change signum if needed - // - - BigDecimal amount = mAmountEditText.getValue(); - - if (amount != null) { - if ((isCredit && amount.signum() > 0) //we switched to debit but the amount is +ve - || (!isCredit && amount.signum() < 0)) { //credit but amount is -ve - - mAmountEditText.setValue(amount.negate()); - } - - } - // // Call other listeners // From 6821ba7298b2b84758949f947fb0f71cf64bdb49 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sat, 7 Mar 2020 22:02:37 +0100 Subject: [PATCH 28/77] #876 - Display amount absolute value when TransactionTypeSwitch is visible --- .../ui/transaction/SplitEditorFragment.java | 8 +- .../TransactionDetailActivity.java | 89 +++++++---- .../transaction/TransactionFormFragment.java | 151 +++++++++++++----- 3 files changed, 173 insertions(+), 75 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java index 6cf6116ff..6706e2925 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java @@ -475,7 +475,7 @@ private void initViews(final Split split) { splitUidTextView.setText(BaseModel.generateUID()); // - // Handle split + // Fill views from Split data // if (split != null) { @@ -484,8 +484,10 @@ private void initViews(final Split split) { splitAmountEditText.setCommodity(split.getValue() .getCommodity()); - splitAmountEditText.setValue(split.getFormattedValue() - .asBigDecimal()); + // TODO TW C 2020-03-07 : Mettre une préférence pour le signe + // Display abs value because switch button is visible + splitAmountEditText.setValue(split.getValueWithSignum() + .asBigDecimal().abs()); splitCurrencyTextView.setText(split.getValue() .getCommodity() diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java index 4ce39ef0a..f227780da 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java @@ -23,7 +23,6 @@ import org.gnucash.android.model.ScheduledAction; import org.gnucash.android.model.Split; import org.gnucash.android.model.Transaction; -import org.gnucash.android.model.TransactionType; import org.gnucash.android.ui.common.FormActivity; import org.gnucash.android.ui.common.UxArgument; import org.gnucash.android.ui.passcode.PasscodeLockActivity; @@ -63,23 +62,22 @@ public SplitAmountViewHolder(View view, accountName.setText(accountsDbAdapter.getAccountFullName(accountUID)); - // quantity (positive or negative number) - Money quantity = split.getFormattedQuantity(); + // splitSignedAmount (positive or negative number) + Money splitSignedAmount = split.getValueWithSignum(); // Define debit or credit view - // #8xx -// TextView balanceView = quantity.isNegative() -// ? splitDebitView -// : splitCreditView; - TextView balanceView = TransactionType.CREDIT.equals(split.getType()) + TextView balanceView = splitSignedAmount.isNegative() ? splitCreditView : splitDebitView; final AccountType accountType = AccountsDbAdapter.getInstance() .getAccountType(split.getAccountUID()); + // Display absolute value because it is displayed either in debit or credit column accountType.displayBalance(balanceView, - quantity); + splitSignedAmount, + // TODO TW C 2020-03-07 : Mettre une préférence pour le signe + true); } } // Class SplitAmountViewHolder @@ -139,7 +137,8 @@ protected void onCreate(Bundle savedInstanceState) { /** * Reads the transaction information from the database and binds it to the views */ - private void bindViews(){ + private void bindViews() { + TransactionsDbAdapter transactionsDbAdapter = TransactionsDbAdapter.getInstance(); Transaction transaction = transactionsDbAdapter.getRecord(mTransactionUID); @@ -150,45 +149,67 @@ private void bindViews(){ mTransactionAccount.setText(getString(R.string.label_inside_account_with_name, AccountsDbAdapter.getInstance().getAccountFullName(mAccountUID))); // - // Account balance + // Add Debit/Credit Labels // - AccountsDbAdapter accountsDbAdapter = AccountsDbAdapter.getInstance(); - - // Compute balance at Transaction time - Money accountBalance = accountsDbAdapter.getAccountBalance(mAccountUID, -1, transaction.getTimeMillis()); - - // #8xx - // Define in which field (Debit or Credit) the balance shall be displayed - TextView balanceTextView = accountBalance.isNegative() ? mCreditBalance : mDebitBalance ; - - final AccountType accountType = accountsDbAdapter.getAccountType(mAccountUID); + mDetailTableRows = mDetailTableLayout.getChildCount(); + int index = 0; - accountType.displayBalance(balanceTextView, - accountBalance); + LayoutInflater inflater = LayoutInflater.from(this); + View view = inflater.inflate(R.layout.item_split_amount_info, + mDetailTableLayout, + false); + ((TextView) view.findViewById(R.id.split_debit)).setText(getString(R.string.label_debit)); + ((TextView) view.findViewById(R.id.split_credit)).setText(getString(R.string.label_credit)); + mDetailTableLayout.addView(view, + index++); // // Détails // - mDetailTableRows = mDetailTableLayout.getChildCount(); + AccountsDbAdapter accountsDbAdapter = AccountsDbAdapter.getInstance(); boolean useDoubleEntry = GnuCashApplication.isDoubleEntryEnabled(); - LayoutInflater inflater = LayoutInflater.from(this); - int index = 0; for (Split split : transaction.getSplits()) { - if (!useDoubleEntry && split.getAccountUID().equals( - accountsDbAdapter.getImbalanceAccountUID(split.getValue().getCommodity()))) { - //do now show imbalance accounts for single entry use case - continue; + if (!useDoubleEntry && split.getAccountUID() + .equals(accountsDbAdapter.getImbalanceAccountUID(split.getValue() + .getCommodity()))) { + //do not show imbalance accounts for single entry use case + + } else { + + view = inflater.inflate(R.layout.item_split_amount_info, + mDetailTableLayout, + false); + SplitAmountViewHolder viewHolder = new SplitAmountViewHolder(view, + split); + mDetailTableLayout.addView(viewHolder.itemView, + index++); } + } // for - View view = inflater.inflate(R.layout.item_split_amount_info, mDetailTableLayout, false); - SplitAmountViewHolder viewHolder = new SplitAmountViewHolder(view, split); - mDetailTableLayout.addView(viewHolder.itemView, index++); - } + // + // Account balance at Transaction time + // + + // Compute balance at Transaction time + Money accountBalance = accountsDbAdapter.getAccountBalance(mAccountUID, + -1, + transaction.getTimeMillis()); + + // #8xx + // Define in which field (Debit or Credit) the balance shall be displayed + TextView balanceTextView = accountBalance.isNegative() + ? mCreditBalance + : mDebitBalance; + + final AccountType accountType = accountsDbAdapter.getAccountType(mAccountUID); + + accountType.displayBalance(balanceTextView, + accountBalance); // // Date diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java index 9101d20ec..271da6b09 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java @@ -251,14 +251,22 @@ public class TransactionFormFragment extends Fragment implements * Create the view and retrieve references to the UI elements */ @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - View v = inflater.inflate(R.layout.fragment_transaction_form, container, false); - ButterKnife.bind(this, v); + public View onCreateView(LayoutInflater inflater, + ViewGroup container, + Bundle savedInstanceState) { + + View v = inflater.inflate(R.layout.fragment_transaction_form, + container, + false); + ButterKnife.bind(this, + v); + mAmountEditText.bindListeners(mKeyboardView); + mOpenSplitEditor.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + openSplitEditor(); } }); @@ -318,8 +326,10 @@ public void onActivityCreated(Bundle savedInstanceState) { } setListeners(); + //updateTransferAccountsList must only be called after initializing mAccountsDbAdapter updateTransferAccountsList(); + mTransferAccountSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { /** * Flag for ignoring first call to this listener. @@ -427,21 +437,36 @@ public Cursor runQuery(CharSequence name) { }); mDescriptionEditText.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override - public void onItemClick(AdapterView adapterView, View view, int position, long id) { - mTransaction = new Transaction(mTransactionsDbAdapter.getRecord(id), true); + public void onItemClick(AdapterView adapterView, + View view, + int position, + long id) { + + mTransaction = new Transaction(mTransactionsDbAdapter.getRecord(id), + true); + mTransaction.setTime(System.currentTimeMillis()); + //we check here because next method will modify it and we want to catch user-modification boolean amountEntered = mAmountEditText.isInputModified(); + initializeViewsWithTransaction(); + List splitList = mTransaction.getSplits(); - boolean isSplitPair = splitList.size() == 2 && splitList.get(0).isPairOf(splitList.get(1)); - if (isSplitPair){ + boolean isSplitPair = splitList.size() == 2 && splitList.get(0) + .isPairOf(splitList.get(1)); + if (isSplitPair) { mSplitsList.clear(); if (!amountEntered) //if user already entered an amount - mAmountEditText.setValue(splitList.get(0).getValue().asBigDecimal()); + { + mAmountEditText.setValue(splitList.get(0) + .getValue() + .asBigDecimal()); + } } else { - if (amountEntered){ //if user entered own amount, clear loaded splits and use the user value + if (amountEntered) { //if user entered own amount, clear loaded splits and use the user value mSplitsList.clear(); setDoubleEntryViewsVisibility(View.VISIBLE); } else { @@ -462,19 +487,10 @@ public void onItemClick(AdapterView adapterView, View view, int position, lon * This method is called if the fragment is used for editing a transaction */ private void initializeViewsWithTransaction(){ + mDescriptionEditText.setText(mTransaction.getDescription()); mDescriptionEditText.setSelection(mDescriptionEditText.getText().length()); - mTransactionTypeSwitch.setViewsToColorize(mAmountEditText, - mCurrencyTextView); - - mTransactionTypeSwitch.setAccountType(mAccountType); - mTransactionTypeSwitch.setChecked(mTransaction.getBalance(mAccountUID).isNegative()); - - if (!mAmountEditText.isInputModified()){ - //when autocompleting, only change the amount if the user has not manually changed it already - mAmountEditText.setValue(mTransaction.getBalance(mAccountUID).asBigDecimal()); - } mCurrencyTextView.setText(mTransaction.getCommodity().getSymbol()); mNotesEditText.setText(mTransaction.getNote()); mDateTextView.setText(DATE_FORMATTER.format(mTransaction.getTimeMillis())); @@ -485,7 +501,10 @@ private void initializeViewsWithTransaction(){ //TODO: deep copy the split list. We need a copy so we can modify with impunity mSplitsList = new ArrayList<>(mTransaction.getSplits()); - toggleAmountInputEntryMode(mSplitsList.size() <= 2); + + final boolean isSimpleSplit = mSplitsList.size() <= 2; + + toggleAmountInputEntryMode(isSimpleSplit); if (mSplitsList.size() == 2){ for (Split split : mSplitsList) { @@ -496,20 +515,36 @@ private void initializeViewsWithTransaction(){ } } } + //if there are more than two splits (which is the default for one entry), then //disable editing of the transfer account. User should open editor if (mSplitsList.size() == 2 && mSplitsList.get(0).isPairOf(mSplitsList.get(1))) { + for (Split split : mTransaction.getSplits()) { //two splits, one belongs to this account and the other to another account if (mUseDoubleEntry && !split.getAccountUID().equals(mAccountUID)) { setSelectedTransferAccount(mAccountsDbAdapter.getID(split.getAccountUID())); } } + } else { setDoubleEntryViewsVisibility(View.GONE); } - String currencyCode = mTransactionsDbAdapter.getAccountCurrencyCode(mAccountUID); + if (!mAmountEditText.isInputModified()) { + //when autocompleting, only change the amount if the user has not manually changed it already + + // Compute balance signed value and display it + final BigDecimal signedTransactionBalance = mTransaction.getBalance(mAccountUID) + .asBigDecimal(); + + // TODO TW C 2020-03-07 : Mettre une préférence pour le signe + mAmountEditText.setValue(isSimpleSplit + ? signedTransactionBalance.abs() // Display abs value because switch button is visible + : signedTransactionBalance); // Display signed value because switch button is hidden + } + + String currencyCode = mTransactionsDbAdapter.getAccountCurrencyCode(mAccountUID); Commodity accountCommodity = Commodity.getInstance(currencyCode); mCurrencyTextView.setText(accountCommodity.getSymbol()); @@ -517,6 +552,7 @@ private void initializeViewsWithTransaction(){ mAmountEditText.setCommodity(commodity); mSaveTemplateCheckbox.setChecked(mTransaction.isTemplate()); + String scheduledActionUID = getArguments().getString(UxArgument.SCHEDULED_ACTION_UID); if (scheduledActionUID != null && !scheduledActionUID.isEmpty()) { ScheduledAction scheduledAction = ScheduledActionDbAdapter.getInstance().getRecord(scheduledActionUID); @@ -524,6 +560,14 @@ private void initializeViewsWithTransaction(){ mEventRecurrence.parse(mRecurrenceRule); mRecurrenceTextView.setText(scheduledAction.getRepeatString()); } + + mTransactionTypeSwitch.setViewsToColorize(mAmountEditText, + mCurrencyTextView); + mTransactionTypeSwitch.setAccountType(mAccountType); + mTransactionTypeSwitch.setChecked(mTransaction.getBalance(mAccountUID) + .isNegative()); + + } private void setDoubleEntryViewsVisibility(int visibility) { @@ -657,6 +701,7 @@ private void openSplitEditor() { mAccountUID); intent.putExtra(UxArgument.AMOUNT_STRING, baseAmountString); + intent.putParcelableArrayListExtra(UxArgument.SPLIT_LIST, (ArrayList) extractSplitsFromView()); @@ -734,35 +779,46 @@ private void setSelectedTransferAccount(long accountId){ /** * Returns a list of splits based on the input in the transaction form. + * * This only gets the splits from the simple view, and not those from the Split Editor. * If the Split Editor has been used and there is more than one split, then it returns {@link #mSplitsList} * @return List of splits in the view or {@link #mSplitsList} is there are more than 2 splits in the transaction */ - private List extractSplitsFromView(){ - if (mTransactionTypeSwitch.getVisibility() != View.VISIBLE){ + private List extractSplitsFromView() { + + if (mTransactionTypeSwitch.getVisibility() != View.VISIBLE) { return mSplitsList; } - BigDecimal amountBigd = mAmountEditText.getValue(); + BigDecimal amountBigD = mAmountEditText.getValue(); + String baseCurrencyCode = mTransactionsDbAdapter.getAccountCurrencyCode(mAccountUID); - Money value = new Money(amountBigd, Commodity.getInstance(baseCurrencyCode)); + + // Store signed amount + Money value = new Money(amountBigD, + Commodity.getInstance(baseCurrencyCode)); + Money quantity = new Money(value); - String transferAcctUID = getTransferAccountUID(); - CommoditiesDbAdapter cmdtyDbAdapter = CommoditiesDbAdapter.getInstance(); + String transferAcctUID = getTransferAccountUID(); + CommoditiesDbAdapter cmdtyDbAdapter = CommoditiesDbAdapter.getInstance(); + + if (isMultiCurrencyTransaction()) { + // multi-currency transaction - if (isMultiCurrencyTransaction()){ //if multi-currency transaction String transferCurrencyCode = mAccountsDbAdapter.getCurrencyCode(transferAcctUID); - String commodityUID = cmdtyDbAdapter.getCommodityUID(baseCurrencyCode); - String targetCmdtyUID = cmdtyDbAdapter.getCommodityUID(transferCurrencyCode); - + String commodityUID = cmdtyDbAdapter.getCommodityUID(baseCurrencyCode); + String targetCmdtyUID = cmdtyDbAdapter.getCommodityUID(transferCurrencyCode); + Pair pricePair = PricesDbAdapter.getInstance() - .getPrice(commodityUID, targetCmdtyUID); + .getPrice(commodityUID, + targetCmdtyUID); if (pricePair.first > 0 && pricePair.second > 0) { + quantity = quantity.multiply(pricePair.first.intValue()) - .divide(pricePair.second.intValue()) - .withCurrency(cmdtyDbAdapter.getRecord(targetCmdtyUID)); + .divide(pricePair.second.intValue()) + .withCurrency(cmdtyDbAdapter.getRecord(targetCmdtyUID)); } } @@ -770,21 +826,38 @@ private List extractSplitsFromView(){ Split split2; // Try to preserve the other split attributes. if (mSplitsList.size() >= 2) { + // Multi-split + split1 = mSplitsList.get(0); + // Store absolute value split1.setValue(value); + // Store absolute value split1.setQuantity(value); split1.setAccountUID(mAccountUID); split2 = mSplitsList.get(1); + // Store absolute value split2.setValue(value); + // Store absolute value split2.setQuantity(quantity); split2.setAccountUID(transferAcctUID); + } else { - split1 = new Split(value, mAccountUID); - split2 = new Split(value, quantity, transferAcctUID); + // Only 2 splits (usual case) + + // Store absolute value + split1 = new Split(value, + mAccountUID); + + // Store absolute value + split2 = new Split(value, + quantity, + transferAcctUID); } + split1.setType(mTransactionTypeSwitch.getTransactionType()); - split2.setType(mTransactionTypeSwitch.getTransactionType().invert()); + split2.setType(mTransactionTypeSwitch.getTransactionType() + .invert()); List splitList = new ArrayList<>(); splitList.add(split1); @@ -815,6 +888,7 @@ private List extractSplitsFromView(){ * @return New transaction object containing all info in the form */ private @NonNull Transaction extractTransactionFromView(){ + Calendar cal = new GregorianCalendar( mDate.get(Calendar.YEAR), mDate.get(Calendar.MONTH), @@ -881,6 +955,7 @@ private void saveNewTransaction() { } Transaction transaction = extractTransactionFromView(); + if (mEditMode) { //if editing an existing transaction transaction.setUID(mTransaction.getUID()); } From bc27e47a4f07d44f6fbd24aebe124def53135055 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sun, 8 Mar 2020 00:50:55 +0100 Subject: [PATCH 29/77] #876 - Add preference key_display_negative_signum_in_splits --- .../android/ui/account/AccountsActivity.java | 22 ++++++-- .../settings/GeneralPreferenceFragment.java | 56 +++++++++++++++---- app/src/main/res/values-fr/strings.xml | 2 + app/src/main/res/values/donottranslate.xml | 1 + app/src/main/res/values/strings.xml | 2 + .../res/xml/fragment_general_preferences.xml | 13 ++++- 6 files changed, 79 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/account/AccountsActivity.java b/app/src/main/java/org/gnucash/android/ui/account/AccountsActivity.java index 5e8a94026..f9a6459fe 100644 --- a/app/src/main/java/org/gnucash/android/ui/account/AccountsActivity.java +++ b/app/src/main/java/org/gnucash/android/ui/account/AccountsActivity.java @@ -221,6 +221,7 @@ public int getTitleRes() { @Override public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); final Intent intent = getIntent(); @@ -333,18 +334,29 @@ public void setCurrentTab(){ *

Also handles displaying the What's New dialog

*/ private void init() { - PreferenceManager.setDefaultValues(this, BooksDbAdapter.getInstance().getActiveBookUID(), - Context.MODE_PRIVATE, R.xml.fragment_transaction_preferences, true); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); - boolean firstRun = prefs.getBoolean(getString(R.string.key_first_run), true); + PreferenceManager.setDefaultValues(this, + BooksDbAdapter.getInstance() + .getActiveBookUID(), + Context.MODE_PRIVATE, + R.xml.fragment_transaction_preferences, + true); + + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + boolean firstRun = prefs.getBoolean(getString(R.string.key_first_run), + true); if (firstRun){ startActivity(new Intent(GnuCashApplication.getAppContext(), FirstRunWizardActivity.class)); - //default to using double entry and save the preference explicitly + // Default Preference to using double entry and save the preference explicitly prefs.edit().putBoolean(getString(R.string.key_use_double_entry), true).apply(); + + // Default preference not to show negative number in splits + prefs.edit().putBoolean(getString(R.string.key_display_negative_signum_in_splits), false).apply(); + finish(); + return; } diff --git a/app/src/main/java/org/gnucash/android/ui/settings/GeneralPreferenceFragment.java b/app/src/main/java/org/gnucash/android/ui/settings/GeneralPreferenceFragment.java index db07e2753..924740312 100644 --- a/app/src/main/java/org/gnucash/android/ui/settings/GeneralPreferenceFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/settings/GeneralPreferenceFragment.java @@ -107,24 +107,58 @@ public boolean onPreferenceClick(Preference preference) { } @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - if (preference.getKey().equals(getString(R.string.key_enable_passcode))) { + public boolean onPreferenceChange(Preference preference, + Object newValue) { + + if (preference.getKey() + .equals(getString(R.string.key_enable_passcode))) { + if ((Boolean) newValue) { - startActivityForResult(new Intent(getActivity(), PasscodePreferenceActivity.class), - GeneralPreferenceFragment.PASSCODE_REQUEST_CODE); + + startActivityForResult(new Intent(getActivity(), + PasscodePreferenceActivity.class), + GeneralPreferenceFragment.PASSCODE_REQUEST_CODE); + } else { - Intent passIntent = new Intent(getActivity(), PasscodeLockScreenActivity.class); - passIntent.putExtra(UxArgument.DISABLE_PASSCODE, UxArgument.DISABLE_PASSCODE); - startActivityForResult(passIntent, GeneralPreferenceFragment.REQUEST_DISABLE_PASSCODE); + + Intent passIntent = new Intent(getActivity(), + PasscodeLockScreenActivity.class); + passIntent.putExtra(UxArgument.DISABLE_PASSCODE, + UxArgument.DISABLE_PASSCODE); + startActivityForResult(passIntent, + GeneralPreferenceFragment.REQUEST_DISABLE_PASSCODE); } } - if (preference.getKey().equals(getString(R.string.key_use_account_color))) { + // + // Set Preference : use_color_in_reports + // + + if (preference.getKey() + .equals(getString(R.string.key_use_account_color))) { + getPreferenceManager().getSharedPreferences() - .edit() - .putBoolean(getString(R.string.key_use_account_color), Boolean.valueOf(newValue.toString())) - .commit(); + .edit() + .putBoolean(getString(R.string.key_use_account_color), + Boolean.valueOf(newValue.toString())) + .commit(); } + + // + // Preference : key_display_negative_signum_in_splits + // + + if (preference.getKey() + .equals(getString(R.string.key_display_negative_signum_in_splits))) { + + // Store the new value of the Preference + getPreferenceManager().getSharedPreferences() + .edit() + .putBoolean(getString(R.string.key_display_negative_signum_in_splits), + Boolean.valueOf(newValue.toString())) + .commit(); + } + return true; } diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 4767b5317..7648ad5b0 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -446,4 +446,6 @@ Sélectionnez la destination une fois l\'exportation terminée Exporter dans le dossier \'/Apps/GnuCash Android/\' sur Dropbox Préférences + Afficher le signe - dans les transactions splittées + Autoriser l\'affichage du signe - même lorsque le bouton Débit/Crédit est affiché diff --git a/app/src/main/res/values/donottranslate.xml b/app/src/main/res/values/donottranslate.xml index 39cae7266..878446c97 100644 --- a/app/src/main/res/values/donottranslate.xml +++ b/app/src/main/res/values/donottranslate.xml @@ -33,6 +33,7 @@ use_account_color last_export_destination use_compact_list + key_display_negative_signum_in_splits prefs_header_general dropbox_access_token backup_location diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9348efdae..3440e101e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -370,6 +370,8 @@ Budgets Enable compact view Enable to always use compact view for transactions list + Display negative signum in splits + Enable to display negative signum amount even when debit/credit toggle button is displayed Invalid exchange rate e.g. 1 %1$s = x.xx %2$s Invalid amount diff --git a/app/src/main/res/xml/fragment_general_preferences.xml b/app/src/main/res/xml/fragment_general_preferences.xml index fb170c2b9..208f2c21c 100644 --- a/app/src/main/res/xml/fragment_general_preferences.xml +++ b/app/src/main/res/xml/fragment_general_preferences.xml @@ -1,18 +1,29 @@ + + + + + + + - + \ No newline at end of file From 362377cf7e660e4f7a2d358f6d5b6e5191a1ac2f Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sun, 8 Mar 2020 00:51:12 +0100 Subject: [PATCH 30/77] #876 - Use preference key_display_negative_signum_in_splits --- .../ui/transaction/SplitEditorFragment.java | 12 ++++++++++-- .../ui/transaction/TransactionDetailActivity.java | 9 +++++++-- .../ui/transaction/TransactionFormFragment.java | 14 +++++++++++--- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java index 6706e2925..f74babb8b 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java @@ -25,6 +25,7 @@ import android.support.v4.widget.SimpleCursorAdapter; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; +import android.support.v7.preference.PreferenceManager; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; @@ -484,9 +485,16 @@ private void initViews(final Split split) { splitAmountEditText.setCommodity(split.getValue() .getCommodity()); - // TODO TW C 2020-03-07 : Mettre une préférence pour le signe + // Get Preference about showing signum in Splits + boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(getActivity()) + .getBoolean(getString(R.string.key_display_negative_signum_in_splits), + false); + // Display abs value because switch button is visible - splitAmountEditText.setValue(split.getValueWithSignum() + splitAmountEditText.setValue(shallDisplayNegativeSignumInSplits + ? split.getValueWithSignum() + .asBigDecimal() + : split.getValueWithSignum() .asBigDecimal().abs()); splitCurrencyTextView.setText(split.getValue() diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java index f227780da..7e887e842 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java @@ -6,6 +6,7 @@ import android.os.Build; import android.os.Bundle; import android.support.v7.app.ActionBar; +import android.support.v7.preference.PreferenceManager; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.MenuItem; @@ -73,11 +74,15 @@ public SplitAmountViewHolder(View view, final AccountType accountType = AccountsDbAdapter.getInstance() .getAccountType(split.getAccountUID()); + // Get Preference about showing signum in Splits + boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(GnuCashApplication.getAppContext()) + .getBoolean(getString(R.string.key_display_negative_signum_in_splits), + false); + // Display absolute value because it is displayed either in debit or credit column accountType.displayBalance(balanceView, splitSignedAmount, - // TODO TW C 2020-03-07 : Mettre une préférence pour le signe - true); + !shallDisplayNegativeSignumInSplits); } } // Class SplitAmountViewHolder diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java index 271da6b09..a63c3a5d8 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java @@ -29,6 +29,7 @@ import android.support.v4.widget.SimpleCursorAdapter; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; +import android.support.v7.preference.PreferenceManager; import android.text.format.DateUtils; import android.util.Log; import android.util.Pair; @@ -258,6 +259,7 @@ public View onCreateView(LayoutInflater inflater, View v = inflater.inflate(R.layout.fragment_transaction_form, container, false); + ButterKnife.bind(this, v); @@ -271,7 +273,7 @@ public void onClick(View v) { } }); return v; - } + } /** * Starts the transfer of funds from one currency to another @@ -304,7 +306,9 @@ public void onConfigurationChanged(Configuration newConfig) { @Override public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + setHasOptionsMenu(true); SharedPreferences sharedPrefs = PreferenceActivity.getActiveBookSharedPreferences(); @@ -538,8 +542,12 @@ private void initializeViewsWithTransaction(){ final BigDecimal signedTransactionBalance = mTransaction.getBalance(mAccountUID) .asBigDecimal(); - // TODO TW C 2020-03-07 : Mettre une préférence pour le signe - mAmountEditText.setValue(isSimpleSplit + // Get Preference about showing signum in Splits + boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(getActivity()) + .getBoolean(getString(R.string.key_display_negative_signum_in_splits), + false); + + mAmountEditText.setValue(isSimpleSplit && !shallDisplayNegativeSignumInSplits ? signedTransactionBalance.abs() // Display abs value because switch button is visible : signedTransactionBalance); // Display signed value because switch button is hidden } From 6db2f628fac03f89bd34e269b3c9d84bd4ae9d4b Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sun, 8 Mar 2020 02:10:22 +0100 Subject: [PATCH 31/77] #876 - Restore General Preferences after Test --- .../ui/settings/GeneralPreferenceFragment.java | 15 --------------- .../main/res/xml/fragment_general_preferences.xml | 6 ------ 2 files changed, 21 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/settings/GeneralPreferenceFragment.java b/app/src/main/java/org/gnucash/android/ui/settings/GeneralPreferenceFragment.java index 924740312..124494760 100644 --- a/app/src/main/java/org/gnucash/android/ui/settings/GeneralPreferenceFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/settings/GeneralPreferenceFragment.java @@ -144,21 +144,6 @@ public boolean onPreferenceChange(Preference preference, .commit(); } - // - // Preference : key_display_negative_signum_in_splits - // - - if (preference.getKey() - .equals(getString(R.string.key_display_negative_signum_in_splits))) { - - // Store the new value of the Preference - getPreferenceManager().getSharedPreferences() - .edit() - .putBoolean(getString(R.string.key_display_negative_signum_in_splits), - Boolean.valueOf(newValue.toString())) - .commit(); - } - return true; } diff --git a/app/src/main/res/xml/fragment_general_preferences.xml b/app/src/main/res/xml/fragment_general_preferences.xml index 208f2c21c..2b1602a47 100644 --- a/app/src/main/res/xml/fragment_general_preferences.xml +++ b/app/src/main/res/xml/fragment_general_preferences.xml @@ -3,12 +3,6 @@ - - From b1c83ce7afefc2cb8559cc0de357405ee2dd1f60 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sun, 8 Mar 2020 03:14:20 +0100 Subject: [PATCH 32/77] #876 - Use a Switch for Debit/Credit Preference instead of a ListPreference --- .../TransactionsPreferenceFragment.java | 104 ++++++++++++++---- .../transaction/TransactionFormFragment.java | 6 +- app/src/main/res/values/donottranslate.xml | 2 +- .../xml/fragment_transaction_preferences.xml | 27 +++-- 4 files changed, 105 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/settings/TransactionsPreferenceFragment.java b/app/src/main/java/org/gnucash/android/ui/settings/TransactionsPreferenceFragment.java index a388e2632..9408926ad 100644 --- a/app/src/main/java/org/gnucash/android/ui/settings/TransactionsPreferenceFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/settings/TransactionsPreferenceFragment.java @@ -22,17 +22,17 @@ import android.support.v7.app.AppCompatActivity; import android.support.v7.preference.Preference; import android.support.v7.preference.PreferenceFragmentCompat; +import android.support.v7.preference.PreferenceManager; import android.support.v7.preference.SwitchPreferenceCompat; import org.gnucash.android.R; +import org.gnucash.android.app.GnuCashApplication; import org.gnucash.android.db.DatabaseSchema; import org.gnucash.android.db.adapter.AccountsDbAdapter; import org.gnucash.android.db.adapter.BooksDbAdapter; -import org.gnucash.android.db.adapter.CommoditiesDbAdapter; import org.gnucash.android.model.Commodity; import org.gnucash.android.ui.settings.dialog.DeleteAllTransactionsConfirmationDialog; -import java.util.Currency; import java.util.List; /** @@ -44,6 +44,7 @@ public class TransactionsPreferenceFragment extends PreferenceFragmentCompat imp @Override public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); getPreferenceManager().setSharedPreferencesName(BooksDbAdapter.getInstance().getActiveBookUID()); @@ -55,29 +56,42 @@ public void onCreate(Bundle savedInstanceState) { } @Override - public void onCreatePreferences(Bundle bundle, String s) { + public void onCreatePreferences(Bundle bundle, + String s) { + addPreferencesFromResource(R.xml.fragment_transaction_preferences); } @Override public void onResume() { + super.onResume(); - - SharedPreferences sharedPreferences = getPreferenceManager().getSharedPreferences(); - String defaultTransactionType = sharedPreferences.getString( - getString(R.string.key_default_transaction_type), - getString(R.string.label_debit)); - Preference pref = findPreference(getString(R.string.key_default_transaction_type)); - setLocalizedSummary(pref, defaultTransactionType); - pref.setOnPreferenceChangeListener(this); - - pref = findPreference(getString(R.string.key_use_double_entry)); + +// SharedPreferences sharedPreferences = getPreferenceManager().getSharedPreferences(); + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(GnuCashApplication.getAppContext()); + + String keyDefaultTransactionType = getString(R.string.key_default_transaction_type_switch); + boolean isCredit = sharedPreferences.getBoolean(keyDefaultTransactionType, + true); + SwitchPreferenceCompat switchPref = (SwitchPreferenceCompat) findPreference(keyDefaultTransactionType); + setLocalizedSummary(switchPref, + isCredit); + switchPref.setChecked(isCredit); + switchPref.setOnPreferenceChangeListener(this); + + Preference pref = findPreference(getString(R.string.key_use_double_entry)); pref.setOnPreferenceChangeListener(this); String keyCompactView = getString(R.string.key_use_compact_list); - SwitchPreferenceCompat switchPref = (SwitchPreferenceCompat) findPreference(keyCompactView); + switchPref = (SwitchPreferenceCompat) findPreference(keyCompactView); switchPref.setChecked(sharedPreferences.getBoolean(keyCompactView, false)); + String keyDisplayNegativeSignumInSplits = getString(R.string.key_display_negative_signum_in_splits); + switchPref = (SwitchPreferenceCompat) findPreference(keyDisplayNegativeSignumInSplits); + switchPref.setChecked(sharedPreferences.getBoolean(keyDisplayNegativeSignumInSplits, + false)); + switchPref.setOnPreferenceChangeListener(this); + String keySaveBalance = getString(R.string.key_save_opening_balances); switchPref = (SwitchPreferenceCompat) findPreference(keySaveBalance); switchPref.setChecked(sharedPreferences.getBoolean(keySaveBalance, false)); @@ -97,13 +111,54 @@ public boolean onPreferenceClick(Preference preference) { } @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - if (preference.getKey().equals(getString(R.string.key_use_double_entry))){ + public boolean onPreferenceChange(Preference preference, + Object newValue) { + + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(GnuCashApplication.getAppContext()); + + // + // Preference : key_default_transaction_type + // + + if (preference.getKey() + .equals(getString(R.string.key_default_transaction_type_switch))) { + + // Store the new value of the Preference + sharedPreferences.edit() + .putBoolean(getString(R.string.key_default_transaction_type_switch), + Boolean.valueOf(newValue.toString())) + .commit(); + + setLocalizedSummary(preference, + ((boolean) newValue)); + } + + // + // Preference : key_use_double_entry + // + + if (preference.getKey() + .equals(getString(R.string.key_use_double_entry))) { + boolean useDoubleEntry = (Boolean) newValue; setImbalanceAccountsHidden(useDoubleEntry); - } else { - setLocalizedSummary(preference, newValue.toString()); - } + + } + + // + // Preference : key_display_negative_signum_in_splits + // + + if (preference.getKey() + .equals(getString(R.string.key_display_negative_signum_in_splits))) { + + // Store the new value of the Preference + sharedPreferences.edit() + .putBoolean(getString(R.string.key_display_negative_signum_in_splits), + Boolean.valueOf(newValue.toString())) + .commit(); + } + return true; } @@ -135,10 +190,15 @@ private void setImbalanceAccountsHidden(boolean useDoubleEntry) { /** * Localizes the label for DEBIT/CREDIT in the settings summary * @param preference Preference whose summary is to be localized - * @param value New value for the preference summary + * @param isCredit New isCredit for the preference summary */ - private void setLocalizedSummary(Preference preference, String value){ - String localizedLabel = value.equals("DEBIT") ? getString(R.string.label_debit) : getActivity().getString(R.string.label_credit); + private void setLocalizedSummary(Preference preference, + boolean isCredit) { + + String localizedLabel = isCredit + ? getString(R.string.label_credit) + : getString(R.string.label_debit); + preference.setSummary(localizedLabel); } diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java index a63c3a5d8..ba9f84575 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java @@ -612,8 +612,10 @@ private void initalizeViews() { mTransactionTypeSwitch.setAccountType(mAccountType); - String typePref = PreferenceActivity.getActiveBookSharedPreferences().getString(getString(R.string.key_default_transaction_type), "DEBIT"); - mTransactionTypeSwitch.setChecked(TransactionType.valueOf(typePref)); + Boolean isCredit = PreferenceActivity.getActiveBookSharedPreferences() + .getBoolean(getString(R.string.key_default_transaction_type_switch), + true); + mTransactionTypeSwitch.setChecked(isCredit); String code = GnuCashApplication.getDefaultCurrencyCode(); if (mAccountUID != null){ diff --git a/app/src/main/res/values/donottranslate.xml b/app/src/main/res/values/donottranslate.xml index 878446c97..2db45a64f 100644 --- a/app/src/main/res/values/donottranslate.xml +++ b/app/src/main/res/values/donottranslate.xml @@ -6,7 +6,7 @@ enable_passcode change_passcode about_gnucash - default_transaction_type + default_transaction_type_switch export_all_transactions delete_transactions_after_export export_email_target diff --git a/app/src/main/res/xml/fragment_transaction_preferences.xml b/app/src/main/res/xml/fragment_transaction_preferences.xml index b565114d3..873fe1d4f 100644 --- a/app/src/main/res/xml/fragment_transaction_preferences.xml +++ b/app/src/main/res/xml/fragment_transaction_preferences.xml @@ -15,16 +15,18 @@ limitations under the License. --> - - + + + + + + From 6754ced94d3aa083d339dc8aa5d1150416fa8df7 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sun, 8 Mar 2020 15:01:28 +0100 Subject: [PATCH 33/77] #876 - Create AccountTypeUtils --- .../android/ui/util/AccountTypeUtils.java | 164 ++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 app/src/main/java/org/gnucash/android/ui/util/AccountTypeUtils.java diff --git a/app/src/main/java/org/gnucash/android/ui/util/AccountTypeUtils.java b/app/src/main/java/org/gnucash/android/ui/util/AccountTypeUtils.java new file mode 100644 index 000000000..c10a1cb53 --- /dev/null +++ b/app/src/main/java/org/gnucash/android/ui/util/AccountTypeUtils.java @@ -0,0 +1,164 @@ +package org.gnucash.android.ui.util; + +import android.content.Context; + +import org.gnucash.android.R; +import org.gnucash.android.app.GnuCashApplication; +import org.gnucash.android.db.DatabaseSchema; +import org.gnucash.android.model.AccountType; + +/** + * Utilities for AccountType UI + * + * @author JeanGarf + */ +public class AccountTypeUtils { + + /** + * Get the debit label customized for the account type + * + * @param accountType + * Account Type + * + * @return + * The debit label customized for the account type + * + * @author JeanGarf + */ + public static String getLabelDebit(final AccountType accountType) { + + final String label; + + Context context = GnuCashApplication.getAppContext().getApplicationContext(); + + switch (accountType) { + + case CASH: + label = context.getString(R.string.label_receive); // DEBIT + break; + + case BANK: + label = context.getString(R.string.label_deposit); // DEBIT + break; + + case CREDIT: + // #876 Change according to GnuCash on Windows + label = context.getString(R.string.label_payment); // DEBIT + break; + + case ASSET: + case EQUITY: + case LIABILITY: + // #876 Change according to GnuCash on Windows + label = context.getString(R.string.label_decrease); // DEBIT + break; + + case INCOME: + // #876 Change according to GnuCash on Windows + label = context.getString(R.string.label_charge); // DEBIT + break; + + case EXPENSE: + label = context.getString(R.string.label_expense); // DEBIT + break; + + case PAYABLE: + // #876 Change according to GnuCash on Windows + label = context.getString(R.string.label_payment); // DEBIT + break; + + case RECEIVABLE: + label = context.getString(R.string.label_invoice); // DEBIT + break; + + case STOCK: + case MUTUAL: + // #876 Change according to GnuCash on Windows + label = context.getString(R.string.label_buy); // DEBIT + break; + + case CURRENCY: + case ROOT: + default: + // #876 Change according to GnuCash on Windows + label = context.getString(R.string.label_debit); // DEBIT + break; + } + + return label; + } + + /** + * Get the credit label customized for the account type + * + * @param accountType + * Account Type + * + * @return + * The credit label customized for the account type + * + * @author JeanGarf + */ + public static String getLabelCredit(final AccountType accountType) { + + final String label; + + Context context = GnuCashApplication.getAppContext().getApplicationContext(); + + switch (accountType) { + + case CASH: + label = context.getString(R.string.label_spend); // CREDIT + break; + + case BANK: + label = context.getString(R.string.label_withdrawal); // CREDIT + break; + + case CREDIT: + // #876 Change according to GnuCash on Windows + label = context.getString(R.string.label_charge); // CREDIT + break; + + case ASSET: + case EQUITY: + case LIABILITY: + // #876 Change according to GnuCash on Windows + label = context.getString(R.string.label_increase); // CREDIT + break; + + case INCOME: + // #876 Change according to GnuCash on Windows + label = context.getString(R.string.label_income); // CREDIT + break; + + case EXPENSE: + label = context.getString(R.string.label_rebate); // CREDIT + break; + + case PAYABLE: + // #876 Change according to GnuCash on Windows + label = context.getString(R.string.label_bill); // CREDIT + break; + + case RECEIVABLE: + label = context.getString(R.string.label_payment); // CREDIT + break; + + case STOCK: + case MUTUAL: + // #876 Change according to GnuCash on Windows + label = context.getString(R.string.label_sell); // CREDIT + break; + + case CURRENCY: + case ROOT: + default: + // #876 Change according to GnuCash on Windows + label = context.getString(R.string.label_credit); // CREDIT + break; + } + + return label; + } +} From 26b9b976bc1f1bc6a26e4c6c918f30243686fa3d Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sun, 8 Mar 2020 15:02:02 +0100 Subject: [PATCH 34/77] #876 - Use AccountTypeUtils --- .../ui/util/widget/TransactionTypeSwitch.java | 78 +++++-------------- 1 file changed, 18 insertions(+), 60 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java b/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java index 24853ce2a..18411806a 100644 --- a/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java +++ b/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java @@ -23,11 +23,10 @@ import android.widget.CompoundButton; import android.widget.TextView; -import org.gnucash.android.R; import org.gnucash.android.model.AccountType; import org.gnucash.android.model.TransactionType; +import org.gnucash.android.ui.util.AccountTypeUtils; -import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; @@ -99,67 +98,18 @@ public void setAccountType(AccountType accountType) { mAccountType = accountType; - Context context = getContext().getApplicationContext(); - - switch (mAccountType) { - case CASH: - setTextOff(context.getString(R.string.label_receive)); // DEBIT - setTextOn(context.getString(R.string.label_spend)); // CREDIT - break; - case BANK: - setTextOff(context.getString(R.string.label_deposit)); // DEBIT - setTextOn(context.getString(R.string.label_withdrawal)); // CREDIT - break; - case CREDIT: - // #876 Change according to GnuCash on Windows - setTextOff(context.getString(R.string.label_payment)); // DEBIT - setTextOn(context.getString(R.string.label_charge)); // CREDIT - break; - case ASSET: - case EQUITY: - case LIABILITY: - // #876 Change according to GnuCash on Windows - setTextOff(context.getString(R.string.label_decrease)); // DEBIT - setTextOn(context.getString(R.string.label_increase)); // CREDIT - break; - case INCOME: - // #876 Change according to GnuCash on Windows - setTextOff(context.getString(R.string.label_charge)); // DEBIT - setTextOn(context.getString(R.string.label_income)); // CREDIT - break; - case EXPENSE: - setTextOff(context.getString(R.string.label_expense)); // DEBIT - setTextOn(context.getString(R.string.label_rebate)); // CREDIT - break; - case PAYABLE: - // #876 Change according to GnuCash on Windows - setTextOff(context.getString(R.string.label_payment)); // DEBIT - setTextOn(context.getString(R.string.label_bill)); // CREDIT - break; - case RECEIVABLE: - setTextOff(context.getString(R.string.label_invoice)); // DEBIT - setTextOn(context.getString(R.string.label_payment)); // CREDIT - break; - case STOCK: - case MUTUAL: - // #876 Change according to GnuCash on Windows - setTextOff(context.getString(R.string.label_buy)); // DEBIT - setTextOn(context.getString(R.string.label_sell)); // CREDIT - break; - case CURRENCY: - case ROOT: - default: - // #876 Change according to GnuCash on Windows - setTextOff(context.getString(R.string.label_debit)); // DEBIT - setTextOn(context.getString(R.string.label_credit)); // CREDIT - break; - } + // + // Set switch button text + // + + setTextOff(AccountTypeUtils.getLabelDebit(mAccountType)); // DEBIT + setTextOn(AccountTypeUtils.getLabelCredit(mAccountType)); // CREDIT // // Set switch text and color // - setSwitchTextAndColor(isChecked()); + setWidgetTextColor(isChecked()); invalidate(); } @@ -221,7 +171,15 @@ public TransactionType getTransactionType() { : TransactionType.DEBIT; } - private void setSwitchTextAndColor(final boolean isCredit) { + /** + * Set the text color of the 3 views of the widget + * according to is a Credit amount + * and the Account type + * + * @param isCredit + * true if amount is negative + */ + private void setWidgetTextColor(final boolean isCredit) { // // Set switch text @@ -286,7 +244,7 @@ public void onCheckedChanged(CompoundButton compoundButton, // Set switch text and color // - setSwitchTextAndColor(isCredit); + setWidgetTextColor(isCredit); // // Call other listeners From 143dbe04f371e397104cdfffa71241d704e875fa Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sun, 8 Mar 2020 15:03:32 +0100 Subject: [PATCH 35/77] #876 - Trying to display customized debit/credit labels in Transaction Detail, but commented because not very readable --- .../TransactionDetailActivity.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java index 7e887e842..8938f3f8e 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java @@ -27,6 +27,7 @@ import org.gnucash.android.ui.common.FormActivity; import org.gnucash.android.ui.common.UxArgument; import org.gnucash.android.ui.passcode.PasscodeLockActivity; +import org.gnucash.android.ui.util.AccountTypeUtils; import java.text.DateFormat; import java.util.Date; @@ -186,9 +187,30 @@ private void bindViews() { } else { +// // +// // Add Account Specific Debit/Credit Labels +// // +// +// view = inflater.inflate(R.layout.item_split_amount_info, +// mDetailTableLayout, +// false); +// +// AccountType accountType = AccountsDbAdapter.getInstance().getAccountType(split.getAccountUID()); +// +// ((TextView) view.findViewById(R.id.split_debit)).setText(AccountTypeUtils.getLabelDebit(accountType)); +// ((TextView) view.findViewById(R.id.split_credit)).setText(AccountTypeUtils.getLabelCredit(accountType)); +// +// mDetailTableLayout.addView(view, +// index++); + + // + // Display Debit/Credit amount + // + view = inflater.inflate(R.layout.item_split_amount_info, mDetailTableLayout, false); + SplitAmountViewHolder viewHolder = new SplitAmountViewHolder(view, split); mDetailTableLayout.addView(viewHolder.itemView, From a21d2e24a453000ea35a15322e1f2c715f76ba14 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sun, 8 Mar 2020 15:03:48 +0100 Subject: [PATCH 36/77] #876 - Enhance Code Quality --- .../java/org/gnucash/android/model/Money.java | 1 - .../java/org/gnucash/android/model/Split.java | 84 ------------------- .../dialog/TransferFundsDialogFragment.java | 1 - 3 files changed, 86 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/model/Money.java b/app/src/main/java/org/gnucash/android/model/Money.java index 2df937a44..013904aef 100644 --- a/app/src/main/java/org/gnucash/android/model/Money.java +++ b/app/src/main/java/org/gnucash/android/model/Money.java @@ -258,7 +258,6 @@ public String asString(){ return toPlainString(); } - // TODO TW C 2020-03-06 : A factoriser avec Split.getFormattedAmount /** * Returns a string representation of the Money object formatted according to * the locale and includes the currency symbol. diff --git a/app/src/main/java/org/gnucash/android/model/Split.java b/app/src/main/java/org/gnucash/android/model/Split.java index 708d775e5..5816b950d 100644 --- a/app/src/main/java/org/gnucash/android/model/Split.java +++ b/app/src/main/java/org/gnucash/android/model/Split.java @@ -314,31 +314,6 @@ public boolean isPairOf(Split other) { .equals(other.mSplitType); } - // TODO TW m 2020-03-07 : To clean -// /** -// * Returns the formatted amount (with or without negation sign) for the split value -// * @return Money amount of value -// * @see #getFormattedAmount(Money, String, TransactionType) -// */ -// public Money getFormattedAmount() { -// -// return getFormattedAmount(mValue, -// mAccountUID, -// mSplitType); -// } -// -// /** -// * Returns the formatted amount (with or without negation sign) for the quantity -// * @return Money amount of quantity -// * @see #getFormattedAmount(Money, String, TransactionType) -// */ -// public Money getFormattedQuantity(){ -// -// return getFormattedAmount(mQuantity, -// mAccountUID, -// mSplitType); -// } - /** * Return the reconciled state of this split *

@@ -358,65 +333,6 @@ public char getReconcileState() { return mReconcileState; } - // TODO TW C 2020-03-07 : To Clean -// /** -// * Splits are saved as absolute values to the database, with no negative numbers. -// * -// * The type of movement the split causes to the balance of an account determines -// * its sign, and that depends on the split type and the account type -// * -// * @return -{@code amount} if the amount would reduce the balance of -// * {@code account}, otherwise +{@code amount} -// */ -// public Money getFormattedValue() { -// -// // Get absolute value of the split amount -// Money amount = getValue(); -// -// // TODO TW M 2020-03-07 : Utiliser une Préférence pour afficher ou pas les nombres négatifs dans les Splits -// if (true) { -// // Preference tells to display only positive amounts in Splits -// -// // NTD -// -// } else { -// // Preference tells to display positive or négative amounts in Splits -// -// boolean isDebitSplit = mSplitType == TransactionType.DEBIT; -// -//// boolean isDebitAccount = AccountsDbAdapter.getInstance() -//// .getAccountType(mAccountUID) -//// .hasDebitNormalBalance(); -//// -//// if (isDebitAccount) { -//// if (isDebitSplit) { -//// return amount; -//// } else { -//// return amount.negate(); -//// } -//// } else { -//// if (isDebitSplit) { -//// return amount.negate(); -//// } else { -//// return amount; -//// } -//// } -// -// if (isDebitSplit) { -// // DEBIT -// -// // NTD -// -// } else { -// // CREDIT -// -// amount = amount.negate(); -// } -// } -// -// return amount; -// } - /** * Check if this split is reconciled * @return {@code true} if the split is reconciled, {@code false} otherwise diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/dialog/TransferFundsDialogFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/dialog/TransferFundsDialogFragment.java index aa2eb94a1..3b25336d2 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/dialog/TransferFundsDialogFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/dialog/TransferFundsDialogFragment.java @@ -97,7 +97,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa View view = inflater.inflate(R.layout.dialog_transfer_funds, container, false); ButterKnife.bind(this, view); - // TODO TW C 2020-03-05 : A vérifier AccountType.ASSET.displayBalance(mStartAmountLabel, mOriginAmount); From 393cc698a7a09573d6188f97aa8c69a50c4bba09 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sun, 8 Mar 2020 15:30:24 +0100 Subject: [PATCH 37/77] #876 - Correct Transaction Detail Balance translation --- app/src/main/res/values-fr/strings.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 7648ad5b0..7aa76ab51 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -109,7 +109,7 @@ Email par défaut pour les exports. Vous pourrez toujours le changer lors de votre prochain export. Toutes les transactions seront un transfert d’un compte à un autre Activer la Double entrée - Solde à la date indiquée ci-dessous + Solde du Compte à la date indiquée ci-dessous Entrer un nom de compte pour créer un compte Monnaie Compte parent diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3440e101e..70406cba3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -109,7 +109,7 @@ The default email address to send exports to. You can still change this when you export. All transactions will be a transfer from one account to another Activate Double Entry - Balance at date below + Account Balance at Date below Enter an account name to create an account Currency Parent account From 05c7c7057a044be53255a5ad11a388de4d0d1a90 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sun, 8 Mar 2020 15:32:53 +0100 Subject: [PATCH 38/77] #876 - Add a separator before Balance in Transaction Detail --- .../res/layout/activity_transaction_detail.xml | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/app/src/main/res/layout/activity_transaction_detail.xml b/app/src/main/res/layout/activity_transaction_detail.xml index aac29c9f7..b46f5c272 100644 --- a/app/src/main/res/layout/activity_transaction_detail.xml +++ b/app/src/main/res/layout/activity_transaction_detail.xml @@ -61,7 +61,7 @@ android:layout_marginLeft="8dp" android:layout_marginEnd="8dp" android:layout_marginRight="8dp" - android:maxLines="1" + android:singleLine="true" android:ellipsize="start" android:textStyle="italic" android:textColor="@android:color/white" @@ -83,6 +83,22 @@ android:stretchColumns="1" android:orientation="vertical"> + + + + + + Date: Sun, 8 Mar 2020 19:07:08 +0100 Subject: [PATCH 39/77] #876 - Fix error on title_default_transaction_type in fragment_transaction_preferences.xml --- app/src/main/res/values-fr/strings.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- app/src/main/res/xml/fragment_transaction_preferences.xml | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 7aa76ab51..608b05de4 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -95,7 +95,7 @@ Préférences des transactions Préférences du compte Type de transaction par défaut - Le type de transaction à utiliser par défaut, CRÉDIT ou DÉBIT + CRÉDIT CRÉDIT DÉBIT diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 70406cba3..a16292a80 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -95,7 +95,7 @@ Transaction Preferences Account Preferences Default Transaction Type - The type of transaction to use by default, CREDIT or DEBIT + CREDIT CREDIT DEBIT diff --git a/app/src/main/res/xml/fragment_transaction_preferences.xml b/app/src/main/res/xml/fragment_transaction_preferences.xml index 873fe1d4f..0149b866b 100644 --- a/app/src/main/res/xml/fragment_transaction_preferences.xml +++ b/app/src/main/res/xml/fragment_transaction_preferences.xml @@ -18,8 +18,8 @@ Date: Wed, 11 Mar 2020 21:59:17 +0100 Subject: [PATCH 40/77] #876 - Correct ASSET toggle switch button labels --- .../gnucash/android/ui/util/AccountTypeUtils.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/util/AccountTypeUtils.java b/app/src/main/java/org/gnucash/android/ui/util/AccountTypeUtils.java index c10a1cb53..4992b63da 100644 --- a/app/src/main/java/org/gnucash/android/ui/util/AccountTypeUtils.java +++ b/app/src/main/java/org/gnucash/android/ui/util/AccountTypeUtils.java @@ -41,12 +41,15 @@ public static String getLabelDebit(final AccountType accountType) { label = context.getString(R.string.label_deposit); // DEBIT break; - case CREDIT: + case ASSET: // #876 Change according to GnuCash on Windows + label = context.getString(R.string.label_increase); // DEBIT + break; + + case CREDIT: label = context.getString(R.string.label_payment); // DEBIT break; - case ASSET: case EQUITY: case LIABILITY: // #876 Change according to GnuCash on Windows @@ -115,12 +118,15 @@ public static String getLabelCredit(final AccountType accountType) { label = context.getString(R.string.label_withdrawal); // CREDIT break; - case CREDIT: + case ASSET: // #876 Change according to GnuCash on Windows + label = context.getString(R.string.label_decrease); // CREDIT + break; + + case CREDIT: label = context.getString(R.string.label_charge); // CREDIT break; - case ASSET: case EQUITY: case LIABILITY: // #876 Change according to GnuCash on Windows From b9d5aee46ab847fd2b802f3e327aae2cc4ddc8d2 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Wed, 11 Mar 2020 22:02:18 +0100 Subject: [PATCH 41/77] #876 - Compute DefaultTransactionType for new Transactions, according to Default new Transaction Type Modes --- .../gnucash/android/model/AccountType.java | 51 ++++++++++++++++++- .../transaction/TransactionFormFragment.java | 13 +++-- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/model/AccountType.java b/app/src/main/java/org/gnucash/android/model/AccountType.java index 130b1d83a..27f37dcd7 100644 --- a/app/src/main/java/org/gnucash/android/model/AccountType.java +++ b/app/src/main/java/org/gnucash/android/model/AccountType.java @@ -7,6 +7,7 @@ import org.gnucash.android.R; import org.gnucash.android.app.GnuCashApplication; +import org.gnucash.android.ui.settings.PreferenceActivity; import java.math.BigDecimal; import java.util.ArrayList; @@ -41,10 +42,19 @@ public enum AccountType { AccountType.BANK)); public final static List LIABLITY_ACCOUNT_TYPES = new ArrayList(Arrays.asList(AccountType.LIABILITY, - AccountType.CREDIT)); + AccountType.CREDIT)); public final static List EQUITY_ACCOUNT_TYPES = new ArrayList(Arrays.asList(AccountType.EQUITY)); + // + // Preference Key (must be the same as in donottranslate.xml) + // + + public static final String KEY_USE_NORMAL_BALANCE_EXPENSE = "KEY_USE_NORMAL_BALANCE_EXPENSE"; + public static final String KEY_USE_NORMAL_BALANCE_INCOME = "KEY_USE_NORMAL_BALANCE_INCOME"; + public static final String KEY_DEBIT = "KEY_DEBIT"; + public static final String KEY_CREDIT = "KEY_CREDIT"; + /** * Indicates that this type of normal balance the account type has @@ -63,6 +73,45 @@ public enum AccountType { this(TransactionType.CREDIT); } + /** + * @return + */ + public TransactionType getDefaultTransactionType() { + + + String transactionTypePref = PreferenceActivity.getActiveBookSharedPreferences() + .getString(GnuCashApplication.getAppContext() + .getString(R.string.key_default_transaction_type), + KEY_USE_NORMAL_BALANCE_EXPENSE); + + final TransactionType transactionType; + + if (KEY_USE_NORMAL_BALANCE_EXPENSE.equals(transactionTypePref)) { + // Use Normal Balance (Expense Mode) + + // Use Account Normal Balance as default, except for Asset which are CREDIT by default + transactionType = isAssetAccount() + ? TransactionType.CREDIT + : getNormalBalanceType(); + + } else if (KEY_USE_NORMAL_BALANCE_INCOME.equals(transactionTypePref)) { + // Use Normal Balance (Income Mode) + + // Use Account Normal Balance as default + transactionType = getNormalBalanceType(); + + } else { + // Not Automatic mode + + // Convert String to Enum + transactionType = KEY_DEBIT.equals(transactionTypePref) + ? TransactionType.DEBIT + : TransactionType.CREDIT; + } + return transactionType; + } + + /** * Display the balance of a transaction in a text view and format the text color to match the sign of the amount * diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java index ba9f84575..7b8516be8 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java @@ -602,6 +602,7 @@ public void onClick(View v) { * Initialize views with default data for new transactions */ private void initalizeViews() { + Date time = new Date(System.currentTimeMillis()); mDateTextView.setText(DATE_FORMATTER.format(time)); mTimeTextView.setText(TIME_FORMATTER.format(time)); @@ -612,10 +613,14 @@ private void initalizeViews() { mTransactionTypeSwitch.setAccountType(mAccountType); - Boolean isCredit = PreferenceActivity.getActiveBookSharedPreferences() - .getBoolean(getString(R.string.key_default_transaction_type_switch), - true); - mTransactionTypeSwitch.setChecked(isCredit); + // + // According to the Default Transaction Type Preference and the AccountType + // Check or Uncheck the TransactionTypeSwitch + // + + final TransactionType transactionType = mAccountType.getDefaultTransactionType(); + + mTransactionTypeSwitch.setChecked(transactionType); String code = GnuCashApplication.getDefaultCurrencyCode(); if (mAccountUID != null){ From 0ef72cf954deff5c1f03086fca3eb710b5b09533 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Wed, 11 Mar 2020 22:10:08 +0100 Subject: [PATCH 42/77] #876 - Preference for new Default Transaction Type computing mode --- .../TransactionsPreferenceFragment.java | 77 +++++++++++++------ app/src/main/res/values-fr/strings.xml | 10 ++- app/src/main/res/values/donottranslate.xml | 10 ++- app/src/main/res/values/strings.xml | 10 ++- .../xml/fragment_transaction_preferences.xml | 6 +- 5 files changed, 76 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/settings/TransactionsPreferenceFragment.java b/app/src/main/java/org/gnucash/android/ui/settings/TransactionsPreferenceFragment.java index 9408926ad..5878f83f7 100644 --- a/app/src/main/java/org/gnucash/android/ui/settings/TransactionsPreferenceFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/settings/TransactionsPreferenceFragment.java @@ -30,6 +30,7 @@ import org.gnucash.android.db.DatabaseSchema; import org.gnucash.android.db.adapter.AccountsDbAdapter; import org.gnucash.android.db.adapter.BooksDbAdapter; +import org.gnucash.android.model.AccountType; import org.gnucash.android.model.Commodity; import org.gnucash.android.ui.settings.dialog.DeleteAllTransactionsConfirmationDialog; @@ -67,31 +68,51 @@ public void onResume() { super.onResume(); -// SharedPreferences sharedPreferences = getPreferenceManager().getSharedPreferences(); SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(GnuCashApplication.getAppContext()); - String keyDefaultTransactionType = getString(R.string.key_default_transaction_type_switch); - boolean isCredit = sharedPreferences.getBoolean(keyDefaultTransactionType, - true); - SwitchPreferenceCompat switchPref = (SwitchPreferenceCompat) findPreference(keyDefaultTransactionType); - setLocalizedSummary(switchPref, - isCredit); - switchPref.setChecked(isCredit); - switchPref.setOnPreferenceChangeListener(this); + // + // Default Transaction Type computing mode + // + + String keyDefaultTransactionType = getString(R.string.key_default_transaction_type); + String defaultTransactionTypeKey = sharedPreferences.getString(keyDefaultTransactionType, + AccountType.KEY_USE_NORMAL_BALANCE_EXPENSE); + + Preference pref = findPreference(keyDefaultTransactionType); + setPrefSummary(pref, + defaultTransactionTypeKey); + + pref.setOnPreferenceChangeListener(this); + + // + // Double entry + // + + pref = findPreference(getString(R.string.key_use_double_entry)); + pref.setOnPreferenceChangeListener(this); - Preference pref = findPreference(getString(R.string.key_use_double_entry)); - pref.setOnPreferenceChangeListener(this); + // + // Compact list + // - String keyCompactView = getString(R.string.key_use_compact_list); - switchPref = (SwitchPreferenceCompat) findPreference(keyCompactView); + String keyCompactView = getString(R.string.key_use_compact_list); + SwitchPreferenceCompat switchPref = (SwitchPreferenceCompat) findPreference(keyCompactView); switchPref.setChecked(sharedPreferences.getBoolean(keyCompactView, false)); + // + // Display negative signums + // + String keyDisplayNegativeSignumInSplits = getString(R.string.key_display_negative_signum_in_splits); switchPref = (SwitchPreferenceCompat) findPreference(keyDisplayNegativeSignumInSplits); switchPref.setChecked(sharedPreferences.getBoolean(keyDisplayNegativeSignumInSplits, false)); switchPref.setOnPreferenceChangeListener(this); + // + // Save opening balance + // + String keySaveBalance = getString(R.string.key_save_opening_balances); switchPref = (SwitchPreferenceCompat) findPreference(keySaveBalance); switchPref.setChecked(sharedPreferences.getBoolean(keySaveBalance, false)); @@ -121,16 +142,16 @@ public boolean onPreferenceChange(Preference preference, // if (preference.getKey() - .equals(getString(R.string.key_default_transaction_type_switch))) { + .equals(getString(R.string.key_default_transaction_type))) { // Store the new value of the Preference sharedPreferences.edit() - .putBoolean(getString(R.string.key_default_transaction_type_switch), - Boolean.valueOf(newValue.toString())) + .putString(getString(R.string.key_default_transaction_type), + newValue.toString()) .commit(); - setLocalizedSummary(preference, - ((boolean) newValue)); + setPrefSummary(preference, + ((String) newValue)); } // @@ -188,16 +209,22 @@ private void setImbalanceAccountsHidden(boolean useDoubleEntry) { } } /** - * Localizes the label for DEBIT/CREDIT in the settings summary + * Localizes the label for AUTOMATIC/DEBIT/CREDIT in the settings summary + * * @param preference Preference whose summary is to be localized - * @param isCredit New isCredit for the preference summary + * @param defaultTransactionTypeKey New defaultTransactionTypeKey for the preference summary */ - private void setLocalizedSummary(Preference preference, - boolean isCredit) { + private void setPrefSummary(Preference preference, + String defaultTransactionTypeKey) { + + String localizedLabel = AccountType.KEY_USE_NORMAL_BALANCE_EXPENSE.equals(defaultTransactionTypeKey) + ? getString(R.string.label_use_account_usual_balance_expense_mode) + : AccountType.KEY_USE_NORMAL_BALANCE_INCOME.equals(defaultTransactionTypeKey) + ? getString(R.string.label_use_account_usual_balance_income_mode) + : AccountType.KEY_DEBIT.equals(defaultTransactionTypeKey) + ? getString(R.string.label_debit) + : getString(R.string.label_credit); - String localizedLabel = isCredit - ? getString(R.string.label_credit) - : getString(R.string.label_debit); preference.setSummary(localizedLabel); } diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 608b05de4..ca5f54be2 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -95,10 +95,14 @@ Préférences des transactions Préférences du compte Type de transaction par défaut - CRÉDIT + @string/label_credit + Utiliser le Solde habituel du Compte (sauf pour les Actifs) + Utiliser le Solde habituel du Compte - CRÉDIT - DÉBIT + @string/label_use_account_usual_balance_expense_mode + @string/label_use_account_usual_balance_income_mode + @string/label_debit + @string/label_credit Êtes vous sûre de vouloir supprimer TOUTES les transactions ? Êtes vous sûre de vouloir supprimer cette transaction ? diff --git a/app/src/main/res/values/donottranslate.xml b/app/src/main/res/values/donottranslate.xml index 2db45a64f..b0e02f15f 100644 --- a/app/src/main/res/values/donottranslate.xml +++ b/app/src/main/res/values/donottranslate.xml @@ -6,7 +6,7 @@ enable_passcode change_passcode about_gnucash - default_transaction_type_switch + default_transaction_type export_all_transactions delete_transactions_after_export export_email_target @@ -37,9 +37,11 @@ prefs_header_general dropbox_access_token backup_location - - CREDIT - DEBIT + + KEY_USE_NORMAL_BALANCE_EXPENSE + KEY_USE_NORMAL_BALANCE_INCOME + KEY_DEBIT + KEY_CREDIT CASH diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a16292a80..ca4433022 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -95,10 +95,14 @@ Transaction Preferences Account Preferences Default Transaction Type - CREDIT + @string/label_credit + Use Account usual Balance (Exept for Assets) + Use Account usual Balance - CREDIT - DEBIT + @string/label_use_account_usual_balance_expense_mode + @string/label_use_account_usual_balance_income_mode + @string/label_debit + @string/label_credit Are you sure you want to delete ALL transactions? Are you sure you want to delete this transaction? diff --git a/app/src/main/res/xml/fragment_transaction_preferences.xml b/app/src/main/res/xml/fragment_transaction_preferences.xml index 0149b866b..e73300b26 100644 --- a/app/src/main/res/xml/fragment_transaction_preferences.xml +++ b/app/src/main/res/xml/fragment_transaction_preferences.xml @@ -16,10 +16,12 @@ --> - Date: Wed, 11 Mar 2020 22:10:30 +0100 Subject: [PATCH 43/77] #876 - Enhance Code Quality --- .../java/org/gnucash/android/model/TransactionType.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/model/TransactionType.java b/app/src/main/java/org/gnucash/android/model/TransactionType.java index 83a58690d..cf3fc626a 100644 --- a/app/src/main/java/org/gnucash/android/model/TransactionType.java +++ b/app/src/main/java/org/gnucash/android/model/TransactionType.java @@ -17,14 +17,17 @@ package org.gnucash.android.model; /** - * Type of transaction, a credit or a debit + * Type of a Split of a Transaction + * + * It is a credit or a debit * * @author Ngewi Fet * @author Jesse Shieh */ // TODO TW m 2020-03-03 : Should be named SplitType public enum TransactionType { - DEBIT, CREDIT; + DEBIT, + CREDIT; private TransactionType opposite; From 6ec5b17762ceaa994d386aa8f51c0cf431cbc884 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sun, 5 Apr 2020 02:27:41 +0200 Subject: [PATCH 44/77] #876 - Use different colors for balance, according to Account Type : Expense/Income or not --- .../gnucash/android/model/AccountType.java | 29 ++++++++++++++++--- app/src/main/res/values/colors.xml | 2 ++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/model/AccountType.java b/app/src/main/java/org/gnucash/android/model/AccountType.java index 27f37dcd7..a4f2e9fb4 100644 --- a/app/src/main/java/org/gnucash/android/model/AccountType.java +++ b/app/src/main/java/org/gnucash/android/model/AccountType.java @@ -190,14 +190,35 @@ public int getAmountColor(final boolean isCreditAmount) { if ((isCreditAmount && !debitCreditInvertedColorAccountType) || (!isCreditAmount && debitCreditInvertedColorAccountType)) { // Credit amount and account like Assets, Bank, Cash..., or Debit amount and account like Expense/Income - // RED - colorRes = R.color.debit_red; + if (!isExpenseOrIncomeAccount()) { + // It is not an Expense/Income account + + // RED + colorRes = R.color.debit_red; + + } else { + // It is an Expense/Income account + + // PURPLE + colorRes = R.color.debit_expense_income; + } } else { // Credit amount and account like Expense/Income, or Debit amount and account like Assets, Bank, Cash...) - // GREEN - colorRes = R.color.credit_green; + if (!isExpenseOrIncomeAccount()) { + // It is not an Expense/Income account + + // GREEN + colorRes = R.color.credit_green; + + } else { + // It is an Expense/Income account + + // BLUE + colorRes = R.color.credit_expense_income; + } + } return GnuCashApplication.getAppContext() diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 9122dd7d7..a2777a6b6 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -17,6 +17,8 @@ #c11b17 #4cc552 + #FF9800 + #00BCD4 #FFAAAAAA #00000000 #ff33b5e5 From 391d0b5ff7d03c6ebba3a2bf2414ec8d3159b872 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sun, 5 Apr 2020 02:39:11 +0200 Subject: [PATCH 45/77] #876 - Balance colors more close --- app/src/main/res/values/colors.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index a2777a6b6..5c06ac41f 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -17,8 +17,8 @@ #c11b17 #4cc552 - #FF9800 - #00BCD4 + #F4511E + #00897B #FFAAAAAA #00000000 #ff33b5e5 From de30be2b6762b8e7be684e2843c7836a3ddb4e71 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sun, 5 Apr 2020 02:43:34 +0200 Subject: [PATCH 46/77] #876 - Add AccountType.getBalanceWithSignumForDisplay() --- .../gnucash/android/model/AccountType.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/app/src/main/java/org/gnucash/android/model/AccountType.java b/app/src/main/java/org/gnucash/android/model/AccountType.java index a4f2e9fb4..b6cec0f5b 100644 --- a/app/src/main/java/org/gnucash/android/model/AccountType.java +++ b/app/src/main/java/org/gnucash/android/model/AccountType.java @@ -246,6 +246,44 @@ public boolean hasDebitNormalBalance() { return mNormalBalance == TransactionType.DEBIT; } + /** + * Returns balance with the right signum to be displayed + * + * A Debit is always the addition of a positive amount + * A credit is always the substraction of a positive amount + * The balance is always Debit - Credit + * Therefore : + * Debit > Credit => balance is > 0 + * Debit < Credit => balance is < 0 + * + * But for display, habit is to reduce the use of negative numbers + * To achieve this, for accounts which USUALLY have : + * Debit > Credit => compute balance as usual + * Debit < Credit => negate balance + * + * @return + * balance with the right signum to be displayed + */ + public Money getBalanceWithSignumForDisplay(final Money balance) { + + final Money balanceWithSignumForDisplay; + + if (hasDebitNormalBalance()) { + // Account usually debitor + + balanceWithSignumForDisplay = balance; + + } else { + // account usually creditor + + // Negate + balanceWithSignumForDisplay = balance.negate(); + } + + return balanceWithSignumForDisplay; + } + + // // Getters/Setters // From 04f958f8b9c32ef054d571123a486c16c5eb0789 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Tue, 7 Apr 2020 08:28:51 +0200 Subject: [PATCH 47/77] #876 - Add colors for Equity --- .../gnucash/android/model/AccountType.java | 38 ++++++++++++------- app/src/main/res/values/colors.xml | 2 + 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/model/AccountType.java b/app/src/main/java/org/gnucash/android/model/AccountType.java index b6cec0f5b..3527b1055 100644 --- a/app/src/main/java/org/gnucash/android/model/AccountType.java +++ b/app/src/main/java/org/gnucash/android/model/AccountType.java @@ -190,33 +190,43 @@ public int getAmountColor(final boolean isCreditAmount) { if ((isCreditAmount && !debitCreditInvertedColorAccountType) || (!isCreditAmount && debitCreditInvertedColorAccountType)) { // Credit amount and account like Assets, Bank, Cash..., or Debit amount and account like Expense/Income - if (!isExpenseOrIncomeAccount()) { - // It is not an Expense/Income account + if (isExpenseOrIncomeAccount()) { + // It is an Expense/Income account - // RED - colorRes = R.color.debit_red; + // BLUE + colorRes = R.color.debit_expense_income; + + } else if(isEquityAccount()) { + // It is an Equity account + + colorRes = R.color.debit_equity; } else { - // It is an Expense/Income account + // It is not an Expense/Income account - // PURPLE - colorRes = R.color.debit_expense_income; + // RED + colorRes = R.color.debit_red; } } else { // Credit amount and account like Expense/Income, or Debit amount and account like Assets, Bank, Cash...) - if (!isExpenseOrIncomeAccount()) { - // It is not an Expense/Income account - - // GREEN - colorRes = R.color.credit_green; - - } else { + if (isExpenseOrIncomeAccount()) { // It is an Expense/Income account // BLUE colorRes = R.color.credit_expense_income; + + } else if(isEquityAccount()) { + // It is an Equity account + + colorRes = R.color.credit_equity; + + } else { + // It is not an Expense/Income account + + // GREEN + colorRes = R.color.credit_green; } } diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 5c06ac41f..08fe7c982 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -15,6 +15,8 @@ limitations under the License. --> + #F4511E + #00897B #c11b17 #4cc552 #F4511E From 183696fa02e39d3fdbdab587406e0ab91ca42705 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sat, 23 May 2020 14:16:21 +0200 Subject: [PATCH 48/77] #876 - Invert Memo and Account Spinner --- app/src/main/res/layout/item_split_entry.xml | 22 ++++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/app/src/main/res/layout/item_split_entry.xml b/app/src/main/res/layout/item_split_entry.xml index 90fa38150..3c9cdf8fd 100644 --- a/app/src/main/res/layout/item_split_entry.xml +++ b/app/src/main/res/layout/item_split_entry.xml @@ -20,10 +20,12 @@ limitations under the License. android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical"> + + android:layout_height="wrap_content" + android:orientation="horizontal"> + + + - - Date: Sat, 23 May 2020 16:30:07 +0200 Subject: [PATCH 49/77] #876 - Update Transaction and Split Amount when changing TransactionTypeSwitch --- .../gnucash/android/model/AccountType.java | 38 -------- .../gnucash/android/model/Transaction.java | 70 +++++++++----- .../ui/transaction/SplitEditorFragment.java | 42 ++++++--- .../transaction/TransactionFormFragment.java | 92 +++++++++++++++---- app/src/main/res/values/colors.xml | 4 +- 5 files changed, 150 insertions(+), 96 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/model/AccountType.java b/app/src/main/java/org/gnucash/android/model/AccountType.java index 3527b1055..4564bae1f 100644 --- a/app/src/main/java/org/gnucash/android/model/AccountType.java +++ b/app/src/main/java/org/gnucash/android/model/AccountType.java @@ -256,44 +256,6 @@ public boolean hasDebitNormalBalance() { return mNormalBalance == TransactionType.DEBIT; } - /** - * Returns balance with the right signum to be displayed - * - * A Debit is always the addition of a positive amount - * A credit is always the substraction of a positive amount - * The balance is always Debit - Credit - * Therefore : - * Debit > Credit => balance is > 0 - * Debit < Credit => balance is < 0 - * - * But for display, habit is to reduce the use of negative numbers - * To achieve this, for accounts which USUALLY have : - * Debit > Credit => compute balance as usual - * Debit < Credit => negate balance - * - * @return - * balance with the right signum to be displayed - */ - public Money getBalanceWithSignumForDisplay(final Money balance) { - - final Money balanceWithSignumForDisplay; - - if (hasDebitNormalBalance()) { - // Account usually debitor - - balanceWithSignumForDisplay = balance; - - } else { - // account usually creditor - - // Negate - balanceWithSignumForDisplay = balance.negate(); - } - - return balanceWithSignumForDisplay; - } - - // // Getters/Setters // diff --git a/app/src/main/java/org/gnucash/android/model/Transaction.java b/app/src/main/java/org/gnucash/android/model/Transaction.java index c6d41dac6..ff73893df 100644 --- a/app/src/main/java/org/gnucash/android/model/Transaction.java +++ b/app/src/main/java/org/gnucash/android/model/Transaction.java @@ -289,39 +289,51 @@ private Money getImbalance(){ * @param splitList List of splits * @return Money list of splits */ + // TODO TW C 2020-04-07 : A renommer computeSplitListBalance() public static Money computeBalance(String accountUID, List splitList) { AccountsDbAdapter accountsDbAdapter = AccountsDbAdapter.getInstance(); - AccountType accountType = accountsDbAdapter.getAccountType(accountUID); - String accountCurrencyCode = accountsDbAdapter.getAccountCurrencyCode(accountUID); -// boolean isDebitAccount = accountType.hasDebitNormalBalance(); + String accountCurrencyCode = accountsDbAdapter.getAccountCurrencyCode(accountUID); Money balance = Money.createZeroInstance(accountCurrencyCode); for (Split split : splitList) { - if (!split.getAccountUID().equals(accountUID)) + if (!split.getAccountUID() + .equals(accountUID)) { + + // TODO TW M 2020-05-23 : Pourrait être supprimé ? continue; - // - // Get Amount absolute value - // + } else { - Money amount; - if (split.getValue().getCommodity().getCurrencyCode().equals(accountCurrencyCode)){ - amount = split.getValue(); - } else { //if this split belongs to the account, then either its value or quantity is in the account currency - amount = split.getQuantity(); - } + // + // Get Amount absolute value + // - // - // Compute balance - // + Money amount; - boolean isDebitSplit = split.getType() == TransactionType.DEBIT; + if (split.getValue() + .getCommodity() + .getCurrencyCode() + .equals(accountCurrencyCode)) { + + amount = split.getValue(); + + } else { + // this split belongs to the account, then either its value or quantity is in the account currency + + amount = split.getQuantity(); + } - // #876 + // + // Compute balance + // + + boolean isDebitSplit = (split.getType() == TransactionType.DEBIT); + + // #876 // if (isDebitAccount) { // if (isDebitSplit) { // balance = balance.add(amount); @@ -335,19 +347,29 @@ public static Money computeBalance(String accountUID, List splitList) { // balance = balance.add(amount); // } // } - if (isDebitSplit) { - // DEBIT - balance = balance.add(amount); + if (isDebitSplit) { + // DEBIT - } else { - // CREDIT + balance = balance.add(amount); + + } else { + // CREDIT - balance = balance.subtract(amount); + balance = balance.subtract(amount); + } } } // for +// // +// // Negate balance if account is usually creditor +// // +// +// AccountType accountType = accountsDbAdapter.getAccountType(accountUID); +// +// balance = accountType.getBalanceWithSignumForDisplay(balance); + return balance; } diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java index f74babb8b..d664e75ff 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java @@ -386,7 +386,7 @@ public SplitViewHolder(View splitView, this.splitView = splitView; // Set Listeners - setListeners(); + setListeners(split); if (split != null && !split.getQuantity() .equals(split.getValue())) { @@ -397,7 +397,7 @@ public SplitViewHolder(View splitView, initViews(split); } - private void setListeners() { + private void setListeners(Split split) { // // Listeners on splitAmountEditText @@ -440,6 +440,13 @@ public void onClick(View view) { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + // Change Transaction Type according to splitTypeSwitch + split.setType(splitTypeSwitch.getTransactionType()); + + // Update Split Amount Signum + updateSplitAmountEditText(split); + + // Recompute Split List Balance mImbalanceWatcher.afterTextChanged(null); } }); @@ -485,17 +492,8 @@ private void initViews(final Split split) { splitAmountEditText.setCommodity(split.getValue() .getCommodity()); - // Get Preference about showing signum in Splits - boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(getActivity()) - .getBoolean(getString(R.string.key_display_negative_signum_in_splits), - false); - - // Display abs value because switch button is visible - splitAmountEditText.setValue(shallDisplayNegativeSignumInSplits - ? split.getValueWithSignum() - .asBigDecimal() - : split.getValueWithSignum() - .asBigDecimal().abs()); + // Update Split Amount EditText + updateSplitAmountEditText(split); splitCurrencyTextView.setText(split.getValue() .getCommodity() @@ -515,6 +513,22 @@ private void initViews(final Split split) { } } + private void updateSplitAmountEditText(final Split split) { + + // Get Preference about showing signum in Splits + boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(getActivity()) + .getBoolean(getString(R.string.key_display_negative_signum_in_splits), + false); + + final Money splitValueWithSignum = split.getValueWithSignum(); + + // Display abs value because switch button is visible + splitAmountEditText.setValue(!shallDisplayNegativeSignumInSplits + ? splitValueWithSignum.asBigDecimal() + .abs() + : splitValueWithSignum.asBigDecimal()); + } + /** * Returns the value of the amount in the splitAmountEditText field without setting the value to the view *

If the expression in the view is currently incomplete or invalid, null is returned. @@ -562,7 +576,7 @@ public void transferComplete(Money amount) { // /** - * Updates the displayed balance of the accounts when the amount of a split is changed + * Updates the displayed balance of the list of Splits when the amount of a split is changed */ private class BalanceTextWatcher implements TextWatcher { diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java index 7b8516be8..374daad6e 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java @@ -44,6 +44,7 @@ import android.widget.AdapterView; import android.widget.AutoCompleteTextView; import android.widget.CheckBox; +import android.widget.CompoundButton; import android.widget.EditText; import android.widget.FilterQueryProvider; import android.widget.ImageView; @@ -462,19 +463,31 @@ public void onItemClick(AdapterView adapterView, boolean isSplitPair = splitList.size() == 2 && splitList.get(0) .isPairOf(splitList.get(1)); if (isSplitPair) { + mSplitsList.clear(); - if (!amountEntered) //if user already entered an amount - { + + if (!amountEntered) { + // user already entered an amount + + // TODO TW C 2020-05-23 : Il faudrait homogénéïser en appelant updateAmountEditText ? mAmountEditText.setValue(splitList.get(0) .getValue() .asBigDecimal()); } + } else { - if (amountEntered) { //if user entered own amount, clear loaded splits and use the user value + + if (amountEntered) { + //if user entered own amount, clear loaded splits and use the user value + mSplitsList.clear(); setDoubleEntryViewsVisibility(View.VISIBLE); + } else { - if (mUseDoubleEntry) { //don't hide the view in single entry mode + + if (mUseDoubleEntry) { + //don't hide the view in single entry mode + setDoubleEntryViewsVisibility(View.GONE); } } @@ -538,18 +551,7 @@ private void initializeViewsWithTransaction(){ if (!mAmountEditText.isInputModified()) { //when autocompleting, only change the amount if the user has not manually changed it already - // Compute balance signed value and display it - final BigDecimal signedTransactionBalance = mTransaction.getBalance(mAccountUID) - .asBigDecimal(); - - // Get Preference about showing signum in Splits - boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(getActivity()) - .getBoolean(getString(R.string.key_display_negative_signum_in_splits), - false); - - mAmountEditText.setValue(isSimpleSplit && !shallDisplayNegativeSignumInSplits - ? signedTransactionBalance.abs() // Display abs value because switch button is visible - : signedTransactionBalance); // Display signed value because switch button is hidden + updateAmountEditText(); } String currencyCode = mTransactionsDbAdapter.getAccountCurrencyCode(mAccountUID); @@ -578,6 +580,33 @@ private void initializeViewsWithTransaction(){ } + private void updateAmountEditText() { + + // Get Preference about showing signum in Splits + boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(getActivity()) + .getBoolean(getString(R.string.key_display_negative_signum_in_splits), + false); + + // Compute balance signed value and display it + final Money signedTransactionBalance = mTransaction.getBalance(mAccountUID); + + // Unsigned transaction balance + final Money unsignedTransactionBalance = signedTransactionBalance.abs(); + + // signed ou unsigned transaction balance to display + final Money signedTransactionBalanceToDisplay; + + signedTransactionBalanceToDisplay = mTransactionTypeSwitch.isChecked() + ? unsignedTransactionBalance.negate() + : unsignedTransactionBalance; + + mAmountEditText.setValue(!shallDisplayNegativeSignumInSplits + ? signedTransactionBalanceToDisplay.asBigDecimal() + .abs() + // Display abs value because switch button is visible + : signedTransactionBalanceToDisplay.asBigDecimal()); // Display signed value because switch button is hidden + } + private void setDoubleEntryViewsVisibility(int visibility) { mDoubleEntryLayout.setVisibility(visibility); mTransactionTypeSwitch.setVisibility(visibility); @@ -622,15 +651,25 @@ private void initalizeViews() { mTransactionTypeSwitch.setChecked(transactionType); + // + // Display Transaction Amount and Currency + // + String code = GnuCashApplication.getDefaultCurrencyCode(); if (mAccountUID != null){ code = mTransactionsDbAdapter.getAccountCurrencyCode(mAccountUID); } Commodity commodity = Commodity.getInstance(code); + mCurrencyTextView.setText(commodity.getSymbol()); + mAmountEditText.setCommodity(commodity); + // + // Select Transfer Other Account + // + if (mUseDoubleEntry){ String currentAccountUID = mAccountUID; long defaultTransferAccountID; @@ -638,7 +677,10 @@ private void initalizeViews() { do { defaultTransferAccountID = mAccountsDbAdapter.getDefaultTransferAccountID(mAccountsDbAdapter.getID(currentAccountUID)); if (defaultTransferAccountID > 0) { + + // Select Other Account setSelectedTransferAccount(defaultTransferAccountID); + break; //we found a parent with default transfer setting } currentAccountUID = mAccountsDbAdapter.getParentAccountUID(currentAccountUID); @@ -728,10 +770,20 @@ private void openSplitEditor() { /** * Sets click listeners for the dialog buttons */ - private void setListeners() { + private void setListeners() { mTransactionTypeSwitch.setColorizeOnCheckedChangeListener(); + mTransactionTypeSwitch.addOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, + boolean isChecked) { + + // Update Amount Signum + updateAmountEditText(); + } + }); + mDateTextView.setOnClickListener(new View.OnClickListener() { @Override @@ -1103,11 +1155,15 @@ private boolean canSave(){ * @param splitList List of splits produced in the fragment */ public void setSplitList(List splitList){ + mSplitsList = splitList; + Money balance = Transaction.computeBalance(mAccountUID, mSplitsList); - mAmountEditText.setValue(balance.asBigDecimal()); mTransactionTypeSwitch.setChecked(balance.isNegative()); + +// mAmountEditText.setValue(balance.asBigDecimal()); + updateAmountEditText(); } diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 08fe7c982..80048cb78 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -15,8 +15,8 @@ limitations under the License. --> - #F4511E - #00897B + #8E24AA + #1E88E5 #c11b17 #4cc552 #F4511E From 529860ef1e6827796bb8a9ffde966fab470c978c Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sat, 23 May 2020 16:33:05 +0200 Subject: [PATCH 50/77] #876 - Cleaning code --- .../java/org/gnucash/android/model/Transaction.java | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/model/Transaction.java b/app/src/main/java/org/gnucash/android/model/Transaction.java index ff73893df..4587aa3b4 100644 --- a/app/src/main/java/org/gnucash/android/model/Transaction.java +++ b/app/src/main/java/org/gnucash/android/model/Transaction.java @@ -289,7 +289,6 @@ private Money getImbalance(){ * @param splitList List of splits * @return Money list of splits */ - // TODO TW C 2020-04-07 : A renommer computeSplitListBalance() public static Money computeBalance(String accountUID, List splitList) { AccountsDbAdapter accountsDbAdapter = AccountsDbAdapter.getInstance(); @@ -303,7 +302,6 @@ public static Money computeBalance(String accountUID, List splitList) { if (!split.getAccountUID() .equals(accountUID)) { - // TODO TW M 2020-05-23 : Pourrait être supprimé ? continue; } else { @@ -479,17 +477,6 @@ public static TransactionType getTypeForBalance(AccountType accountType, boolean public static boolean wouldDecreaseBalance(AccountType accountType, TransactionType transactionType) { - // TODO TW M 2020-03-02 : TBC -// if (accountType.hasDebitNormalBalance()) { -// // Account usually with DEBIT > CREDIT -// -// return transactionType == TransactionType.CREDIT; -// -// } else { -// // Account usually with DEBIT < CREDIT -// -// return transactionType == TransactionType.DEBIT; -// } return transactionType == TransactionType.CREDIT; } From 000a42e994c157ee2dfbb2a6f7e0cad8fb790175 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sat, 23 May 2020 22:37:05 +0200 Subject: [PATCH 51/77] #876 - Factorize calls to AccountType.displayBalance() + add shallDisplayNegativeSignumInSplits parameter --- .../gnucash/android/model/AccountType.java | 67 +++++++++++++------ .../ui/account/AccountsListFragment.java | 10 ++- .../ui/report/ReportsOverviewFragment.java | 19 ++++-- .../ui/report/sheet/BalanceSheetFragment.java | 26 ++++++- .../ui/transaction/SplitEditorFragment.java | 15 ++++- .../TransactionDetailActivity.java | 12 ++-- .../transaction/TransactionFormFragment.java | 28 ++++---- .../ui/transaction/TransactionsActivity.java | 14 ++-- .../transaction/TransactionsListFragment.java | 9 ++- .../dialog/TransferFundsDialogFragment.java | 9 ++- .../android/ui/util/AccountBalanceTask.java | 9 ++- .../ui/util/widget/TransactionTypeSwitch.java | 3 + 12 files changed, 163 insertions(+), 58 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/model/AccountType.java b/app/src/main/java/org/gnucash/android/model/AccountType.java index 4564bae1f..90e9268bd 100644 --- a/app/src/main/java/org/gnucash/android/model/AccountType.java +++ b/app/src/main/java/org/gnucash/android/model/AccountType.java @@ -3,6 +3,7 @@ import android.content.Context; import android.support.annotation.ColorInt; import android.support.annotation.ColorRes; +import android.support.v7.preference.PreferenceManager; import android.widget.TextView; import org.gnucash.android.R; @@ -120,16 +121,18 @@ public TransactionType getDefaultTransactionType() { */ public void displayBalance(final TextView balanceTextView, final Money balance, - final boolean shallDisplayAbsValue) { + final boolean shallDisplayNegativeSignumInSplits) { // // Display amount // - balanceTextView.setText(shallDisplayAbsValue - ? balance.abs() - .formattedString() - : balance.formattedString()); + final Money balanceToDisplay = getBalanceWithSignumForDisplay(balance); + + balanceTextView.setText(!shallDisplayNegativeSignumInSplits + ? balanceToDisplay.abs() + .formattedString() + : balanceToDisplay.formattedString()); // // Define amount color @@ -157,21 +160,6 @@ public void displayBalance(final TextView balanceTextView, balanceTextView.setTextColor(fontColor); } - /** - * Display the balance of a transaction in a text view and format the text color to match the sign of the amount - * - * @param balanceTextView - * {@link android.widget.TextView} where balance is to be displayed - * @param balance - * {@link org.gnucash.android.model.Money} balance (>0 or <0) to display - */ - public void displayBalance(final TextView balanceTextView, - final Money balance) { - - displayBalance(balanceTextView, - balance, - false); - } /** * Compute red/green color according to accountType and isCreditAmount * @@ -256,6 +244,45 @@ public boolean hasDebitNormalBalance() { return mNormalBalance == TransactionType.DEBIT; } + /** + * Returns balance with the right signum to be displayed + * + * A Debit is always the addition of a positive amount + * A credit is always the substraction of a positive amount + * The balance is always Debit - Credit + * Therefore : + * Debit > Credit => balance is > 0 + * Debit < Credit => balance is < 0 + * + * But for display, habit is to reduce the use of negative numbers + * To achieve this, for accounts which USUALLY have : + * Debit > Credit => compute balance as usual + * Debit < Credit => negate balance + * + * @return + * balance with the right signum to be displayed + */ + public Money getBalanceWithSignumForDisplay(final Money balance) { + + final Money balanceWithSignumForDisplay; + + if (hasDebitNormalBalance()) { + // Account usually debitor + + // balance = debit - credit => usually > 0 if hasDebitNormalBalance() + balanceWithSignumForDisplay = balance; + + } else { + // account usually creditor + + // balance = debit - credit => usually < 0 if !hasDebitNormalBalance() => negate() to get a usually > 0 value + balanceWithSignumForDisplay = balance.negate(); + } + + return balanceWithSignumForDisplay; + } + + // // Getters/Setters // diff --git a/app/src/main/java/org/gnucash/android/ui/account/AccountsListFragment.java b/app/src/main/java/org/gnucash/android/ui/account/AccountsListFragment.java index 62b67a641..117f1d492 100644 --- a/app/src/main/java/org/gnucash/android/ui/account/AccountsListFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/account/AccountsListFragment.java @@ -25,7 +25,6 @@ import android.database.Cursor; import android.graphics.Color; import android.os.AsyncTask; -import android.os.Build; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager.LoaderCallbacks; @@ -33,6 +32,7 @@ import android.support.v4.view.MenuItemCompat; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; +import android.support.v7.preference.PreferenceManager; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.PopupMenu; @@ -499,8 +499,14 @@ public void onBindViewHolderCursor(final AccountViewHolder holder, final Cursor // add a summary of transactions to the account view + // Get Preference about showing signum in Splits + boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(getActivity()) + .getBoolean(getString(R.string.key_display_negative_signum_in_splits), + false); // Make sure the balance task is truly multithread - new AccountBalanceTask(holder.accountBalance).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, accountUID); + new AccountBalanceTask(holder.accountBalance, + shallDisplayNegativeSignumInSplits).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, + accountUID); String accountColor = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseSchema.AccountEntry.COLUMN_COLOR_CODE)); int colorCode = accountColor == null ? Color.TRANSPARENT : Color.parseColor(accountColor); diff --git a/app/src/main/java/org/gnucash/android/ui/report/ReportsOverviewFragment.java b/app/src/main/java/org/gnucash/android/ui/report/ReportsOverviewFragment.java index df5d58e62..cb7aef695 100644 --- a/app/src/main/java/org/gnucash/android/ui/report/ReportsOverviewFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/report/ReportsOverviewFragment.java @@ -22,6 +22,7 @@ import android.support.v4.app.FragmentManager; import android.support.v4.content.ContextCompat; import android.support.v4.view.ViewCompat; +import android.support.v7.preference.PreferenceManager; import android.support.v7.widget.AppCompatButton; import android.view.Menu; import android.view.View; @@ -44,7 +45,6 @@ import org.gnucash.android.ui.report.linechart.CashFlowLineChartFragment; import org.gnucash.android.ui.report.piechart.PieChartFragment; import org.gnucash.android.ui.report.sheet.BalanceSheetFragment; -import org.gnucash.android.ui.transaction.TransactionsActivity; import org.joda.time.LocalDate; import java.util.ArrayList; @@ -213,10 +213,21 @@ protected void displayReport() { mChart.highlightValues(null); mChart.invalidate(); - AccountType.ASSET.displayBalance(mTotalAssets, mAssetsBalance); - AccountType.LIABILITY.displayBalance(mTotalLiabilities, mLiabilitiesBalance); + // Get Preference about showing signum in Splits + boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(getActivity()) + .getBoolean(getString(R.string.key_display_negative_signum_in_splits), + false); + + AccountType.ASSET.displayBalance(mTotalAssets, + mAssetsBalance, + shallDisplayNegativeSignumInSplits); + AccountType.LIABILITY.displayBalance(mTotalLiabilities, + mLiabilitiesBalance, + shallDisplayNegativeSignumInSplits); // #8xx - AccountType.ASSET.displayBalance(mNetWorth, mAssetsBalance.add(mLiabilitiesBalance)); + AccountType.ASSET.displayBalance(mNetWorth, + mAssetsBalance.add(mLiabilitiesBalance), + shallDisplayNegativeSignumInSplits); } /** diff --git a/app/src/main/java/org/gnucash/android/ui/report/sheet/BalanceSheetFragment.java b/app/src/main/java/org/gnucash/android/ui/report/sheet/BalanceSheetFragment.java index 2ce902d97..41bb8b7ec 100644 --- a/app/src/main/java/org/gnucash/android/ui/report/sheet/BalanceSheetFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/report/sheet/BalanceSheetFragment.java @@ -19,6 +19,7 @@ import android.graphics.Typeface; import android.os.Bundle; import android.support.annotation.Nullable; +import android.support.v7.preference.PreferenceManager; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.Menu; @@ -102,9 +103,14 @@ protected void displayReport() { loadAccountViews(LIABLITY_ACCOUNT_TYPES, mLiabilitiesTableLayout); loadAccountViews(EQUITY_ACCOUNT_TYPES, mEquityTableLayout); + // Get Preference about showing signum in Splits + boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(getActivity()) + .getBoolean(getString(R.string.key_display_negative_signum_in_splits), + false); AccountType.ASSET.displayBalance(mNetWorth, // #8xx - mAssetsBalance.add(mLiabilitiesBalance)); + mAssetsBalance.add(mLiabilitiesBalance), + shallDisplayNegativeSignumInSplits); } @Override @@ -137,8 +143,16 @@ private void loadAccountViews(List accountTypes, TableLayout tableL ((TextView)view.findViewById(R.id.account_name)).setText(name); TextView balanceTextView = (TextView) view.findViewById(R.id.account_balance); accountType = AccountType.valueOf(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseSchema.AccountEntry.COLUMN_TYPE))); + + // Get Preference about showing signum in Splits + boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(getActivity()) + .getBoolean(getString(R.string.key_display_negative_signum_in_splits), + false); + accountType.displayBalance(balanceTextView, - balance); + balance, + shallDisplayNegativeSignumInSplits); + tableLayout.addView(view); } @@ -153,10 +167,16 @@ private void loadAccountViews(List accountTypes, TableLayout tableL TextView accountBalance = (TextView) totalView.findViewById(R.id.account_balance); accountBalance.setTextSize(16); accountBalance.setTypeface(null, Typeface.BOLD); + + // Get Preference about showing signum in Splits + boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(getActivity()) + .getBoolean(getString(R.string.key_display_negative_signum_in_splits), + false); accountType.displayBalance(accountBalance, mAccountsDbAdapter.getAccountBalance(accountTypes, -1, - System.currentTimeMillis())); + System.currentTimeMillis()), + shallDisplayNegativeSignumInSplits); tableLayout.addView(totalView); } diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java index d664e75ff..7073d7db0 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java @@ -153,9 +153,14 @@ public void onActivityCreated(Bundle savedInstanceState) { view.findViewById(R.id.input_accounts_spinner).setEnabled(false); view.findViewById(R.id.btn_remove_split).setVisibility(View.GONE); + // Get Preference about showing signum in Splits + boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(getActivity()) + .getBoolean(getString(R.string.key_display_negative_signum_in_splits), + false); accountType.displayBalance(mImbalanceTextView, new Money(mBaseAmount.negate(), - mCommodity)); + mCommodity), + shallDisplayNegativeSignumInSplits); } } @@ -652,9 +657,15 @@ public void afterTextChanged(Editable editable) { } // for + // Get Preference about showing signum in Splits + boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(getActivity()) + .getBoolean(getString(R.string.key_display_negative_signum_in_splits), + false); + AccountType.ASSET.displayBalance(mImbalanceTextView, new Money(imbalance, - mCommodity)); + mCommodity), + shallDisplayNegativeSignumInSplits); } } diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java index 8938f3f8e..380409832 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java @@ -27,7 +27,6 @@ import org.gnucash.android.ui.common.FormActivity; import org.gnucash.android.ui.common.UxArgument; import org.gnucash.android.ui.passcode.PasscodeLockActivity; -import org.gnucash.android.ui.util.AccountTypeUtils; import java.text.DateFormat; import java.util.Date; @@ -67,7 +66,7 @@ public SplitAmountViewHolder(View view, // splitSignedAmount (positive or negative number) Money splitSignedAmount = split.getValueWithSignum(); - // Define debit or credit view + // Use debit or credit view TextView balanceView = splitSignedAmount.isNegative() ? splitCreditView : splitDebitView; @@ -83,7 +82,7 @@ public SplitAmountViewHolder(View view, // Display absolute value because it is displayed either in debit or credit column accountType.displayBalance(balanceView, splitSignedAmount, - !shallDisplayNegativeSignumInSplits); + shallDisplayNegativeSignumInSplits); } } // Class SplitAmountViewHolder @@ -235,8 +234,13 @@ private void bindViews() { final AccountType accountType = accountsDbAdapter.getAccountType(mAccountUID); + // Get Preference about showing signum in Splits + boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()) + .getBoolean(getString(R.string.key_display_negative_signum_in_splits), + false); accountType.displayBalance(balanceTextView, - accountBalance); + accountBalance, + shallDisplayNegativeSignumInSplits); // // Date diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java index 374daad6e..4c8e7e84d 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java @@ -587,24 +587,26 @@ private void updateAmountEditText() { .getBoolean(getString(R.string.key_display_negative_signum_in_splits), false); - // Compute balance signed value and display it + // Compute balance signed value of saved transaction final Money signedTransactionBalance = mTransaction.getBalance(mAccountUID); - // Unsigned transaction balance - final Money unsignedTransactionBalance = signedTransactionBalance.abs(); + final boolean isCredit = signedTransactionBalance.isNegative(); - // signed ou unsigned transaction balance to display - final Money signedTransactionBalanceToDisplay; + // + // Negate balance if account is usually creditor + // + + AccountType accountType = GnuCashApplication.getAccountsDbAdapter() + .getAccountType(mAccountUID); - signedTransactionBalanceToDisplay = mTransactionTypeSwitch.isChecked() - ? unsignedTransactionBalance.negate() - : unsignedTransactionBalance; + // New signed transaction balance if typeSwitch has changed + final Money newSignedTransactionBalance = mTransactionTypeSwitch.isChecked() != isCredit + ? signedTransactionBalance.negate() + : signedTransactionBalance; - mAmountEditText.setValue(!shallDisplayNegativeSignumInSplits - ? signedTransactionBalanceToDisplay.asBigDecimal() - .abs() - // Display abs value because switch button is visible - : signedTransactionBalanceToDisplay.asBigDecimal()); // Display signed value because switch button is hidden + accountType.displayBalance(mAmountEditText, + newSignedTransactionBalance, + shallDisplayNegativeSignumInSplits); } private void setDoubleEntryViewsVisibility(int visibility) { diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java index 565395a33..c4cdb75c2 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java @@ -24,7 +24,6 @@ import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; -import android.support.annotation.ColorInt; import android.support.annotation.NonNull; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.TabLayout; @@ -33,6 +32,7 @@ import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; +import android.support.v7.preference.PreferenceManager; import android.text.format.DateUtils; import android.util.Log; import android.util.SparseArray; @@ -51,8 +51,6 @@ import org.gnucash.android.db.adapter.AccountsDbAdapter; import org.gnucash.android.db.adapter.TransactionsDbAdapter; import org.gnucash.android.model.Account; -import org.gnucash.android.model.AccountType; -import org.gnucash.android.model.Money; import org.gnucash.android.ui.account.AccountsActivity; import org.gnucash.android.ui.account.AccountsListFragment; import org.gnucash.android.ui.account.OnAccountClickedListener; @@ -64,7 +62,6 @@ import org.gnucash.android.util.QualifiedAccountNameCursorAdapter; import org.joda.time.LocalDate; -import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.Date; @@ -276,8 +273,13 @@ public void refresh(String accountUID) { if (mPagerAdapter != null) mPagerAdapter.notifyDataSetChanged(); - new AccountBalanceTask(mSumTextView).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, - getCurrentAccountUID()); + // Get Preference about showing signum in Splits + boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()) + .getBoolean(getString(R.string.key_display_negative_signum_in_splits), + false); + new AccountBalanceTask(mSumTextView, + shallDisplayNegativeSignumInSplits).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, + getCurrentAccountUID()); } diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsListFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsListFragment.java index 37e8c5dd6..058f883c4 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsListFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsListFragment.java @@ -27,6 +27,7 @@ import android.support.v4.content.Loader; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; +import android.support.v7.preference.PreferenceManager; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.PopupMenu; @@ -280,8 +281,14 @@ public void onBindViewHolderCursor(ViewHolder holder, Cursor cursor) { final AccountType accountType = GnuCashApplication.getAccountsDbAdapter() .getAccountType(mAccountUID); + // Get Preference about showing signum in Splits + boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(getActivity()) + .getBoolean(getString(R.string.key_display_negative_signum_in_splits), + false); + accountType.displayBalance(holder.transactionAmount, - amount); + amount, + shallDisplayNegativeSignumInSplits); long dateMillis = cursor.getLong(cursor.getColumnIndexOrThrow(DatabaseSchema.TransactionEntry.COLUMN_TIMESTAMP)); String dateText = TransactionsActivity.getPrettyDateFormat(getActivity(), dateMillis); diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/dialog/TransferFundsDialogFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/dialog/TransferFundsDialogFragment.java index 3b25336d2..4a4fce08f 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/dialog/TransferFundsDialogFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/dialog/TransferFundsDialogFragment.java @@ -22,6 +22,7 @@ import android.support.annotation.Nullable; import android.support.design.widget.TextInputLayout; import android.support.v4.app.DialogFragment; +import android.support.v7.preference.PreferenceManager; import android.text.Editable; import android.text.TextWatcher; import android.util.Pair; @@ -97,8 +98,14 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa View view = inflater.inflate(R.layout.dialog_transfer_funds, container, false); ButterKnife.bind(this, view); + // Get Preference about showing signum in Splits + boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(getActivity()) + .getBoolean(getString(R.string.key_display_negative_signum_in_splits), + false); + AccountType.ASSET.displayBalance(mStartAmountLabel, - mOriginAmount); + mOriginAmount, + shallDisplayNegativeSignumInSplits); String fromCurrencyCode = mOriginAmount.getCommodity().getCurrencyCode(); mFromCurrencyLabel.setText(fromCurrencyCode); diff --git a/app/src/main/java/org/gnucash/android/ui/util/AccountBalanceTask.java b/app/src/main/java/org/gnucash/android/ui/util/AccountBalanceTask.java index d9b68acd2..a6807e656 100644 --- a/app/src/main/java/org/gnucash/android/ui/util/AccountBalanceTask.java +++ b/app/src/main/java/org/gnucash/android/ui/util/AccountBalanceTask.java @@ -40,10 +40,13 @@ public class AccountBalanceTask extends AsyncTask { private final WeakReference accountBalanceTextViewReference; private final AccountsDbAdapter accountsDbAdapter; private String mAccountUID; + private boolean mShallDisplayNegativeSignumInSplits; - public AccountBalanceTask(TextView balanceTextView){ + public AccountBalanceTask(TextView balanceTextView, + boolean shallDisplayNegativeSignumInSplits) { accountBalanceTextViewReference = new WeakReference<>(balanceTextView); accountsDbAdapter = AccountsDbAdapter.getInstance(); + mShallDisplayNegativeSignumInSplits = shallDisplayNegativeSignumInSplits; } @Override @@ -78,8 +81,10 @@ protected void onPostExecute(Money balance) { final AccountType accountType = accountsDbAdapter.getAccountType(mAccountUID); + // Get Preference about showing signum in Splits accountType.displayBalance(balanceTextView, - balance); + balance, + mShallDisplayNegativeSignumInSplits); } } } diff --git a/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java b/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java index 18411806a..a9bf752a5 100644 --- a/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java +++ b/app/src/main/java/org/gnucash/android/ui/util/widget/TransactionTypeSwitch.java @@ -211,7 +211,10 @@ private void setWidgetTextColor(final boolean isCredit) { @ColorInt final int color = getAccountType().getAmountColor(isCredit); + // Set switch color TransactionTypeSwitch.this.setTextColor(color); + + // Set Currency color mAmountEditText.setTextColor(color); mCurrencyTextView.setTextColor(color); } From c1a40f351e1c3c21324705cdf0307571a806d9a8 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sun, 24 May 2020 00:18:35 +0200 Subject: [PATCH 52/77] #876 - Create AccountType.displayBalanceWithoutCurrency() --- .../gnucash/android/model/AccountType.java | 62 +++++++++++++++++-- .../java/org/gnucash/android/model/Money.java | 10 +++ 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/model/AccountType.java b/app/src/main/java/org/gnucash/android/model/AccountType.java index 90e9268bd..a72ad51e0 100644 --- a/app/src/main/java/org/gnucash/android/model/AccountType.java +++ b/app/src/main/java/org/gnucash/android/model/AccountType.java @@ -3,7 +3,6 @@ import android.content.Context; import android.support.annotation.ColorInt; import android.support.annotation.ColorRes; -import android.support.v7.preference.PreferenceManager; import android.widget.TextView; import org.gnucash.android.R; @@ -121,7 +120,8 @@ public TransactionType getDefaultTransactionType() { */ public void displayBalance(final TextView balanceTextView, final Money balance, - final boolean shallDisplayNegativeSignumInSplits) { + final boolean shallDisplayNegativeSignum, + final boolean shallDisplayCurrency) { // // Display amount @@ -129,10 +129,24 @@ public void displayBalance(final TextView balanceTextView, final Money balanceToDisplay = getBalanceWithSignumForDisplay(balance); - balanceTextView.setText(!shallDisplayNegativeSignumInSplits - ? balanceToDisplay.abs() - .formattedString() - : balanceToDisplay.formattedString()); + if (shallDisplayCurrency) { + // Shall currency + + // Display currency + balanceTextView.setText(!shallDisplayNegativeSignum + ? balanceToDisplay.abs() + .formattedString() + : balanceToDisplay.formattedString()); + + } else { + // Shall not display currency + + // Display value without currency and without decimals + balanceTextView.setText(!shallDisplayNegativeSignum + ? balanceToDisplay.abs() + .toShortString() + : balanceToDisplay.toShortString()); + } // // Define amount color @@ -160,6 +174,42 @@ public void displayBalance(final TextView balanceTextView, balanceTextView.setTextColor(fontColor); } + /** + * Display the balance of an account in a text view and format the text color to match the sign of the amount + * + * @param balanceTextView + * {@link android.widget.TextView} where balance is to be displayed + * @param balance + * {@link org.gnucash.android.model.Money} balance (>0 or <0) to display + */ + public void displayBalance(final TextView balanceTextView, + final Money balance, + // TODO TW C 2020-05-23 : A supprimer + final boolean shallDisplayNegativeSignumInSplits) { + + displayBalance(balanceTextView, + balance, + true, + true); + } + + /** + * Display the balance of a transaction in a text view and format the text color to match the sign of the amount + * + * @param transactionBalanceTextView + * {@link android.widget.TextView} where balance is to be displayed + * @param transactionBalance + * {@link org.gnucash.android.model.Money} balance (>0 or <0) to display + */ + public void displayBalanceWithoutCurrency(final TextView transactionBalanceTextView, + final Money transactionBalance, + final boolean shallDisplayNegativeSignumInSplits) { + + displayBalance(transactionBalanceTextView, + transactionBalance, + shallDisplayNegativeSignumInSplits, + false); + } /** * Compute red/green color according to accountType and isCreditAmount * diff --git a/app/src/main/java/org/gnucash/android/model/Money.java b/app/src/main/java/org/gnucash/android/model/Money.java index 013904aef..331a0bd95 100644 --- a/app/src/main/java/org/gnucash/android/model/Money.java +++ b/app/src/main/java/org/gnucash/android/model/Money.java @@ -435,6 +435,15 @@ public String toPlainString(){ return mAmount.setScale(mCommodity.getSmallestFractionDigits(), ROUNDING_MODE).toPlainString(); } + /** + * Returns a locale-specific representation of the amount of the Money object (excluding the currency) + * + * @return String representation of the amount (without currency) of the Money object + */ + public String toShortString(){ + return String.format(Locale.getDefault(), "%.0f", asDouble()); + } + /** * Returns a locale-specific representation of the amount of the Money object (excluding the currency) * @@ -444,6 +453,7 @@ public String toLocaleString(){ return String.format(Locale.getDefault(), "%.2f", asDouble()); } + /** * Returns the string representation of the Money object (value + currency) formatted according * to the default locale From 07497fe83c5d3ab04f37cbc8b06b66a192d18e2a Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sun, 24 May 2020 00:20:33 +0200 Subject: [PATCH 53/77] #876 - Use AccountType.displayBalanceWithoutCurrency() for Splits and Transaction Form --- .../android/ui/transaction/SplitEditorFragment.java | 11 +++++++---- .../ui/transaction/TransactionFormFragment.java | 7 ++++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java index 7073d7db0..41def7645 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java @@ -48,6 +48,7 @@ import net.objecthunter.exp4j.ExpressionBuilder; import org.gnucash.android.R; +import org.gnucash.android.app.GnuCashApplication; import org.gnucash.android.db.DatabaseSchema; import org.gnucash.android.db.adapter.AccountsDbAdapter; import org.gnucash.android.db.adapter.CommoditiesDbAdapter; @@ -527,11 +528,13 @@ private void updateSplitAmountEditText(final Split split) { final Money splitValueWithSignum = split.getValueWithSignum(); + AccountType accountType = GnuCashApplication.getAccountsDbAdapter() + .getAccountType(split.getAccountUID()); + // Display abs value because switch button is visible - splitAmountEditText.setValue(!shallDisplayNegativeSignumInSplits - ? splitValueWithSignum.asBigDecimal() - .abs() - : splitValueWithSignum.asBigDecimal()); + accountType.displayBalanceWithoutCurrency(splitAmountEditText, + splitValueWithSignum, + shallDisplayNegativeSignumInSplits); } /** diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java index 4c8e7e84d..25dbffa66 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java @@ -604,9 +604,10 @@ private void updateAmountEditText() { ? signedTransactionBalance.negate() : signedTransactionBalance; - accountType.displayBalance(mAmountEditText, - newSignedTransactionBalance, - shallDisplayNegativeSignumInSplits); + accountType.displayBalanceWithoutCurrency(mAmountEditText, + newSignedTransactionBalance, + shallDisplayNegativeSignumInSplits || (mTransactionTypeSwitch.getVisibility() + == View.GONE)); } private void setDoubleEntryViewsVisibility(int visibility) { From 15bcce9ea14d9b7cdc62b2f31c2fcbd3d61faa39 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sun, 24 May 2020 00:44:05 +0200 Subject: [PATCH 54/77] #876 - Add param to TransactionFormFragment.updateAmountEditText(final Money signedTransactionBalance) --- .../transaction/TransactionFormFragment.java | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java index 25dbffa66..c11841b1d 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java @@ -551,7 +551,11 @@ private void initializeViewsWithTransaction(){ if (!mAmountEditText.isInputModified()) { //when autocompleting, only change the amount if the user has not manually changed it already - updateAmountEditText(); + // Compute balance signed value of saved transaction + final Money signedTransactionBalance = Transaction.computeBalance(mAccountUID, + mSplitsList); + + updateAmountEditText(signedTransactionBalance); } String currencyCode = mTransactionsDbAdapter.getAccountCurrencyCode(mAccountUID); @@ -580,16 +584,13 @@ private void initializeViewsWithTransaction(){ } - private void updateAmountEditText() { + private void updateAmountEditText(final Money signedTransactionBalance) { // Get Preference about showing signum in Splits boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(getActivity()) .getBoolean(getString(R.string.key_display_negative_signum_in_splits), false); - // Compute balance signed value of saved transaction - final Money signedTransactionBalance = mTransaction.getBalance(mAccountUID); - final boolean isCredit = signedTransactionBalance.isNegative(); // @@ -782,8 +783,11 @@ private void setListeners() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + // Compute balance signed value of saved transaction + final Money signedTransactionBalance = mTransaction.getBalance(mAccountUID); + // Update Amount Signum - updateAmountEditText(); + updateAmountEditText(signedTransactionBalance); } }); @@ -1014,6 +1018,7 @@ private boolean isMultiCurrencyTransaction(){ * and save a transaction */ private void saveNewTransaction() { + mAmountEditText.getCalculatorKeyboard().hideCustomKeyboard(); //determine whether we need to do currency conversion @@ -1165,8 +1170,7 @@ public void setSplitList(List splitList){ mTransactionTypeSwitch.setChecked(balance.isNegative()); -// mAmountEditText.setValue(balance.asBigDecimal()); - updateAmountEditText(); + updateAmountEditText(balance); } @@ -1259,7 +1263,9 @@ public void onRecurrenceSet(String rrule) { @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { + if (resultCode == Activity.RESULT_OK){ + List splitList = data.getParcelableArrayListExtra(UxArgument.SPLIT_LIST); setSplitList(splitList); From 05090666d2d83b628feef131ed03f396bf4d89a1 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sun, 24 May 2020 02:45:32 +0200 Subject: [PATCH 55/77] #876 - Remove shallDisplayNegativeSignumInSplits parameter from AccountType.displayBalance() --- .../gnucash/android/model/AccountType.java | 4 +--- .../ui/account/AccountsListFragment.java | 7 +----- .../ui/report/ReportsOverviewFragment.java | 14 +++-------- .../ui/report/sheet/BalanceSheetFragment.java | 23 +++---------------- .../ui/transaction/TransactionsActivity.java | 7 +----- .../transaction/TransactionsListFragment.java | 8 +------ .../dialog/TransferFundsDialogFragment.java | 8 +------ .../android/ui/util/AccountBalanceTask.java | 8 ++----- 8 files changed, 13 insertions(+), 66 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/model/AccountType.java b/app/src/main/java/org/gnucash/android/model/AccountType.java index a72ad51e0..82c2503fe 100644 --- a/app/src/main/java/org/gnucash/android/model/AccountType.java +++ b/app/src/main/java/org/gnucash/android/model/AccountType.java @@ -183,9 +183,7 @@ public void displayBalance(final TextView balanceTextView, * {@link org.gnucash.android.model.Money} balance (>0 or <0) to display */ public void displayBalance(final TextView balanceTextView, - final Money balance, - // TODO TW C 2020-05-23 : A supprimer - final boolean shallDisplayNegativeSignumInSplits) { + final Money balance) { displayBalance(balanceTextView, balance, diff --git a/app/src/main/java/org/gnucash/android/ui/account/AccountsListFragment.java b/app/src/main/java/org/gnucash/android/ui/account/AccountsListFragment.java index 117f1d492..6c60b28be 100644 --- a/app/src/main/java/org/gnucash/android/ui/account/AccountsListFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/account/AccountsListFragment.java @@ -499,13 +499,8 @@ public void onBindViewHolderCursor(final AccountViewHolder holder, final Cursor // add a summary of transactions to the account view - // Get Preference about showing signum in Splits - boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(getActivity()) - .getBoolean(getString(R.string.key_display_negative_signum_in_splits), - false); // Make sure the balance task is truly multithread - new AccountBalanceTask(holder.accountBalance, - shallDisplayNegativeSignumInSplits).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, + new AccountBalanceTask(holder.accountBalance).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, accountUID); String accountColor = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseSchema.AccountEntry.COLUMN_COLOR_CODE)); diff --git a/app/src/main/java/org/gnucash/android/ui/report/ReportsOverviewFragment.java b/app/src/main/java/org/gnucash/android/ui/report/ReportsOverviewFragment.java index cb7aef695..3b230eeed 100644 --- a/app/src/main/java/org/gnucash/android/ui/report/ReportsOverviewFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/report/ReportsOverviewFragment.java @@ -213,21 +213,13 @@ protected void displayReport() { mChart.highlightValues(null); mChart.invalidate(); - // Get Preference about showing signum in Splits - boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(getActivity()) - .getBoolean(getString(R.string.key_display_negative_signum_in_splits), - false); - AccountType.ASSET.displayBalance(mTotalAssets, - mAssetsBalance, - shallDisplayNegativeSignumInSplits); + mAssetsBalance); AccountType.LIABILITY.displayBalance(mTotalLiabilities, - mLiabilitiesBalance, - shallDisplayNegativeSignumInSplits); + mLiabilitiesBalance); // #8xx AccountType.ASSET.displayBalance(mNetWorth, - mAssetsBalance.add(mLiabilitiesBalance), - shallDisplayNegativeSignumInSplits); + mAssetsBalance.add(mLiabilitiesBalance)); } /** diff --git a/app/src/main/java/org/gnucash/android/ui/report/sheet/BalanceSheetFragment.java b/app/src/main/java/org/gnucash/android/ui/report/sheet/BalanceSheetFragment.java index 41bb8b7ec..36d8082db 100644 --- a/app/src/main/java/org/gnucash/android/ui/report/sheet/BalanceSheetFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/report/sheet/BalanceSheetFragment.java @@ -19,7 +19,6 @@ import android.graphics.Typeface; import android.os.Bundle; import android.support.annotation.Nullable; -import android.support.v7.preference.PreferenceManager; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.Menu; @@ -103,14 +102,9 @@ protected void displayReport() { loadAccountViews(LIABLITY_ACCOUNT_TYPES, mLiabilitiesTableLayout); loadAccountViews(EQUITY_ACCOUNT_TYPES, mEquityTableLayout); - // Get Preference about showing signum in Splits - boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(getActivity()) - .getBoolean(getString(R.string.key_display_negative_signum_in_splits), - false); AccountType.ASSET.displayBalance(mNetWorth, // #8xx - mAssetsBalance.add(mLiabilitiesBalance), - shallDisplayNegativeSignumInSplits); + mAssetsBalance.add(mLiabilitiesBalance)); } @Override @@ -144,14 +138,8 @@ private void loadAccountViews(List accountTypes, TableLayout tableL TextView balanceTextView = (TextView) view.findViewById(R.id.account_balance); accountType = AccountType.valueOf(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseSchema.AccountEntry.COLUMN_TYPE))); - // Get Preference about showing signum in Splits - boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(getActivity()) - .getBoolean(getString(R.string.key_display_negative_signum_in_splits), - false); - accountType.displayBalance(balanceTextView, - balance, - shallDisplayNegativeSignumInSplits); + balance); tableLayout.addView(view); } @@ -168,15 +156,10 @@ private void loadAccountViews(List accountTypes, TableLayout tableL accountBalance.setTextSize(16); accountBalance.setTypeface(null, Typeface.BOLD); - // Get Preference about showing signum in Splits - boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(getActivity()) - .getBoolean(getString(R.string.key_display_negative_signum_in_splits), - false); accountType.displayBalance(accountBalance, mAccountsDbAdapter.getAccountBalance(accountTypes, -1, - System.currentTimeMillis()), - shallDisplayNegativeSignumInSplits); + System.currentTimeMillis())); tableLayout.addView(totalView); } diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java index c4cdb75c2..736a98c50 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java @@ -273,12 +273,7 @@ public void refresh(String accountUID) { if (mPagerAdapter != null) mPagerAdapter.notifyDataSetChanged(); - // Get Preference about showing signum in Splits - boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()) - .getBoolean(getString(R.string.key_display_negative_signum_in_splits), - false); - new AccountBalanceTask(mSumTextView, - shallDisplayNegativeSignumInSplits).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, + new AccountBalanceTask(mSumTextView).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, getCurrentAccountUID()); } diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsListFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsListFragment.java index 058f883c4..db9592eba 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsListFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsListFragment.java @@ -281,14 +281,8 @@ public void onBindViewHolderCursor(ViewHolder holder, Cursor cursor) { final AccountType accountType = GnuCashApplication.getAccountsDbAdapter() .getAccountType(mAccountUID); - // Get Preference about showing signum in Splits - boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(getActivity()) - .getBoolean(getString(R.string.key_display_negative_signum_in_splits), - false); - accountType.displayBalance(holder.transactionAmount, - amount, - shallDisplayNegativeSignumInSplits); + amount); long dateMillis = cursor.getLong(cursor.getColumnIndexOrThrow(DatabaseSchema.TransactionEntry.COLUMN_TIMESTAMP)); String dateText = TransactionsActivity.getPrettyDateFormat(getActivity(), dateMillis); diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/dialog/TransferFundsDialogFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/dialog/TransferFundsDialogFragment.java index 4a4fce08f..64bf63ec6 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/dialog/TransferFundsDialogFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/dialog/TransferFundsDialogFragment.java @@ -98,14 +98,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa View view = inflater.inflate(R.layout.dialog_transfer_funds, container, false); ButterKnife.bind(this, view); - // Get Preference about showing signum in Splits - boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(getActivity()) - .getBoolean(getString(R.string.key_display_negative_signum_in_splits), - false); - AccountType.ASSET.displayBalance(mStartAmountLabel, - mOriginAmount, - shallDisplayNegativeSignumInSplits); + mOriginAmount); String fromCurrencyCode = mOriginAmount.getCommodity().getCurrencyCode(); mFromCurrencyLabel.setText(fromCurrencyCode); diff --git a/app/src/main/java/org/gnucash/android/ui/util/AccountBalanceTask.java b/app/src/main/java/org/gnucash/android/ui/util/AccountBalanceTask.java index a6807e656..23966b580 100644 --- a/app/src/main/java/org/gnucash/android/ui/util/AccountBalanceTask.java +++ b/app/src/main/java/org/gnucash/android/ui/util/AccountBalanceTask.java @@ -40,13 +40,10 @@ public class AccountBalanceTask extends AsyncTask { private final WeakReference accountBalanceTextViewReference; private final AccountsDbAdapter accountsDbAdapter; private String mAccountUID; - private boolean mShallDisplayNegativeSignumInSplits; - public AccountBalanceTask(TextView balanceTextView, - boolean shallDisplayNegativeSignumInSplits) { + public AccountBalanceTask(TextView balanceTextView) { accountBalanceTextViewReference = new WeakReference<>(balanceTextView); accountsDbAdapter = AccountsDbAdapter.getInstance(); - mShallDisplayNegativeSignumInSplits = shallDisplayNegativeSignumInSplits; } @Override @@ -83,8 +80,7 @@ protected void onPostExecute(Money balance) { // Get Preference about showing signum in Splits accountType.displayBalance(balanceTextView, - balance, - mShallDisplayNegativeSignumInSplits); + balance); } } } From 07cf91e7aabcbcc9828cddf7ce7dc48c314220ff Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sun, 24 May 2020 02:47:00 +0200 Subject: [PATCH 56/77] #876 - For Transaction Details, use unsigned balanced in debit/credit column, but signed balance for Account balance at time --- .../TransactionDetailActivity.java | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java index 380409832..34aaaf4e4 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java @@ -74,15 +74,11 @@ public SplitAmountViewHolder(View view, final AccountType accountType = AccountsDbAdapter.getInstance() .getAccountType(split.getAccountUID()); - // Get Preference about showing signum in Splits - boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(GnuCashApplication.getAppContext()) - .getBoolean(getString(R.string.key_display_negative_signum_in_splits), - false); - // Display absolute value because it is displayed either in debit or credit column accountType.displayBalance(balanceView, splitSignedAmount, - shallDisplayNegativeSignumInSplits); + false, + true); } } // Class SplitAmountViewHolder @@ -227,20 +223,18 @@ private void bindViews() { transaction.getTimeMillis()); // #8xx - // Define in which field (Debit or Credit) the balance shall be displayed - TextView balanceTextView = accountBalance.isNegative() - ? mCreditBalance - : mDebitBalance; + // Use Debit TextView to display the account balance (with signum) +// TextView balanceTextView = accountBalance.isNegative() +// ? mCreditBalance +// : mDebitBalance; + TextView balanceTextView = mDebitBalance; final AccountType accountType = accountsDbAdapter.getAccountType(mAccountUID); - // Get Preference about showing signum in Splits - boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()) - .getBoolean(getString(R.string.key_display_negative_signum_in_splits), - false); accountType.displayBalance(balanceTextView, accountBalance, - shallDisplayNegativeSignumInSplits); + true, + true); // // Date From b59e69b9ffa7acb12e706f7eddb203f295bbe530 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sun, 24 May 2020 02:47:57 +0200 Subject: [PATCH 57/77] #876 - In Splits Editor, display imbalance as if account is an asset --- .../ui/transaction/SplitEditorFragment.java | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java index 41def7645..841b32744 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java @@ -154,14 +154,10 @@ public void onActivityCreated(Bundle savedInstanceState) { view.findViewById(R.id.input_accounts_spinner).setEnabled(false); view.findViewById(R.id.btn_remove_split).setVisibility(View.GONE); - // Get Preference about showing signum in Splits - boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(getActivity()) - .getBoolean(getString(R.string.key_display_negative_signum_in_splits), - false); - accountType.displayBalance(mImbalanceTextView, + // Display imbalance with signum and currency as if it was an asset + AccountType.ASSET.displayBalance(mImbalanceTextView, new Money(mBaseAmount.negate(), - mCommodity), - shallDisplayNegativeSignumInSplits); + mCommodity)); } } @@ -660,15 +656,12 @@ public void afterTextChanged(Editable editable) { } // for - // Get Preference about showing signum in Splits - boolean shallDisplayNegativeSignumInSplits = PreferenceManager.getDefaultSharedPreferences(getActivity()) - .getBoolean(getString(R.string.key_display_negative_signum_in_splits), - false); +// AccountType accountType = mAccountsDbAdapter.getAccountType(mAccountUID); + // Display imbalance with signum and currency as if it was an asset AccountType.ASSET.displayBalance(mImbalanceTextView, new Money(imbalance, - mCommodity), - shallDisplayNegativeSignumInSplits); + mCommodity)); } } From eb0f00d4969b3de0df8b84542c718c4c787bd806 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Thu, 28 May 2020 11:50:31 +0200 Subject: [PATCH 58/77] #876 - Fix NPE when type switching on a new transaction --- .../transaction/TransactionFormFragment.java | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java index c11841b1d..a5f9b91e0 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java @@ -783,8 +783,11 @@ private void setListeners() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - // Compute balance signed value of saved transaction - final Money signedTransactionBalance = mTransaction.getBalance(mAccountUID); + // UI to Transaction + Transaction transaction = extractTransactionFromView(); + + // Compute balance signed value of transaction + final Money signedTransactionBalance = transaction.getBalance(mAccountUID); // Update Amount Signum updateAmountEditText(signedTransactionBalance); @@ -984,6 +987,13 @@ private List extractSplitsFromView() { transaction.setSplits(splits); transaction.setExported(false); //not necessary as exports use timestamps now. Because, legacy + if (mEditMode) { + // Editing an existing transaction + + // reset the transaction UID + transaction.setUID(mTransaction.getUID()); + } + return transaction; } @@ -1029,13 +1039,16 @@ private void saveNewTransaction() { return; } - Transaction transaction = extractTransactionFromView(); + // + // UI to Transaction, with same UID if transaction edit mode + // + + mTransaction = extractTransactionFromView(); - if (mEditMode) { //if editing an existing transaction - transaction.setUID(mTransaction.getUID()); - } + // + // Save transaction in DB + // - mTransaction = transaction; mAccountsDbAdapter.beginTransaction(); try { From 7fc3485bb755bff8e2c0900f13991e0f1de0366e Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Wed, 10 Jun 2020 22:36:41 +0200 Subject: [PATCH 59/77] #876 - Set amount to 0 if text field is empty --- .../ui/transaction/TransactionFormFragment.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java index a5f9b91e0..a5dc76b1b 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java @@ -869,6 +869,18 @@ private List extractSplitsFromView() { BigDecimal amountBigD = mAmountEditText.getValue(); + if (amountBigD != null) { + // Amount is null + + // RAF + + } else { + // Amount is not null + + // init to 0 + amountBigD=new BigDecimal(0); + } + String baseCurrencyCode = mTransactionsDbAdapter.getAccountCurrencyCode(mAccountUID); // Store signed amount From a1626c1bb2d3cb1a7b3be45ffeb4ff616a93e870 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sat, 13 Jun 2020 18:32:10 +0200 Subject: [PATCH 60/77] #876 - Rename mTransactionsAdapter into mTransactionsDbAdapter --- .../android/db/adapter/AccountsDbAdapter.java | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/db/adapter/AccountsDbAdapter.java b/app/src/main/java/org/gnucash/android/db/adapter/AccountsDbAdapter.java index 2a67184e0..c5a950e1a 100644 --- a/app/src/main/java/org/gnucash/android/db/adapter/AccountsDbAdapter.java +++ b/app/src/main/java/org/gnucash/android/db/adapter/AccountsDbAdapter.java @@ -74,7 +74,7 @@ public class AccountsDbAdapter extends DatabaseAdapter { /** * Transactions database adapter for manipulating transactions associated with accounts */ - private final TransactionsDbAdapter mTransactionsAdapter; + private final TransactionsDbAdapter mTransactionsDbAdapter; /** * Commodities database adapter for commodity manipulation @@ -101,7 +101,7 @@ public AccountsDbAdapter(SQLiteDatabase db, TransactionsDbAdapter transactionsDb AccountEntry.COLUMN_PARENT_ACCOUNT_UID, AccountEntry.COLUMN_DEFAULT_TRANSFER_ACCOUNT_UID }); - mTransactionsAdapter = transactionsDbAdapter; + mTransactionsDbAdapter = transactionsDbAdapter; mCommoditiesDbAdapter = new CommoditiesDbAdapter(db); } @@ -129,7 +129,7 @@ public AccountsDbAdapter(SQLiteDatabase db){ AccountEntry.COLUMN_DEFAULT_TRANSFER_ACCOUNT_UID }); - mTransactionsAdapter = new TransactionsDbAdapter(db, new SplitsDbAdapter(db)); + mTransactionsDbAdapter = new TransactionsDbAdapter(db, new SplitsDbAdapter(db)); mCommoditiesDbAdapter = new CommoditiesDbAdapter(db); } @@ -150,7 +150,7 @@ public static AccountsDbAdapter getInstance(){ public void addRecord(@NonNull Account account, UpdateMethod updateMethod){ Log.d(LOG_TAG, "Replace account to db"); //in-case the account already existed, we want to update the templates based on it as well - List templateTransactions = mTransactionsAdapter.getScheduledTransactionsForAccount(account.getUID()); + List templateTransactions = mTransactionsDbAdapter.getScheduledTransactionsForAccount(account.getUID()); super.addRecord(account, updateMethod); String accountUID = account.getUID(); //now add transactions if there are any @@ -159,10 +159,10 @@ public void addRecord(@NonNull Account account, UpdateMethod updateMethod){ updateRecord(accountUID, AccountEntry.COLUMN_FULL_NAME, getFullyQualifiedAccountName(accountUID)); for (Transaction t : account.getTransactions()) { t.setCommodity(account.getCommodity()); - mTransactionsAdapter.addRecord(t, updateMethod); + mTransactionsDbAdapter.addRecord(t, updateMethod); } for (Transaction transaction : templateTransactions) { - mTransactionsAdapter.addRecord(transaction, UpdateMethod.update); + mTransactionsDbAdapter.addRecord(transaction, UpdateMethod.update); } } } @@ -187,12 +187,12 @@ public long bulkAddRecords(@NonNull List accountList, UpdateMethod upda List transactionList = new ArrayList<>(accountList.size()*2); for (Account account : accountList) { transactionList.addAll(account.getTransactions()); - transactionList.addAll(mTransactionsAdapter.getScheduledTransactionsForAccount(account.getUID())); + transactionList.addAll(mTransactionsDbAdapter.getScheduledTransactionsForAccount(account.getUID())); } long nRow = super.bulkAddRecords(accountList, updateMethod); if (nRow > 0 && !transactionList.isEmpty()){ - mTransactionsAdapter.bulkAddRecords(transactionList, updateMethod); + mTransactionsDbAdapter.bulkAddRecords(transactionList, updateMethod); } return nRow; } @@ -374,7 +374,7 @@ public boolean recursiveDeleteAccount(long accountId){ try { descendantAccountUIDs.add(accountUID); //add account to descendants list just for convenience for (String descendantAccountUID : descendantAccountUIDs) { - mTransactionsAdapter.deleteTransactionsForAccount(descendantAccountUID); + mTransactionsDbAdapter.deleteTransactionsForAccount(descendantAccountUID); } String accountUIDList = "'" + TextUtils.join("','", descendantAccountUIDs) + "'"; @@ -413,7 +413,7 @@ public boolean recursiveDeleteAccount(long accountId){ @Override public Account buildModelInstance(@NonNull final Cursor c){ Account account = buildSimpleAccountInstance(c); - account.setTransactions(mTransactionsAdapter.getAllTransactionsForAccount(account.getUID())); + account.setTransactions(mTransactionsDbAdapter.getAllTransactionsForAccount(account.getUID())); return account; } @@ -801,7 +801,7 @@ public Money getAccountBalance(AccountType accountType, long startTimestamp, lon String currencyCode = GnuCashApplication.getDefaultCurrencyCode(); Log.d(LOG_TAG, "all account list : " + accountUidList.size()); - SplitsDbAdapter splitsDbAdapter = mTransactionsAdapter.getSplitDbAdapter(); + SplitsDbAdapter splitsDbAdapter = mTransactionsDbAdapter.getSplitDbAdapter(); return (startTimestamp == -1 && endTimestamp == -1) ? splitsDbAdapter.computeSplitBalance(accountUidList, currencyCode, hasDebitNormalBalance) @@ -825,7 +825,7 @@ public Money getAccountBalance(List accountTypes, long start, long private Money computeBalance(String accountUID, long startTimestamp, long endTimestamp) { Log.d(LOG_TAG, "Computing account balance for account ID " + accountUID); - String currencyCode = mTransactionsAdapter.getAccountCurrencyCode(accountUID); + String currencyCode = mTransactionsDbAdapter.getAccountCurrencyCode(accountUID); boolean hasDebitNormalBalance = getAccountType(accountUID).hasDebitNormalBalance(); List accountsList = getDescendantAccountUIDs(accountUID, @@ -834,7 +834,7 @@ private Money computeBalance(String accountUID, long startTimestamp, long endTim accountsList.add(0, accountUID); Log.d(LOG_TAG, "all account list : " + accountsList.size()); - SplitsDbAdapter splitsDbAdapter = mTransactionsAdapter.getSplitDbAdapter(); + SplitsDbAdapter splitsDbAdapter = mTransactionsDbAdapter.getSplitDbAdapter(); return (startTimestamp == -1 && endTimestamp == -1) ? splitsDbAdapter.computeSplitBalance(accountsList, currencyCode, hasDebitNormalBalance) : splitsDbAdapter.computeSplitBalance(accountsList, currencyCode, hasDebitNormalBalance, startTimestamp, endTimestamp); @@ -858,7 +858,7 @@ public Money getAccountsBalance(@NonNull List accountUIDList, long star boolean hasDebitNormalBalance = getAccountType(accountUIDList.get(0)).hasDebitNormalBalance(); - SplitsDbAdapter splitsDbAdapter = mTransactionsAdapter.getSplitDbAdapter(); + SplitsDbAdapter splitsDbAdapter = mTransactionsDbAdapter.getSplitDbAdapter(); Money splitSum = (startTimestamp == -1 && endTimestamp == -1) ? splitsDbAdapter.computeSplitBalance(accountUIDList, currencyCode, hasDebitNormalBalance) : splitsDbAdapter.computeSplitBalance(accountUIDList, currencyCode, hasDebitNormalBalance, startTimestamp, endTimestamp); @@ -1145,7 +1145,7 @@ public List getAllOpeningBalanceTransactions(){ Cursor cursor = fetchAccounts(null, null, null); List openingTransactions = new ArrayList<>(); try { - SplitsDbAdapter splitsDbAdapter = mTransactionsAdapter.getSplitDbAdapter(); + SplitsDbAdapter splitsDbAdapter = mTransactionsDbAdapter.getSplitDbAdapter(); while (cursor.moveToNext()) { long id = cursor.getLong(cursor.getColumnIndexOrThrow(AccountEntry._ID)); String accountUID = getUID(id); From 5db92fb9e7506562560b8ff5c960e4ca2742d25f Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sat, 13 Jun 2020 18:33:22 +0200 Subject: [PATCH 61/77] #876 - Invert finish() and BookUtils.loadBook --- .../android/ui/common/BaseDrawerActivity.java | 44 ++++++++++++++----- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/common/BaseDrawerActivity.java b/app/src/main/java/org/gnucash/android/ui/common/BaseDrawerActivity.java index 83c409d95..7316250d6 100644 --- a/app/src/main/java/org/gnucash/android/ui/common/BaseDrawerActivity.java +++ b/app/src/main/java/org/gnucash/android/ui/common/BaseDrawerActivity.java @@ -314,21 +314,45 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { @Override public boolean onMenuItemClick(MenuItem item) { - long id = item.getItemId(); - if (id == ID_MANAGE_BOOKS){ + + long itemId = item.getItemId(); + + if (itemId == ID_MANAGE_BOOKS){ + // Click on "Manage books..." item + + // Start "Manage books" Activity Intent intent = new Intent(this, PreferenceActivity.class); intent.setAction(PreferenceActivity.ACTION_MANAGE_BOOKS); startActivity(intent); + mDrawerLayout.closeDrawer(mNavigationView); - return true; - } - BooksDbAdapter booksDbAdapter = BooksDbAdapter.getInstance(); - String bookUID = booksDbAdapter.getUID(id); - if (!bookUID.equals(booksDbAdapter.getActiveBookUID())){ - BookUtils.loadBook(bookUID); - finish(); + + } else { + // Click on an existing book item + + BooksDbAdapter booksDbAdapter = BooksDbAdapter.getInstance(); + + String selectedBookUID = booksDbAdapter.getUID(itemId); + + if (!selectedBookUID.equals(booksDbAdapter.getActiveBookUID())){ + // Selected Book is not the active one + + // Close current Activity + finish(); + + // load book and Start Account Activity and reset Activity Stack + BookUtils.loadBook(selectedBookUID); + + } else { + // Selected Book is the current one + + // Start Account Activity and reset Activity Stack + AccountsActivity.start(GnuCashApplication.getAppContext()); + } + } - AccountsActivity.start(GnuCashApplication.getAppContext()); + + return true; } From 453252e95844bb0a830f59292b215a6ee5e8e025 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sat, 13 Jun 2020 18:33:54 +0200 Subject: [PATCH 62/77] #876 - Logs and comments --- .../org/gnucash/android/db/adapter/DatabaseAdapter.java | 8 +++++++- .../org/gnucash/android/ui/account/AccountsActivity.java | 6 ++++++ .../android/ui/transaction/TransactionsActivity.java | 4 +++- app/src/main/java/org/gnucash/android/util/BookUtils.java | 2 ++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/db/adapter/DatabaseAdapter.java b/app/src/main/java/org/gnucash/android/db/adapter/DatabaseAdapter.java index ccaa4e18c..e753ad46f 100644 --- a/app/src/main/java/org/gnucash/android/db/adapter/DatabaseAdapter.java +++ b/app/src/main/java/org/gnucash/android/db/adapter/DatabaseAdapter.java @@ -541,7 +541,13 @@ public long getID(@NonNull String uid){ if (cursor.moveToFirst()) { result = cursor.getLong(cursor.getColumnIndexOrThrow(DatabaseSchema.CommonColumns._ID)); } else { - throw new IllegalArgumentException(mTableName + " with GUID " + uid + " does not exist in the db"); + throw new IllegalArgumentException("UID (" + + uid + + ") does not exist in Table (" + + mTableName + + ") of db (" + + mDb.getPath() + + ")"); } } finally { cursor.close(); diff --git a/app/src/main/java/org/gnucash/android/ui/account/AccountsActivity.java b/app/src/main/java/org/gnucash/android/ui/account/AccountsActivity.java index f9a6459fe..cc2735d33 100644 --- a/app/src/main/java/org/gnucash/android/ui/account/AccountsActivity.java +++ b/app/src/main/java/org/gnucash/android/ui/account/AccountsActivity.java @@ -281,6 +281,7 @@ protected void onResume() { @Override protected void onStart() { + super.onStart(); if (BuildConfig.CAN_REQUEST_RATING) { @@ -288,6 +289,11 @@ protected void onStart() { RateThisApp.onStart(this); RateThisApp.showRateDialogIfNeeded(this); } + + Log.i(LOG_TAG, + "New active db (" + GnuCashApplication.getActiveDb() + .getPath() + ")"); + } /** diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java index 736a98c50..5f36cf322 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java @@ -382,7 +382,9 @@ protected void onResume() { */ private void setTitleIndicatorColor() { - int iColor = AccountsDbAdapter.getActiveAccountColorResource(getCurrentAccountUID()); + final String currentAccountUID = getCurrentAccountUID(); + + int iColor = AccountsDbAdapter.getActiveAccountColorResource(currentAccountUID); mTabLayout.setBackgroundColor(iColor); diff --git a/app/src/main/java/org/gnucash/android/util/BookUtils.java b/app/src/main/java/org/gnucash/android/util/BookUtils.java index dbd308d7e..f0e6de4f3 100644 --- a/app/src/main/java/org/gnucash/android/util/BookUtils.java +++ b/app/src/main/java/org/gnucash/android/util/BookUtils.java @@ -25,7 +25,9 @@ public static void activateBook(@NonNull String bookUID){ * @param bookUID GUID of the book to be loaded */ public static void loadBook(@NonNull String bookUID){ + activateBook(bookUID); + AccountsActivity.start(GnuCashApplication.getAppContext()); } } From 3e333ec4a3d8038411d8dbd872ba6e375be41339 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sun, 14 Jun 2020 11:51:28 +0200 Subject: [PATCH 63/77] #876 - ** Allow null to enter SplitEditor + Transfert if from TransactionFormFragment.extractSplitsFromView to Money.setAmount() --- .../java/org/gnucash/android/model/Money.java | 13 +++ .../transaction/TransactionFormFragment.java | 88 +++++++------------ 2 files changed, 47 insertions(+), 54 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/model/Money.java b/app/src/main/java/org/gnucash/android/model/Money.java index 331a0bd95..e2df19714 100644 --- a/app/src/main/java/org/gnucash/android/model/Money.java +++ b/app/src/main/java/org/gnucash/android/model/Money.java @@ -315,6 +315,19 @@ public Money negate(){ * @param amount {@link BigDecimal} amount to be set */ private void setAmount(@NonNull BigDecimal amount) { + + if (amount != null) { + // Amount is not null + + // NTD + + } else { + // Amount is null + + // init to 0 + amount = new BigDecimal(0); + } + mAmount = amount.setScale(mCommodity.getSmallestFractionDigits(), ROUNDING_MODE); } diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java index a5dc76b1b..3f3e2eae1 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java @@ -716,59 +716,51 @@ private void updateTransferAccountsList(){ */ private void openSplitEditor() { - if (mAmountEditText.getValue() == null) { + String baseAmountString; - Toast.makeText(getActivity(), - R.string.toast_enter_amount_to_split, - Toast.LENGTH_SHORT) - .show(); - - } else { - - String baseAmountString; - - if (mTransaction == null) { - // we are creating a new transaction (not editing an existing one) + if (mTransaction == null) { + // we are creating a new transaction (not editing an existing one) - BigDecimal enteredAmount = mAmountEditText.getValue(); + BigDecimal enteredAmount = mAmountEditText.getValue() != null + ? mAmountEditText.getValue() + : new BigDecimal(0); - baseAmountString = enteredAmount.toPlainString(); + baseAmountString = enteredAmount.toPlainString(); - } else { - // we are editing an existing transaction + } else { + // we are editing an existing transaction - // - // Find splits biggest amount (in absolute value) - // + // + // Find splits biggest amount (in absolute value) + // - Money biggestAmount = Money.createZeroInstance(mTransaction.getCurrencyCode()); + Money biggestAmount = Money.createZeroInstance(mTransaction.getCurrencyCode()); - for (Split split : mTransaction.getSplits()) { - if (split.getValue() - .asBigDecimal() - .compareTo(biggestAmount.asBigDecimal()) > 0) { - biggestAmount = split.getValue(); - } - } // for + for (Split split : mTransaction.getSplits()) { + if (split.getValue() + .asBigDecimal() + .compareTo(biggestAmount.asBigDecimal()) > 0) { + biggestAmount = split.getValue(); + } + } // for - baseAmountString = biggestAmount.toPlainString(); - } + baseAmountString = biggestAmount.toPlainString(); + } - Intent intent = new Intent(getActivity(), - FormActivity.class); - intent.putExtra(UxArgument.FORM_TYPE, - FormActivity.FormType.SPLIT_EDITOR.name()); - intent.putExtra(UxArgument.SELECTED_ACCOUNT_UID, - mAccountUID); - intent.putExtra(UxArgument.AMOUNT_STRING, - baseAmountString); + Intent intent = new Intent(getActivity(), + FormActivity.class); + intent.putExtra(UxArgument.FORM_TYPE, + FormActivity.FormType.SPLIT_EDITOR.name()); + intent.putExtra(UxArgument.SELECTED_ACCOUNT_UID, + mAccountUID); + intent.putExtra(UxArgument.AMOUNT_STRING, + baseAmountString); - intent.putParcelableArrayListExtra(UxArgument.SPLIT_LIST, - (ArrayList) extractSplitsFromView()); + intent.putParcelableArrayListExtra(UxArgument.SPLIT_LIST, + (ArrayList) extractSplitsFromView()); - startActivityForResult(intent, - REQUEST_SPLIT_EDITOR); - } + startActivityForResult(intent, + REQUEST_SPLIT_EDITOR); } /** @@ -869,18 +861,6 @@ private List extractSplitsFromView() { BigDecimal amountBigD = mAmountEditText.getValue(); - if (amountBigD != null) { - // Amount is null - - // RAF - - } else { - // Amount is not null - - // init to 0 - amountBigD=new BigDecimal(0); - } - String baseCurrencyCode = mTransactionsDbAdapter.getAccountCurrencyCode(mAccountUID); // Store signed amount From 03225b1a0a9ce3bec93580b65d8d21ed8de92ca1 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sun, 14 Jun 2020 11:52:20 +0200 Subject: [PATCH 64/77] #876 - * Check split not null in Listener onCheckedChanged --- .../ui/transaction/SplitEditorFragment.java | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java index 841b32744..a8c7c9412 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java @@ -442,14 +442,23 @@ public void onClick(View view) { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - // Change Transaction Type according to splitTypeSwitch - split.setType(splitTypeSwitch.getTransactionType()); + if (split != null) { + // Split not null - // Update Split Amount Signum - updateSplitAmountEditText(split); + // Change Transaction Type according to splitTypeSwitch + split.setType(splitTypeSwitch.getTransactionType()); - // Recompute Split List Balance - mImbalanceWatcher.afterTextChanged(null); + // Update Split Amount Signum + updateSplitAmountEditText(split); + + // Recompute Split List Balance + mImbalanceWatcher.afterTextChanged(null); + + } else { + // Split is null + + // RAF + } } }); } From 8f1767e84a0309469885f85e43e5d20f8c01652c Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sun, 14 Jun 2020 12:14:29 +0200 Subject: [PATCH 65/77] #876 - ** Add shallDisplayZero in displayBalance(...) to avoid displaying 0 --- .../gnucash/android/model/AccountType.java | 30 +++++++++++++++---- .../TransactionDetailActivity.java | 2 ++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/model/AccountType.java b/app/src/main/java/org/gnucash/android/model/AccountType.java index 82c2503fe..0738bc9f6 100644 --- a/app/src/main/java/org/gnucash/android/model/AccountType.java +++ b/app/src/main/java/org/gnucash/android/model/AccountType.java @@ -117,10 +117,16 @@ public TransactionType getDefaultTransactionType() { * * @param balanceTextView {@link android.widget.TextView} where balance is to be displayed * @param balance {@link org.gnucash.android.model.Money} balance (>0 or <0) to display + * @param shallDisplayNegativeSignum if true will display Negative signum (often bind to Preference R.string.key_display_negative_signum_in_splits + * @param shallDisplayZero if true will display 0, else will display nothing (usefull when first creation of a Transaction) + * @param shallDisplayCurrency if true will display Currency as well as amount (usefull when displaying balance in + * TransactionListFragment, BalanceSheetFragment...) + * else will not display Currency (usefull in SplitEditorFragment or TransactionFormFragment) */ public void displayBalance(final TextView balanceTextView, final Money balance, final boolean shallDisplayNegativeSignum, + final boolean shallDisplayZero, final boolean shallDisplayCurrency) { // @@ -141,11 +147,23 @@ public void displayBalance(final TextView balanceTextView, } else { // Shall not display currency - // Display value without currency and without decimals - balanceTextView.setText(!shallDisplayNegativeSignum - ? balanceToDisplay.abs() - .toShortString() - : balanceToDisplay.toShortString()); + // Shall display value in all other cases than zero balance and shall not display zero balance + final boolean shallDisplayValue = !(balanceToDisplay.isAmountZero() && !shallDisplayZero); + + if (shallDisplayValue) { + // Shall display balance + + // Display value without currency and without decimals + balanceTextView.setText(!shallDisplayNegativeSignum + ? balanceToDisplay.abs() + .toShortString() + : balanceToDisplay.toShortString()); + + } else { + // Shall not display balance + + balanceTextView.setText(""); + } } // @@ -188,6 +206,7 @@ public void displayBalance(final TextView balanceTextView, displayBalance(balanceTextView, balance, true, + true, true); } @@ -206,6 +225,7 @@ public void displayBalanceWithoutCurrency(final TextView transactionBalanceTextV displayBalance(transactionBalanceTextView, transactionBalance, shallDisplayNegativeSignumInSplits, + false, false); } /** diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java index 34aaaf4e4..62b210d8e 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionDetailActivity.java @@ -78,6 +78,7 @@ public SplitAmountViewHolder(View view, accountType.displayBalance(balanceView, splitSignedAmount, false, + true, true); } @@ -234,6 +235,7 @@ private void bindViews() { accountType.displayBalance(balanceTextView, accountBalance, true, + true, true); // From 978a5eb1bba2c49f3a774ddd8ff0b390478e8cbb Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sun, 14 Jun 2020 12:50:07 +0200 Subject: [PATCH 66/77] #876 - ** Add shallDisplayZero in displayBalanceWithoutCurrency(...) to avoid displaying 0 --- .../java/org/gnucash/android/model/AccountType.java | 5 +++-- .../android/ui/transaction/SplitEditorFragment.java | 3 ++- .../ui/transaction/TransactionFormFragment.java | 13 ++++++++----- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/model/AccountType.java b/app/src/main/java/org/gnucash/android/model/AccountType.java index 0738bc9f6..b45374ac7 100644 --- a/app/src/main/java/org/gnucash/android/model/AccountType.java +++ b/app/src/main/java/org/gnucash/android/model/AccountType.java @@ -220,12 +220,13 @@ public void displayBalance(final TextView balanceTextView, */ public void displayBalanceWithoutCurrency(final TextView transactionBalanceTextView, final Money transactionBalance, - final boolean shallDisplayNegativeSignumInSplits) { + final boolean shallDisplayNegativeSignumInSplits, + final boolean shallDisplayZero) { displayBalance(transactionBalanceTextView, transactionBalance, shallDisplayNegativeSignumInSplits, - false, + shallDisplayZero, false); } /** diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java index a8c7c9412..d4e3503f2 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java @@ -539,7 +539,8 @@ private void updateSplitAmountEditText(final Split split) { // Display abs value because switch button is visible accountType.displayBalanceWithoutCurrency(splitAmountEditText, splitValueWithSignum, - shallDisplayNegativeSignumInSplits); + shallDisplayNegativeSignumInSplits, + false); } /** diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java index 3f3e2eae1..5f95277eb 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java @@ -605,10 +605,12 @@ private void updateAmountEditText(final Money signedTransactionBalance) { ? signedTransactionBalance.negate() : signedTransactionBalance; + final boolean isTransactionTypeSwitchVisible = mTransactionTypeSwitch.getVisibility() != View.GONE; + accountType.displayBalanceWithoutCurrency(mAmountEditText, newSignedTransactionBalance, - shallDisplayNegativeSignumInSplits || (mTransactionTypeSwitch.getVisibility() - == View.GONE)); + shallDisplayNegativeSignumInSplits || !isTransactionTypeSwitchVisible, + !isTransactionTypeSwitchVisible); } private void setDoubleEntryViewsVisibility(int visibility) { @@ -1271,12 +1273,13 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK){ + // Once split editor has been used and saved, only allow editing through it + toggleAmountInputEntryMode(false); + setDoubleEntryViewsVisibility(View.GONE); + List splitList = data.getParcelableArrayListExtra(UxArgument.SPLIT_LIST); setSplitList(splitList); - //once split editor has been used and saved, only allow editing through it - toggleAmountInputEntryMode(false); - setDoubleEntryViewsVisibility(View.GONE); mOpenSplitEditor.setVisibility(View.VISIBLE); } } From b8d58a7d0b90493e66f715d1e24d23f4ee6c885c Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sun, 14 Jun 2020 18:15:59 +0200 Subject: [PATCH 67/77] #876 - Renaming toggleAmountInputEntryMode() to setAllowAmountEdit --- .../transaction/TransactionFormFragment.java | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java index 5f95277eb..42a6a6096 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java @@ -521,7 +521,7 @@ private void initializeViewsWithTransaction(){ final boolean isSimpleSplit = mSplitsList.size() <= 2; - toggleAmountInputEntryMode(isSimpleSplit); + setAllowAmountEdit(isSimpleSplit); if (mSplitsList.size() == 2){ for (Split split : mSplitsList) { @@ -618,15 +618,25 @@ private void setDoubleEntryViewsVisibility(int visibility) { mTransactionTypeSwitch.setVisibility(visibility); } - private void toggleAmountInputEntryMode(boolean enabled){ - if (enabled){ + private void setAllowAmountEdit(boolean isAmountEditAllowed){ + + if (isAmountEditAllowed){ + // Amount is allowed to be edited using keyboard + mAmountEditText.setFocusable(true); + + // Use Keyboard to edit amount mAmountEditText.bindListeners(mKeyboardView); + } else { + // Amount is not allowed to be edited, but can be clicked to open SplitEditor + mAmountEditText.setFocusable(false); + mAmountEditText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + openSplitEditor(); } }); @@ -1272,15 +1282,22 @@ public void onRecurrenceSet(String rrule) { public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK){ + // Splits have been saved => Use // Once split editor has been used and saved, only allow editing through it - toggleAmountInputEntryMode(false); + + // Do not allow keyboard anymore to edit amount + setAllowAmountEdit(false); + + // Display Split Editor button + mOpenSplitEditor.setVisibility(View.VISIBLE); + + // Hide double Entry setDoubleEntryViewsVisibility(View.GONE); + // Set Split list from intent data coming from Split Editor List splitList = data.getParcelableArrayListExtra(UxArgument.SPLIT_LIST); setSplitList(splitList); - - mOpenSplitEditor.setVisibility(View.VISIBLE); } } } From d92391b800781ca3baa6135df543743c85bf6243 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sun, 14 Jun 2020 18:22:10 +0200 Subject: [PATCH 68/77] #876 - Remove black color for 0 balance, to be homogeneous with TransactionTypeSwitch.setWidgetTextColor() --- .../gnucash/android/model/AccountType.java | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/model/AccountType.java b/app/src/main/java/org/gnucash/android/model/AccountType.java index b45374ac7..6826f598b 100644 --- a/app/src/main/java/org/gnucash/android/model/AccountType.java +++ b/app/src/main/java/org/gnucash/android/model/AccountType.java @@ -1,6 +1,5 @@ package org.gnucash.android.model; -import android.content.Context; import android.support.annotation.ColorInt; import android.support.annotation.ColorRes; import android.widget.TextView; @@ -9,7 +8,6 @@ import org.gnucash.android.app.GnuCashApplication; import org.gnucash.android.ui.settings.PreferenceActivity; -import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -172,22 +170,9 @@ public void displayBalance(final TextView balanceTextView, @ColorInt int fontColor; - if (balance.asBigDecimal() - .compareTo(BigDecimal.ZERO) == 0) { - // balance is null + final boolean isCreditBalance = balance.isNegative(); - Context context = GnuCashApplication.getAppContext(); - - fontColor = context.getResources() - .getColor(android.R.color.black); - - } else { - // balance is not null - - final boolean isCreditBalance = balance.isNegative(); - - fontColor = getAmountColor(isCreditBalance); - } + fontColor = getAmountColor(isCreditBalance); balanceTextView.setTextColor(fontColor); } From 1f41df36f59defb02f1a0c41f1771229ac382989 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sun, 14 Jun 2020 18:30:52 +0200 Subject: [PATCH 69/77] #876 - Use updateAmountEditText(...) to factorize code --- .../android/ui/transaction/TransactionFormFragment.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java index 42a6a6096..8712fb804 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java @@ -469,10 +469,9 @@ public void onItemClick(AdapterView adapterView, if (!amountEntered) { // user already entered an amount - // TODO TW C 2020-05-23 : Il faudrait homogénéïser en appelant updateAmountEditText ? - mAmountEditText.setValue(splitList.get(0) - .getValue() - .asBigDecimal()); + final Money firstSplitAmount = splitList.get(0) + .getValue(); + updateAmountEditText(firstSplitAmount); } } else { From 511f8894e8ffa1aa5ce29899e185e1f6d546d9da Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Sun, 14 Jun 2020 18:42:40 +0200 Subject: [PATCH 70/77] #876 - Code formatting --- .../android/db/adapter/DatabaseAdapter.java | 11 +-- .../ui/account/AccountsListFragment.java | 70 +++++++++++++------ 2 files changed, 55 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/db/adapter/DatabaseAdapter.java b/app/src/main/java/org/gnucash/android/db/adapter/DatabaseAdapter.java index e753ad46f..ca958c29e 100644 --- a/app/src/main/java/org/gnucash/android/db/adapter/DatabaseAdapter.java +++ b/app/src/main/java/org/gnucash/android/db/adapter/DatabaseAdapter.java @@ -531,12 +531,15 @@ public int deleteAllRecords(){ * @throws IllegalArgumentException if the GUID does not exist in the database */ public long getID(@NonNull String uid){ + Cursor cursor = mDb.query(mTableName, - new String[] {DatabaseSchema.CommonColumns._ID}, - DatabaseSchema.CommonColumns.COLUMN_UID + " = ?", - new String[]{uid}, - null, null, null); + new String[]{DatabaseSchema.CommonColumns._ID}, + DatabaseSchema.CommonColumns.COLUMN_UID + " = ?", + new String[]{uid}, + null, null, null); + long result = -1; + try{ if (cursor.moveToFirst()) { result = cursor.getLong(cursor.getColumnIndexOrThrow(DatabaseSchema.CommonColumns._ID)); diff --git a/app/src/main/java/org/gnucash/android/ui/account/AccountsListFragment.java b/app/src/main/java/org/gnucash/android/ui/account/AccountsListFragment.java index 6c60b28be..aa934eb4a 100644 --- a/app/src/main/java/org/gnucash/android/ui/account/AccountsListFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/account/AccountsListFragment.java @@ -32,7 +32,6 @@ import android.support.v4.view.MenuItemCompat; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; -import android.support.v7.preference.PreferenceManager; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.PopupMenu; @@ -484,27 +483,39 @@ public AccountViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { @Override public void onBindViewHolderCursor(final AccountViewHolder holder, final Cursor cursor) { + final String accountUID = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseSchema.AccountEntry.COLUMN_UID)); + mAccountsDbAdapter = AccountsDbAdapter.getInstance(); - holder.accoundId = mAccountsDbAdapter.getID(accountUID); + holder.accoundId = mAccountsDbAdapter.getID(accountUID); holder.accountName.setText(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseSchema.AccountEntry.COLUMN_NAME))); + int subAccountCount = mAccountsDbAdapter.getSubAccountCount(accountUID); + if (subAccountCount > 0) { holder.description.setVisibility(View.VISIBLE); - String text = getResources().getQuantityString(R.plurals.label_sub_accounts, subAccountCount, subAccountCount); + String text = getResources().getQuantityString(R.plurals.label_sub_accounts, + subAccountCount, + subAccountCount); holder.description.setText(text); - } else + + } else { holder.description.setVisibility(View.GONE); + } // add a summary of transactions to the account view - // Make sure the balance task is truly multithread + // Make sure the balance task is truly multithread new AccountBalanceTask(holder.accountBalance).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, - accountUID); + accountUID); String accountColor = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseSchema.AccountEntry.COLUMN_COLOR_CODE)); - int colorCode = accountColor == null ? Color.TRANSPARENT : Color.parseColor(accountColor); + + int colorCode = accountColor == null + ? Color.TRANSPARENT + : Color.parseColor(accountColor); + holder.colorStripView.setBackgroundColor(colorCode); boolean isPlaceholderAccount = mAccountsDbAdapter.isPlaceholderAccount(accountUID); @@ -515,21 +526,30 @@ public void onBindViewHolderCursor(final AccountViewHolder holder, final Cursor @Override public void onClick(View v) { - Intent intent = new Intent(getActivity(), FormActivity.class); + + Intent intent = new Intent(getActivity(), + FormActivity.class); intent.setAction(Intent.ACTION_INSERT_OR_EDIT); - intent.putExtra(UxArgument.SELECTED_ACCOUNT_UID, accountUID); - intent.putExtra(UxArgument.FORM_TYPE, FormActivity.FormType.TRANSACTION.name()); + intent.putExtra(UxArgument.SELECTED_ACCOUNT_UID, + accountUID); + intent.putExtra(UxArgument.FORM_TYPE, + FormActivity.FormType.TRANSACTION.name()); getActivity().startActivity(intent); } }); } - List budgets = BudgetsDbAdapter.getInstance().getAccountBudgets(accountUID); + List budgets = BudgetsDbAdapter.getInstance() + .getAccountBudgets(accountUID); //TODO: include fetch only active budgets - if (budgets.size() == 1){ - Budget budget = budgets.get(0); - Money balance = mAccountsDbAdapter.getAccountBalance(accountUID, budget.getStartofCurrentPeriod(), budget.getEndOfCurrentPeriod()); - double budgetProgress = balance.divide(budget.getAmount(accountUID)).asBigDecimal().doubleValue() * 100; + if (budgets.size() == 1) { + Budget budget = budgets.get(0); + Money balance = mAccountsDbAdapter.getAccountBalance(accountUID, + budget.getStartofCurrentPeriod(), + budget.getEndOfCurrentPeriod()); + double budgetProgress = balance.divide(budget.getAmount(accountUID)) + .asBigDecimal() + .doubleValue() * 100; holder.budgetIndicator.setVisibility(View.VISIBLE); holder.budgetIndicator.setProgress((int) budgetProgress); @@ -538,7 +558,7 @@ public void onClick(View v) { } - if (mAccountsDbAdapter.isFavoriteAccount(accountUID)){ + if (mAccountsDbAdapter.isFavoriteAccount(accountUID)) { holder.favoriteStatus.setImageResource(R.drawable.ic_star_black_24dp); } else { holder.favoriteStatus.setImageResource(R.drawable.ic_star_border_black_24dp); @@ -547,23 +567,29 @@ public void onClick(View v) { holder.favoriteStatus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + boolean isFavoriteAccount = mAccountsDbAdapter.isFavoriteAccount(accountUID); ContentValues contentValues = new ContentValues(); - contentValues.put(DatabaseSchema.AccountEntry.COLUMN_FAVORITE, !isFavoriteAccount); - mAccountsDbAdapter.updateRecord(accountUID, contentValues); - - int drawableResource = !isFavoriteAccount ? - R.drawable.ic_star_black_24dp : R.drawable.ic_star_border_black_24dp; + contentValues.put(DatabaseSchema.AccountEntry.COLUMN_FAVORITE, + !isFavoriteAccount); + mAccountsDbAdapter.updateRecord(accountUID, + contentValues); + + int drawableResource = !isFavoriteAccount + ? R.drawable.ic_star_black_24dp + : R.drawable.ic_star_border_black_24dp; holder.favoriteStatus.setImageResource(drawableResource); - if (mDisplayMode == DisplayMode.FAVORITES) + if (mDisplayMode == DisplayMode.FAVORITES) { refresh(); + } } }); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + onListItemClick(accountUID); } }); From 849bc2aff18af6047af35150c7f120102a72482e Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Mon, 15 Jun 2020 02:40:26 +0200 Subject: [PATCH 71/77] #876 - ** Must be on the Account Page to change Book --- .../android/ui/common/BaseDrawerActivity.java | 106 +++++++++++++++--- 1 file changed, 92 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/common/BaseDrawerActivity.java b/app/src/main/java/org/gnucash/android/ui/common/BaseDrawerActivity.java index 7316250d6..24b8ca873 100644 --- a/app/src/main/java/org/gnucash/android/ui/common/BaseDrawerActivity.java +++ b/app/src/main/java/org/gnucash/android/ui/common/BaseDrawerActivity.java @@ -16,6 +16,7 @@ package org.gnucash.android.ui.common; import android.app.Activity; +import android.app.ActivityManager; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; @@ -32,11 +33,14 @@ import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.widget.PopupMenu; import android.support.v7.widget.Toolbar; +import android.util.Log; +import android.view.Gravity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ProgressBar; import android.widget.TextView; +import android.widget.Toast; import com.uservoice.uservoicesdk.UserVoice; @@ -51,6 +55,8 @@ import org.gnucash.android.ui.transaction.ScheduledActionsActivity; import org.gnucash.android.util.BookUtils; +import java.util.List; + import butterknife.BindView; import butterknife.ButterKnife; @@ -210,10 +216,11 @@ public void onConfigurationChanged(Configuration newConfig) { @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home){ - if (!mDrawerLayout.isDrawerOpen(mNavigationView)) + if (!isNavigationViewOpen()) { mDrawerLayout.openDrawer(mNavigationView); - else - mDrawerLayout.closeDrawer(mNavigationView); + } else { + closeNavigationView(); + } return true; } @@ -286,7 +293,7 @@ protected void onDrawerMenuItemClicked(int itemId) { UserVoice.launchUserVoice(this); break; } - mDrawerLayout.closeDrawer(mNavigationView); + closeNavigationView(); } @Override @@ -315,6 +322,8 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { @Override public boolean onMenuItemClick(MenuItem item) { + closeNavigationView(); + long itemId = item.getItemId(); if (itemId == ID_MANAGE_BOOKS){ @@ -325,8 +334,6 @@ public boolean onMenuItemClick(MenuItem item) { intent.setAction(PreferenceActivity.ACTION_MANAGE_BOOKS); startActivity(intent); - mDrawerLayout.closeDrawer(mNavigationView); - } else { // Click on an existing book item @@ -334,14 +341,67 @@ public boolean onMenuItemClick(MenuItem item) { String selectedBookUID = booksDbAdapter.getUID(itemId); - if (!selectedBookUID.equals(booksDbAdapter.getActiveBookUID())){ + if (!selectedBookUID.equals(booksDbAdapter.getActiveBookUID())) { // Selected Book is not the active one - // Close current Activity - finish(); - - // load book and Start Account Activity and reset Activity Stack - BookUtils.loadBook(selectedBookUID); + // + // Check if current Activity is the first Activity + // + + Log.d("BaseDrawerActivity", + "This is (" + this.getClass() + .getName() + ")"); + + ActivityManager mngr = (ActivityManager) getSystemService(ACTIVITY_SERVICE); + List taskList = mngr.getRunningTasks(10); + final ActivityManager.RunningTaskInfo task0RunningInfo = taskList.get(0); + + if (task0RunningInfo.numActivities <= 1) { + // This is the first Activity + + // Close current Activity (pop Activity stack) + finish(); + + // + // load selected book and Start Account Activity and reset Activity Stack + // + + BookUtils.loadBook(selectedBookUID); + + } else { + // This is not the first Activity + + Toast toast = Toast.makeText(this, + "You must be on the Account Page to change Book", + Toast.LENGTH_LONG); + + // + // Align-Center text inside the Toast + // + + TextView toastTextView = (TextView) toast.getView() + .findViewById(android.R.id.message); + if (toastTextView != null) { + toastTextView.setGravity(Gravity.CENTER); + } + + // Show toast + toast.show(); + } + +// // Android handler to delay actions +// Handler handler = new Handler(); +// +// // After two seconds, it is not more considered as already pressed +// handler.postDelayed(new Runnable() { +// @Override +// public void run() { +// +// // load book and Start Account Activity and reset Activity Stack +// BookUtils.loadBook(selectedBookUID); +// } +// }, +// 5000); } else { // Selected Book is the current one @@ -356,9 +416,17 @@ public boolean onMenuItemClick(MenuItem item) { return true; } - public void onClickAppTitle(View view){ + protected void onClickAppTitle(View view) { + + closeNavigationView(); + + // Do not launch AccountsActivity to stay on current Activity +// AccountsActivity.start(this); + } + + protected void closeNavigationView() { + mDrawerLayout.closeDrawer(mNavigationView); - AccountsActivity.start(this); } public void onClickBook(View view){ @@ -378,4 +446,14 @@ public void onClickBook(View view){ popup.show(); } + + /** + * Return true if main navigation menu is open + * + * @return true if main navigation menu is open + */ + protected boolean isNavigationViewOpen() { + + return mDrawerLayout.isDrawerOpen(mNavigationView); + } } From 5d36c85d1d30702600929b3a367212bb4dde3624 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Mon, 15 Jun 2020 02:41:00 +0200 Subject: [PATCH 72/77] #876 - Code formatting --- .../gnucash/android/ui/transaction/TransactionsActivity.java | 2 +- .../android/ui/transaction/TransactionsListFragment.java | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java index 5f36cf322..609eef7d6 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsActivity.java @@ -306,7 +306,7 @@ protected void onCreate(Bundle savedInstanceState) { mAccountsDbAdapter = AccountsDbAdapter.getInstance(); // - // Add Tranbsaction Page + // Add Transaction Page // mIsPlaceholderAccount = mAccountsDbAdapter.isPlaceholderAccount(getCurrentAccountUID()); diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsListFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsListFragment.java index db9592eba..22c4d3ed2 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsListFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsListFragment.java @@ -270,6 +270,7 @@ public int getItemViewType(int position) { @Override public void onBindViewHolderCursor(ViewHolder holder, Cursor cursor) { + holder.transactionId = cursor.getLong(cursor.getColumnIndexOrThrow(DatabaseSchema.TransactionEntry._ID)); String description = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseSchema.TransactionEntry.COLUMN_DESCRIPTION)); From 1e2d83acbd8eb0c9b96d6b1e49dd326ee0a7befc Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Mon, 15 Jun 2020 02:42:10 +0200 Subject: [PATCH 73/77] #876 - Remove #586 --- .../org/gnucash/android/ui/account/AccountsActivity.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/src/main/java/org/gnucash/android/ui/account/AccountsActivity.java b/app/src/main/java/org/gnucash/android/ui/account/AccountsActivity.java index cc2735d33..ae16ef372 100644 --- a/app/src/main/java/org/gnucash/android/ui/account/AccountsActivity.java +++ b/app/src/main/java/org/gnucash/android/ui/account/AccountsActivity.java @@ -154,7 +154,9 @@ public AccountViewPagerAdapter(FragmentManager fm){ @Override public Fragment getItem(int i) { AccountsListFragment currentFragment = (AccountsListFragment) mFragmentPageReferenceMap.get(i); + if (currentFragment == null) { + switch (i) { case INDEX_RECENT_ACCOUNTS_FRAGMENT: currentFragment = AccountsListFragment.newInstance(AccountsListFragment.DisplayMode.RECENT); @@ -169,13 +171,16 @@ public Fragment getItem(int i) { currentFragment = AccountsListFragment.newInstance(AccountsListFragment.DisplayMode.TOP_LEVEL); break; } + mFragmentPageReferenceMap.put(i, currentFragment); + } return currentFragment; } @Override public void destroyItem(ViewGroup container, int position, Object object) { +// #586 By putting this in comment, there is no more crash, but I don't know if there are side effects super.destroyItem(container, position, object); mFragmentPageReferenceMap.remove(position); } @@ -376,6 +381,7 @@ private void init() { @Override protected void onDestroy() { super.onDestroy(); + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); preferences.edit().putInt(LAST_OPEN_TAB_INDEX, mViewPager.getCurrentItem()).apply(); } From 1ee83dd5823378a3aedb885510897f6858ab4487 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Tue, 16 Jun 2020 23:20:44 +0200 Subject: [PATCH 74/77] #876 - Toast message internationalization --- .../org/gnucash/android/ui/common/BaseDrawerActivity.java | 6 +++--- app/src/main/res/values-fr/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/ui/common/BaseDrawerActivity.java b/app/src/main/java/org/gnucash/android/ui/common/BaseDrawerActivity.java index 24b8ca873..52c93ba5b 100644 --- a/app/src/main/java/org/gnucash/android/ui/common/BaseDrawerActivity.java +++ b/app/src/main/java/org/gnucash/android/ui/common/BaseDrawerActivity.java @@ -372,15 +372,15 @@ public boolean onMenuItemClick(MenuItem item) { // This is not the first Activity Toast toast = Toast.makeText(this, - "You must be on the Account Page to change Book", - Toast.LENGTH_LONG); + R.string.toast_must_be_on_account_page_to_change_book, + Toast.LENGTH_LONG); // // Align-Center text inside the Toast // TextView toastTextView = (TextView) toast.getView() - .findViewById(android.R.id.message); + .findViewById(android.R.id.message); if (toastTextView != null) { toastTextView.setGravity(Gravity.CENTER); } diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index ca5f54be2..fc1e15b79 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -452,4 +452,5 @@ Préférences Afficher le signe - dans les transactions splittées Autoriser l\'affichage du signe - même lorsque le bouton Débit/Crédit est affiché + Vous devez être sur la page des Comptes pour pouvoir changer de Comptabilité diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ca4433022..74c22cb3d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -439,6 +439,7 @@ INCOME + You must be on the Account Page to change Book Connected to Google Drive Unable to connect to Google Drive Please enter an amount to split From 56bb0a026882f099f8ccca511e98b3aa3480424d Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Tue, 16 Jun 2020 23:58:15 +0200 Subject: [PATCH 75/77] Remove TODO --- .../main/java/org/gnucash/android/model/TransactionType.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/org/gnucash/android/model/TransactionType.java b/app/src/main/java/org/gnucash/android/model/TransactionType.java index cf3fc626a..2e4613fb2 100644 --- a/app/src/main/java/org/gnucash/android/model/TransactionType.java +++ b/app/src/main/java/org/gnucash/android/model/TransactionType.java @@ -24,7 +24,7 @@ * @author Ngewi Fet * @author Jesse Shieh */ -// TODO TW m 2020-03-03 : Should be named SplitType +// TW m 2020-03-03 : Should be named SplitType public enum TransactionType { DEBIT, CREDIT; From 13b3baf9ee86e35ad76e7d8940e5bea86e9aa87e Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Tue, 16 Jun 2020 23:58:59 +0200 Subject: [PATCH 76/77] #876 - Add another test to reduce probability of not being able to change Book --- .../org/gnucash/android/ui/common/BaseDrawerActivity.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/gnucash/android/ui/common/BaseDrawerActivity.java b/app/src/main/java/org/gnucash/android/ui/common/BaseDrawerActivity.java index 52c93ba5b..76382c25a 100644 --- a/app/src/main/java/org/gnucash/android/ui/common/BaseDrawerActivity.java +++ b/app/src/main/java/org/gnucash/android/ui/common/BaseDrawerActivity.java @@ -356,7 +356,9 @@ public boolean onMenuItemClick(MenuItem item) { List taskList = mngr.getRunningTasks(10); final ActivityManager.RunningTaskInfo task0RunningInfo = taskList.get(0); - if (task0RunningInfo.numActivities <= 1) { + if (task0RunningInfo.numActivities <= 1 || task0RunningInfo.baseActivity.getClassName() + .equals(this.getClass() + .getName())) { // This is the first Activity // Close current Activity (pop Activity stack) From 8cca2e3db7eb2599a41e42f7bed6dc97423adb29 Mon Sep 17 00:00:00 2001 From: JeanGarf Date: Tue, 8 Jun 2021 00:46:15 +0200 Subject: [PATCH 77/77] #876 - ** when editing transaction, display 2 decimals but not currency symbol because it shall be evaluated (in order to allows arithmetic expressions as value) --- .../org/gnucash/android/model/AccountType.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/model/AccountType.java b/app/src/main/java/org/gnucash/android/model/AccountType.java index 6826f598b..fa51113f0 100644 --- a/app/src/main/java/org/gnucash/android/model/AccountType.java +++ b/app/src/main/java/org/gnucash/android/model/AccountType.java @@ -120,6 +120,8 @@ public TransactionType getDefaultTransactionType() { * @param shallDisplayCurrency if true will display Currency as well as amount (usefull when displaying balance in * TransactionListFragment, BalanceSheetFragment...) * else will not display Currency (usefull in SplitEditorFragment or TransactionFormFragment) + * because this allows the value to be an arithmetic expression (ex. 130.67+3) that can be + * evluated when loosing focus */ public void displayBalance(final TextView balanceTextView, final Money balance, @@ -134,7 +136,7 @@ public void displayBalance(final TextView balanceTextView, final Money balanceToDisplay = getBalanceWithSignumForDisplay(balance); if (shallDisplayCurrency) { - // Shall currency + // Shall display currency // Display currency balanceTextView.setText(!shallDisplayNegativeSignum @@ -143,7 +145,8 @@ public void displayBalance(final TextView balanceTextView, : balanceToDisplay.formattedString()); } else { - // Shall not display currency + // Shall not display currency, because the value may be an aritmetic expression (ex. 130.67+3) + // and must not contain currency to be evaluated // Shall display value in all other cases than zero balance and shall not display zero balance final boolean shallDisplayValue = !(balanceToDisplay.isAmountZero() && !shallDisplayZero); @@ -151,11 +154,12 @@ public void displayBalance(final TextView balanceTextView, if (shallDisplayValue) { // Shall display balance - // Display value without currency and without decimals + // Display value without currency but with 2 decimals, in order to be able to be evaluated + // in the case where the value is an arithmetic expression like 130.67+3 balanceTextView.setText(!shallDisplayNegativeSignum ? balanceToDisplay.abs() - .toShortString() - : balanceToDisplay.toShortString()); + .toLocaleString() + : balanceToDisplay.toLocaleString()); } else { // Shall not display balance