Skip to content

Commit

Permalink
Menu provider.
Browse files Browse the repository at this point in the history
  • Loading branch information
pnemonic78 committed Jan 2, 2025
1 parent c93d6ef commit f78ec11
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 73 deletions.
28 changes: 28 additions & 0 deletions app/src/main/java/org/gnucash/android/app/MenuFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.gnucash.android.app

import android.os.Bundle
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import androidx.activity.ComponentActivity
import androidx.core.view.MenuProvider
import androidx.fragment.app.Fragment

open class MenuFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val activity = requireActivity()
if (activity is ComponentActivity) {
activity.addMenuProvider(object : MenuProvider {
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
menu.clear()
onCreateOptionsMenu(menu, menuInflater)
}

override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
return onOptionsItemSelected(menuItem)
}
})
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.database.Cursor;
Expand Down Expand Up @@ -51,14 +50,13 @@
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.cursoradapter.widget.SimpleCursorAdapter;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentResultListener;

import org.gnucash.android.R;
import org.gnucash.android.app.GnuCashApplication;
import org.gnucash.android.app.MenuFragment;
import org.gnucash.android.databinding.FragmentAccountFormBinding;
import org.gnucash.android.app.GnuCashApplication;
import org.gnucash.android.db.DatabaseSchema;
import org.gnucash.android.db.adapter.AccountsDbAdapter;
import org.gnucash.android.db.adapter.CommoditiesDbAdapter;
Expand All @@ -68,7 +66,6 @@
import org.gnucash.android.model.Commodity;
import org.gnucash.android.ui.colorpicker.ColorPickerDialog;
import org.gnucash.android.ui.common.UxArgument;
import org.gnucash.android.ui.settings.PreferenceActivity;
import org.gnucash.android.util.CommoditiesCursorAdapter;
import org.gnucash.android.util.QualifiedAccountNameCursorAdapter;

Expand All @@ -86,7 +83,7 @@
* @author Ngewi Fet <[email protected]>
* @author Yongxin Wang <[email protected]>
*/
public class AccountFormFragment extends Fragment implements FragmentResultListener {
public class AccountFormFragment extends MenuFragment implements FragmentResultListener {
/**
* Accounts database adapter
*/
Expand Down Expand Up @@ -173,7 +170,6 @@ static public AccountFormFragment newInstance() {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mAccountsDbAdapter = AccountsDbAdapter.getInstance();
mUseDoubleEntry = GnuCashApplication.isDoubleEntryEnabled();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.PopupMenu;
import androidx.appcompat.widget.SearchView;
import androidx.core.view.MenuItemCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentResultListener;
import androidx.lifecycle.Lifecycle;
Expand All @@ -57,7 +55,7 @@
import androidx.recyclerview.widget.RecyclerView;

import org.gnucash.android.R;
import org.gnucash.android.app.GnuCashApplication;
import org.gnucash.android.app.MenuFragment;
import org.gnucash.android.databinding.CardviewAccountBinding;
import org.gnucash.android.databinding.FragmentAccountsListBinding;
import org.gnucash.android.db.DatabaseCursorLoader;
Expand All @@ -83,7 +81,7 @@
*
* @author Ngewi Fet <[email protected]>
*/
public class AccountsListFragment extends Fragment implements
public class AccountsListFragment extends MenuFragment implements
Refreshable,
LoaderManager.LoaderCallbacks<Cursor>,
SearchView.OnQueryTextListener,
Expand Down Expand Up @@ -151,6 +149,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,

mBinding.list.setHasFixedSize(true);
mBinding.list.setEmptyView(mBinding.emptyView);
mBinding.list.setAdapter(mAccountRecyclerAdapter);

switch (mDisplayMode) {

Expand Down Expand Up @@ -190,20 +189,15 @@ public void onCreate(Bundle savedInstanceState) {
mDisplayMode = (DisplayMode) savedInstanceState.getSerializable(STATE_DISPLAY_MODE);
}
}
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

ActionBar actionbar = ((AppCompatActivity) getActivity()).getSupportActionBar();
AppCompatActivity activity = (AppCompatActivity) requireActivity();
ActionBar actionbar = activity.getSupportActionBar();
assert actionbar != null;
actionbar.setTitle(R.string.title_accounts);
actionbar.setDisplayHomeAsUpEnabled(true);
setHasOptionsMenu(true);

// specify an adapter (see also next example)
mAccountRecyclerAdapter = new AccountRecyclerAdapter(null);
mBinding.list.setAdapter(mAccountRecyclerAdapter);
}

@Override
Expand Down Expand Up @@ -281,21 +275,20 @@ private void showConfirmationDialog(String accountUID) {
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
if (mParentAccountUID != null)
if (!TextUtils.isEmpty(mParentAccountUID))
inflater.inflate(R.menu.sub_account_actions, menu);
else {
inflater.inflate(R.menu.account_actions, menu);
// Associate searchable configuration with the SearchView

SearchManager searchManager =
(SearchManager) GnuCashApplication.getAppContext().getSystemService(Context.SEARCH_SERVICE);
mSearchView = (SearchView)
MenuItemCompat.getActionView(menu.findItem(R.id.menu_search));
mSearchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
if (mSearchView == null)
return;

Activity context = requireActivity();
SearchManager searchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
mSearchView.setSearchableInfo(
searchManager.getSearchableInfo(getActivity().getComponentName()));
searchManager.getSearchableInfo(context.getComponentName()));
mSearchView.setOnQueryTextListener(this);
mSearchView.setOnCloseListener(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import org.gnucash.android.R;
import org.gnucash.android.app.MenuFragment;
import org.gnucash.android.databinding.FragmentBudgetAmountEditorBinding;
import org.gnucash.android.databinding.ItemBudgetAmountBinding;
import org.gnucash.android.db.DatabaseSchema;
Expand All @@ -60,7 +60,7 @@
/**
* Fragment for editing budgeting amounts
*/
public class BudgetAmountEditorFragment extends Fragment {
public class BudgetAmountEditorFragment extends MenuFragment {

private Cursor mAccountCursor;
private QualifiedAccountNameCursorAdapter mAccountCursorAdapter;
Expand Down Expand Up @@ -98,7 +98,6 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
assert actionBar != null;
actionBar.setTitle("Edit Budget Amounts");
setHasOptionsMenu(true);

ArrayList<BudgetAmount> budgetAmounts = getArguments().getParcelableArrayList(UxArgument.BUDGET_AMOUNT_LIST);
if (budgetAmounts != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import com.github.mikephil.charting.data.BarEntry;

import org.gnucash.android.R;
import org.gnucash.android.app.MenuFragment;
import org.gnucash.android.databinding.CardviewBudgetAmountBinding;
import org.gnucash.android.databinding.FragmentBudgetDetailBinding;
import org.gnucash.android.db.DatabaseSchema;
Expand All @@ -69,7 +70,7 @@
/**
* Fragment for displaying budget details
*/
public class BudgetDetailFragment extends Fragment implements Refreshable {
public class BudgetDetailFragment extends MenuFragment implements Refreshable {
private String mBudgetUID;
private BudgetsDbAdapter mBudgetsDbAdapter;

Expand Down Expand Up @@ -110,8 +111,6 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
mBudgetsDbAdapter = BudgetsDbAdapter.getInstance();
mBudgetUID = getArguments().getString(UxArgument.BUDGET_UID);
bindViews();

setHasOptionsMenu(true);
}

private void bindViews() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.codetroopers.betterpickers.recurrencepicker.EventRecurrence;
import com.codetroopers.betterpickers.recurrencepicker.EventRecurrenceFormatter;
import com.codetroopers.betterpickers.recurrencepicker.RecurrencePickerDialogFragment;

import org.gnucash.android.R;
import org.gnucash.android.app.MenuFragment;
import org.gnucash.android.databinding.FragmentBudgetFormBinding;
import org.gnucash.android.db.DatabaseSchema;
import org.gnucash.android.db.adapter.AccountsDbAdapter;
Expand Down Expand Up @@ -73,7 +73,7 @@
/**
* Fragment for creating or editing Budgets
*/
public class BudgetFormFragment extends Fragment implements RecurrencePickerDialogFragment.OnRecurrenceSetListener, DatePickerDialog.OnDateSetListener {
public class BudgetFormFragment extends MenuFragment implements RecurrencePickerDialogFragment.OnRecurrenceSetListener, DatePickerDialog.OnDateSetListener {

public static final int REQUEST_EDIT_BUDGET_AMOUNTS = 0xBA;

Expand Down Expand Up @@ -118,7 +118,6 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setHasOptionsMenu(true);

mBinding.budgetAmountLayout.btnRemoveItem.setVisibility(View.GONE);
mBinding.budgetAmountLayout.inputBudgetAmount.bindKeyboard(mBinding.calculatorKeyboard);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,14 @@
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.codetroopers.betterpickers.recurrencepicker.EventRecurrence;
import com.codetroopers.betterpickers.recurrencepicker.EventRecurrenceFormatter;
import com.codetroopers.betterpickers.recurrencepicker.RecurrencePickerDialogFragment;

import org.gnucash.android.R;
import org.gnucash.android.app.GnuCashApplication;
import org.gnucash.android.app.MenuFragment;
import org.gnucash.android.databinding.FragmentExportFormBinding;
import org.gnucash.android.db.adapter.BooksDbAdapter;
import org.gnucash.android.db.adapter.DatabaseAdapter;
Expand Down Expand Up @@ -94,7 +93,7 @@
*
* @author Ngewi Fet <[email protected]>
*/
public class ExportFormFragment extends Fragment implements
public class ExportFormFragment extends MenuFragment implements
RecurrencePickerDialogFragment.OnRecurrenceSetListener,
DatePickerDialog.OnDateSetListener,
TimePickerDialog.OnTimeSetListener {
Expand Down Expand Up @@ -321,10 +320,9 @@ public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

AppCompatActivity activity = (AppCompatActivity) requireActivity();
ActionBar supportActionBar = activity.getSupportActionBar();
assert supportActionBar != null;
supportActionBar.setTitle(R.string.title_export_dialog);
setHasOptionsMenu(true);
ActionBar actionBar = activity.getSupportActionBar();
assert actionBar != null;
actionBar.setTitle(R.string.title_export_dialog);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import androidx.annotation.StringRes;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.github.mikephil.charting.data.ChartData;
import com.github.mikephil.charting.data.Entry;
Expand All @@ -46,6 +45,7 @@
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;

import org.gnucash.android.R;
import org.gnucash.android.app.MenuFragment;
import org.gnucash.android.db.adapter.AccountsDbAdapter;
import org.gnucash.android.model.AccountType;
import org.gnucash.android.model.Commodity;
Expand All @@ -69,7 +69,7 @@
*
* @author Ngewi Fet <[email protected]>
*/
public abstract class BaseReportFragment extends Fragment implements
public abstract class BaseReportFragment extends MenuFragment implements
OnChartValueSelectedListener, ReportOptionsListener, Refreshable {

/**
Expand Down Expand Up @@ -201,8 +201,6 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
assert actionBar != null;
actionBar.setTitle(getTitle());

setHasOptionsMenu(true);

ReportsActivity reportsActivity = (ReportsActivity) getActivity();
mReportPeriodStart = reportsActivity.getReportPeriodStart();
mReportPeriodEnd = reportsActivity.getReportPeriodEnd();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.PopupMenu;
import androidx.fragment.app.Fragment;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.Loader;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.gnucash.android.R;
import org.gnucash.android.app.ActivityExtKt;
import org.gnucash.android.app.MenuFragment;
import org.gnucash.android.databinding.FragmentScheduledEventsListBinding;
import org.gnucash.android.databinding.ListItemScheduledTrxnBinding;
import org.gnucash.android.db.adapter.ScheduledActionDbAdapter;
Expand All @@ -60,7 +60,7 @@
*
* @author Ngewi Fet <[email protected]>
*/
public abstract class ScheduledActionsListFragment extends Fragment implements
public abstract class ScheduledActionsListFragment extends MenuFragment implements
Refreshable,
LoaderManager.LoaderCallbacks<Cursor> {

Expand Down Expand Up @@ -94,7 +94,6 @@ public void onActivityCreated(Bundle savedInstanceState) {
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
setHasOptionsMenu(true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cursoradapter.widget.SimpleCursorAdapter;
import androidx.fragment.app.Fragment;

import com.google.android.material.snackbar.Snackbar;

import org.gnucash.android.R;
import org.gnucash.android.app.MenuFragment;
import org.gnucash.android.databinding.FragmentSplitEditorBinding;
import org.gnucash.android.databinding.ItemSplitEntryBinding;
import org.gnucash.android.db.DatabaseSchema;
Expand Down Expand Up @@ -80,7 +80,7 @@
*
* @author Ngewi Fet <[email protected]>
*/
public class SplitEditorFragment extends Fragment {
public class SplitEditorFragment extends MenuFragment {
private AccountsDbAdapter mAccountsDbAdapter;
private Cursor mCursor;
private SimpleCursorAdapter mCursorAdapter;
Expand Down Expand Up @@ -136,7 +136,6 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
ActionBar actionBar = ((AppCompatActivity) requireActivity()).getSupportActionBar();
assert actionBar != null;
actionBar.setTitle(R.string.title_split_editor);
setHasOptionsMenu(true);

mSplitItemViewList = new ArrayList<>();

Expand Down
Loading

0 comments on commit f78ec11

Please sign in to comment.