1 /*
<lambda>null2  * Copyright 2020 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 @file:Suppress("NOTHING_TO_INLINE")
18 
19 package androidx.navigation.dynamicfeatures
20 
21 import android.content.ComponentName
22 import android.net.Uri
23 import androidx.annotation.IdRes
24 import androidx.navigation.ActivityNavigator
25 import androidx.navigation.NavDestinationBuilder
26 import androidx.navigation.NavDestinationDsl
27 import androidx.navigation.NavType
28 import androidx.navigation.get
29 import kotlin.reflect.KClass
30 import kotlin.reflect.KType
31 
32 /**
33  * Construct a new [DynamicActivityNavigator.Destination]
34  *
35  * @param id Destination id.
36  */
37 @Suppress("Deprecation")
38 @Deprecated(
39     "Use routes to build your DynamicActivityDestination instead",
40     ReplaceWith("activity(route = id.toString()) { builder.invoke() }")
41 )
42 public inline fun DynamicNavGraphBuilder.activity(
43     @IdRes id: Int,
44     builder: DynamicActivityNavigatorDestinationBuilder.() -> Unit
45 ): Unit =
46     destination(
47         DynamicActivityNavigatorDestinationBuilder(provider[DynamicActivityNavigator::class], id)
48             .apply(builder)
49     )
50 
51 /**
52  * Construct a new [DynamicActivityNavigator.Destination]
53  *
54  * @param route Destination route.
55  * @param builder the builder used to construct the graph
56  */
57 public inline fun DynamicNavGraphBuilder.activity(
58     route: String,
59     builder: DynamicActivityNavigatorDestinationBuilder.() -> Unit
60 ): Unit =
61     destination(
62         DynamicActivityNavigatorDestinationBuilder(provider[DynamicActivityNavigator::class], route)
63             .apply(builder)
64     )
65 
66 /**
67  * Construct a new [DynamicActivityNavigator.Destination]
68  *
69  * @param T Destination route from a [KClass]
70  * @param typeMap A mapping of KType to custom NavType<*> in the [T]. May be empty if [T] does not
71  *   use custom NavTypes.
72  * @param builder the builder used to construct the graph
73  */
74 public inline fun <reified T : Any> DynamicNavGraphBuilder.activity(
75     typeMap: Map<KType, @JvmSuppressWildcards NavType<*>> = emptyMap(),
76     builder: DynamicActivityNavigatorDestinationBuilder.() -> Unit
77 ): Unit =
78     destination(
79         DynamicActivityNavigatorDestinationBuilder(
80                 provider[DynamicActivityNavigator::class],
81                 T::class,
82                 typeMap
83             )
84             .apply(builder)
85     )
86 
87 /** DSL for constructing a new [DynamicActivityNavigator.Destination] */
88 @NavDestinationDsl
89 public class DynamicActivityNavigatorDestinationBuilder :
90     NavDestinationBuilder<ActivityNavigator.Destination> {
91     private var activityNavigator: DynamicActivityNavigator
92 
93     @Suppress("Deprecation")
94     @Deprecated(
95         "Use routes to build your DynamicActivityDestination instead",
96         ReplaceWith(
97             "DynamicActivityNavigatorDestinationBuilder(activityNavigator, route = id.toString())"
98         )
99     )
100     public constructor(
101         activityNavigator: DynamicActivityNavigator,
102         @IdRes id: Int
103     ) : super(activityNavigator, id) {
104         this.activityNavigator = activityNavigator
105     }
106 
107     public constructor(
108         activityNavigator: DynamicActivityNavigator,
109         route: String
110     ) : super(activityNavigator, route) {
111         this.activityNavigator = activityNavigator
112     }
113 
114     /**
115      * DSL for constructing a new [DynamicActivityNavigator.Destination]
116      *
117      * @param activityNavigator navigator used to create the destination
118      * @param route the route from a [KClass] of the destination
119      * @param typeMap map of destination arguments' kotlin type [KType] to its respective custom
120      *   [NavType]. May be empty if [route] does not use custom NavTypes.
121      */
122     public constructor(
123         activityNavigator: DynamicActivityNavigator,
124         route: KClass<*>,
125         typeMap: Map<KType, @JvmSuppressWildcards NavType<*>>
126     ) : super(activityNavigator, route, typeMap) {
127         this.activityNavigator = activityNavigator
128     }
129 
130     public var moduleName: String? = null
131 
132     public var targetPackage: String? = null
133 
134     public var activityClassName: String? = null
135 
136     public var action: String? = null
137 
138     public var data: Uri? = null
139 
140     public var dataPattern: String? = null
141 
142     override fun build(): DynamicActivityNavigator.Destination =
143         (super.build() as DynamicActivityNavigator.Destination).also { destination ->
144             activityClassName?.also {
145                 destination.setComponentName(
146                     ComponentName(
147                         if (targetPackage != null) {
148                             targetPackage!!
149                         } else {
150                             activityNavigator.packageName
151                         },
152                         it
153                     )
154                 )
155             }
156             destination.setTargetPackage(targetPackage)
157             destination.moduleName = moduleName
158             destination.setAction(action)
159             destination.setData(data)
160             destination.setDataPattern(dataPattern)
161         }
162 }
163