Permalink
Browse files

configure reset_account menu item from fragment, thus getting rid of …

…one expensive db call
  • Loading branch information...
mtotschnig committed Jul 4, 2013
1 parent b37e33d commit 9b945b98dfc51ebc3ec37b9b3eef5785ab80950e
@@ -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)
@@ -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;
@@ -49,6 +50,7 @@
private Account mAccount;
private TextView balanceTv;
private View bottomLine;
private boolean hasItems;
public static TransactionList newInstance(long accountId) {
@@ -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());
@@ -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) {
@@ -214,11 +222,17 @@ public void onItemClick(AdapterView<?> a, View v,int position, long id)
@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) {

0 comments on commit 9b945b9

Please sign in to comment.