From fe6b658c028e30f2972dd854b02ef2dd9a99d5b7 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 9 Mar 2023 18:05:37 -0800 Subject: [PATCH] Use MenuProvider --- .../com/topjohnwu/magisk/arch/BaseFragment.kt | 3 +++ .../magisk/ui/deny/DenyListFragment.kt | 15 +++++++-------- .../topjohnwu/magisk/ui/flash/FlashFragment.kt | 8 ++++---- .../topjohnwu/magisk/ui/home/HomeFragment.kt | 17 +++++++++++------ .../com/topjohnwu/magisk/ui/log/LogFragment.kt | 11 +++++------ 5 files changed, 30 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/arch/BaseFragment.kt b/app/src/main/java/com/topjohnwu/magisk/arch/BaseFragment.kt index b0f244bf6ac7..69852a265257 100644 --- a/app/src/main/java/com/topjohnwu/magisk/arch/BaseFragment.kt +++ b/app/src/main/java/com/topjohnwu/magisk/arch/BaseFragment.kt @@ -5,6 +5,7 @@ import android.view.KeyEvent import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.core.view.MenuProvider import androidx.databinding.DataBindingUtil import androidx.databinding.OnRebindCallback import androidx.databinding.ViewDataBinding @@ -66,6 +67,8 @@ abstract class BaseFragment : Fragment(), ViewModelHo override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + if (this is MenuProvider) + activity?.addMenuProvider(this, viewLifecycleOwner) binding.addOnRebindCallback(object : OnRebindCallback() { override fun onPreBind(binding: Binding): Boolean { this@BaseFragment.onPreBind(binding) diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/deny/DenyListFragment.kt b/app/src/main/java/com/topjohnwu/magisk/ui/deny/DenyListFragment.kt index dc312e1d4977..6a757a359749 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/deny/DenyListFragment.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/deny/DenyListFragment.kt @@ -1,12 +1,12 @@ package com.topjohnwu.magisk.ui.deny -import android.content.Context import android.os.Bundle import android.view.Menu import android.view.MenuInflater import android.view.MenuItem import android.view.View import androidx.appcompat.widget.SearchView +import androidx.core.view.MenuProvider import androidx.recyclerview.widget.RecyclerView import com.topjohnwu.magisk.R import com.topjohnwu.magisk.arch.BaseFragment @@ -17,17 +17,16 @@ import rikka.recyclerview.addEdgeSpacing import rikka.recyclerview.addItemSpacing import rikka.recyclerview.fixEdgeEffect -class DenyListFragment : BaseFragment() { +class DenyListFragment : BaseFragment(), MenuProvider { override val layoutRes = R.layout.fragment_deny_md2 override val viewModel by viewModel() private lateinit var searchView: SearchView - override fun onAttach(context: Context) { - super.onAttach(context) + override fun onStart() { + super.onStart() activity?.setTitle(R.string.denylist) - setHasOptionsMenu(true) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { @@ -56,7 +55,7 @@ class DenyListFragment : BaseFragment() { return super.onBackPressed() } - override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { + override fun onCreateMenu(menu: Menu, inflater: MenuInflater) { inflater.inflate(R.menu.menu_deny_md2, menu) searchView = menu.findItem(R.id.action_search).actionView as SearchView searchView.queryHint = searchView.context.getString(R.string.hide_filter_hint) @@ -73,7 +72,7 @@ class DenyListFragment : BaseFragment() { }) } - override fun onOptionsItemSelected(item: MenuItem): Boolean { + override fun onMenuItemSelected(item: MenuItem): Boolean { when (item.itemId) { R.id.action_show_system -> { val check = !item.isChecked @@ -91,7 +90,7 @@ class DenyListFragment : BaseFragment() { return super.onOptionsItemSelected(item) } - override fun onPrepareOptionsMenu(menu: Menu) { + override fun onPrepareMenu(menu: Menu) { val showSystem = menu.findItem(R.id.action_show_system) val showOS = menu.findItem(R.id.action_show_OS) showOS.isEnabled = showSystem.isChecked diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashFragment.kt b/app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashFragment.kt index 43c0ee1b595a..a3ed40ef7d17 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashFragment.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashFragment.kt @@ -6,6 +6,7 @@ import android.content.pm.ActivityInfo import android.net.Uri import android.os.Bundle import android.view.* +import androidx.core.view.MenuProvider import androidx.core.view.isVisible import androidx.navigation.NavDeepLinkBuilder import com.topjohnwu.magisk.MainDirections @@ -17,7 +18,7 @@ import com.topjohnwu.magisk.core.cmp import com.topjohnwu.magisk.databinding.FragmentFlashMd2Binding import com.topjohnwu.magisk.ui.MainActivity -class FlashFragment : BaseFragment() { +class FlashFragment : BaseFragment(), MenuProvider { override val layoutRes = R.layout.fragment_flash_md2 override val viewModel by viewModel() @@ -34,7 +35,6 @@ class FlashFragment : BaseFragment() { override fun onStart() { super.onStart() - setHasOptionsMenu(true) activity?.setTitle(R.string.flash_screen_title) viewModel.state.observe(this) { @@ -54,11 +54,11 @@ class FlashFragment : BaseFragment() { } } - override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { + override fun onCreateMenu(menu: Menu, inflater: MenuInflater) { inflater.inflate(R.menu.menu_flash, menu) } - override fun onOptionsItemSelected(item: MenuItem): Boolean { + override fun onMenuItemSelected(item: MenuItem): Boolean { return viewModel.onMenuItemClicked(item) } diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/home/HomeFragment.kt b/app/src/main/java/com/topjohnwu/magisk/ui/home/HomeFragment.kt index 18d5195053b7..69da8e9361d1 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/home/HomeFragment.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/home/HomeFragment.kt @@ -1,9 +1,15 @@ package com.topjohnwu.magisk.ui.home import android.os.Bundle -import android.view.* +import android.view.LayoutInflater +import android.view.Menu +import android.view.MenuInflater +import android.view.MenuItem +import android.view.View +import android.view.ViewGroup import android.widget.ImageView import android.widget.TextView +import androidx.core.view.MenuProvider import com.topjohnwu.magisk.R import com.topjohnwu.magisk.arch.BaseFragment import com.topjohnwu.magisk.arch.viewModel @@ -12,15 +18,14 @@ import com.topjohnwu.magisk.core.download.DownloadService import com.topjohnwu.magisk.databinding.FragmentHomeMd2Binding import com.topjohnwu.magisk.events.RebootEvent -class HomeFragment : BaseFragment() { +class HomeFragment : BaseFragment(), MenuProvider { override val layoutRes = R.layout.fragment_home_md2 override val viewModel by viewModel() override fun onStart() { super.onStart() - activity?.title = resources.getString(R.string.section_home) - setHasOptionsMenu(true) + activity?.setTitle(R.string.section_home) DownloadService.observeProgress(this, viewModel::onProgressUpdate) } @@ -54,13 +59,13 @@ class HomeFragment : BaseFragment() { return binding.root } - override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { + override fun onCreateMenu(menu: Menu, inflater: MenuInflater) { inflater.inflate(R.menu.menu_home_md2, menu) if (!Info.isRooted) menu.removeItem(R.id.action_reboot) } - override fun onOptionsItemSelected(item: MenuItem): Boolean { + override fun onMenuItemSelected(item: MenuItem): Boolean { when (item.itemId) { R.id.action_settings -> HomeFragmentDirections.actionHomeFragmentToSettingsFragment().navigate() diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/log/LogFragment.kt b/app/src/main/java/com/topjohnwu/magisk/ui/log/LogFragment.kt index d20fb7c11288..da2bef5a33e4 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/log/LogFragment.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/log/LogFragment.kt @@ -5,6 +5,7 @@ import android.view.Menu import android.view.MenuInflater import android.view.MenuItem import android.view.View +import androidx.core.view.MenuProvider import androidx.core.view.isVisible import com.topjohnwu.magisk.R import com.topjohnwu.magisk.arch.BaseFragment @@ -16,7 +17,7 @@ import rikka.recyclerview.addEdgeSpacing import rikka.recyclerview.addItemSpacing import rikka.recyclerview.fixEdgeEffect -class LogFragment : BaseFragment() { +class LogFragment : BaseFragment(), MenuProvider { override val layoutRes = R.layout.fragment_log_md2 override val viewModel by viewModel() @@ -40,8 +41,7 @@ class LogFragment : BaseFragment() { override fun onStart() { super.onStart() - setHasOptionsMenu(true) - activity?.title = resources.getString(R.string.logs) + activity?.setTitle(R.string.logs) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { @@ -58,15 +58,14 @@ class LogFragment : BaseFragment() { } - override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { - super.onCreateOptionsMenu(menu, inflater) + override fun onCreateMenu(menu: Menu, inflater: MenuInflater) { inflater.inflate(R.menu.menu_log_md2, menu) actionSave = menu.findItem(R.id.action_save)?.also { it.isVisible = !isMagiskLogVisible } } - override fun onOptionsItemSelected(item: MenuItem): Boolean { + override fun onMenuItemSelected(item: MenuItem): Boolean { when (item.itemId) { R.id.action_save -> viewModel.saveMagiskLog() R.id.action_clear ->