https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
j

joney

11/16/2023, 2:51 PM
Im looking for a RFC 6570 implementation to expand and and extract URI parameters similar to this php lib. The use case is matching deep links in native apps and extract uri-path and -query parameters from those.
c

Casey Brooks

11/16/2023, 3:49 PM
Are you strictly limited to the format in RFC 6570? Most navigation libs should have some support for handling deep links on their own, if you are able to choose the format used for the URL template
j

joney

11/16/2023, 5:53 PM
The navigation library (voyager) which we are using does not really support deeplinks. We'd want to share code for resolving uris to objets (or simple
Map<String, List<String>>
) between multiple platforms (ios/jvmAndroid).
c

Casey Brooks

11/16/2023, 6:16 PM
Ballast Navigation uses URLs for routing, rather than objects, so is suited for handling deep-links out-of-the-box. It’s definitely a much more barebones option than Voyager which intentionally does not depend on Compose, but you may find it to be a better navigation option for your application. You can also use it without the full navigation and backstack management to just parse URLs, with something like this:
Copy code
enum class AppScreens(
    routeFormat: String,
    override val annotations: Set<RouteAnnotation> = emptySet(),
) : Route {
    Home("/app/home"),
    PostList("/app/posts?sort={?}"),
    PostDetails("/app/posts/{postId}"),
    ;

    override val matcher: RouteMatcher = RouteMatcher.create(routeFormat)

    companion object {
        fun handleDeepLink(deepLinkUrl: String) {
            val routingTable = RoutingTable.fromEnum(AppScreens.values())

            val destination: Destination<AppScreens> = routingTable.findMatch(
                UnmatchedDestination.parse(deepLinkUrl),
            )

            if (destination is Destination.Match<AppScreens>) {
                when (destination.originalRoute) {
                    Home -> {
                    }

                    PostList -> {
                        val sort: String? by destination.optionalStringQuery()
                        // do something with the sort parameter in the PostList route query string, if it was provided
                    }

                    PostDetails -> {
                        val postId: Int by destination.intPath()
                        // do something with the postId in the PostDetails route path
                    }
                }
            }
        }
    }
}
👀 1
2 Views