Skip to content

Commit 8a16718

Browse files
feat: voyager module routing function improvements
1 parent 212a675 commit 8a16718

20 files changed

+198
-523
lines changed

core-stack/common/src/dev/programadorthi/routing/core/StackManager.kt

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/*
2-
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3-
*/
4-
51
package dev.programadorthi.routing.core
62

73
import dev.programadorthi.routing.core.application.Application

core-stack/common/src/dev/programadorthi/routing/core/StackRegexRouting.kt

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/*
2-
* Copyright 2014-2023 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3-
*/
4-
51
package dev.programadorthi.routing.core
62

73
import dev.programadorthi.routing.core.application.ApplicationCall

core-stack/common/src/dev/programadorthi/routing/core/StackRouting.kt

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/*
2-
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3-
*/
4-
51
package dev.programadorthi.routing.core
62

73
import io.ktor.http.Parameters

core-stack/common/src/dev/programadorthi/routing/core/StackRoutingBuilder.kt

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/*
2-
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3-
*/
4-
51
package dev.programadorthi.routing.core
62

73
import dev.programadorthi.routing.core.application.ApplicationCall

resources-stack/common/src/dev/programadorthi/routing/resources/StackResourceRouting.kt

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/*
2-
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3-
*/
4-
51
package dev.programadorthi.routing.resources
62

73
import dev.programadorthi.routing.core.Route

voyager/common/src/dev/programadorthi/routing/voyager/VoyagerCallHook.kt

-20
This file was deleted.

voyager/common/src/dev/programadorthi/routing/voyager/VoyagerEventManager.kt

-40
This file was deleted.

voyager/common/src/dev/programadorthi/routing/voyager/VoyagerEventManagerAttribute.kt

-27
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package dev.programadorthi.routing.voyager
2+
3+
import cafe.adriel.voyager.core.screen.Screen
4+
import cafe.adriel.voyager.navigator.Navigator
5+
import dev.programadorthi.routing.core.application.Application
6+
import kotlinx.coroutines.CoroutineScope
7+
import kotlin.coroutines.CoroutineContext
8+
9+
internal class VoyagerNavigatorManager(
10+
private val application: Application,
11+
val initialUri: String,
12+
) : CoroutineScope {
13+
14+
override val coroutineContext: CoroutineContext
15+
get() = application.coroutineContext
16+
17+
internal var navigator: Navigator? = null
18+
19+
fun pop() {
20+
val nav = navigator ?: navigator?.parent ?: return
21+
nav.pop()
22+
}
23+
24+
fun popUntil(predicate: (Screen) -> Boolean) {
25+
val nav = navigator ?: navigator?.parent ?: return
26+
nav.popUntil(predicate)
27+
}
28+
29+
fun push(screen: Screen) {
30+
val nav = navigator ?: navigator?.parent ?: return
31+
nav.push(screen)
32+
}
33+
34+
fun replace(screen: Screen, replaceAll: Boolean) {
35+
val nav = navigator ?: navigator?.parent ?: return
36+
when {
37+
replaceAll -> nav.replaceAll(screen)
38+
else -> nav.replace(screen)
39+
}
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package dev.programadorthi.routing.voyager
2+
3+
import dev.programadorthi.routing.core.application.Application
4+
import dev.programadorthi.routing.core.application.ApplicationCall
5+
import io.ktor.util.AttributeKey
6+
import io.ktor.util.Attributes
7+
8+
private val VoyagerNavigatorManagerAttributeKey: AttributeKey<VoyagerNavigatorManager> =
9+
AttributeKey("VoyagerNavigatorManager")
10+
11+
private var Attributes.voyagerNavigatorManager: VoyagerNavigatorManager
12+
get() = get(VoyagerNavigatorManagerAttributeKey)
13+
private set(value) {
14+
put(VoyagerNavigatorManagerAttributeKey, value)
15+
}
16+
17+
internal var Application.voyagerNavigatorManager: VoyagerNavigatorManager
18+
get() = attributes.voyagerNavigatorManager
19+
set(value) {
20+
attributes.voyagerNavigatorManager = value
21+
}
22+
23+
internal var ApplicationCall.voyagerNavigatorManager: VoyagerNavigatorManager
24+
get() = application.voyagerNavigatorManager
25+
set(value) {
26+
application.voyagerNavigatorManager = value
27+
}

voyager/common/src/dev/programadorthi/routing/voyager/VoyagerNavigatorPlugin.kt

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package dev.programadorthi.routing.voyager
22

3-
import dev.programadorthi.routing.core.StackRouteMethod
43
import dev.programadorthi.routing.core.StackRouting
54
import dev.programadorthi.routing.core.application.ApplicationPlugin
65
import dev.programadorthi.routing.core.application.createApplicationPlugin
@@ -14,19 +13,12 @@ public val VoyagerNavigator: ApplicationPlugin<VoyagerNavigatorConfig> = createA
1413
"VoyagerNavigator",
1514
::VoyagerNavigatorConfig,
1615
) {
17-
application.install(StackRouting)
16+
application.install(StackRouting) // Helps to track last pushed path
1817

19-
application.voyagerEventManager = VoyagerEventManager(
20-
coroutineContext = application.coroutineContext,
18+
application.voyagerNavigatorManager = VoyagerNavigatorManager(
19+
application = application,
2120
initialUri = pluginConfig.initialUri,
2221
)
23-
24-
// Intercepts all call, check for a pop and emit to [VoyagerEventManager]
25-
on(VoyagerCallHook) { call ->
26-
if (call.routeMethod == StackRouteMethod.Pop) {
27-
call.voyagerEventManager.pop()
28-
}
29-
}
3022
}
3123

3224
@KtorDsl
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,10 @@
1-
/*
2-
* Copyright 2014-2023 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3-
*/
4-
51
package dev.programadorthi.routing.voyager
62

73
import dev.programadorthi.routing.core.Route
84
import dev.programadorthi.routing.core.application.ApplicationCall
95
import dev.programadorthi.routing.core.route
10-
import io.ktor.util.KtorDsl
11-
12-
@KtorDsl
13-
public fun Route.pushScreen(
14-
path: Regex,
15-
body: VoyagerPipelineInterceptor<Unit, ApplicationCall>,
16-
): Route = route(path = path) {
17-
pushScreen(body)
18-
}
19-
20-
@KtorDsl
21-
public fun Route.replaceScreen(
22-
path: Regex,
23-
body: VoyagerPipelineInterceptor<Unit, ApplicationCall>,
24-
): Route = route(path = path) {
25-
replaceScreen(body)
26-
}
27-
28-
@KtorDsl
29-
public fun Route.replaceAllScreen(
30-
path: Regex,
31-
body: VoyagerPipelineInterceptor<Unit, ApplicationCall>,
32-
): Route = route(path = path) {
33-
replaceAllScreen(body)
34-
}
356

36-
@KtorDsl
37-
public fun Route.handleScreen(
7+
public fun Route.screen(
388
path: Regex,
399
body: VoyagerPipelineInterceptor<Unit, ApplicationCall>,
40-
): Route = route(path) {
41-
pushScreen(body)
42-
replaceScreen(body)
43-
replaceAllScreen(body)
44-
// By default, there is reason to pop handle return a Screen
45-
}
10+
): Route = route(path) { handle(body) }

voyager/common/src/dev/programadorthi/routing/voyager/VoyagerRouteEvent.kt

-46
This file was deleted.

voyager/common/src/dev/programadorthi/routing/voyager/VoyagerRouter.kt

+4-11
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package dev.programadorthi.routing.voyager
22

33
import androidx.compose.runtime.Composable
44
import androidx.compose.runtime.CompositionLocalProvider
5-
import androidx.compose.runtime.LaunchedEffect
6-
import androidx.compose.runtime.collectAsState
7-
import androidx.compose.runtime.getValue
5+
import androidx.compose.runtime.SideEffect
86
import androidx.compose.runtime.remember
97
import cafe.adriel.voyager.core.screen.Screen
108
import cafe.adriel.voyager.navigator.CurrentScreen
@@ -18,18 +16,13 @@ public fun VoyagerRouter(
1816
initialScreen: Screen = VoyagerEmptyScreen(),
1917
) {
2018
val manager = remember(router) {
21-
router.application.voyagerEventManager
19+
router.application.voyagerNavigatorManager
2220
}
2321

2422
CompositionLocalProvider(VoyagerLocalRouter provides router) {
2523
Navigator(initialScreen) { navigator ->
26-
val event by manager.navigation.collectAsState()
27-
28-
LaunchedEffect(key1 = event) {
29-
event.execute(navigator)
30-
if (event !is VoyagerRouteEvent.Idle) {
31-
manager.clearEvent()
32-
}
24+
SideEffect {
25+
manager.navigator = navigator
3326
}
3427

3528
CurrentScreen()

0 commit comments

Comments
 (0)