@@ -26,11 +26,12 @@ Add this library dependency and KotlinX.Serialization support
26
26
27
27
``` kotlin
28
28
plugins {
29
- id(" org.jetbrains.kotlin.plugin.serialization" ) version " 1.8.10"
29
+ id(" org.jetbrains.kotlin.plugin.serialization" ) version " 1.8.10"
30
30
}
31
+
31
32
dependencies {
32
- implementation(" com.kiwi.navigation-compose.typed:core:<version>" )
33
- implementation(" org.jetbrains.kotlinx:kotlinx-serialization-core:1.5.0" )
33
+ implementation(" com.kiwi.navigation-compose.typed:core:<version>" )
34
+ implementation(" org.jetbrains.kotlinx:kotlinx-serialization-core:1.5.0" )
34
35
}
35
36
```
36
37
@@ -44,13 +45,14 @@ Create app's destinations
44
45
import com.kiwi.navigationcompose.typed.Destination
45
46
46
47
sealed interface Destinations : Destination {
47
- @Serializable
48
- data object Home : Destinations
49
-
50
- @Serializable
51
- data class Article (
52
- val id : String ,
53
- ) : Destinations
48
+
49
+ @Serializable
50
+ data object Home : Destinations
51
+
52
+ @Serializable
53
+ data class Article (
54
+ val id : String ,
55
+ ) : Destinations
54
56
}
55
57
```
56
58
@@ -61,15 +63,15 @@ import com.kiwi.navigationcompose.typed.composable
61
63
import com.kiwi.navigationcompose.typed.createRoutePattern
62
64
63
65
NavGraph (
64
- startDestination = createRoutePattern<Destinations .Home >(),
66
+ startDestination = createRoutePattern<Destinations .Home >(),
65
67
) {
66
- composable<Destinations .Home > {
67
- Home ()
68
- }
69
- composable<Destinations .Article > {
70
- // this is Destinations.Article
71
- Article (id)
72
- }
68
+ composable<Destinations .Home > {
69
+ Home ()
70
+ }
71
+ composable<Destinations .Article > {
72
+ // this is Destinations.Article
73
+ Article (id)
74
+ }
73
75
}
74
76
```
75
77
@@ -81,33 +83,33 @@ import com.kiwi.navigationcompose.typed.navigate
81
83
82
84
@Composable
83
85
fun AppNavHost () {
84
- val navController = rememberNavController()
85
- NavGraph (
86
- navController = navController,
87
- ) {
88
- composable<Destinations .Home > {
89
- Home (navController::navigate)
90
- }
91
- }
86
+ val navController = rememberNavController()
87
+ NavGraph (
88
+ navController = navController,
89
+ ) {
90
+ composable<Destinations .Home > {
91
+ Home (navController::navigate)
92
+ }
93
+ }
92
94
}
93
95
94
96
@Composable
95
97
private fun Home (
96
- onNavigate : (Destination ) -> Unit ,
98
+ onNavigate : (Destination ) -> Unit ,
97
99
) {
98
- Home (
99
- onArticleClick = { id -> onNavigate(Destinations .Article (id)) },
100
- )
100
+ Home (
101
+ onArticleClick = { id -> onNavigate(Destinations .Article (id)) },
102
+ )
101
103
}
102
104
103
105
@Composable
104
106
private fun Home (
105
- onArticleClick : (id: Int ) -> Unit ,
107
+ onArticleClick : (id: Int ) -> Unit ,
106
108
) {
107
- Column {
108
- Button (onClick = { onArticleClick(1 ) }) { Text (" ..." ) }
109
- Button (onClick = { onArticleClick(2 ) }) { Text (" ..." ) }
110
- }
109
+ Column {
110
+ Button (onClick = { onArticleClick(1 ) }) { Text (" ..." ) }
111
+ Button (onClick = { onArticleClick(2 ) }) { Text (" ..." ) }
112
+ }
111
113
}
112
114
```
113
115
@@ -119,25 +121,25 @@ For example, in Koin:
119
121
120
122
``` kotlin
121
123
val KoinModule = module {
122
- viewModelOf(::DemoViewModel )
124
+ viewModelOf(::DemoViewModel )
123
125
}
124
126
125
127
fun DemoScreen (arguments : HomeDestinations .Demo ) {
126
- val viewModel = getViewModel<DemoViewModel > { parametersOf(arguments) }
128
+ val viewModel = getViewModel<DemoViewModel > { parametersOf(arguments) }
127
129
}
128
130
129
131
class DemoViewModel (
130
- arguments : HomeDestinations .Demo ,
132
+ arguments : HomeDestinations .Demo ,
131
133
)
132
134
```
133
135
134
136
Alternatively, you can read your destination from a ` SavedStateHandle ` instance:
135
137
136
138
``` kotlin
137
139
class DemoViewModel (
138
- state : SavedStateHandle ,
140
+ state : SavedStateHandle ,
139
141
) : ViewModel() {
140
- val arguments = state.decodeArguments<HomeDestinations .Demo >()
142
+ val arguments = state.decodeArguments<HomeDestinations .Demo >()
141
143
}
142
144
```
143
145
@@ -153,23 +155,23 @@ import com.kiwi.navigationcompose.typed.Destination
153
155
import com.kiwi.navigationcompose.typed.registerDestinationType
154
156
155
157
private inline fun <reified T : Destination > NavGraphBuilder.bottomSheet (
156
- noinline content : @Composable T .(NavBackStackEntry ) -> Unit ,
158
+ noinline content : @Composable T .(NavBackStackEntry ) -> Unit ,
157
159
) {
158
- val serializer = serializer<T >()
159
- registerDestinationType(T ::class , serializer)
160
- bottomSheet(
161
- route = createRoutePattern(serializer),
162
- arguments = createNavArguments(serializer),
163
- ) {
164
- val arguments = decodeArguments(serializer, it)
165
- arguments.content(it)
166
- }
160
+ val serializer = serializer<T >()
161
+ registerDestinationType(T ::class , serializer)
162
+ bottomSheet(
163
+ route = createRoutePattern(serializer),
164
+ arguments = createNavArguments(serializer),
165
+ ) {
166
+ val arguments = decodeArguments(serializer, it)
167
+ arguments.content(it)
168
+ }
167
169
}
168
170
169
171
NavGraph {
170
- bottomSheet<Destinations .Article > {
171
- Article (id)
172
- }
172
+ bottomSheet<Destinations .Article > {
173
+ Article (id)
174
+ }
173
175
}
174
176
```
175
177
@@ -185,38 +187,39 @@ import com.kiwi.navigationcompose.typed.ResultDestination
185
187
import com.kiwi.navigationcompose.typed.setResult
186
188
187
189
sealed interface Destinations : Destination {
188
- @Serializable
189
- data object Dialog : Destinations , ResultDestination <Dialog .Result > {
190
- @Serializable
191
- data class Result (
192
- val something : Int ,
193
- )
194
- }
190
+
191
+ @Serializable
192
+ data object Dialog : Destinations , ResultDestination <Dialog .Result > {
193
+ @Serializable
194
+ data class Result (
195
+ val something : Int ,
196
+ )
197
+ }
195
198
}
196
199
197
200
@Composable
198
201
fun Host (navController : NavController ) {
199
- DialogResultEffect (navController) { result: Destinations .Dialog .Result ->
200
- println (result)
201
- // process the result
202
- }
203
-
204
- Button (
205
- onClick = { navController.navigate(Destinations .Dialog ) },
206
- ) {
207
- Text (" Open" )
208
- }
202
+ DialogResultEffect (navController) { result: Destinations .Dialog .Result ->
203
+ println (result)
204
+ // process the result
205
+ }
206
+
207
+ Button (
208
+ onClick = { navController.navigate(Destinations .Dialog ) },
209
+ ) {
210
+ Text (" Open" )
211
+ }
209
212
}
210
213
211
214
@Composable
212
215
fun Dialog (navController : NavController ) {
213
- Button (
214
- onClick = {
215
- navController.setResult(Destinations .Dialog .Result (something = 42 ))
216
- navController.popBackStack()
217
- }
218
- ) {
219
- Text (" Set and close" )
220
- }
216
+ Button (
217
+ onClick = {
218
+ navController.setResult(Destinations .Dialog .Result (something = 42 ))
219
+ navController.popBackStack()
220
+ }
221
+ ) {
222
+ Text (" Set and close" )
223
+ }
221
224
}
222
225
```
0 commit comments