Skip to content

Commit

Permalink
codinguser#876 - Correct errors in debit/credit labels vs GnuCash Win…
Browse files Browse the repository at this point in the history
…dows. isChecked() : true == CREDIT. CREDIT => red, DEBIT => green
  • Loading branch information
JeanGarf committed Mar 2, 2020
1 parent 931bde4 commit 5367a26
Showing 1 changed file with 85 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
*/
Expand All @@ -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());
Expand Down Expand Up @@ -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));
}

/**
Expand All @@ -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{
Expand All @@ -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);
}
}
}

0 comments on commit 5367a26

Please sign in to comment.