Skip to content
Sam Hindmarsh edited this page Mar 26, 2019 · 1 revision

Plunge is intended to simplify the matching of URLs to callbacks when a deep link is accepted by the application. This module defines a new domain-specific way of matching URLS, and extracting useful information required to link the user to the right place.

Plunge provides a simple method of defining which URLs you want to handle, and which information you want to extract. It then (optionally) includes a unified method of testing your deep linking logic, as well as if your app successfully catches URLs from a system level.

Getting Started

There's two parts to matching a URL: checking the host matches, and checking the path matches. To define a simple match on a URL (https://plunge.example.com/submarines for example), we must define a UrlSchemeHandler. An implementation may look like this (adapted from the README):

class PlungeExampleSchemeHandler(private val router: DeepLinkRouter): UrlSchemeHandler() {
    override fun hostMatches(host: String): Boolean = host.contains("plunge.example.com")
 
    override val matchers by patterns {
        pattern("/submarines") { result -> router.launchSubmarinesPage() }
    }
}

In this simple example, we've managed to condense the matching on the /submarines path and invoking the router into a single line. The pattern matching language this library implements can do more than just match simple paths, however - see The Pattern Language for more information.

To set up and perform matching, all you need to do is construct an instance of DeepLinkHandler and call the processUri function. An example of this can be seen below:

val linkHandler = DeepLinkHandler.withSchemeHandlers(
  PlungeExampleSchemeHandler(this)
)
 
fun onDeepLinkCaught() {
  val link = Uri.parse(getIntent().getData())
 
  val handled = linkHandler.processUri(link)
  if (!handled) {
      // ... Some kind of default fallback
  }
}

If the DeepLinkHandler finds a match in any of the UrlSchemeHandlers you've defined, it'll call the associated lambda and return true.

Clone this wiki locally