RxRouter

A lightweight, simple, intelligent and powerful Android routing library

Making the address

Getting started

Add the dependent

Add the following dependencies to the build.gradle file:

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

If Kotlin is used, replace the annotationProcessor with kapt

Hello World

First add the @URL annotation to the Activity we want to route:

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

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

@Router
class MainRouter{}Copy the code

This class doesn’t need any additional code. RxRouter automatically generates a RouterProvider based on the class name. For example, MainRouter generates a MainRouterProvider.

We then need to add these routers to the RxRouterProviders:

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

Finally, we can begin our performance:

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

Parameter passing

Jump with parameters:

With the with method, you can add a list of parameters to the 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()
Copy the code

No longer needonActivityResultMethods the

Want to get the value returned by the jump? No more writing a bunch of onActivityResult methods! Chain call, in one step!

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()
            }
        }
Copy the code

If I get a result back, I just subscribe.

The Class jump

Don’t want Url annotations? RxRouter also supports the original class name jump, which is the same as the URL jump:

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()
            }
        }
Copy the code

The Action to jump

Similarly, RxRouter supports system actions and custom Action jumps.

Custom Action jump:

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

System Action jump:

// Make a phone call
RxRouter.of(this)
        .addUri(Uri.parse("tel:123456"))
        .routeSystemAction(Intent.ACTION_DIAL)
        .subscribe()

// Send SMS messages
val bundle = Bundle()
bundle.putString("sms_body"."This is information content.")
RxRouter.of(this)
        .addUri(Uri.parse("smsto:10086"))
        .with(bundle)
        .routeSystemAction(Intent.ACTION_SENDTO)
        .subscribe()
Copy the code

A firewall

RxRouter has a small but powerful firewall that can be blocked 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) {
            "You haven't logged in yet, please log in first.".toast()
            return false
        }
        return true}}// Add the Firewall to the route
RxRouter.of(this)
        .addFirewall(LoginFirewall())
        .route("this is a url")
        .subscribe()
Copy the code

License

Copyright 2018 Season.Zlc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except inThe 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 toin 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.
Copy the code