-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: voyager state restoration and web history mode
- Loading branch information
1 parent
1d345c3
commit ca88590
Showing
18 changed files
with
515 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
.../voyager/common/src/dev/programadorthi/routing/voyager/history/VoyagerHistoryAttribute.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package dev.programadorthi.routing.voyager.history | ||
|
||
import dev.programadorthi.routing.core.Routing | ||
import dev.programadorthi.routing.core.application | ||
import dev.programadorthi.routing.core.application.Application | ||
import io.ktor.util.AttributeKey | ||
|
||
internal val VoyagerHistoryModeAttributeKey: AttributeKey<VoyagerHistoryMode> = | ||
AttributeKey("VoyagerHistoryModeAttributeKey") | ||
|
||
internal var Application.historyMode: VoyagerHistoryMode | ||
get() = attributes[VoyagerHistoryModeAttributeKey] | ||
set(value) { | ||
attributes.put(VoyagerHistoryModeAttributeKey, value) | ||
} | ||
|
||
internal var Routing.historyMode: VoyagerHistoryMode | ||
get() = application.historyMode | ||
set(value) { | ||
application.historyMode = value | ||
} |
26 changes: 26 additions & 0 deletions
26
...ration/voyager/common/src/dev/programadorthi/routing/voyager/history/VoyagerHistoryExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package dev.programadorthi.routing.voyager.history | ||
|
||
import androidx.compose.runtime.Composable | ||
import cafe.adriel.voyager.core.screen.Screen | ||
import dev.programadorthi.routing.core.Routing | ||
import dev.programadorthi.routing.core.application.ApplicationCall | ||
|
||
internal expect suspend fun ApplicationCall.platformPush( | ||
routing: Routing, | ||
body: suspend () -> Screen, | ||
) | ||
|
||
internal expect suspend fun ApplicationCall.platformReplace( | ||
routing: Routing, | ||
body: suspend () -> Screen, | ||
) | ||
|
||
internal expect suspend fun ApplicationCall.platformReplaceAll( | ||
routing: Routing, | ||
body: suspend () -> Screen, | ||
) | ||
|
||
internal expect fun ApplicationCall.shouldNeglect(): Boolean | ||
|
||
@Composable | ||
internal expect fun Routing.restoreState(onState: (Any) -> Unit) |
28 changes: 28 additions & 0 deletions
28
...ation/voyager/common/src/dev/programadorthi/routing/voyager/history/VoyagerHistoryMode.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package dev.programadorthi.routing.voyager.history | ||
|
||
/** | ||
* Options that how the web history is controlled | ||
* | ||
* These options affects web application only. Memory will be used by default in other targets | ||
*/ | ||
public enum class VoyagerHistoryMode { | ||
/** | ||
* Hash URLs pattern. E.g: host/#/path | ||
* Each route will have an entry on the browser history. | ||
* To avoid browser history, set neglect = true before routing to a route | ||
*/ | ||
Hash, | ||
|
||
/** | ||
* Traditional URLs pattern. E.g: host/path | ||
* Each route will have an entry on the browser history. | ||
* To avoid browser history, set neglect = true before routing to a route | ||
*/ | ||
Html5, | ||
|
||
/** | ||
* No updates to URL or History stack. | ||
* All route will be neglected. | ||
*/ | ||
Memory, | ||
} |
35 changes: 35 additions & 0 deletions
35
...tion/voyager/common/src/dev/programadorthi/routing/voyager/history/VoyagerHistoryState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package dev.programadorthi.routing.voyager.history | ||
|
||
import dev.programadorthi.routing.core.RouteMethod | ||
import dev.programadorthi.routing.core.application.Application | ||
import dev.programadorthi.routing.core.application.ApplicationCall | ||
import io.ktor.http.parametersOf | ||
import io.ktor.util.toMap | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
internal data class VoyagerHistoryState( | ||
val routeMethod: String, | ||
val name: String, | ||
val uri: String, | ||
val parameters: Map<String, List<String>>, | ||
) | ||
|
||
internal fun VoyagerHistoryState.toCall(application: Application): ApplicationCall { | ||
return ApplicationCall( | ||
application = application, | ||
name = name, | ||
uri = uri, | ||
routeMethod = RouteMethod.parse(routeMethod), | ||
parameters = parametersOf(parameters), | ||
) | ||
} | ||
|
||
internal fun ApplicationCall.toHistoryState(): VoyagerHistoryState { | ||
return VoyagerHistoryState( | ||
routeMethod = routeMethod.value, | ||
name = name, | ||
uri = uri, | ||
parameters = parameters.toMap(), | ||
) | ||
} |
19 changes: 19 additions & 0 deletions
19
integration/voyager/js/src/dev/programadorthi/routing/voyager/VoyagerRoutingExt.js.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package dev.programadorthi.routing.voyager | ||
|
||
import dev.programadorthi.routing.core.Routing | ||
import dev.programadorthi.routing.voyager.history.VoyagerHistoryMode | ||
import dev.programadorthi.routing.voyager.history.historyMode | ||
import dev.programadorthi.routing.voyager.history.popWindowHistory | ||
|
||
internal actual fun Routing.popOnPlatform( | ||
result: Any?, | ||
fallback: () -> Unit, | ||
) { | ||
when (historyMode) { | ||
VoyagerHistoryMode.Memory -> fallback() | ||
else -> popWindowHistory() | ||
} | ||
} | ||
|
||
public actual val Routing.canPop: Boolean | ||
get() = historyMode != VoyagerHistoryMode.Memory || voyagerNavigator.canPop |
Oops, something went wrong.