Skip to content

Commit 7c0e778

Browse files
committed
Merge branch 'hotfix/lazy_loading' into develop
2 parents ac231c6 + 847e1c8 commit 7c0e778

5 files changed

Lines changed: 116 additions & 35 deletions

File tree

app/src/org/gnucash/android/data/Money.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,21 @@ public final class Money implements Comparable<Money>{
8080
* otherwise US dollars are used
8181
*/
8282
public static String DEFAULT_CURRENCY_CODE = "USD";
83-
83+
84+
/**
85+
* A zero instance with the currency of the default locale.
86+
* This can be used anywhere where a starting amount is required without having to create a new object
87+
*/
88+
public static final Money sDefaultZero = Money.createInstance(Currency.getInstance(Locale.getDefault()).getCurrencyCode());
89+
90+
/**
91+
* Returns a Money instance initialized to the local currency and value 0
92+
* @return Money instance of value 0 in locale currency
93+
*/
94+
public static Money getZeroInstance(){
95+
return sDefaultZero;
96+
}
97+
8498
/**
8599
* Default constructor
86100
* Initializes the object with an amount of 0 and currency set to the device default locale
@@ -229,7 +243,15 @@ public String formattedString(Locale locale){
229243
formatter.setMaximumFractionDigits(DECIMAL_PLACES);
230244
return formatter.format(asDouble()) + " " + mCurrency.getSymbol(locale);
231245
}
232-
246+
247+
/**
248+
* Equivalent to calling formattedString(Locale.getDefault())
249+
* @return String formatted Money representation in default locale
250+
*/
251+
public String formattedString(){
252+
return formattedString(Locale.getDefault());
253+
}
254+
233255
/**
234256
* Returns a new Money object whose amount is the negated value of this object amount.
235257
* The original <code>Money</code> object remains unchanged.

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,15 @@ public String getGnuCashRootAccountUID(){
453453
* @return Number of sub accounts
454454
*/
455455
public int getSubAccountCount(long accountId){
456-
return getSubAccountIds(accountId).size();
456+
//TODO: at some point when API level 11 and above only is supported, use DatabaseUtils.queryNumEntries
457+
458+
String queryCount = "SELECT COUNT(*) FROM " + DatabaseHelper.ACCOUNTS_TABLE_NAME + " WHERE "
459+
+ DatabaseHelper.KEY_PARENT_ACCOUNT_UID + " = ?";
460+
Cursor cursor = mDb.rawQuery(queryCount, new String[]{getAccountUID(accountId)});
461+
cursor.moveToFirst();
462+
int count = cursor.getInt(0);
463+
cursor.close();
464+
return count;
457465
}
458466

459467
/**
@@ -524,5 +532,4 @@ public int deleteAllRecords(){
524532
return mDb.delete(DatabaseHelper.ACCOUNTS_TABLE_NAME, null, null);
525533
}
526534

527-
528535
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,23 @@ public void close(){
101101
mDbHelper.close();
102102
mDb.close();
103103
}
104-
104+
105+
/**
106+
* Checks if the database is open
107+
* @return <code>true</code> if the database is open, <code>false</code> otherwise
108+
*/
109+
public boolean isOpen(){
110+
return mDb.isOpen();
111+
}
112+
113+
/**
114+
* Returns the context used to create this adapter
115+
* @return Android application context
116+
*/
117+
public Context getContext(){
118+
return mContext.getApplicationContext();
119+
}
120+
105121
/**
106122
* Retrieves record with id <code>rowId</code> from table <code>tableName</code>
107123
* @param tableName Name of table where record is found

app/src/org/gnucash/android/ui/accounts/AccountsListFragment.java

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import android.content.DialogInterface;
2424
import android.content.Intent;
2525
import android.database.Cursor;
26+
import android.os.AsyncTask;
2627
import android.os.Bundle;
2728
import android.support.v4.app.DialogFragment;
2829
import android.support.v4.app.Fragment;
@@ -55,6 +56,7 @@
5556
import org.gnucash.android.ui.widget.WidgetConfigurationActivity;
5657
import org.gnucash.android.util.OnAccountClickedListener;
5758

59+
import java.lang.ref.WeakReference;
5860
import java.util.Locale;
5961

6062
/**
@@ -589,9 +591,6 @@ public void bindView(View v, Context context, Cursor cursor) {
589591
// perform the default binding
590592
super.bindView(v, context, cursor);
591593

592-
// add a summary of transactions to the account view
593-
TextView summary = (TextView) v
594-
.findViewById(R.id.transactions_summary);
595594
final long accountId = cursor.getLong(DatabaseAdapter.COLUMN_ROW_ID);
596595

597596
TextView subAccountTextView = (TextView) v.findViewById(R.id.secondary_text);
@@ -603,11 +602,10 @@ public void bindView(View v, Context context, Cursor cursor) {
603602
} else
604603
subAccountTextView.setVisibility(View.GONE);
605604

606-
Money balance = mAccountsDbAdapter.getAccountBalance(accountId);
607-
summary.setText(balance.formattedString(Locale.getDefault()));
608-
int fontColor = balance.isNegative() ? getResources().getColor(R.color.debit_red) :
609-
getResources().getColor(R.color.credit_green);
610-
summary.setTextColor(fontColor);
605+
// add a summary of transactions to the account view
606+
TextView summary = (TextView) v
607+
.findViewById(R.id.transactions_summary);
608+
new AccountBalanceTask(summary, getActivity()).execute(accountId);
611609

612610
ImageView newTransactionButton = (ImageView) v.findViewById(R.id.btn_new_transaction);
613611
if (inSubAcccount()){
@@ -628,4 +626,46 @@ public void onClick(View v) {
628626
}
629627
}
630628

629+
/**
630+
* An asynchronous task for computing the account balance of an account.
631+
* This is done asynchronously because in cases of deeply nested accounts,
632+
* it can take some time and would block the UI thread otherwise.
633+
*/
634+
public static class AccountBalanceTask extends AsyncTask<Long, Void, Money> {
635+
private final WeakReference<TextView> accountBalanceTextViewReference;
636+
private final AccountsDbAdapter accountsDbAdapter;
637+
638+
public AccountBalanceTask(TextView balanceTextView, Context context){
639+
accountBalanceTextViewReference = new WeakReference<TextView>(balanceTextView);
640+
accountsDbAdapter = new AccountsDbAdapter(context);
641+
}
642+
643+
@Override
644+
protected Money doInBackground(Long... params) {
645+
//if the view for which we are doing this job is dead, kill the job as well
646+
if (accountBalanceTextViewReference == null || accountBalanceTextViewReference.get() == null){
647+
cancel(true);
648+
accountsDbAdapter.close();
649+
return Money.getZeroInstance();
650+
}
651+
Money balance = accountsDbAdapter.getAccountBalance(params[0]);
652+
accountsDbAdapter.close();
653+
return balance;
654+
}
655+
656+
@Override
657+
protected void onPostExecute(Money balance) {
658+
if (accountBalanceTextViewReference != null && balance != null){
659+
final Context context = accountsDbAdapter.getContext();
660+
final TextView balanceTextView = accountBalanceTextViewReference.get();
661+
if (balanceTextView != null){
662+
balanceTextView.setText(balance.formattedString());
663+
int fontColor = balance.isNegative() ? context.getResources().getColor(R.color.debit_red) :
664+
context.getResources().getColor(R.color.credit_green);
665+
balanceTextView.setTextColor(fontColor);
666+
}
667+
}
668+
}
669+
}
670+
631671
}

app/src/org/gnucash/android/ui/transactions/TransactionsListFragment.java

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,6 @@
1616

1717
package org.gnucash.android.ui.transactions;
1818

19-
import java.text.SimpleDateFormat;
20-
import java.util.Date;
21-
import java.util.HashMap;
22-
import java.util.Locale;
23-
24-
import org.gnucash.android.R;
25-
import org.gnucash.android.data.Money;
26-
import org.gnucash.android.db.*;
27-
import org.gnucash.android.ui.widget.WidgetConfigurationActivity;
28-
import org.gnucash.android.util.OnTransactionClickedListener;
29-
3019
import android.app.Activity;
3120
import android.content.Context;
3221
import android.database.Cursor;
@@ -48,13 +37,26 @@
4837
import android.widget.CompoundButton.OnCheckedChangeListener;
4938
import android.widget.ListView;
5039
import android.widget.TextView;
51-
5240
import com.actionbarsherlock.app.ActionBar;
5341
import com.actionbarsherlock.app.SherlockListFragment;
5442
import com.actionbarsherlock.view.ActionMode;
5543
import com.actionbarsherlock.view.Menu;
5644
import com.actionbarsherlock.view.MenuInflater;
5745
import com.actionbarsherlock.view.MenuItem;
46+
import org.gnucash.android.R;
47+
import org.gnucash.android.data.Money;
48+
import org.gnucash.android.db.DatabaseAdapter;
49+
import org.gnucash.android.db.DatabaseCursorLoader;
50+
import org.gnucash.android.db.DatabaseHelper;
51+
import org.gnucash.android.db.TransactionsDbAdapter;
52+
import org.gnucash.android.ui.accounts.AccountsListFragment;
53+
import org.gnucash.android.ui.widget.WidgetConfigurationActivity;
54+
import org.gnucash.android.util.OnTransactionClickedListener;
55+
56+
import java.text.SimpleDateFormat;
57+
import java.util.Date;
58+
import java.util.HashMap;
59+
import java.util.Locale;
5860

5961
/**
6062
* List Fragment for displaying list of transactions for an account
@@ -213,15 +215,9 @@ public void refreshList(long accountId){
213215
public void refreshList(){
214216
getLoaderManager().restartLoader(0, null, this);
215217

216-
AccountsDbAdapter accountsDbAdapter = new AccountsDbAdapter(getActivity());
217-
Money sum = accountsDbAdapter.getAccountBalance(mAccountID);// mTransactionsDbAdapter.getTransactionsSum(mAccountID);
218-
accountsDbAdapter.close();
219-
mSumTextView = (TextView) getView().findViewById(R.id.transactions_sum);
220-
mSumTextView.setText(sum.formattedString(Locale.getDefault()));
221-
if (sum.isNegative())
222-
mSumTextView.setTextColor(getResources().getColor(R.color.debit_red));
223-
else
224-
mSumTextView.setTextColor(getResources().getColor(R.color.credit_green));
218+
mSumTextView = (TextView) getView().findViewById(R.id.transactions_sum);
219+
new AccountsListFragment.AccountBalanceTask(mSumTextView, getActivity()).execute(mAccountID);
220+
225221
}
226222

227223
@Override
@@ -481,7 +477,7 @@ public void bindView(View view, Context context, Cursor cursor) {
481477
long transactionTime = cursor.getLong(DatabaseAdapter.COLUMN_TIMESTAMP);
482478
int position = cursor.getPosition();
483479

484-
boolean hasSectionHeader = false;
480+
boolean hasSectionHeader;
485481
if (position == 0){
486482
hasSectionHeader = true;
487483
} else {

0 commit comments

Comments
 (0)