ui: migrate to MenuProvider

AndroidX Activity 1.4.0 introduced the new lifecycle-dependent MenuProvider
APIs that supersede existing onCreateOptionsMenu-style menus.

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2023-03-22 21:18:49 +05:30
parent 35c8c10b7e
commit b44380c81a
No known key found for this signature in database
2 changed files with 26 additions and 13 deletions

View File

@ -8,9 +8,12 @@ import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.Menu import android.view.Menu
import android.view.MenuInflater import android.view.MenuInflater
import android.view.MenuItem
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.core.view.MenuProvider
import androidx.databinding.DataBindingUtil import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import com.wireguard.android.R import com.wireguard.android.R
import com.wireguard.android.backend.Tunnel import com.wireguard.android.backend.Tunnel
@ -24,18 +27,17 @@ import kotlinx.coroutines.launch
/** /**
* Fragment that shows details about a specific tunnel. * Fragment that shows details about a specific tunnel.
*/ */
class TunnelDetailFragment : BaseFragment() { class TunnelDetailFragment : BaseFragment(), MenuProvider {
private var binding: TunnelDetailFragmentBinding? = null private var binding: TunnelDetailFragmentBinding? = null
private var lastState = Tunnel.State.TOGGLE private var lastState = Tunnel.State.TOGGLE
private var timerActive = true private var timerActive = true
override fun onCreate(savedInstanceState: Bundle?) { override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
super.onCreate(savedInstanceState) return false
setHasOptionsMenu(true)
} }
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
inflater.inflate(R.menu.tunnel_detail, menu) menuInflater.inflate(R.menu.tunnel_detail, menu)
} }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
@ -46,6 +48,11 @@ class TunnelDetailFragment : BaseFragment() {
return binding?.root return binding?.root
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
requireActivity().addMenuProvider(this, viewLifecycleOwner, Lifecycle.State.RESUMED)
}
override fun onDestroyView() { override fun onDestroyView() {
binding = null binding = null
super.onDestroyView() super.onDestroyView()

View File

@ -19,6 +19,8 @@ import android.view.WindowManager
import android.view.inputmethod.InputMethodManager import android.view.inputmethod.InputMethodManager
import android.widget.EditText import android.widget.EditText
import android.widget.Toast import android.widget.Toast
import androidx.core.view.MenuProvider
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import com.wireguard.android.Application import com.wireguard.android.Application
@ -36,7 +38,7 @@ import kotlinx.coroutines.launch
/** /**
* Fragment for editing a WireGuard configuration. * Fragment for editing a WireGuard configuration.
*/ */
class TunnelEditorFragment : BaseFragment() { class TunnelEditorFragment : BaseFragment(), MenuProvider {
private var haveShownKeys = false private var haveShownKeys = false
private var binding: TunnelEditorFragmentBinding? = null private var binding: TunnelEditorFragmentBinding? = null
private var tunnel: ObservableTunnel? = null private var tunnel: ObservableTunnel? = null
@ -66,11 +68,10 @@ class TunnelEditorFragment : BaseFragment() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
} }
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
inflater.inflate(R.menu.config_editor, menu) menuInflater.inflate(R.menu.config_editor, menu)
} }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
@ -84,6 +85,11 @@ class TunnelEditorFragment : BaseFragment() {
return binding?.root return binding?.root
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
requireActivity().addMenuProvider(this, viewLifecycleOwner, Lifecycle.State.RESUMED)
}
override fun onDestroyView() { override fun onDestroyView() {
activity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_SECURE) activity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
binding = null binding = null
@ -106,8 +112,8 @@ class TunnelEditorFragment : BaseFragment() {
selectedTunnel = tunnel selectedTunnel = tunnel
} }
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
if (item.itemId == R.id.menu_action_save) { if (menuItem.itemId == R.id.menu_action_save) {
binding ?: return false binding ?: return false
val newConfig = try { val newConfig = try {
binding!!.config!!.resolve() binding!!.config!!.resolve()
@ -153,7 +159,7 @@ class TunnelEditorFragment : BaseFragment() {
} }
return true return true
} }
return super.onOptionsItemSelected(item) return false
} }
@Suppress("UNUSED_PARAMETER") @Suppress("UNUSED_PARAMETER")