diff --git a/app/src/main/java/org/gnucash/android/app/MenuFragment.kt b/app/src/main/java/org/gnucash/android/app/MenuFragment.kt new file mode 100644 index 000000000..9a05ee09c --- /dev/null +++ b/app/src/main/java/org/gnucash/android/app/MenuFragment.kt @@ -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) + } + }) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/org/gnucash/android/ui/account/AccountFormFragment.java b/app/src/main/java/org/gnucash/android/ui/account/AccountFormFragment.java index 1d05718a2..510c4755b 100644 --- a/app/src/main/java/org/gnucash/android/ui/account/AccountFormFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/account/AccountFormFragment.java @@ -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; @@ -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; @@ -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; @@ -86,7 +83,7 @@ * @author Ngewi Fet * @author Yongxin Wang */ -public class AccountFormFragment extends Fragment implements FragmentResultListener { +public class AccountFormFragment extends MenuFragment implements FragmentResultListener { /** * Accounts database adapter */ @@ -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(); } diff --git a/app/src/main/java/org/gnucash/android/ui/account/AccountsListFragment.java b/app/src/main/java/org/gnucash/android/ui/account/AccountsListFragment.java index 8f0ad4f12..2cdc4105a 100644 --- a/app/src/main/java/org/gnucash/android/ui/account/AccountsListFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/account/AccountsListFragment.java @@ -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; @@ -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; @@ -83,7 +81,7 @@ * * @author Ngewi Fet */ -public class AccountsListFragment extends Fragment implements +public class AccountsListFragment extends MenuFragment implements Refreshable, LoaderManager.LoaderCallbacks, SearchView.OnQueryTextListener, @@ -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) { @@ -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 @@ -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); } diff --git a/app/src/main/java/org/gnucash/android/ui/budget/BudgetAmountEditorFragment.java b/app/src/main/java/org/gnucash/android/ui/budget/BudgetAmountEditorFragment.java index ed59f6273..d592dfe11 100644 --- a/app/src/main/java/org/gnucash/android/ui/budget/BudgetAmountEditorFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/budget/BudgetAmountEditorFragment.java @@ -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; @@ -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; @@ -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 budgetAmounts = getArguments().getParcelableArrayList(UxArgument.BUDGET_AMOUNT_LIST); if (budgetAmounts != null) { diff --git a/app/src/main/java/org/gnucash/android/ui/budget/BudgetDetailFragment.java b/app/src/main/java/org/gnucash/android/ui/budget/BudgetDetailFragment.java index 7e432f5b3..e65b860c7 100644 --- a/app/src/main/java/org/gnucash/android/ui/budget/BudgetDetailFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/budget/BudgetDetailFragment.java @@ -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; @@ -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; @@ -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() { diff --git a/app/src/main/java/org/gnucash/android/ui/budget/BudgetFormFragment.java b/app/src/main/java/org/gnucash/android/ui/budget/BudgetFormFragment.java index d75910f3a..b23d85013 100644 --- a/app/src/main/java/org/gnucash/android/ui/budget/BudgetFormFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/budget/BudgetFormFragment.java @@ -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; @@ -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; @@ -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); diff --git a/app/src/main/java/org/gnucash/android/ui/export/ExportFormFragment.java b/app/src/main/java/org/gnucash/android/ui/export/ExportFormFragment.java index d1e4ce500..6ee8e880a 100644 --- a/app/src/main/java/org/gnucash/android/ui/export/ExportFormFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/export/ExportFormFragment.java @@ -49,8 +49,6 @@ 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; @@ -58,6 +56,7 @@ 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; @@ -94,7 +93,7 @@ * * @author Ngewi Fet */ -public class ExportFormFragment extends Fragment implements +public class ExportFormFragment extends MenuFragment implements RecurrencePickerDialogFragment.OnRecurrenceSetListener, DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener { @@ -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 diff --git a/app/src/main/java/org/gnucash/android/ui/report/BaseReportFragment.java b/app/src/main/java/org/gnucash/android/ui/report/BaseReportFragment.java index 124dc89a4..1300b9b58 100644 --- a/app/src/main/java/org/gnucash/android/ui/report/BaseReportFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/report/BaseReportFragment.java @@ -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; @@ -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; @@ -69,7 +69,7 @@ * * @author Ngewi Fet */ -public abstract class BaseReportFragment extends Fragment implements +public abstract class BaseReportFragment extends MenuFragment implements OnChartValueSelectedListener, ReportOptionsListener, Refreshable { /** @@ -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(); diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/ScheduledActionsListFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/ScheduledActionsListFragment.java index 5ced4a331..6fd92e091 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/ScheduledActionsListFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/ScheduledActionsListFragment.java @@ -33,7 +33,6 @@ 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; @@ -41,6 +40,7 @@ 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; @@ -60,7 +60,7 @@ * * @author Ngewi Fet */ -public abstract class ScheduledActionsListFragment extends Fragment implements +public abstract class ScheduledActionsListFragment extends MenuFragment implements Refreshable, LoaderManager.LoaderCallbacks { @@ -94,7 +94,6 @@ public void onActivityCreated(Bundle savedInstanceState) { actionBar.setDisplayShowTitleEnabled(true); actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setHomeButtonEnabled(true); - setHasOptionsMenu(true); } @Override diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java index d8e301046..713035347 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/SplitEditorFragment.java @@ -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; @@ -80,7 +80,7 @@ * * @author Ngewi Fet */ -public class SplitEditorFragment extends Fragment { +public class SplitEditorFragment extends MenuFragment { private AccountsDbAdapter mAccountsDbAdapter; private Cursor mCursor; private SimpleCursorAdapter mCursorAdapter; @@ -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<>(); diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java index 563f586f6..c941fa29a 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionFormFragment.java @@ -48,7 +48,6 @@ import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; import androidx.cursoradapter.widget.SimpleCursorAdapter; -import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentActivity; import com.codetroopers.betterpickers.recurrencepicker.EventRecurrence; @@ -58,6 +57,7 @@ import org.gnucash.android.R; import org.gnucash.android.app.GnuCashApplication; +import org.gnucash.android.app.MenuFragment; import org.gnucash.android.databinding.FragmentTransactionFormBinding; import org.gnucash.android.db.DatabaseSchema; import org.gnucash.android.db.adapter.AccountsDbAdapter; @@ -101,7 +101,7 @@ * * @author Ngewi Fet */ -public class TransactionFormFragment extends Fragment implements +public class TransactionFormFragment extends MenuFragment implements DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener, RecurrencePickerDialogFragment.OnRecurrenceSetListener, OnTransferFundsListener { @@ -300,7 +300,6 @@ public void onCreate(@Nullable Bundle savedInstanceState) { @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - setHasOptionsMenu(true); ActionBar actionBar = ((AppCompatActivity) requireActivity()).getSupportActionBar(); assert actionBar != null; diff --git a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsListFragment.java b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsListFragment.java index 9175aa4e2..575454173 100644 --- a/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsListFragment.java +++ b/app/src/main/java/org/gnucash/android/ui/transaction/TransactionsListFragment.java @@ -40,7 +40,6 @@ import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.PopupMenu; -import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentResultListener; import androidx.loader.app.LoaderManager; @@ -51,6 +50,7 @@ import org.gnucash.android.R; import org.gnucash.android.app.GnuCashApplication; +import org.gnucash.android.app.MenuFragment; import org.gnucash.android.databinding.CardviewCompactTransactionBinding; import org.gnucash.android.databinding.CardviewTransactionBinding; import org.gnucash.android.databinding.FragmentTransactionsListBinding; @@ -81,7 +81,7 @@ * * @author Ngewi Fet */ -public class TransactionsListFragment extends Fragment implements +public class TransactionsListFragment extends MenuFragment implements Refreshable, LoaderManager.LoaderCallbacks, FragmentResultListener { private TransactionsDbAdapter mTransactionsDbAdapter; @@ -96,7 +96,6 @@ public class TransactionsListFragment extends Fragment implements @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setHasOptionsMenu(true); Bundle args = getArguments(); mAccountUID = args.getString(UxArgument.SELECTED_ACCOUNT_UID); @@ -144,8 +143,6 @@ public void onActivityCreated(Bundle savedInstanceState) { mTransactionRecyclerAdapter = new TransactionRecyclerAdapter(null); mBinding.list.setAdapter(mTransactionRecyclerAdapter); - - setHasOptionsMenu(true); } /** diff --git a/app/src/main/res/menu/account_actions.xml b/app/src/main/res/menu/account_actions.xml index fef4d9e41..8ddbce722 100644 --- a/app/src/main/res/menu/account_actions.xml +++ b/app/src/main/res/menu/account_actions.xml @@ -14,13 +14,14 @@ limitations under the License. --> - - + + + \ No newline at end of file diff --git a/app/src/main/res/menu/global_actions.xml b/app/src/main/res/menu/global_actions.xml index 8ddbce722..d021d5f4a 100644 --- a/app/src/main/res/menu/global_actions.xml +++ b/app/src/main/res/menu/global_actions.xml @@ -17,11 +17,4 @@ - - \ No newline at end of file