RxRouter

RxRouter

项目地址:ssseasonnn/RxRouter 

简介:A lightweight, simple, smart and powerful Android routing library.

更多:作者   提 Bug   

标签:

 

  Download

Read this in other languages: 中文English

A lightweight, simple, smart and powerful Android routing library.

Getting started

Setting up the dependency

Add to your project build.gradle file:

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

(Please replace x and y and z with the latest version numbers: )

For Kotlin you should use kapt instead of annotationProcessor

Hello World

First add the @url annotation to the activity we need to route:

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

Then create a class that is annotated with @Router to tell RxRouter that there is a router:

@Router
class MainRouter{
}

This class doesn't need to have any remaining code. RxRouter will automatically generate a RouterProviderbased on the class name of the class. For example, MainRouter will generate MainRouterProvider.

Then we need to add these routers to RxRouterProviders:

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

Finally, we can start our performance:

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

Parameters

Routing with parameters:

With the with method, you can add a series of parameters to this route.

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()

No longer needs the onActivityResult method

Want to get the value returned by the route? No longer have to write a bunch of onActivityResult methods! One step in place with RxJava!

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()
            }
        }

If there is a result return, it will be processed in subscribe.

Routing through Class

Do not want to use Url annotations? No problem, RxRouter also supports the original jump of the specified class name, in the same way:

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()
            }
        }

Routing through Action

Similarly, RxRouter also supports system Actions and custom Actions.

Routing through custom 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()
        })

Routing through system action:

//dial
RxRouter.of(this)
        .addUri(Uri.parse("tel:123456"))
        .routeSystemAction(Intent.ACTION_DIAL)
        .subscribe()

//send message
val bundle = Bundle()
bundle.putString("sms_body", "this is message")
RxRouter.of(this)
        .addUri(Uri.parse("smsto:10086"))
        .with(bundle)
        .routeSystemAction(Intent.ACTION_SENDTO)
        .subscribe()

Firewall

The RxRouter has a small and powerful firewall that can be intercepted according to firewall rules before routing. You can add one or more firewalls.

//create a LoginFirewall
class LoginFirewall : Firewall {
    override fun allow(datagram: Datagram): Boolean {
        if (notLogin) {
            "please log in".toast()
            return false
        }
        return true
    }
}

//Add Firewall to Route
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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值