造輪子 -- RxRouter

RxRouter

Github地址

一個輕量級、簡單、智能并且強大的安卓路由庫

Getting started

添加依賴

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

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

(替換上面的 x 、 yz為最新的版本號)

如果使用 Kotlin ,用 kapt 替換 annotationProcessor

Hello World

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

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

然后創(chuàng)建一個被 @Router 注解的類,用來告訴RxRouter這里有一個路由器:

@Router
class MainRouter{
}

這個類不需要有任何其余的代碼,RxRouter會根據(jù)這個類的類名自動生成一個 RouterProvider ,比如這里的 MainRouter 將會生成 MainRouterProvider .

接著我們需要把這些路由器添加到 RxRouterProviders 中:

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

最后,就可以開始我們的表演了:

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

參數(shù)傳遞

攜帶參數(shù)跳轉(zhuǎn):

通過with方法,你可以給本次路由添加一系列參數(shù).

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 方法了

想要獲取跳轉(zhuǎn)返回的值?再也不用寫一大堆 onActivityResult 方法了!鏈?zhǔn)秸{(diào)用,一步到位!

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

如果有結(jié)果返回,在subscribe中處理就行了.

Class 跳轉(zhuǎn)

不想用Url注解?沒問題,RxRouter同樣支持原始的指定類名的跳轉(zhuǎn)方式,和url跳轉(zhuǎn)的方式相同:

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 跳轉(zhuǎn)

同樣的,RxRouter也支持系統(tǒng)的Action和自定義的Action跳轉(zhuǎn).

自定義Action跳轉(zhuǎn):

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

系統(tǒng)Action跳轉(zhuǎn):

//撥打電話
RxRouter.of(this)
        .addUri(Uri.parse("tel:123456"))
        .routeSystemAction(Intent.ACTION_DIAL)
        .subscribe()

//發(fā)送短信
val bundle = Bundle()
bundle.putString("sms_body", "這是信息內(nèi)容")
RxRouter.of(this)
        .addUri(Uri.parse("smsto:10086"))
        .with(bundle)
        .routeSystemAction(Intent.ACTION_SENDTO)
        .subscribe()

防火墻

RxRouter擁有一個小巧而強大的防火墻,能夠在路由之前根據(jù)防火墻的規(guī)則進行攔截,您可以添加一個或者多個防火墻.

//創(chuàng)建一個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.
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,323評論 25 708
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,688評論 19 139
  • 我走著 故鄉(xiāng)的歷史一頁一頁翻過 濃墨重彩的每一個場面 指頭笨拙的踏進 扔出
    癡大師閱讀 243評論 0 3
  • 5月 生在火爐城市重慶 這個月份的天氣 如同慢慢在加熱的油鍋 對于我們這種住宿的學(xué)生更是煎熬 走出門口 撒下了大片...
    去年的野馬閱讀 223評論 0 0

友情鏈接更多精彩內(nèi)容