造輪子 -- RxRouter

weixin_34402408發表於2018-04-11

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.

相關文章