1 Star 2 Fork 0

你需要一台永动机 / Metro

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Metro

License API
🚇地铁 一款简单的Kotlin路由库

用法

初始化

//在Application中
Metro.init(this)

添加站点

注意:站点现支持Activity,Service,Fragment

  1. 注解方式
//在对应的类上增加注解
@Station("/main")
class MainActivity{}
  1. 代码方式
MetroMap.addStation("/main", MainActivity::class.java)

跳转

//跳转到Activity
Metro.with(this)
    .path("/main")
    .put("currIndex", 1)
    .go()
    
//跳转到Service
Metro.with(this)
    .path("/main")
    .serviceLauncher()
    .go()

//跳转到Fragment
Metro.with(this)
    .path("/main")
    .fragmentLauncher()
    .go()

错误回调

Metro.with(this)
    .path("/main")
    .fail {
        Log.e("metro", it.toString())
    }
    .go()

拦截器

//增加中转站(类似拦截器)
MetroMap.addTransferStation(object : Transfer {

    override fun run(chain: Transfer.Chain): Ticket {
        val ticket = chain.ticket()
        Logger.d("目的路径 = ${ticket.path}")
        return chain.proceed(ticket)
    }
})
//登录拦截器
class UserAuthTransfer : Transfer {

    override fun transfer(chain: Transfer.Chain): Ticket {
        val ticket = chain.ticket()
        if (needLogin(ticket.path)) {
            ticket.clear()
            ticket.path = "/account/login"
            ticket.overridePendingTransition(R.anim.slide_in_from_bottom, R.anim.noting)
        }
        return chain.proceed(ticket)
    }

    private fun needLogin(path: String) = when {
        path.isBlank() -> false
        //增加需要校验登录权限的页面
        path.startsWith("/main") -> true
        else -> false
    }

}

获取依赖

project的build.gradle

allprojects {
    repositories {
        ......       
        maven { url "https://jitpack.io" } 
    }
}

具体模块的build.gradle,例如app.gradle

注意:默认使用kotlin1.3.60版本的库

//Kapt插件,必须!
apply plugin: 'kotlin-kapt'

//生成的路由配置类为UUID命名,若要让生成路由配置类明确模块,可加入以下操作
kapt {
    arguments {
        arg("metroModuleName", project.getName())
    }
}

//常规依赖
dependencies {
    //库本体只要在底层模块依赖一次即可
    implementation 'com.github.caoyanglee.Metro:lib:{latestVersion}'

    //在需要路由功能的模块下,使用注解处理器
    kapt 'com.github.caoyanglee.Metro:compiler:{latestVersion}'
}

优化建议

在Application中的Metro.init(this),默认会加载所有路由配置文件,因为是自动扫描所以是耗时操作,我们建议做以下优化

Metro.init(
    context = this,
    autoLoadConfigClass = false //不在使用自动加载
    )
//指定加载路由配置文件,速度快
Metro.loadConfigClass("MetroRoute_xxx")

PS:路由配置文件生成的位置在各个moduel的build/generated/source/kaptKotlin/debug中,每个配置类对应的包名都是com.pmm.metro.route,唯一不同的是类名。

MIT License Copyright (c) 2020 你需要一台永动机 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Metro,A Simple Kotlin Route Lib. 地铁,一款简单的Kotlin路由库。 展开 收起
Kotlin
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Kotlin
1
https://gitee.com/caoyanglee/Metro.git
git@gitee.com:caoyanglee/Metro.git
caoyanglee
Metro
Metro
master

搜索帮助