Skip to content

Commit

Permalink
configure reset_account menu item from fragment, thus getting rid of …
Browse files Browse the repository at this point in the history
…one expensive db call
  • Loading branch information
mtotschnig committed Jul 4, 2013
1 parent b37e33d commit 9b945b9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 0 additions & 2 deletions src/org/totschnig/myexpenses/activity/MyExpenses.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,6 @@ public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
//I would prefer to use setEnabled, but the disabled state unfortunately is not visually reflected
//in the actionbar
menu.findItem(R.id.RESET_ACCOUNT_COMMAND)
.setVisible(mCurrentAccount.getSize() > 0);
menu.findItem(R.id.INSERT_TRANSFER_COMMAND)
.setVisible(Account.countPerCurrency(mCurrentAccount.currency) > 1);
menu.findItem(R.id.NEW_FROM_TEMPLATE_COMMAND)
Expand Down
14 changes: 14 additions & 0 deletions src/org/totschnig/myexpenses/fragment/TransactionList.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;

import android.content.ContentResolver;
import android.content.Intent;
Expand Down Expand Up @@ -49,6 +50,7 @@ public class TransactionList extends SherlockFragment implements LoaderManager.L
private Account mAccount;
private TextView balanceTv;
private View bottomLine;
private boolean hasItems;

public static TransactionList newInstance(long accountId) {

Expand All @@ -61,6 +63,7 @@ public static TransactionList newInstance(long accountId) {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
accountId = getArguments().getLong("account_id");
mAccount = Account.getInstanceFromDb(getArguments().getLong("account_id"));
tObserver = new TransactionsObserver(new Handler());
Expand All @@ -87,6 +90,11 @@ public void onDestroy() {
// Do Nothing. Observer has already been unregistered.
}
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
if (isVisible())
menu.findItem(R.id.RESET_ACCOUNT_COMMAND).setVisible(hasItems);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Expand Down Expand Up @@ -214,11 +222,17 @@ public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor c) {
mAdapter.swapCursor(c);
hasItems = c.getCount()>0;
if (isVisible())
getSherlockActivity().supportInvalidateOptionsMenu();
}

@Override
public void onLoaderReset(Loader<Cursor> arg0) {
mAdapter.swapCursor(null);
hasItems = false;
if (isVisible())
getSherlockActivity().supportInvalidateOptionsMenu();
}
class TransactionsObserver extends ContentObserver {
public TransactionsObserver(Handler handler) {
Expand Down

0 comments on commit 9b945b9

Please sign in to comment.