Skip to content

Commit ca21a51

Browse files
committed
完善route功能,增加降级策略
1 parent 7142648 commit ca21a51

File tree

10 files changed

+170
-6
lines changed

10 files changed

+170
-6
lines changed

app/src/main/java/com/flyjingfish/modulecommunication/CollectApp.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ import com.flyjingfish.module_communication_annotation.interfaces.BindClass
1010
import com.flyjingfish.module_communication_intercept.intercept.RouterIntercept
1111
import com.flyjingfish.module_communication_intercept.RouterInterceptManager
1212
import com.flyjingfish.module_communication_route.ModuleRoute
13+
import com.flyjingfish.module_communication_route.lost.RouterLost
14+
import com.flyjingfish.module_communication_route.lost.RouterLostManager
1315

1416
object CollectApp {
1517
private val allRouterIntercept = mutableSetOf<RouterIntercept>()
1618
private val allIApplication = mutableSetOf<IApplication>()
1719
private val allRouteClazz = mutableSetOf<BaseRouterClass>()
20+
private val allRouterLost = mutableSetOf<RouterLost>()
1821

1922
/**
2023
* 这一步才可以收集到所有的拦截器
@@ -57,6 +60,16 @@ object CollectApp {
5760
ImplementClassUtils.addBindClass(sub)
5861
}
5962

63+
/**
64+
* 收集所有的 module 的 @ExposeInterface 共享实现类
65+
*/
66+
@AndroidAopCollectMethod
67+
@JvmStatic
68+
fun collectRouterLost(sub: RouterLost){
69+
Log.e("CollectIntercept","collectRouterLost=$sub")
70+
allRouterLost.add(sub)
71+
}
72+
6073
fun onCreate(application: Application){
6174
Log.e("CollectIntercept","getAllRouterIntercept-size=${allRouterIntercept.size}")
6275
//设置全部的拦截器让其起作用
@@ -67,6 +80,9 @@ object CollectApp {
6780
allIApplication.forEach {
6881
it.onCreate(application)
6982
}
83+
//设置 application 之后可以不用传上下文
7084
ModuleRoute.setApplication(application)
85+
//设置全部降级策略类
86+
RouterLostManager.addAllRouterLost(allRouterLost)
7187
}
7288
}

app/src/main/java/com/flyjingfish/modulecommunication/MainActivity.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import com.flyjingfish.login.LoginActivity
88
import com.flyjingfish.module_communication_intercept.RouterInterceptManager
99
import com.flyjingfish.module_communication_intercept.intercept.InterceptPoint
1010
import com.flyjingfish.module_communication_intercept.intercept.RouterIntercept
11+
import com.flyjingfish.module_communication_route.ModuleRoute
12+
import com.flyjingfish.module_communication_route.bean.NavigationResult
13+
import com.flyjingfish.module_communication_route.callback.OnNavigationBack
1114
import com.flyjingfish.modulecommunication.databinding.ActivityMainBinding
1215
import com.flyjingfish.user.UserActivity
1316

@@ -27,6 +30,12 @@ class MainActivity : ComponentActivity() {
2730
}
2831

2932
binding.btnGoUri.setOnClickListener {
33+
ModuleRoute.builder("/sss/sss").go(object : OnNavigationBack{
34+
override fun onResult(result: NavigationResult) {
35+
Log.e("onLost","--MainActivity--result=${result.found}")
36+
}
37+
38+
})
3039
RouterInterceptManager.addIntercept(object :RouterIntercept{
3140
override fun onIntercept(point: InterceptPoint) {
3241
Log.e("onIntercept","--MainActivity--${point.path},params = ${point.paramsMap},byPath = ${point.byPath}")
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.flyjingfish.login
2+
3+
import android.util.Log
4+
import com.flyjingfish.module_communication_route.lost.LostPoint
5+
import com.flyjingfish.module_communication_route.lost.RouterLost
6+
7+
class LoginLost : RouterLost{
8+
override fun order(): Int {
9+
return 1
10+
}
11+
12+
override fun onLost(lostPoint: LostPoint) {
13+
Log.e("onLost","--LoginLost--")
14+
lostPoint.proceed()
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.flyjingfish.user
2+
3+
import android.util.Log
4+
import com.flyjingfish.module_communication_route.lost.LostPoint
5+
import com.flyjingfish.module_communication_route.lost.RouterLost
6+
7+
class UserLost : RouterLost{
8+
override fun order(): Int {
9+
return 2
10+
}
11+
12+
override fun onLost(lostPoint: LostPoint) {
13+
Log.e("onLost","--UserLost--")
14+
lostPoint.proceed()
15+
}
16+
}

module-communication-route/src/main/java/com/flyjingfish/module_communication_route/ModuleRoute.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import com.flyjingfish.module_communication_annotation.enums.PathType
1414
import com.flyjingfish.module_communication_annotation.interfaces.BaseRouterClass
1515
import com.flyjingfish.module_communication_annotation.interfaces.NewAny
1616
import com.flyjingfish.module_communication_route.bean.ClassInfo
17+
import com.flyjingfish.module_communication_route.bean.NavigationResult
1718
import com.flyjingfish.module_communication_route.callback.OnNavigationBack
19+
import com.flyjingfish.module_communication_route.lost.RouterLostManager
1820
import com.flyjingfish.module_communication_route.utils.Utils
1921
import com.flyjingfish.module_communication_route.utils.putValue
2022
import java.io.Serializable
@@ -141,8 +143,11 @@ object ModuleRoute {
141143
{
142144
add(ParamsInfo("params1",String::class,null,false))
143145
})
144-
145-
onNavigationBack?.onResult(clazzInfo != null,this)
146+
val found = clazzInfo != null
147+
onNavigationBack?.onResult(NavigationResult(found,this,context))
148+
if (!found){
149+
RouterLostManager.notifyRouterLost(context,this)
150+
}
146151
if (clazzInfo != null && clazzInfo.pathInfo.type == PathType.ACTIVITY){
147152
intent.setClass(context, clazzInfo.pathInfo.clazz.java)
148153
if (context !is Activity) {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.flyjingfish.module_communication_route.bean
2+
3+
import android.content.Context
4+
import com.flyjingfish.module_communication_route.ModuleRoute
5+
6+
class NavigationResult(
7+
/**
8+
* 是否找到路由页面
9+
*/
10+
val found: Boolean,
11+
/**
12+
* 跳转页面时构建的 [ModuleRoute.RouteBuilder]
13+
*/
14+
val routeBuilder: ModuleRoute.RouteBuilder,
15+
/**
16+
* 上下文
17+
*/
18+
val context: Context
19+
)
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package com.flyjingfish.module_communication_route.callback
22

3-
import com.flyjingfish.module_communication_route.ModuleRoute
3+
import com.flyjingfish.module_communication_route.bean.NavigationResult
44

55
interface OnNavigationBack {
66
/**
7-
* @param found 是否找到路由页面
8-
* @param routeBuilder 跳转页面时构建的 [ModuleRoute.RouteBuilder]
7+
* @param result 发起路由的结果
98
*/
10-
fun onResult(found:Boolean,routeBuilder: ModuleRoute.RouteBuilder)
9+
fun onResult(result: NavigationResult)
1110
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.flyjingfish.module_communication_route.lost
2+
3+
import android.content.Context
4+
import com.flyjingfish.module_communication_route.ModuleRoute
5+
6+
class LostPoint(
7+
/**
8+
* 上下文
9+
*/
10+
val context: Context,
11+
/**
12+
* 路由页面的 [ModuleRoute.RouteBuilder]
13+
*/
14+
val builder: ModuleRoute.RouteBuilder
15+
) {
16+
internal var hasNext = false
17+
internal lateinit var listener: OnProceedListener
18+
fun proceed() {
19+
if (hasNext) {
20+
listener.onInvoke()
21+
}
22+
}
23+
24+
internal interface OnProceedListener {
25+
fun onInvoke()
26+
}
27+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.flyjingfish.module_communication_route.lost
2+
3+
interface RouterLost {
4+
fun order(): Int
5+
fun onLost(lostPoint: LostPoint)
6+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.flyjingfish.module_communication_route.lost
2+
3+
import android.content.Context
4+
import com.flyjingfish.module_communication_route.ModuleRoute
5+
6+
7+
object RouterLostManager {
8+
private val routerLoses = mutableSetOf<RouterLost>()
9+
10+
fun addRouterLost(routerLost: RouterLost) {
11+
routerLoses.add(routerLost)
12+
val newRouterLoses = routerLoses.sortedBy { it.order() }
13+
routerLoses.clear()
14+
routerLoses.addAll(newRouterLoses)
15+
}
16+
/**
17+
* 添加多个降级处理
18+
*/
19+
fun addAllRouterLost(routerLoses: MutableSet<RouterLost>) {
20+
this.routerLoses.clear()
21+
this.routerLoses.addAll(routerLoses.sortedBy { it.order() })
22+
}
23+
24+
internal fun notifyRouterLost(context: Context,builder: ModuleRoute.RouteBuilder) {
25+
if (routerLoses.isEmpty()) {
26+
return
27+
}
28+
29+
val point = LostPoint(context,builder)
30+
31+
val thisRouterLoses = mutableSetOf<RouterLost>().apply {
32+
addAll(routerLoses)
33+
}
34+
val iterator = thisRouterLoses.iterator()
35+
point.listener = object : LostPoint.OnProceedListener {
36+
override fun onInvoke() {
37+
if (iterator.hasNext()) {
38+
val intercept = iterator.next()
39+
iterator.remove()
40+
point.hasNext = iterator.hasNext()
41+
intercept.onLost(point)
42+
}
43+
}
44+
}
45+
46+
point.hasNext = thisRouterLoses.size > 1
47+
val routerLost = iterator.next()
48+
iterator.remove()
49+
routerLost.onLost(point)
50+
}
51+
}

0 commit comments

Comments
 (0)