Skip to content

Commit ec4e668

Browse files
committed
Fixed: Transaction edits not persisted to db. Fixes #180
Fixed: Fatal crash when error occurs in importing/exporting transaction (instead of displaying error message) Fixed: Editing a transfer transaction does not edit other side of the transaction Removed progress dialog from database migration (seems to be cause of some crashes) Transaction templates are not only available when creating new transactions Added some input validation to database adapters
1 parent 6ba22c7 commit ec4e668

7 files changed

Lines changed: 76 additions & 44 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -651,11 +651,11 @@ public List<Long> getSubAccountIds(long accountId){
651651
*/
652652
public Cursor fetchSubAccounts(long accountId){
653653
Log.v(TAG, "Fetching sub accounts for account id " + accountId);
654+
String accountUID = getAccountUID(accountId);
654655
return mDb.query(AccountEntry.TABLE_NAME,
655656
null,
656-
AccountEntry.COLUMN_PARENT_ACCOUNT_UID + " = ?",
657-
new String[]{getAccountUID(accountId)},
658-
null, null, AccountEntry.COLUMN_NAME + " ASC");
657+
AccountEntry.COLUMN_PARENT_ACCOUNT_UID + " = '" + accountUID + "'",
658+
null, null, null, AccountEntry.COLUMN_NAME + " ASC");
659659
}
660660

661661
/**

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,6 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
127127
Log.i(LOG_TAG, "Upgrading database from version "
128128
+ oldVersion + " to " + newVersion);
129129

130-
ProgressDialog progressDialog = ProgressDialog.show(mContext, "Upgrading database", "Processing...", true);
131-
132130
if (oldVersion < newVersion){
133131
//introducing double entry accounting
134132
Log.i(LOG_TAG, "Upgrading database to version " + newVersion);
@@ -194,7 +192,6 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
194192

195193
if (oldVersion == 5 && newVersion >= 6){
196194
Log.i(LOG_TAG, "Upgrading database to version 6");
197-
progressDialog.setMessage("Upgrading database to version 6");
198195

199196
String addFullAccountNameQuery = " ALTER TABLE " + AccountEntry.TABLE_NAME
200197
+ " ADD COLUMN " + AccountEntry.COLUMN_FULL_NAME + " varchar(255) ";
@@ -227,7 +224,6 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
227224

228225
if (oldVersion == 6 && newVersion >= DatabaseSchema.SPLITS_DB_VERSION){
229226
Log.i(LOG_TAG, "Upgrading database to version 7");
230-
progressDialog.setMessage("Upgrading to version " + SPLITS_DB_VERSION);
231227

232228
//for users who do not have double-entry activated, we create imbalance accounts for their splits
233229
//TODO: Enable when we can hide imbalance accounts from user
@@ -237,17 +233,12 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
237233
// accountsDbAdapter.getOrCreateImbalanceAccountUID(currency);
238234
// }
239235

240-
progressDialog.setMessage("Backing up database");
241236
try {
242237
String filepath = MigrationHelper.exportDatabase(db, ExportFormat.GNC_XML);
243238

244-
progressDialog.setMessage("Upgrading database schema");
245-
246239
dropAllDatabaseTables(db);
247240
createDatabaseTables(db);
248241

249-
progressDialog.setMessage("Restoring database");
250-
251242
MigrationHelper.importGnucashXML(db, filepath);
252243
} catch (Exception e){
253244
Toast.makeText(mContext, "Error upgrading database.\n" + e.getMessage(), Toast.LENGTH_LONG).show();
@@ -257,7 +248,6 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
257248
}
258249
}
259250

260-
progressDialog.dismiss();
261251
if (oldVersion != newVersion) {
262252
Log.w(LOG_TAG, "Upgrade for the database failed. The Database is currently at version " + oldVersion);
263253
}

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,15 @@ public Split buildSplitInstance(Cursor cursor){
114114
* @return {@link org.gnucash.android.model.Split} instance
115115
*/
116116
public Split getSplit(String uid){
117-
long id = getID(uid);
117+
return getSplit(getID(uid));
118+
}
119+
120+
/**
121+
* Returns the Split instance given the database id
122+
* @param id Database record ID of the split
123+
* @return {@link org.gnucash.android.model.Split} instance
124+
*/
125+
public Split getSplit(long id){
118126
Cursor cursor = fetchRecord(id);
119127

120128
Split split = null;
@@ -233,6 +241,9 @@ public Cursor fetchSplits(String condition, String sortOrder){
233241
* @return Database record ID of split
234242
*/
235243
public long getID(String uid){
244+
if (uid == null)
245+
return 0;
246+
236247
Cursor cursor = mDb.query(SplitEntry.TABLE_NAME,
237248
new String[] {SplitEntry._ID},
238249
SplitEntry.COLUMN_UID + " = ?", new String[]{uid}, null, null, null);
@@ -355,9 +366,13 @@ public Cursor fetchAllRecords() {
355366

356367
@Override
357368
public boolean deleteRecord(long rowId) {
358-
String transactionUID = getSplit(getUID(rowId)).getTransactionUID();
369+
Split split = getSplit(rowId);
370+
String transactionUID = split == null ? null : split.getTransactionUID();
359371
boolean result = deleteRecord(SplitEntry.TABLE_NAME, rowId);
360372

373+
if (!result) //we didn't delete for whatever reason, invalid rowId etc
374+
return false;
375+
361376
//if we just deleted the last split, then remove the transaction from db
362377
Cursor cursor = fetchSplitsForTransaction(transactionUID);
363378
if (cursor != null){
@@ -376,6 +391,9 @@ public boolean deleteRecord(long rowId) {
376391
*/
377392
public long getTransactionID(String transactionUID){
378393
long id = -1;
394+
if (transactionUID == null)
395+
return id;
396+
379397
Cursor c = mDb.query(TransactionEntry.TABLE_NAME,
380398
new String[]{TransactionEntry._ID},
381399
TransactionEntry.COLUMN_UID + "=?",

app/src/org/gnucash/android/export/ExporterAsyncTask.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.gnucash.android.export;
1818

19+
import android.app.Activity;
1920
import android.app.ProgressDialog;
2021
import android.content.Context;
2122
import android.content.Intent;
@@ -48,7 +49,7 @@ public class ExporterAsyncTask extends AsyncTask<ExportParams, Void, Boolean> {
4849
/**
4950
* App context
5051
*/
51-
private final Context mContext;
52+
private final Activity mContext;
5253

5354
private ProgressDialog mProgressDialog;
5455

@@ -62,7 +63,7 @@ public class ExporterAsyncTask extends AsyncTask<ExportParams, Void, Boolean> {
6263
*/
6364
private ExportParams mExportParams;
6465

65-
public ExporterAsyncTask(Context context){
66+
public ExporterAsyncTask(Activity context){
6667
this.mContext = context;
6768
}
6869

@@ -110,9 +111,15 @@ protected Boolean doInBackground(ExportParams... params) {
110111
} catch (Exception e) {
111112
e.printStackTrace();
112113
Log.e(TAG, e.getMessage());
113-
Toast.makeText(mContext, R.string.toast_export_error,
114-
Toast.LENGTH_SHORT).show();
115-
Toast.makeText(mContext, e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
114+
final String err_msg = e.getLocalizedMessage();
115+
mContext.runOnUiThread(new Runnable() {
116+
@Override
117+
public void run() {
118+
Toast.makeText(mContext, R.string.toast_export_error,
119+
Toast.LENGTH_SHORT).show();
120+
Toast.makeText(mContext, err_msg, Toast.LENGTH_LONG).show();
121+
}
122+
});
116123
return false;
117124
}
118125
return true;

app/src/org/gnucash/android/importer/ImportAsyncTask.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/package org.gnucash.android.importer;
1616

17+
import android.app.Activity;
1718
import android.app.ProgressDialog;
1819
import android.content.Context;
1920
import android.os.AsyncTask;
@@ -30,10 +31,10 @@
3031
* The AccountsActivity is opened when importing is done.
3132
*/
3233
public class ImportAsyncTask extends AsyncTask<InputStream, Void, Boolean> {
33-
private final Context context;
34+
private final Activity context;
3435
private ProgressDialog progressDialog;
3536

36-
public ImportAsyncTask(Context context){
37+
public ImportAsyncTask(Activity context){
3738
this.context = context;
3839
}
3940

@@ -57,10 +58,17 @@ protected Boolean doInBackground(InputStream... inputStreams) {
5758
GncXmlImporter.parse(context, inputStreams[0]);
5859
} catch (Exception exception){
5960
exception.printStackTrace();
61+
final String err_msg = exception.getLocalizedMessage();
6062
Log.e(ImportAsyncTask.class.getName(), exception.getMessage());
61-
Toast.makeText(context,
62-
context.getString(R.string.toast_error_importing_accounts) + "\n" + exception.getLocalizedMessage(),
63-
Toast.LENGTH_LONG).show();
63+
context.runOnUiThread(new Runnable() {
64+
@Override
65+
public void run() {
66+
Toast.makeText(context,
67+
context.getString(R.string.toast_error_importing_accounts) + "\n" + err_msg,
68+
Toast.LENGTH_LONG).show();
69+
}
70+
});
71+
6472
return false;
6573
}
6674
return true;

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

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import android.support.v4.app.FragmentManager;
2626
import android.widget.*;
2727
import org.gnucash.android.R;
28-
import org.gnucash.android.app.GnuCashApplication;
2928
import org.gnucash.android.db.*;
3029
import org.gnucash.android.model.*;
3130
import org.gnucash.android.ui.transaction.dialog.DatePickerDialogFragment;
@@ -252,13 +251,14 @@ public void onNothingSelected(AdapterView<?> adapterView) {
252251
});
253252

254253
setListeners();
255-
if (mTransaction == null)
256-
initalizeViews();
257-
else {
254+
if (mTransaction == null) {
255+
initalizeViews();
256+
initTransactionNameAutocomplete();
257+
} else {
258258
initializeViewsWithTransaction();
259259
}
260260

261-
initTransactionNameAutocomplete();
261+
262262
}
263263

264264
/**
@@ -302,7 +302,7 @@ public void onItemClick(AdapterView<?> adapterView, View view, int position, lon
302302
if (!amountEntered) //if user already entered an amount
303303
mAmountEditText.setText(splitList.get(0).getAmount().toPlainString());
304304
} else {
305-
if (amountEntered){ //if user entered own amount, clear
305+
if (amountEntered){ //if user entered own amount, clear loaded splits and use the user value
306306
mSplitsList.clear();
307307
setAmountEditViewVisible(View.VISIBLE);
308308
} else {
@@ -449,9 +449,8 @@ private void openSplitEditor(){
449449
}
450450
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
451451
String baseAmountString;
452-
long transactionId = getArguments().getLong(UxArgument.SELECTED_TRANSACTION_ID);
453452

454-
if (transactionId <= 0){
453+
if (mTransaction == null){ //if we are creating a new transaction (not editing an existing one)
455454
BigDecimal enteredAmount = parseInputToDecimal(mAmountEditText.getText().toString());
456455
baseAmountString = enteredAmount.toPlainString();
457456
} else {
@@ -578,19 +577,28 @@ private void saveNewTransaction() {
578577
Currency currency = Currency.getInstance(mTransactionsDbAdapter.getCurrencyCode(accountID));
579578
Money amount = new Money(amountBigd, currency).absolute();
580579

581-
if (mTransaction != null){
582-
if (mSplitsList.size() == 2) {
583-
//if it is a simple transfer where the editor was not used, then respect the button
584-
for (Split split : mSplitsList) {
585-
if (split.getAccountUID().equals(accountUID)){
586-
split.setType(mTransactionTypeButton.getTransactionType());
587-
split.setAmount(amount.absolute());
588-
} else {
589-
split.setType(mTransactionTypeButton.getTransactionType().invert());
590-
}
580+
//capture any edits which were done directly (not using split editor)
581+
if (mSplitsList.size() == 2 && mSplitsList.get(0).isPairOf(mSplitsList.get(1))) {
582+
//if it is a simple transfer where the editor was not used, then respect the button
583+
for (Split split : mSplitsList) {
584+
if (split.getAccountUID().equals(accountUID)){
585+
split.setType(mTransactionTypeButton.getTransactionType());
586+
split.setAmount(amount);
587+
} else {
588+
split.setType(mTransactionTypeButton.getTransactionType().invert());
589+
split.setAmount(amount);
591590
}
592591
}
593-
mTransaction.setSplits(mSplitsList);
592+
}
593+
594+
if (mTransaction != null){
595+
if (!mUseDoubleEntry){
596+
Split split = new Split(amount, accountUID);
597+
split.setType(mTransactionTypeButton.getTransactionType());
598+
mTransaction.getSplits().clear();
599+
mTransaction.addSplit(split);
600+
} else
601+
mTransaction.setSplits(mSplitsList);
594602
mTransaction.setDescription(description);
595603
} else {
596604
mTransaction = new Transaction(description);

app/src/org/gnucash/android/ui/widget/WidgetConfigurationActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ public static void updateWidget(Context context, int appWidgetId, long accountId
201201
views.setOnClickPendingIntent(R.id.btn_new_transaction, pendingIntent);
202202

203203
appWidgetManager.updateAppWidget(appWidgetId, views);
204+
accountsDbAdapter.close();
204205
}
205206

206207
/**

0 commit comments

Comments
 (0)