1
0
Fork 0

For #456 - Adds popover menu to menu button

master
Jeff Boek 2019-02-12 16:04:01 -08:00
parent 4873d989a0
commit 5addcca524
3 changed files with 73 additions and 10 deletions

View File

@ -1,4 +1,6 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.fenix.home package org.mozilla.fenix.home
@ -16,6 +18,7 @@ import androidx.navigation.Navigation
import kotlinx.android.synthetic.main.fragment_home.* import kotlinx.android.synthetic.main.fragment_home.*
import kotlinx.android.synthetic.main.fragment_home.view.* import kotlinx.android.synthetic.main.fragment_home.view.*
import kotlinx.android.synthetic.main.tab_list_header.view.* import kotlinx.android.synthetic.main.tab_list_header.view.*
import mozilla.components.browser.menu.BrowserMenu
import mozilla.components.browser.session.Session import mozilla.components.browser.session.Session
import mozilla.components.browser.session.SessionManager import mozilla.components.browser.session.SessionManager
import org.mozilla.fenix.DefaultThemeManager import org.mozilla.fenix.DefaultThemeManager
@ -38,6 +41,7 @@ import kotlin.math.roundToInt
class HomeFragment : Fragment() { class HomeFragment : Fragment() {
private val bus = ActionBusFactory.get(this) private val bus = ActionBusFactory.get(this)
private var sessionObserver: SessionManager.Observer? = null private var sessionObserver: SessionManager.Observer? = null
private lateinit var homeMenu: HomeMenu
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,
@ -59,6 +63,7 @@ class HomeFragment : Fragment() {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
(activity as AppCompatActivity).supportActionBar?.hide() (activity as AppCompatActivity).supportActionBar?.hide()
setupHomeMenu()
getSafeManagedObservable<TabsAction>() getSafeManagedObservable<TabsAction>()
.subscribe { .subscribe {
@ -78,10 +83,10 @@ class HomeFragment : Fragment() {
BitmapDrawable(resources, it.icon) BitmapDrawable(resources, it.icon)
} }
// Temporary so we can easily test settings
view.menuButton.setOnClickListener { view.menuButton.setOnClickListener {
val directions = HomeFragmentDirections.actionHomeFragmentToSettingsFragment() homeMenu.menuBuilder.build(requireContext()).show(
Navigation.findNavController(it).navigate(directions) anchor = it,
orientation = BrowserMenu.Orientation.DOWN)
} }
view.toolbar.setCompoundDrawablesWithIntrinsicBounds(searchIcon, null, null, null) view.toolbar.setCompoundDrawablesWithIntrinsicBounds(searchIcon, null, null, null)
@ -155,6 +160,18 @@ class HomeFragment : Fragment() {
} }
} }
private fun setupHomeMenu() {
homeMenu = HomeMenu(requireContext()) {
val directions = when (it) {
HomeMenu.Item.Settings -> HomeFragmentDirections.actionHomeFragmentToSettingsFragment()
HomeMenu.Item.Library -> HomeFragmentDirections.actionHomeFragmentToLibraryFragment()
HomeMenu.Item.Help -> return@HomeMenu // Not implemented yetN
}
Navigation.findNavController(homeLayout).navigate(directions)
}
}
private fun subscribeToSessions(): SessionManager.Observer { private fun subscribeToSessions(): SessionManager.Observer {
val observer = object : SessionManager.Observer { val observer = object : SessionManager.Observer {
override fun onSessionAdded(session: Session) { override fun onSessionAdded(session: Session) {

View File

@ -1,9 +1,56 @@
package org.mozilla.fenix.home
/* This Source Code Form is subject to the terms of the Mozilla Public /* This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/. */ file, You can obtain one at http://mozilla.org/MPL/2.0/. */
class HomeMenu { package org.mozilla.fenix.home
} import android.content.Context
import mozilla.components.browser.menu.BrowserMenuBuilder
import mozilla.components.browser.menu.item.BrowserMenuDivider
import mozilla.components.browser.menu.item.BrowserMenuImageText
import org.mozilla.fenix.DefaultThemeManager
import org.mozilla.fenix.R
class HomeMenu(
private val context: Context,
private val onItemTapped: (Item) -> Unit = {}
) {
sealed class Item {
object Help : Item()
object Settings : Item()
object Library : Item()
}
val menuBuilder by lazy { BrowserMenuBuilder(menuItems) }
private val menuItems by lazy {
listOf(
BrowserMenuImageText(
context.getString(R.string.browser_menu_settings),
R.drawable.ic_settings,
context.getString(R.string.browser_menu_settings),
DefaultThemeManager.resolveAttribute(R.attr.browserToolbarIcons, context)
) {
onItemTapped.invoke(HomeMenu.Item.Settings)
},
BrowserMenuImageText(
context.getString(R.string.browser_menu_library),
R.drawable.ic_library,
context.getString(R.string.browser_menu_library),
DefaultThemeManager.resolveAttribute(R.attr.browserToolbarIcons, context)
) {
onItemTapped.invoke(HomeMenu.Item.Library)
},
BrowserMenuDivider(),
BrowserMenuImageText(
context.getString(R.string.browser_menu_help),
R.drawable.ic_help,
context.getString(R.string.browser_menu_help),
DefaultThemeManager.resolveAttribute(R.attr.browserToolbarIcons, context)
) {
onItemTapped.invoke(HomeMenu.Item.Help)
})
}
}

View File

@ -49,8 +49,7 @@ class LibraryFragment : Fragment() {
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) { return when (item.itemId) {
R.id.libraryClose -> { R.id.libraryClose -> {
Navigation.findNavController(requireActivity(), R.id.container) Navigation.findNavController(requireActivity(), R.id.container).navigateUp()
.popBackStack(R.id.browserFragment, false)
true true
} }
R.id.librarySearch -> { R.id.librarySearch -> {