Skip to content

Commit

Permalink
codinguser#876 - Transfer displayBalance(...) into AccountType and ma…
Browse files Browse the repository at this point in the history
…ke methods non static
  • Loading branch information
JeanGarf committed Mar 7, 2020
1 parent 8e04ba5 commit 535e3d5
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 72 deletions.
92 changes: 69 additions & 23 deletions app/src/main/java/org/gnucash/android/model/AccountType.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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. <br>
Expand Down

0 comments on commit 535e3d5

Please sign in to comment.