DEPRECATION WARNING! This project has been deprecated. Please visit Neuro project as the next generation of URL Router with a lot of performance improvements.
A wrapper for easily routing URL on Android
/*
* Mapping the schema and host
*/
Router.INSTANCE.preMap("*://<subdomain:[a-z]+>.mysite.com/*", {
val subdomain = it.variables.getString("subdomain")
if (subdomain == "blog") {
// Launch intent
...
null // Don't continue to path routing below by returning null
} else {
Uri.parse(it.url).path // Continue to path routing below
}
})
/*
* Mapping the path
*/
/* Simple mapping */
Router.INSTANCE.map("/about", {
...
})
/* Wildcard at the end for any characters until end of url */
Router.INSTANCE.map("/promo/*", {
...
})
/* Wildcard in segment for any character in specific segment */
Router.INSTANCE.map("/promo/*/discounted", {
...
})
/* Get value from parsed queries */
Router.INSTANCE.map("/register", {
val referrer = it.queries.getString("referrer")
...
})
/* Get value from parsed variables in segment of path */
Router.INSTANCE.map("/transaction/<transaction_id>/view", {
val transactionId = it.variables.getLong("transaction_id")
...
})
/* Get value from parsed variables in subsegment of segment */
Router.INSTANCE.map("/product/<product_id:[a-z0-9]+>-*", {
val productId = it.variables.getString("product_id")
...
})
Router.INSTANCE.route(context, url, optionalArgs)
val loginUserRouter = Router()
val nonloginUserRouter = Router()
- Can be placed anywhere except query
- Used for replacing any character inside URL
- Regex:
.+
-> Any character more than 1
- Can be placed anywhere except query
- Used for getting value inside URL
- Variable name can only use
A-Z
,a-z
,0-9
, and_
- Default regex:
[^/]+
-> Any character more than 1 except/
- Default regex can be changed
- Can be placed anywhere except query
- Used for getting value inside URL
- Variable name can only use
A-Z
,a-z
,0-9
, and_
- Default regex:
[^/]+
-> Any character more than 1 except/
- Default regex can be changed
- Variable name and regex separated with
:
- Specific regex overrides default regex
Add this line in your build.gradle
file:
compile 'com.bukalapak:url-router:1.1.6'
This library is hosted in the JCenter repository, so you have to ensure that the repository is included:
buildscript {
repositories {
jcenter()
}
}
Feel free to create issues and pull requests.
URL Router library for Android
Copyright (c) 2017 Bukalapak (http://github.com/bukalapak).
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.