compose-get路由-APT實現(xiàn)compose路由管理

compose是android開發(fā)的未來。

官方的路由框架navigation-animation

目前官方的提供的路由框架已支持進(jìn)出場動畫(僅支持slide類型transition),奈何其聲明與配置步驟較多,出于敏捷開發(fā)的考慮,故想開發(fā)一套類似于Arouter的路由框架。

具體實現(xiàn)如下

image.png

一、get-processor 注解處理器

、、、對apt不熟悉的請自行腦補(bǔ)

該處理器是針對使用了PageRoute 注解標(biāo)注的Compolseable函數(shù)進(jìn)行頁面標(biāo)記,將其作為 Page(頁面)添加到navigation中

具體PageRoute 代碼如下:

@Target(
   AnnotationTarget.FUNCTION,//對函數(shù)生效
)
@Retention(AnnotationRetention.RUNTIME)//不刪除編譯后保留
annotation class PageRoute(
   val route: String,// 該page的路由地址(用過arouter的應(yīng)該都很熟)
   val enter: Animation = Animation.LEFT_IN, // 進(jìn)場動畫
   val exit: Animation = Animation.LEFT_OUT, // 出場動畫
   val popEnter: Animation = Animation.RIGHT_IN,// 推出
   val popExit: Animation = Animation.RIGHT_OUT// 推入
) {
   enum class Animation {
       LEFT_IN,
       LEFT_OUT,
       RIGHT_IN,
       RIGHT_OUT,

       UP_IN,
       UP_OUT,
       DOWN_IN,
       DOWN_OUT,
   }
}

二、 使用

1. 添加注解

@Composable
@PageRoute(route = "Text")
fun MainPage() {
    Text("Main頁面")
}

2.編譯

rebuild 項目即可生成路由集合類擴(kuò)展函數(shù)


image.png

生成擴(kuò)展函數(shù)如下:

@OptIn(ExperimentalAnimationApi::class)
public fun NavGraphBuilder.appComposePages(): Unit {
  definedPage(
      route="Main",
      enter=PageRoute.Animation.LEFT_IN,
      exit=PageRoute.Animation.LEFT_OUT,
      popEnter=PageRoute.Animation.RIGHT_IN,
      popExit=PageRoute.Animation.RIGHT_OUT) {
    MainPage()
  }
}

上訴代碼中擴(kuò)展函數(shù)appComposePages 中的前綴即 模塊名稱,通過對一模塊的build.gradle聲明,在模塊build.gradle頂級添加

kapt {
   arguments {
       arg("moduleName", project.getName())
       includeCompileClasspath = true
       generateStubs = true
   }
}

3.配置多個模塊到主app

1).activity

class MainActivity : ComponentActivity() {
   override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       WindowCompat.setDecorFitsSystemWindows(window, false)
       setContent {
           ComposeGetTheme(darkTheme = false) {
               // A surface container using the 'background' color from the theme
               App()
           }
       }
   }
}

2).App

@OptIn(ExperimentalAnimationApi::class)
@Composable
fun App() {
   GetApp(
       startDestination = "Main" //目標(biāo)首頁 一般為Splash
   ) {
       //添加對應(yīng)模塊的擴(kuò)展函數(shù)
       appComposePages() // 主工程
       // module_aComposePages() 
       // module_bComposePages() 
       // module_cComposePages() 
   }
}

4.使用路由地址跳轉(zhuǎn)

navigator.navigate("xxxx") ///xxxx即在PageRoute注解上的route 地址

詳細(xì)參考Demo

代碼已上傳至mavenCentral 可直接依賴使用

各個模塊build.gradle 添加

dependencies {
    .......
    implementation "io.github.sunshaobei:satis-compose-get:1.0.0"
    implementation "io.github.sunshaobei:satis-compose-get-annotation:1.0.0"
    kapt "io.github.sunshaobei:satis-compose-get-processor:1.0.0"
}
最后編輯于
?著作權(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)容

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