Butterfly - 蝴蝶
Butterfly - 小巧而強大的武器,擁有它,讓你的Android開發如虎添翼,Carry全場!
只有最強大和最經驗的勇士才能揮動蝴蝶,但它在戰斗中提供了令人難以置信的靈巧
物品介紹: +30 敏捷 +35% 閃避 +25 攻擊 +30 攻速
功能介紹
蝴蝶通過兩個不同的注解來實現不同的功能:
- Agile 用于頁面導航
- Evade 用于組件化通信
Agile
- 導航
通過給Activity添加Agile注解,并設置對應的scheme,隨后即可通過Butterfly進行導航,或者導航并獲取返回數據
@Agile("test/scheme")
class AgileTestActivity : AppCompatActivity() {
//...
}
//導航
Butterfly.agile("test/scheme").carry()
//導航并獲取返回數據
Butterfly.agile("test/scheme")
.carry {
val result = it.getStringExtra("result")
binding.tvResult.text = result
}
- 傳遞參數
Agile支持附帶參數導航,有兩種方式,一種是通過拼接scheme,將參數添加到scheme中
另一種是通過調用params方法手動傳入參數,或者兩者混合進行, 隨后可在導航后的頁面中獲取對應的參數
//拼接scheme
Butterfly.agile("test/scheme?a=1&b=2").carry()
//調用params
Butterfly.agile("test/scheme?a=1&b=2")
.params("intValue" to 1)
.params("booleanValue" to true)
.params("stringValue" to "test value")
.carry()
- 解析參數
在導航目的頁面,可通過參數的key字段來獲取傳遞的參數值
@Agile("test/scheme")
class AgileTestActivity : AppCompatActivity() {
val a by lazy { intent?.getStringExtra("a") ?: "" }
val b by lazy { intent?.getStringExtra("b") ?: "" }
val intValue by lazy { intent?.getIntExtra("intValue", 0) ?: 0 }
}
除了手動解析參數以外,還可以裝備Bracer來實現全自動進行參數解析
@Agile("test/scheme")
class AgileTestActivity : AppCompatActivity() {
val a by params<String>()
val b by params<String>()
val intValue by params<Int>()
}
Bracer 使用方式詳情見: Github 地址 Bracer
- 攔截器
Agile支持攔截器,可用于在導航前預處理部分邏輯,如進行登錄檢測
此外攔截器中也可進行導航,但為了避免攔截器套娃,需要添加skipInterceptor()方法以忽略攔截器
//實現自定義攔截器
class TestInterceptor : ButterflyInterceptor {
override fun shouldIntercept(agileRequest: AgileRequest): Boolean {
//檢測是否需要攔截
return true
}
override suspend fun intercept(agileRequest: AgileRequest) {
//處理攔截邏輯
println("intercepting")
delay(5000)
println("intercept finish")
}
}
//注冊攔截器
ButterflyCore.addInterceptor(TestInterceptor())
//跳過攔截器
Butterfly.agile("test/scheme").skipInterceptor().carry()
- Action
Agile除了支持頁面導航以外,還支持導航Action,Action無頁面,可進行某些邏輯處理
首先讓自定義的Class繼承Action,然后添加@Agile注解并設置scheme,其余和頁面導航一致
@Agile("test/action")
class TestAction : Action {
override fun doAction(context: Context, scheme: String, data: Bundle) {
//可從data中獲取傳入的參數
Toast.makeText(context, "This is an Action", Toast.LENGTH_SHORT).show()
}
}
//啟動Action
Butterfly.agile("test/action").carry()
//Action同樣支持傳參
Butterfly.agile("test/action?a=1&b=2").carry()
//params 傳參
Butterfly.agile("test/action")
.params("intValue" to 1)
.carry()
- 流程控制
Agile除了直接調用carry導航以外,還可以調用flow返回Flow對象, 利用Flow對象可對導航流程進行處理
Butterfly.agile("test/scheme").flow()
.onStart { println("start") }
.onCompletion { println("complete") }
.onEach { println("process result") }
.launchIn(lifecycleScope)
Evade
蝴蝶使用簡單的兩個注解即可實現任意組件之間進行通信,而組件之間無需任何直接或間接依賴
例如有兩個組件:Module Foo 和Module Bar 需要通信
在Module Foo中,定義接口,并添加Evade注解:
@Evade
interface Home {
//定義方法
fun showHome(fragmentManager: FragmentManager, container: Int)
}
在Module Bar中,定義實現,,并添加EvadeImpl注解:
//實現類名必須以Impl結尾
@EvadeImpl
class HomeImpl {
val TAG = "home_tag"
//實現Home接口中的方法, 方法名和方法參數必須相同
fun showHome(fragmentManager: FragmentManager, container: Int) {
val homeFragment = HomeFragment()
fragmentManager.beginTransaction()
.replace(container, homeFragment, TAG)
.commit()
}
}
由于Evade使用類名稱作為定義和實現關聯的重要依據,因此接口類名和實現類名必須相同,并且實現類名以Impl結尾.
如無法以類名作為關聯,也可使用相同的字符串類型作為關聯key@Evade(identity = "same key") interface Home @EvadeImpl(identity = "same key") class OtherNameImpl
隨后即可在Module Foo中,使用evade方法獲取Home并調用:
val home = Butterfly.evade<Home>()
home.showHome(supportFragmentManager, R.id.container)
除此之外, Evade也支持通過下沉依賴的形式, 進行強關聯類型的通信
例如以下三個組件:公共組件Module Base,Module Foo,Module Bar
首先將Home接口下沉至公共組件Module Base中:
@Evade
interface Home {
fun showHome(fragmentManager: FragmentManager, container: Int)
}
然后在Module Bar中,實現接口:
//同樣需要使用相同的命名規則, 實現類名必須以Impl結尾
@EvadeImpl
class HomeImpl : Home {
val TAG = "home_tag"
override fun showHome(fragmentManager: FragmentManager, container: Int) {
val homeFragment = HomeFragment()
fragmentManager.beginTransaction()
.replace(container, homeFragment, TAG)
.commit()
}
}
之后便可在Module Foo中,使用evade方法獲取Home并調用:
val home = Butterfly.evade<Home>()
home.showHome(supportFragmentManager, R.id.container)
路由表
Butterfly會為每個使用了注解的Module生成一個路由表, 命名規則為: Butterfly[模塊名稱]Module
手動注冊:
class DemoApplication : Application() {
override fun onCreate() {
super.onCreate()
//注冊
ButterflyCore.addModule(ButterflyHomeModule())
ButterflyCore.addModule(ButterflyFooModule())
ButterflyCore.addModule(ButterflyBarModule())
}
}
使用插件自動注冊:
- 添加插件依賴
//使用 plugins DSL:
plugins {
id "io.github.ssseasonnn.butterfly" version "1.0.1"
}
//或者使用legacy plugin application:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "io.github.ssseasonnn:plugin:1.0.1"
}
}
//添加plugin
apply plugin: "io.github.ssseasonnn.butterfly"
- 實現自己的Application類
class DemoApplication : Application() {
override fun onCreate() {
super.onCreate()
}
}
混淆配置
-keep public class zlc.season.butterfly.module.**
-keep public class zlc.season.butterfly.annotation.**
-keep public class zlc.season.butterfly.ButterflyCore {*;}
-keep public class * extends zlc.season.butterfly.Action
-keep @zlc.season.butterfly.annotation.Agile class * {*;}
-keep @zlc.season.butterfly.annotation.Evade class * {*;}
-keep @zlc.season.butterfly.annotation.EvadeImpl class * {*;}
俗話說得好,蝴蝶在手,天下我有! 這么好的裝備你不來一把?