造轮子 -- RxRouter

RxRouter

Github地址

一个轻量级、简单、智能并且强大的安卓路由库

Getting started

添加依赖

在build.gradle文件中添加以下依赖:

dependencies {
    implementation 'zlc.season:rxrouter:x.y.z'
    annotationProcessor 'zlc.season:rxrouter-compiler:x.y.z'
}

(替换上面的 xyz为最新的版本号)

如果使用 Kotlin ,用 kapt 替换 annotationProcessor

Hello World

首先在我们需要路由的Activity上添加 @Url 注解:

@Url("this is a url")
class UrlActivity : AppCompatActivity() {
    ...
}

然后创建一个被 @Router 注解的类,用来告诉RxRouter这里有一个路由器:

@Router
class MainRouter{
}

这个类不需要有任何其余的代码,RxRouter会根据这个类的类名自动生成一个 RouterProvider ,比如这里的 MainRouter 将会生成 MainRouterProvider .

接着我们需要把这些路由器添加到 RxRouterProviders 中:

class CustomApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        RxRouterProviders.add(MainRouterProvider())
    }
}

最后,就可以开始我们的表演了:

RxRouter.of(context)
        .route("this is a uri")
        .subscribe()

参数传递

携带参数跳转:

通过with方法,你可以给本次路由添加一系列参数.

RxRouter.of(context)
        .with(10)                           //int value
        .with(true)                         //boolean value
        .with(20.12)                        //double value
        .with("this is a string value")     //string value
        .with(Bundle())                     //Bundle value
        .route("this is a uri")
        .subscribe()

不再需要 onActivityResult 方法了

想要获取跳转返回的值?再也不用写一大堆 onActivityResult 方法了!链式调用,一步到位!

RxRouter.of(context)
        .with(false)
        .with(2000)
        .with(9999999999999999)
        .route("this is a uri")
        .subscribe {
            if (it.resultCode == Activity.RESULT_OK) {
                val intent = it.data
                val stringResult = intent.getStringExtra("result")
                result_text.text = stringResult
                stringResult.toast()
            }
        }

如果有结果返回,在subscribe中处理就行了.

Class 跳转

不想用Url注解?没问题,RxRouter同样支持原始的指定类名的跳转方式,和url跳转的方式相同:

RxRouter.of(context)
        .routeClass(ClassForResultActivity::class.java)
        .subscribe{
            if (it.resultCode == Activity.RESULT_OK) {
                val intent = it.data
                val stringResult = intent.getStringExtra("result")
                result_text.text = stringResult
                stringResult.toast()
            }
        }

Action 跳转

同样的,RxRouter也支持系统的Action和自定义的Action跳转.

自定义Action跳转:

<activity android:name=".ActionActivity">
    <intent-filter>
        <action android:name="zlc.season.sample.action" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>
RxRouter.of(context)
        .routeAction("zlc.season.sample.action")
        .subscribe({
            "no result".toast()
        }, {
            it.message?.toast()
        })

系统Action跳转:

//拨打电话
RxRouter.of(this)
        .addUri(Uri.parse("tel:123456"))
        .routeSystemAction(Intent.ACTION_DIAL)
        .subscribe()

//发送短信
val bundle = Bundle()
bundle.putString("sms_body", "这是信息内容")
RxRouter.of(this)
        .addUri(Uri.parse("smsto:10086"))
        .with(bundle)
        .routeSystemAction(Intent.ACTION_SENDTO)
        .subscribe()

防火墙

RxRouter拥有一个小巧而强大的防火墙,能够在路由之前根据防火墙的规则进行拦截,您可以添加一个或者多个防火墙.

//创建一个LoginFirewall
class LoginFirewall : Firewall {
    override fun allow(datagram: Datagram): Boolean {
        if (notLogin) {
            "您还没有登录,请先登录".toast()
            return false
        }
        return true
    }
}

//将Firewall添加到路由中
RxRouter.of(this)
        .addFirewall(LoginFirewall())
        .route("this is a url")
        .subscribe()

License

Copyright 2018 Season.Zlc

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.
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,237评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,908评论 18 139
  • 我走着 故乡的历史一页一页翻过 浓墨重彩的每一个场面 指头笨拙的踏进 扔出
    痴大师阅读 231评论 0 3
  • 5月 生在火炉城市重庆 这个月份的天气 如同慢慢在加热的油锅 对于我们这种住宿的学生更是煎熬 走出门口 撒下了大片...
    去年的野马阅读 211评论 0 0