造轮子 -- RxRouter

作者: Season_zlc | 来源:发表于2018-04-11 14:29 被阅读1976次

    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.
    

    相关文章

      网友评论

      • Todo2:组件化和插件化的开发里程总结之一路由
        https://www.jianshu.com/p/df2a6717009d
      • Newamber:想问问您,我的需求是根据时机发送一个对象或者基本类型到某一个组件,例如fragment或者activity中指定的接受方法,以前用的eventbus,不能指定接收者,路由框架是第一次听闻,还不熟悉概念。
      • leonliu2:这个类库 使用java开发的app可以调用吗? 谢谢
        leonliu2:@Season_zlc 我看demo代码都是kotlin写的, 最好能提供一个同等功能的java demo吧, 谢谢 :)
        Season_zlc:@leonliu2 可以
      • 岳麓青石:MainRouterProvider 怎么生成的啊,按步骤继承有错误提示
        Season_zlc:@岳麓青石 一样的呀,你先定义一个被@Router注解的类,编译一下就生成了
        岳麓青石:@Season_zlc 不是kotlin环境,该怎么调用啊?MainRouterProvider 找不到
        Season_zlc:根据@Router注解的类的类名生成的呀
      • f2d6be98e045:如果不同的Activity有不同的配置呢,比如主题和orientation
        f2d6be98e045:@Season_zlc 是没什么关系,如果能解决这个问题会更吊一点
        Season_zlc:这和路由没啥关系吧?
      • 风之丨旅人:老哥,ARoute和RxRouter那个好一些
        Season_zlc:@风之丨旅人 看你觉得哪个方便

      本文标题:造轮子 -- RxRouter

      本文链接:https://www.haomeiwen.com/subject/icaahftx.html