|
|
@@ -1,24 +1,27 @@
|
|
|
package com.atmob.keyboard_android.component.base.interceptor
|
|
|
|
|
|
+import com.atmob.keyboard_android.component.base.IUIComponent
|
|
|
+
|
|
|
/**
|
|
|
* 路由拦截器管理器
|
|
|
*/
|
|
|
class RouteInterceptorManager {
|
|
|
companion object {
|
|
|
+ /**
|
|
|
+ * 拦截器列表
|
|
|
+ */
|
|
|
private val interceptors = mutableListOf<IRouteInterceptor>()
|
|
|
|
|
|
- init {
|
|
|
- interceptors.add(DefaultRouteInterceptor())
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 注册,路由拦截器
|
|
|
*
|
|
|
* @param interceptor 路由拦截器
|
|
|
*/
|
|
|
fun registerInterceptor(interceptor: IRouteInterceptor) {
|
|
|
- interceptors.add(interceptor)
|
|
|
- interceptor.onInit()
|
|
|
+ if (!interceptors.contains(interceptor)) {
|
|
|
+ interceptors.add(interceptor)
|
|
|
+ interceptor.onInit()
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -33,12 +36,41 @@ class RouteInterceptorManager {
|
|
|
/**
|
|
|
* 执行路由拦截器
|
|
|
*
|
|
|
- * @param callback 拦截器回调
|
|
|
+ * @param targetComponent 要路由到的目标组件
|
|
|
+ * @param finalCallback 最终拦截器回调
|
|
|
*/
|
|
|
- fun executeInterceptors(callback: InterceptorCallback) {
|
|
|
- for (interceptor in interceptors) {
|
|
|
- interceptor.doProcess(callback)
|
|
|
+ fun executeInterceptors(targetComponent: IUIComponent, finalCallback: InterceptorCallback) {
|
|
|
+ // 没有设置拦截器,直接全部放行
|
|
|
+ if (interceptors.isEmpty()) {
|
|
|
+ finalCallback.onContinue()
|
|
|
+ return
|
|
|
}
|
|
|
+
|
|
|
+ // 当前遍历到的拦截器的索引
|
|
|
+ var currentIndex = 0
|
|
|
+
|
|
|
+ // 定义一个内部的拦截器,当前拦截器执行放行,则执行下一个拦截器,类似链表
|
|
|
+ val chainCallback = object : InterceptorCallback {
|
|
|
+ override fun onContinue() {
|
|
|
+ // 当前拦截器放行,累加索引
|
|
|
+ currentIndex++
|
|
|
+ // 查找下一个拦截器,继续执行拦截器逻辑
|
|
|
+ if (currentIndex < interceptors.size) {
|
|
|
+ interceptors[currentIndex].doProcess(targetComponent, this)
|
|
|
+ } else {
|
|
|
+ // 所有拦截器都放行了,执行最终回调
|
|
|
+ finalCallback.onContinue()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onInterrupt(error: Throwable) {
|
|
|
+ // 被拦截器,执行最终回调,通知被拦截了
|
|
|
+ finalCallback.onInterrupt(error)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 执行内部的回调,启动第一个拦截器
|
|
|
+ interceptors[currentIndex].doProcess(targetComponent, chainCallback)
|
|
|
}
|
|
|
}
|
|
|
}
|