Skip to content

Commit

Permalink
Use MenuProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
topjohnwu committed Mar 10, 2023
1 parent 3cf66d1 commit fe6b658
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 24 deletions.
3 changes: 3 additions & 0 deletions app/src/main/java/com/topjohnwu/magisk/arch/BaseFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -66,6 +67,8 @@ abstract class BaseFragment<Binding : ViewDataBinding> : 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<Binding>() {
override fun onPreBind(binding: Binding): Boolean {
this@BaseFragment.onPreBind(binding)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,17 +17,16 @@ import rikka.recyclerview.addEdgeSpacing
import rikka.recyclerview.addItemSpacing
import rikka.recyclerview.fixEdgeEffect

class DenyListFragment : BaseFragment<FragmentDenyMd2Binding>() {
class DenyListFragment : BaseFragment<FragmentDenyMd2Binding>(), MenuProvider {

override val layoutRes = R.layout.fragment_deny_md2
override val viewModel by viewModel<DenyListViewModel>()

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?) {
Expand Down Expand Up @@ -56,7 +55,7 @@ class DenyListFragment : BaseFragment<FragmentDenyMd2Binding>() {
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)
Expand All @@ -73,7 +72,7 @@ class DenyListFragment : BaseFragment<FragmentDenyMd2Binding>() {
})
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
override fun onMenuItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.action_show_system -> {
val check = !item.isChecked
Expand All @@ -91,7 +90,7 @@ class DenyListFragment : BaseFragment<FragmentDenyMd2Binding>() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<FragmentFlashMd2Binding>() {
class FlashFragment : BaseFragment<FragmentFlashMd2Binding>(), MenuProvider {

override val layoutRes = R.layout.fragment_flash_md2
override val viewModel by viewModel<FlashViewModel>()
Expand All @@ -34,7 +35,6 @@ class FlashFragment : BaseFragment<FragmentFlashMd2Binding>() {

override fun onStart() {
super.onStart()
setHasOptionsMenu(true)
activity?.setTitle(R.string.flash_screen_title)

viewModel.state.observe(this) {
Expand All @@ -54,11 +54,11 @@ class FlashFragment : BaseFragment<FragmentFlashMd2Binding>() {
}
}

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)
}

Expand Down
17 changes: 11 additions & 6 deletions app/src/main/java/com/topjohnwu/magisk/ui/home/HomeFragment.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<FragmentHomeMd2Binding>() {
class HomeFragment : BaseFragment<FragmentHomeMd2Binding>(), MenuProvider {

override val layoutRes = R.layout.fragment_home_md2
override val viewModel by viewModel<HomeViewModel>()

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)
}

Expand Down Expand Up @@ -54,13 +59,13 @@ class HomeFragment : BaseFragment<FragmentHomeMd2Binding>() {
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()
Expand Down
11 changes: 5 additions & 6 deletions app/src/main/java/com/topjohnwu/magisk/ui/log/LogFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,7 +17,7 @@ import rikka.recyclerview.addEdgeSpacing
import rikka.recyclerview.addItemSpacing
import rikka.recyclerview.fixEdgeEffect

class LogFragment : BaseFragment<FragmentLogMd2Binding>() {
class LogFragment : BaseFragment<FragmentLogMd2Binding>(), MenuProvider {

override val layoutRes = R.layout.fragment_log_md2
override val viewModel by viewModel<LogViewModel>()
Expand All @@ -40,8 +41,7 @@ class LogFragment : BaseFragment<FragmentLogMd2Binding>() {

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?) {
Expand All @@ -58,15 +58,14 @@ class LogFragment : BaseFragment<FragmentLogMd2Binding>() {
}


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 ->
Expand Down

0 comments on commit fe6b658

Please sign in to comment.