Skip to content

Commit f5fa3fb

Browse files
committed
[wip]migrate precompose navigation
1 parent 2908ddc commit f5fa3fb

File tree

17 files changed

+318
-328
lines changed

17 files changed

+318
-328
lines changed

common/routeProcessor/src/main/java/com/dimension/maskbook/common/routeProcessor/RouteDefinition.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,19 @@ internal data class ParameterRouteDefinition(
8585
childRoute.forEach {
8686
if (it is FunctionRouteDefinition) {
8787
val pathParams = it.parameters.filter { !it.parameter.type.resolve().isMarkedNullable }
88-
val queryParams = it.parameters.filter { it.parameter.type.resolve().isMarkedNullable }
88+
// val queryParams = it.parameters.filter { it.parameter.type.resolve().isMarkedNullable }
8989
addProperty(
9090
PropertySpec.builder("path", String::class)
9191
.addModifiers(KModifier.CONST)
9292
.initializer(
93-
"%S + %S + %S + %S + %S + %S + %S",
93+
"%S + %S + %S + %S + %S",
9494
parentPath,
9595
RouteDivider,
9696
name,
9797
if (pathParams.any()) RouteDivider else "",
9898
pathParams.joinToString(RouteDivider) { "{${it.name}}" },
99-
if (queryParams.any()) "?" else "",
100-
queryParams.joinToString("&") { "${it.name}={${it.name}}" }
99+
// if (queryParams.any()) "?" else "",
100+
// queryParams.joinToString("&") { "${it.name}={${it.name}}" }
101101
)
102102
.build()
103103
)

common/routeProcessor/src/main/java/com/dimension/maskbook/common/routeProcessor/RouteGraphProcessor.kt

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,12 @@ import com.squareup.kotlinpoet.FunSpec
4242
import com.squareup.kotlinpoet.KModifier
4343
import com.squareup.kotlinpoet.buildCodeBlock
4444
import com.squareup.kotlinpoet.ksp.KotlinPoetKspPreview
45-
import com.squareup.kotlinpoet.ksp.toClassName
4645
import com.squareup.kotlinpoet.ksp.toTypeName
4746
import com.squareup.kotlinpoet.ksp.writeTo
4847
import com.squareup.kotlinpoet.withIndent
4948

50-
private val navControllerType = ClassName("androidx.navigation", "NavController")
51-
private val navBackStackEntryType = ClassName("androidx.navigation", "NavBackStackEntry")
49+
private val navControllerType = ClassName("moe.tlaster.precompose.navigation", "NavController")
50+
private val navBackStackEntryType = ClassName("moe.tlaster.precompose.navigation", "BackStackEntry")
5251
private const val navControllerName = "controller"
5352

5453
@OptIn(KotlinPoetKspPreview::class, KspExperimental::class)
@@ -93,14 +92,11 @@ internal class RouteGraphProcessor(
9392
)
9493
val packageName = data.first().packageName
9594
FileSpec.builder(packageName.asString(), "RouteGraph")
96-
.addImport("androidx.navigation", "NavType")
97-
.addImport("androidx.navigation", "navDeepLink")
98-
.addImport("androidx.navigation", "navArgument")
9995
.also { fileBuilder ->
10096
fileBuilder.addFunction(
10197
FunSpec.builder(generatedFunctionName)
10298
.addModifiers(KModifier.INTERNAL)
103-
.receiver(ClassName("androidx.navigation", "NavGraphBuilder"))
99+
.receiver(ClassName("moe.tlaster.precompose.navigation", "RouteBuilder"))
104100
.addParameter(
105101
navControllerName,
106102
navControllerType,
@@ -133,42 +129,11 @@ internal class RouteGraphProcessor(
133129
"route = %S,",
134130
annotation.route,
135131
)
136-
val parameters = ksFunctionDeclaration.parameters.filter {
137-
it.isAnnotationPresent(
138-
Query::class
139-
) || it.isAnnotationPresent(Path::class)
140-
}
141-
if (parameters.isNotEmpty()) {
142-
addStatement("arguments = listOf(")
143-
withIndent {
144-
parameters.forEach {
145-
val type = it.type.resolve()
146-
val typeName = type.toClassName()
147-
148-
val argumentName = when {
149-
it.isAnnotationPresent(Path::class) -> it.getAnnotationsByType(Path::class).first().name
150-
it.isAnnotationPresent(Query::class) -> it.getAnnotationsByType(Query::class).first().name
151-
else -> it.name?.asString().orEmpty()
152-
}
153-
154-
addStatement(
155-
"navArgument(%S) { type = NavType.%NType; nullable = %L },",
156-
argumentName,
157-
if (typeName.isBoolean) "Bool" else type.declaration.simpleName.asString(),
158-
it.isAnnotationPresent(Query::class) && !typeName.isLong
159-
)
160-
}
161-
}
162-
addStatement("),")
163-
}
164132
if (annotation.deeplink.isNotEmpty()) {
165133
addStatement("deepLinks = listOf(")
166134
withIndent {
167135
annotation.deeplink.forEach {
168-
addStatement(
169-
"navDeepLink { uriPattern = %S }",
170-
it
171-
)
136+
add("%S", it)
172137
}
173138
}
174139
addStatement("),")
@@ -187,17 +152,17 @@ internal class RouteGraphProcessor(
187152
if (it.isAnnotationPresent(Path::class)) {
188153
val path = it.getAnnotationsByType(Path::class).first()
189154
builder.addStatement(
190-
"val ${it.name?.asString()} = it.arguments!!.get(%S) as %T",
155+
"val ${it.name?.asString()}: %T = it.path(%S)!!",
156+
it.type.toTypeName(),
191157
path.name,
192-
it.type.toTypeName()
193158
)
194159
} else if (it.isAnnotationPresent(Query::class)) {
195160
val query =
196161
it.getAnnotationsByType(Query::class).first()
197162
builder.addStatement(
198-
"val ${it.name?.asString()} = it.arguments?.get(%S) as? %T",
163+
"val ${it.name?.asString()}: %T? = it.query(%S)",
164+
it.type.toTypeName(),
199165
query.name,
200-
it.type.toTypeName()
201166
)
202167
}
203168
}

common/src/androidMain/kotlin/com/dimension/maskbook/common/route/AnimatedNavHost.kt

Lines changed: 0 additions & 19 deletions
This file was deleted.

common/src/androidMain/kotlin/com/dimension/maskbook/common/route/Consts.kt

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@
2020
*/
2121
package com.dimension.maskbook.common.route
2222

23-
import androidx.compose.runtime.Composable
24-
import moe.tlaster.precompose.navigation.BackStackEntry
25-
import moe.tlaster.precompose.navigation.RouteBuilder
26-
2723
const val navigationComposeDialogPackage = "com.dimension.maskbook.common.route"
2824
const val navigationComposeDialog = "dialog"
2925

@@ -35,75 +31,3 @@ const val navigationComposeModalComposable = "modalComposable"
3531

3632
const val navigationComposeBottomSheetPackage = "com.dimension.maskbook.common.route"
3733
const val navigationComposeBottomSheet = "bottomSheet"
38-
39-
fun RouteBuilder.composable(
40-
route: String,
41-
// arguments: List<NamedNavArgument> = emptyList(),
42-
deepLinks: List<String> = emptyList(),
43-
content: @Composable (BackStackEntry) -> Unit
44-
) {
45-
scene(
46-
route = route,
47-
// arguments = arguments,
48-
deepLinks = deepLinks,
49-
content = content,
50-
)
51-
}
52-
53-
fun RouteBuilder.modalComposable(
54-
route: String,
55-
// arguments: List<NamedNavArgument> = emptyList(),
56-
deepLinks: List<String> = emptyList(),
57-
content: @Composable (BackStackEntry) -> Unit
58-
) {
59-
scene(
60-
route = route,
61-
// arguments = arguments,
62-
deepLinks = deepLinks,
63-
content = content,
64-
// enterTransition = {
65-
// slideInVertically { it }
66-
// },
67-
// exitTransition = null,
68-
// popEnterTransition = null,
69-
// popExitTransition = {
70-
// slideOutVertically { it }
71-
// },
72-
)
73-
}
74-
75-
fun RouteBuilder.bottomSheet(
76-
route: String,
77-
// arguments: List<NamedNavArgument> = emptyList(),
78-
deepLinks: List<String> = emptyList(),
79-
content: @Composable (BackStackEntry) -> Unit
80-
) {
81-
scene(
82-
route = route,
83-
// arguments = arguments,
84-
deepLinks = deepLinks,
85-
content = content
86-
)
87-
}
88-
89-
fun RouteBuilder.dialog(
90-
route: String,
91-
// arguments: List<NamedNavArgument> = emptyList(),
92-
deepLinks: List<String> = emptyList(),
93-
content: @Composable (BackStackEntry) -> Unit
94-
) {
95-
dialog(
96-
route = route,
97-
// arguments = arguments,
98-
// deepLinks = deepLinks,
99-
content = content,
100-
)
101-
}
102-
103-
fun RouteBuilder.navigation(
104-
route: String,
105-
startDestination: String,
106-
content: @Composable RouteBuilder.(BackStackEntry) -> Unit
107-
) {
108-
// TODO
109-
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Mask-Android
3+
*
4+
* Copyright (C) 2022 DimensionDev and Contributors
5+
*
6+
* This file is part of Mask-Android.
7+
*
8+
* Mask-Android is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* Mask-Android is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with Mask-Android. If not, see <http://www.gnu.org/licenses/>.
20+
*/
21+
package com.dimension.maskbook.common.route
22+
23+
import androidx.compose.animation.ExperimentalAnimationApi
24+
import androidx.compose.runtime.Composable
25+
import moe.tlaster.precompose.navigation.BackStackEntry
26+
import moe.tlaster.precompose.navigation.RouteBuilder
27+
28+
@OptIn(ExperimentalAnimationApi::class)
29+
fun RouteBuilder.composable(
30+
route: String,
31+
// arguments: List<NamedNavArgument> = emptyList(),
32+
deepLinks: List<String> = emptyList(),
33+
content: @Composable (BackStackEntry) -> Unit
34+
) {
35+
scene(
36+
route = route,
37+
// arguments = arguments,
38+
deepLinks = deepLinks,
39+
content = content,
40+
)
41+
}
42+
43+
@OptIn(ExperimentalAnimationApi::class)
44+
fun RouteBuilder.modalComposable(
45+
route: String,
46+
// arguments: List<NamedNavArgument> = emptyList(),
47+
deepLinks: List<String> = emptyList(),
48+
content: @Composable (BackStackEntry) -> Unit
49+
) {
50+
scene(
51+
route = route,
52+
// arguments = arguments,
53+
deepLinks = deepLinks,
54+
content = content,
55+
// enterTransition = {
56+
// slideInVertically { it }
57+
// },
58+
// exitTransition = null,
59+
// popEnterTransition = null,
60+
// popExitTransition = {
61+
// slideOutVertically { it }
62+
// },
63+
)
64+
}
65+
66+
fun RouteBuilder.bottomSheet(
67+
route: String,
68+
// arguments: List<NamedNavArgument> = emptyList(),
69+
deepLinks: List<String> = emptyList(),
70+
content: @Composable (BackStackEntry) -> Unit
71+
) {
72+
// bottomSheet(
73+
// route = route,
74+
// // arguments = arguments,
75+
// deepLinks = deepLinks,
76+
// content = content
77+
// )
78+
}
79+
80+
fun RouteBuilder.dialog(
81+
route: String,
82+
// arguments: List<NamedNavArgument> = emptyList(),
83+
deepLinks: List<String> = emptyList(),
84+
content: @Composable (BackStackEntry) -> Unit
85+
) {
86+
dialog(
87+
route = route,
88+
// arguments = arguments,
89+
deepLinks = deepLinks,
90+
content = content,
91+
)
92+
}
93+
94+
fun RouteBuilder.navigation(
95+
route: String,
96+
startDestination: String,
97+
content: @Composable RouteBuilder.(BackStackEntry) -> Unit
98+
) {
99+
// TODO
100+
}

0 commit comments

Comments
 (0)