Bug 1885602 - Part 4: Implement navigating to the settings from the menu header for...
[gecko.git] / mobile / android / fenix / app / src / main / java / org / mozilla / fenix / components / menu / middleware / MenuNavigationMiddleware.kt
blob0f2e5add6efbf9dcec3f13c81b6ca2f23b07e015
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 package org.mozilla.fenix.components.menu.middleware
7 import androidx.navigation.NavController
8 import kotlinx.coroutines.CoroutineScope
9 import kotlinx.coroutines.Dispatchers
10 import kotlinx.coroutines.launch
11 import mozilla.components.lib.state.Middleware
12 import mozilla.components.lib.state.MiddlewareContext
13 import org.mozilla.fenix.R
14 import org.mozilla.fenix.components.menu.MenuDialogFragmentDirections
15 import org.mozilla.fenix.components.menu.store.MenuAction
16 import org.mozilla.fenix.components.menu.store.MenuState
17 import org.mozilla.fenix.components.menu.store.MenuStore
18 import org.mozilla.fenix.ext.nav
20 /**
21  * [Middleware] implementation for handling navigating events based on [MenuAction]s that are
22  * dispatched to the [MenuStore].
23  */
24 class MenuNavigationMiddleware(
25     private val navController: NavController,
26     private val scope: CoroutineScope = CoroutineScope(Dispatchers.Main),
27 ) : Middleware<MenuState, MenuAction> {
29     override fun invoke(
30         context: MiddlewareContext<MenuState, MenuAction>,
31         next: (MenuAction) -> Unit,
32         action: MenuAction,
33     ) {
34         next(action)
36         scope.launch {
37             when (action) {
38                 is MenuAction.Navigate.Settings -> navController.nav(
39                     R.id.menuDialogFragment,
40                     MenuDialogFragmentDirections.actionGlobalSettingsFragment(),
41                 )
43                 else -> Unit
44             }
45         }
46     }