1 /*
<lambda>null2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settingslib.spa.framework.compose
18 
19 import androidx.activity.OnBackPressedDispatcher
20 import androidx.activity.compose.LocalOnBackPressedDispatcherOwner
21 import androidx.compose.runtime.Composable
22 import androidx.compose.runtime.ProvidedValue
23 import androidx.compose.runtime.compositionLocalOf
24 import androidx.compose.runtime.remember
25 import androidx.navigation.NavHostController
26 
27 interface NavControllerWrapper {
28     fun navigate(route: String, popUpCurrent: Boolean = false)
29     fun navigateBack()
30 
31     val highlightEntryId: String?
32         get() = null
33 
34     val sessionSourceName: String?
35         get() = null
36 }
37 
38 @Composable
localNavControllernull39 fun NavHostController.localNavController(): ProvidedValue<NavControllerWrapper> {
40     val onBackPressedDispatcherOwner = LocalOnBackPressedDispatcherOwner.current
41     return LocalNavController provides remember {
42         NavControllerWrapperImpl(
43             navController = this,
44             onBackPressedDispatcher = onBackPressedDispatcherOwner?.onBackPressedDispatcher,
45         )
46     }
47 }
48 
<lambda>null49 val LocalNavController = compositionLocalOf<NavControllerWrapper> {
50     object : NavControllerWrapper {
51         override fun navigate(route: String, popUpCurrent: Boolean) {}
52 
53         override fun navigateBack() {}
54     }
55 }
56 
57 @Composable
navigatornull58 fun navigator(route: String?, popUpCurrent: Boolean = false): () -> Unit {
59     if (route == null) return {}
60     val navController = LocalNavController.current
61     return { navController.navigate(route, popUpCurrent) }
62 }
63 
64 internal class NavControllerWrapperImpl(
65     val navController: NavHostController,
66     private val onBackPressedDispatcher: OnBackPressedDispatcher?,
67 ) : NavControllerWrapper {
68     var highlightId: String? = null
69     var sessionName: String? = null
70 
navigatenull71     override fun navigate(route: String, popUpCurrent: Boolean) {
72         navController.navigate(route) {
73             if (popUpCurrent) {
74                 navController.currentDestination?.let { currentDestination ->
75                     popUpTo(currentDestination.id) {
76                         inclusive = true
77                     }
78                 }
79             }
80         }
81     }
82 
navigateBacknull83     override fun navigateBack() {
84         onBackPressedDispatcher?.onBackPressed()
85     }
86 
87     override val highlightEntryId: String?
88         get() = highlightId
89 
90     override val sessionSourceName: String?
91         get() = sessionName
92 }
93