Skip to content

Instantly share code, notes, and snippets.

@chuross
Last active Mar 22, 2018
Embed
What would you like to do?
val router = MoriRouter.from(fm, R.id.container)
router.pop()
router.hoge().start()
router.hoge(fuga).start()
router.hoge(fuga).piyo(foo).start()
// SharedElement support
router.hoge().shareFooImage(imageView).start()
router.dispatch(Uri.parse("example://foo/1"))
-----
@RouterPath(
name = "hoge",
uri = "example://foo/{id}",
transitionNames = *arrayOf("fooImage"),
enterTransitionFactory = ExplodeSetFactory.class,
exitTransitionFactory = ExplodeSetFactory.class
)
class HogeFragment
@RouterParam
lateinit var fuga: Fuga
@RouterParam(name = "piyo", required = false)
var piyo: String? = null
@RouterPathParam(name = "id")
var fooId: String? = null
router.dispatch(Uri.parse("morirouter://second/1"))
---
val dispatcher: UriDispatcher = UriDispatcher(this) // this = MoriRouter
fun dispatch(uri: Uri) = dispatcher.dispatch(uri)
---
class UriDispatcher
val launchers = listOf(
{RouterPath}UriLauncer(router)
...
)
launchers.forEach {
if (!it.isAvailable(uri)) return@forEach
it.launch(uri)
}
---
class {RouterPath}UriLauncher
private val router: MoriRouter
private val format: String = "morirouter://foo/{id}" //使わなそう
private val uriRegex: Regex = "morirouter://foo/([^\/]+)\/?$"
fun isAvailable(uri: Uri): Boolean = uriRegex.matches(uri.toString())
fun launch(uri: Uri) {
if (!isAvailable(uri)) throw IllegalArgumentExeption("error!!")
val groupValues = uriRegex.find(uri.toString())?.groupValues ?: throw IllegalStateExeption("generate regex error. please pull request!")
val id = groupValues[1]
router.{RouterPath.name}().id(id).launch()
}
---
val format: String = "morirouter://foo/{id}"
val uriRegex: Regex = format.replace("""\{[0-9a-zA-Z_-]\}""".toRegex(), "(.+)").toRegex()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment