Skip to content

Commit 0b49a97

Browse files
committed
Fixed: Crash when deleting transactions with splits
Fixed: Splits editor shows wrong transaction type when editing splits
1 parent 713e96e commit 0b49a97

File tree

6 files changed

+46
-61
lines changed

6 files changed

+46
-61
lines changed

app/src/org/gnucash/android/db/SplitsDbAdapter.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,12 @@ public List<Split> getSplitsForTransaction(long transactionID){
207207
public List<Split> getSplitsForTransactionInAccount(String transactionUID, String accountUID){
208208
Cursor cursor = fetchSplitsForTransactionAndAccount(transactionUID, accountUID);
209209
List<Split> splitList = new ArrayList<Split>();
210-
while (cursor != null && cursor.moveToNext()){
211-
splitList.add(buildSplitInstance(cursor));
212-
}
213-
if (cursor != null)
210+
if (cursor != null){
211+
while (cursor.moveToNext()){
212+
splitList.add(buildSplitInstance(cursor));
213+
}
214214
cursor.close();
215-
215+
}
216216
return splitList;
217217
}
218218

@@ -312,6 +312,9 @@ public Cursor fetchSplitsForAccount(String accountUID){
312312
* @return Cursor to splits data set
313313
*/
314314
public Cursor fetchSplitsForTransactionAndAccount(String transactionUID, String accountUID){
315+
if (transactionUID == null || accountUID == null)
316+
return null;
317+
315318
Log.v(TAG, "Fetching all splits for transaction ID " + transactionUID
316319
+ "and account ID " + accountUID);
317320
return mDb.query(SplitEntry.TABLE_NAME,

app/src/org/gnucash/android/model/Transaction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,8 @@ public static TransactionType getTypeForBalance(AccountType accountType, boolean
428428
}
429429

430430
/**
431-
* Returns true if the amount represents a decrease for the account balance in accounty of type <code>accountType</code>, false otherwise
432-
* @return true if the amount represents a decrease movement for the account balance, false otherwise
431+
* Returns true if the transaction type represents a decrease for the account balance for the <code>accountType</code>, false otherwise
432+
* @return true if the amount represents a decrease in the account balance, false otherwise
433433
* @see #getTypeForBalance(AccountType, boolean)
434434
*/
435435
public static boolean shouldDecreaseBalance(AccountType accountType, TransactionType transactionType){

app/src/org/gnucash/android/ui/transaction/TransactionFormFragment.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import java.text.ParseException;
2323
import java.util.*;
2424

25-
import android.app.AlarmManager;
26-
import android.app.PendingIntent;
2725
import android.support.v4.app.FragmentManager;
2826
import android.widget.*;
2927
import org.gnucash.android.R;
@@ -55,7 +53,6 @@
5553
import android.view.View;
5654
import android.view.ViewGroup;
5755
import android.view.inputmethod.InputMethodManager;
58-
import android.widget.CompoundButton.OnCheckedChangeListener;
5956

6057
import com.actionbarsherlock.app.ActionBar;
6158
import com.actionbarsherlock.app.SherlockFragment;
@@ -365,11 +362,9 @@ private void initalizeViews() {
365362
mTimeTextView.setText(TIME_FORMATTER.format(time));
366363
mTime = mDate = Calendar.getInstance();
367364

368-
String typePref = PreferenceManager.getDefaultSharedPreferences(getActivity()).getString(getString(R.string.key_default_transaction_type), "DEBIT");
369-
if (typePref.equals("CREDIT")){
370-
mTransactionTypeButton.setChecked(false);
371-
}
372365
mTransactionTypeButton.setAccountType(mAccountType);
366+
String typePref = PreferenceManager.getDefaultSharedPreferences(getActivity()).getString(getString(R.string.key_default_transaction_type), "DEBIT");
367+
mTransactionTypeButton.setChecked(TransactionType.valueOf(typePref));
373368

374369
final long accountId = getArguments().getLong(UxArgument.SELECTED_ACCOUNT_ID);
375370
String code = Money.DEFAULT_CURRENCY_CODE;
@@ -464,7 +459,7 @@ private void openSplitEditor(){
464459
* Sets click listeners for the dialog buttons
465460
*/
466461
private void setListeners() {
467-
mAmountInputFormatter = new AmountInputFormatter(mAmountEditText, mTransactionTypeButton);
462+
mAmountInputFormatter = new AmountInputFormatter(mAmountEditText);
468463
mAmountEditText.addTextChangedListener(mAmountInputFormatter);
469464

470465
mOpenSplitsButton.setOnClickListener(new View.OnClickListener() {
@@ -474,7 +469,7 @@ public void onClick(View view) {
474469
}
475470
});
476471

477-
mTransactionTypeButton.setupCheckedListener(mAmountEditText, mCurrencyTextView);
472+
mTransactionTypeButton.setAmountFormattingListener(mAmountEditText, mCurrencyTextView);
478473

479474
mDateTextView.setOnClickListener(new View.OnClickListener() {
480475

app/src/org/gnucash/android/ui/transaction/dialog/SplitEditorDialogFragment.java

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,7 @@ public void onActivityCreated(Bundle savedInstanceState) {
117117
final Currency currency = Currency.getInstance(mAccountsDbAdapter.getCurrencyCode(mAccountUID));
118118
Split split = new Split(new Money(mBaseAmount, currency), mAccountUID);
119119
AccountType accountType = mAccountsDbAdapter.getAccountType(mAccountUID);
120-
TransactionType transactionType;
121-
if (accountType.hasDebitNormalBalance()) {
122-
transactionType = mBaseAmount.signum() < 0 ? TransactionType.CREDIT : TransactionType.DEBIT;
123-
} else {
124-
transactionType = mBaseAmount.signum() < 0 ? TransactionType.CREDIT : TransactionType.DEBIT;
125-
}
120+
TransactionType transactionType = Transaction.getTypeForBalance(accountType, mBaseAmount.signum() < 0);
126121
split.setType(transactionType);
127122
View view = addSplitView(split);
128123
view.findViewById(R.id.input_accounts_spinner).setEnabled(false);
@@ -186,8 +181,7 @@ private void bindSplitView(final View splitView, Split split){
186181
final TextView splitUidTextView = (TextView) splitView.findViewById(R.id.split_uid);
187182
final TransactionTypeToggleButton splitTypeButton = (TransactionTypeToggleButton) splitView.findViewById(R.id.btn_split_type);
188183

189-
splitAmountEditText.addTextChangedListener(new AmountInputFormatter(splitAmountEditText,splitTypeButton));
190-
splitAmountEditText.addTextChangedListener(mBalanceUpdater);
184+
splitAmountEditText.addTextChangedListener(new AmountInputFormatter(splitAmountEditText));
191185

192186
removeSplitButton.setOnClickListener(new View.OnClickListener() {
193187
@Override
@@ -199,28 +193,31 @@ public void onClick(View view) {
199193
}
200194
});
201195

202-
accountsSpinner.setOnItemSelectedListener(new TypeButtonLabelUpdater(splitTypeButton));
203196
updateTransferAccountsList(accountsSpinner);
197+
accountsSpinner.setOnItemSelectedListener(new TypeButtonLabelUpdater(splitTypeButton));
204198

205-
splitTypeButton.setupCheckedListener(splitAmountEditText, splitCurrencyTextView);
206-
splitTypeButton.setOnClickListener(new View.OnClickListener() {
207-
@Override
208-
public void onClick(View view) {
209-
updateTotal();
210-
}
211-
});
212-
199+
splitTypeButton.setAmountFormattingListener(splitAmountEditText, splitCurrencyTextView);
213200
splitTypeButton.setChecked(mBaseAmount.signum() > 0);
214201
splitUidTextView.setText(UUID.randomUUID().toString());
215202

216203
if (split != null) {
217204
splitAmountEditText.setText(split.getAmount().toPlainString());
218205
splitMemoEditText.setText(split.getMemo());
219206
splitUidTextView.setText(split.getUID());
220-
221-
setSelectedTransferAccount(mAccountsDbAdapter.getAccountID(split.getAccountUID()), accountsSpinner);
222-
splitTypeButton.setChecked(Transaction.shouldDecreaseBalance(splitTypeButton.getAccountType(), split.getType()));
207+
String splitAccountUID = split.getAccountUID();
208+
setSelectedTransferAccount(mAccountsDbAdapter.getAccountID(splitAccountUID), accountsSpinner);
209+
splitTypeButton.setAccountType(mAccountsDbAdapter.getAccountType(splitAccountUID));
210+
splitTypeButton.setChecked(split.getType());
223211
}
212+
213+
//put these balance update triggers last last so as to avoid computing while still loading
214+
splitAmountEditText.addTextChangedListener(mBalanceUpdater);
215+
splitTypeButton.setOnClickListener(new View.OnClickListener() {
216+
@Override
217+
public void onClick(View view) {
218+
updateTotal();
219+
}
220+
});
224221
}
225222

226223
/**
@@ -230,13 +227,7 @@ public void onClick(View view) {
230227
private void setSelectedTransferAccount(long accountId, final Spinner accountsSpinner){
231228
for (int pos = 0; pos < mCursorAdapter.getCount(); pos++) {
232229
if (mCursorAdapter.getItemId(pos) == accountId){
233-
final int position = pos;
234-
accountsSpinner.postDelayed(new Runnable() {
235-
@Override
236-
public void run() {
237-
accountsSpinner.setSelection(position);
238-
}
239-
}, 100);
230+
accountsSpinner.setSelection(pos);
240231
break;
241232
}
242233
}
@@ -290,7 +281,7 @@ private List<Split> extractSplitsFromView(){
290281
List<Split> splitList = new ArrayList<Split>();
291282
for (View splitView : mSplitItemViewList) {
292283
EditText splitMemoEditText = (EditText) splitView.findViewById(R.id.input_split_memo);
293-
EditText splitAmountEditText = (EditText) splitView.findViewById(R.id.input_split_amount);
284+
EditText splitAmountEditText = (EditText) splitView.findViewById(R.id.input_split_amount);
294285
Spinner accountsSpinner = (Spinner) splitView.findViewById(R.id.input_accounts_spinner);
295286
TextView splitUidTextView = (TextView) splitView.findViewById(R.id.split_uid);
296287
TransactionTypeToggleButton splitTypeButton = (TransactionTypeToggleButton) splitView.findViewById(R.id.btn_split_type);

app/src/org/gnucash/android/ui/util/AmountInputFormatter.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import android.text.Editable;
1919
import android.text.TextWatcher;
2020
import android.widget.EditText;
21-
import android.widget.ToggleButton;
2221
import org.gnucash.android.ui.transaction.TransactionFormFragment;
2322

2423
import java.math.BigDecimal;
@@ -37,15 +36,13 @@
3736
public class AmountInputFormatter implements TextWatcher {
3837
private String current = "0";
3938
private EditText amountEditText;
40-
private ToggleButton mTypeButton;
4139
/**
4240
* Flag to note if the user has manually edited the amount of the transaction
4341
*/
4442
private boolean isModified = false;
4543

46-
public AmountInputFormatter(EditText amountInput, ToggleButton typeButton) {
44+
public AmountInputFormatter(EditText amountInput) {
4745
this.amountEditText = amountInput;
48-
this.mTypeButton = typeButton;
4946
}
5047

5148
@Override
@@ -54,22 +51,12 @@ public void afterTextChanged(Editable s) {
5451
return;
5552
//make sure that the sign of the input is in line with the type button state
5653
BigDecimal amount = TransactionFormFragment.parseInputToDecimal(s.toString());
57-
if (mTypeButton.isChecked()) {
58-
if (amount.signum() > 0) {
59-
amount = amount.negate();
60-
}
61-
} else { //if it is to increase account balance
62-
if (amount.signum() <= 0) {
63-
//make the number positive
64-
amount = amount.negate();
65-
}
66-
}
6754

6855
DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Locale.getDefault());
69-
formatter.setMinimumFractionDigits(2);
56+
formatter.setMinimumFractionDigits(2); //TODO: Use fraction of the currency in question
7057
formatter.setMaximumFractionDigits(2);
7158

72-
current = formatter.format(amount.doubleValue());
59+
current = formatter.format(amount.doubleValue()); //TODO: Try with Bigdecimal string instead of double
7360
amountEditText.removeTextChangedListener(this);
7461
amountEditText.setText(current);
7562
amountEditText.setSelection(current.length());

app/src/org/gnucash/android/ui/util/TransactionTypeToggleButton.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import android.widget.ToggleButton;
2525
import org.gnucash.android.R;
2626
import org.gnucash.android.model.AccountType;
27+
import org.gnucash.android.model.Transaction;
2728
import org.gnucash.android.model.TransactionType;
2829
import org.gnucash.android.ui.transaction.TransactionFormFragment;
2930

@@ -105,10 +106,18 @@ public void setAccountType(AccountType accountType){
105106
* @param amoutView Amount string {@link android.widget.EditText}
106107
* @param currencyTextView Currency symbol text view
107108
*/
108-
public void setupCheckedListener(EditText amoutView, TextView currencyTextView){
109+
public void setAmountFormattingListener(EditText amoutView, TextView currencyTextView){
109110
setOnCheckedChangeListener(new OnTypeChangedListener(amoutView, currencyTextView));
110111
}
111112

113+
/**
114+
* Toggles the button checked based on the movement caused by the transaction type for the specified account
115+
* @param transactionType {@link org.gnucash.android.model.TransactionType} of the split
116+
*/
117+
public void setChecked(TransactionType transactionType){
118+
setChecked(Transaction.shouldDecreaseBalance(mAccountType, transactionType));
119+
}
120+
112121
/**
113122
* Returns the account type associated with this button
114123
* @return

0 commit comments

Comments
 (0)