1 /*
<lambda>null2  * Copyright 2019 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 androidx.navigation.fragment
18 
19 import androidx.annotation.IdRes
20 import androidx.fragment.app.DialogFragment
21 import androidx.navigation.NavDestinationBuilder
22 import androidx.navigation.NavDestinationDsl
23 import androidx.navigation.NavGraphBuilder
24 import androidx.navigation.NavType
25 import androidx.navigation.get
26 import kotlin.reflect.KClass
27 import kotlin.reflect.KType
28 
29 /**
30  * Construct a new [DialogFragmentNavigator.Destination]
31  *
32  * @param id the destination's unique id
33  */
34 @Suppress("Deprecation")
35 @Deprecated(
36     "Use routes to create your DialogFragmentDestination instead",
37     ReplaceWith("dialog<F>(route = id.toString())")
38 )
39 public inline fun <reified F : DialogFragment> NavGraphBuilder.dialog(@IdRes id: Int): Unit =
40     dialog<F>(id) {}
41 
42 /**
43  * Construct a new [DialogFragmentNavigator.Destination]
44  *
45  * @param id the destination's unique id
46  * @param builder the builder used to construct the fragment destination
47  */
48 @Suppress("Deprecation")
49 @Deprecated(
50     "Use routes to create your DialogFragmentDestination instead",
51     ReplaceWith("dialog<F>(route = id.toString()) { builder.invoke() }")
52 )
dialognull53 public inline fun <reified F : DialogFragment> NavGraphBuilder.dialog(
54     @IdRes id: Int,
55     builder: DialogFragmentNavigatorDestinationBuilder.() -> Unit
56 ): Unit =
57     destination(
58         DialogFragmentNavigatorDestinationBuilder(
59                 provider[DialogFragmentNavigator::class],
60                 id,
61                 F::class
62             )
63             .apply(builder)
64     )
65 
66 /**
67  * Construct a new [DialogFragmentNavigator.Destination]
68  *
69  * @param route the destination's unique route
70  */
71 public inline fun <reified F : DialogFragment> NavGraphBuilder.dialog(route: String): Unit =
72     dialog<F>(route) {}
73 
74 /**
75  * Construct a new [DialogFragmentNavigator.Destination]
76  *
77  * @param route the destination's unique route
78  * @param builder the builder used to construct the fragment destination
79  */
dialognull80 public inline fun <reified F : DialogFragment> NavGraphBuilder.dialog(
81     route: String,
82     builder: DialogFragmentNavigatorDestinationBuilder.() -> Unit
83 ): Unit =
84     destination(
85         DialogFragmentNavigatorDestinationBuilder(
86                 provider[DialogFragmentNavigator::class],
87                 route,
88                 F::class
89             )
90             .apply(builder)
91     )
92 
93 /**
94  * Construct a new [DialogFragmentNavigator.Destination]
95  *
96  * @param T the destination's unique route from a [KClass]
97  * @param typeMap map of destination arguments' kotlin type [KType] to its respective custom
98  *   [NavType]. May be empty if [T] does not use custom NavTypes.
99  */
100 public inline fun <reified F : DialogFragment, reified T : Any> NavGraphBuilder.dialog(
101     typeMap: Map<KType, @JvmSuppressWildcards NavType<*>> = emptyMap(),
102 ): Unit = dialog<F, T>(typeMap) {}
103 
104 /**
105  * Construct a new [DialogFragmentNavigator.Destination]
106  *
107  * @param T the destination's unique route from a [KClass]
108  * @param typeMap map of destination arguments' kotlin type [KType] to its respective custom
109  *   [NavType]. May be empty if [T] does not use custom NavTypes.
110  * @param builder the builder used to construct the fragment destination
111  */
dialognull112 public inline fun <reified F : DialogFragment, reified T : Any> NavGraphBuilder.dialog(
113     typeMap: Map<KType, @JvmSuppressWildcards NavType<*>> = emptyMap(),
114     builder: DialogFragmentNavigatorDestinationBuilder.() -> Unit
115 ): Unit =
116     destination(
117         DialogFragmentNavigatorDestinationBuilder(
118                 provider[DialogFragmentNavigator::class],
119                 T::class,
120                 typeMap,
121                 F::class
122             )
123             .apply(builder)
124     )
125 
126 /** DSL for constructing a new [DialogFragmentNavigator.Destination] */
127 @NavDestinationDsl
128 public class DialogFragmentNavigatorDestinationBuilder :
129     NavDestinationBuilder<DialogFragmentNavigator.Destination> {
130 
131     private var fragmentClass: KClass<out DialogFragment>
132 
133     /**
134      * DSL for constructing a new [DialogFragmentNavigator.Destination]
135      *
136      * @param navigator navigator used to create the destination
137      * @param id the destination's unique id
138      * @param fragmentClass the class name of the DialogFragment to show when you navigate to this
139      *   destination
140      */
141     @Suppress("Deprecation")
142     @Deprecated(
143         "Use routes to build your DialogFragmentNavigatorDestination instead",
144         ReplaceWith(
145             "DialogFragmentNavigatorDestinationBuilder(navigator, route = id.toString(), " +
146                 "fragmentClass) "
147         )
148     )
149     public constructor(
150         navigator: DialogFragmentNavigator,
151         @IdRes id: Int,
152         fragmentClass: KClass<out DialogFragment>
153     ) : super(navigator, id) {
154         this.fragmentClass = fragmentClass
155     }
156 
157     /**
158      * DSL for constructing a new [DialogFragmentNavigator.Destination]
159      *
160      * @param navigator navigator used to create the destination
161      * @param route the destination's unique route. This sets the [route] on the newly constructed
162      *   [NavDestination]. This can be any valid non-empty String.
163      * @param fragmentClass the class name of the DialogFragment to show when you navigate to this
164      *   destination
165      */
166     public constructor(
167         navigator: DialogFragmentNavigator,
168         route: String,
169         fragmentClass: KClass<out DialogFragment>
170     ) : super(navigator, route) {
171         this.fragmentClass = fragmentClass
172     }
173 
174     /**
175      * DSL for constructing a new [DialogFragmentNavigator.Destination]
176      *
177      * @param navigator navigator used to create the destination
178      * @param route the destination's unique route from a [KClass]. This sets the [route] on the
179      *   newly constructed [NavDestination].
180      * @param typeMap map of destination arguments' kotlin type [KType] to its respective custom
181      *   [NavType]. May be empty if [route] does not use custom NavTypes.
182      * @param fragmentClass the class name of the DialogFragment to show when you navigate to this
183      *   destination
184      */
185     public constructor(
186         navigator: DialogFragmentNavigator,
187         route: KClass<out Any>,
188         typeMap: Map<KType, @JvmSuppressWildcards NavType<*>>,
189         fragmentClass: KClass<out DialogFragment>
190     ) : super(navigator, route, typeMap) {
191         this.fragmentClass = fragmentClass
192     }
193 
194     override fun build(): DialogFragmentNavigator.Destination =
195         super.build().also { destination -> destination.setClassName(fragmentClass.java.name) }
196 }
197