DoggyZhang 238e55aabd init 5 luni în urmă
..
router-annotation 238e55aabd init 5 luni în urmă
router-api 238e55aabd init 5 luni în urmă
router-compiler 238e55aabd init 5 luni în urmă
CLAUDE.md 238e55aabd init 5 luni în urmă
README.md 238e55aabd init 5 luni în urmă

README.md

功能说明

路由跳转

path

//RouterUri注解,拦截器按定义顺序调用
@RouterUri(path = ["/test"], interceptors = [TestUriInterceptor1::class, TestUriInterceptor2::class])
class TestRouterBindActivity : AppCompatActivity(R.layout.activity_router_test) {
		
  	//BindExtra注解字段必须为public
    @BindExtra(name = "extra_test")
    var extraTest: String? = null

    private val binding by viewBinding(ActivityRouterTestBinding::inflate)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Router.bind(this) //Activity必须手动调用才会自动解析BindExtra
    }

}

//Router跳转
Router.build(context, "/test")
      .putExtra("extra_test", "测试extra")
      .start()

scheme + host

//scheme和host必须同时设置
@RouterUri(
    scheme = "weparty",
    host = "test",
    interceptors = [TestUriInterceptor1::class, TestUriInterceptor2::class]
)
class TestUriHandler : UriHandler() {

  	//BindExtra注解字段必须为public,不需要调用bind会自动绑定
    @BindExtra(name = "extra_test")
    var extraTest: String? = null

    override fun canHandle(request: UriRequest): Boolean {
        return true
    }

    override fun handle(request: UriRequest) {

    }

}

//Router跳转
Router.build(context, "weparty://test")
      .putExtra("extra_test", "测试extra")
      .start()

scheme + host + path

//scheme和host必须同时设置
@RouterUri(
    scheme = "weparty",
    host = "test",
  	path = ["/path1", "/path2"],
    interceptors = [TestUriInterceptor1::class, TestUriInterceptor2::class]
)
class TestUriHandler : UriHandler() {

  	//BindExtra注解字段必须为public,不需要调用bind会自动绑定
    @BindExtra(name = "extra_test")
    var extraTest: String? = null

    override fun canHandle(request: UriRequest): Boolean {
        return true
    }

    override fun handle(request: UriRequest) {

    }

}

//Router跳转
Router.build(context, "weparty://test/path1")
      .putExtra("extra_test", "测试extra")
      .start()
Router.build(context, "weparty://test/path2")
      .putExtra("extra_test", "测试extra")
      .start()

regex

regex优先级低于sheme+host+path,即优先匹配scheme+host+path

//regex定义正则表达式,priority定义优先级,数字越大优先级越高
@RouterUri(
    regex = "http(s)?://(.*\\.)?(weparty)\\.(com|info|cn).*",
    priority = 2
)
class WebViewActivity : AppCompatActivity()

//Router跳转
Router.build(context, "https://weparty.com/test")
      .putExtra("extra_test", "测试extra")
      .start()

拦截器

//拦截器在触发handler之前执行
class TestUriInterceptor1  : UriInterceptor {

    override fun intercept(chain: UriInterceptor.Chain) {
      	val request = chain.request()
        chain.proceed(request) //继续请求
      	chain.abort() //终止请求
    }

}

DeepLink支持

//注册RouterDeepLinkActivity
<activity
    android:name="com.adealink.frame.router.RouterDeepLinkActivity"
    android:configChanges="keyboardHidden|orientation|screenSize">

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="weparty" />
    </intent-filter>

</activity>

//触发deeplink,会执行TestUriHandler
adb shell am start -W -a android.intent.action.VIEW -d "weparty://test" com.adealink.weparty

获取实例

@RouterUri(path = ["/bind_fragment"], desc = "测试绑定Fragment")
class BindFragment : Fragment() {

    @BindExtra(name = "extra_test")
    var extraTest: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Router.bind(this)
    }

}

//获取实例
val fragment = Router.getRouterInstance<Fragment>("/bind_fragment")