Skip to content

Commit 6f3e3c5

Browse files
committed
完善库功能
1 parent 82c3eb6 commit 6f3e3c5

File tree

4 files changed

+145
-95
lines changed

4 files changed

+145
-95
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,6 @@ object CollectApp {
6767
allIApplication.forEach {
6868
it.onCreate(application)
6969
}
70+
ModuleRoute.setApplication(application)
7071
}
7172
}

lib-login/src/main/java/com/flyjingfish/login/LoginActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class LoginActivity: AppCompatActivity() {
2121
ModuleRoute.builder("user/UserActivity")
2222
.putValue("params1","lalla")
2323
.putValue("params2",user)
24-
.go(this)
24+
.go()
2525

2626
}
2727
binding.btnGoFragment.setOnClickListener {

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

Lines changed: 42 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package com.flyjingfish.module_communication_route
22

3+
import android.app.Activity
4+
import android.app.Application
35
import android.content.Context
46
import android.content.Intent
57
import android.os.Bundle
6-
import android.os.Parcelable
78
import com.flyjingfish.module_communication_annotation.bean.PathInfo
89
import com.flyjingfish.module_communication_annotation.interfaces.BaseRouterClass
910
import com.flyjingfish.module_communication_route.bean.ClassInfo
10-
import java.io.Serializable
11+
import com.flyjingfish.module_communication_route.utils.putValue
1112

1213
object ModuleRoute {
1314
private val allRouteClass = mutableMapOf<String, BaseRouterClass>()
1415
private val allClazz = mutableMapOf<String, ClassInfo?>()
16+
private var application : Application ?= null
1517

1618
private fun addRouteClass(moduleName: String, routeClazz: BaseRouterClass) {
1719
allRouteClass[moduleName] = routeClazz
@@ -25,6 +27,14 @@ object ModuleRoute {
2527
}
2628
}
2729

30+
fun setApplication(application : Application){
31+
this.application = application
32+
}
33+
34+
fun getApplication():Application?{
35+
return application
36+
}
37+
2838
/**
2939
* 只指定路由页面,这种方式会寻找所有模块匹配的路由信息
3040
*/
@@ -45,100 +55,14 @@ object ModuleRoute {
4555
return this
4656
}
4757
paramsMap[paramName] = (paramsValue as Any)
48-
when (paramsValue) {
49-
is Char -> {
50-
intent.putExtra(paramName, paramsValue)
51-
}
52-
53-
is CharArray -> {
54-
intent.putExtra(paramName, paramsValue)
55-
}
56-
57-
is Byte -> {
58-
intent.putExtra(paramName, paramsValue)
59-
}
60-
61-
is ByteArray -> {
62-
intent.putExtra(paramName, paramsValue)
63-
}
64-
65-
is Short -> {
66-
intent.putExtra(paramName, paramsValue)
67-
}
68-
69-
is ShortArray -> {
70-
intent.putExtra(paramName, paramsValue)
71-
}
72-
73-
is Int -> {
74-
intent.putExtra(paramName, paramsValue)
75-
}
76-
77-
is IntArray -> {
78-
intent.putExtra(paramName, paramsValue)
79-
}
80-
81-
is Long -> {
82-
intent.putExtra(paramName, paramsValue)
83-
}
84-
85-
is LongArray -> {
86-
intent.putExtra(paramName, paramsValue)
87-
}
88-
89-
is Float -> {
90-
intent.putExtra(paramName, paramsValue)
91-
}
92-
93-
is FloatArray -> {
94-
intent.putExtra(paramName, paramsValue)
95-
}
96-
97-
is Double -> {
98-
intent.putExtra(paramName, paramsValue)
99-
}
100-
101-
is DoubleArray -> {
102-
intent.putExtra(paramName, paramsValue)
103-
}
104-
105-
is Boolean -> {
106-
intent.putExtra(paramName, paramsValue)
107-
}
108-
109-
is BooleanArray -> {
110-
intent.putExtra(paramName, paramsValue)
111-
}
112-
113-
is String -> {
114-
intent.putExtra(paramName, paramsValue)
115-
}
116-
117-
is Array<*> -> {
118-
if (paramsValue.isArrayOf<Parcelable>()) {
119-
intent.putExtra(paramName, paramsValue as Array<out Parcelable>)
120-
} else if (paramsValue.isArrayOf<CharSequence>()) {
121-
intent.putExtra(paramName, paramsValue as Array<out CharSequence>)
122-
} else if (paramsValue.isArrayOf<String>()) {
123-
intent.putExtra(paramName, paramsValue as Array<out String>)
124-
}
125-
}
126-
127-
is Bundle -> {
128-
intent.putExtra(paramName, paramsValue)
129-
}
130-
131-
is Serializable -> {
132-
intent.putExtra(paramName, paramsValue)
133-
}
134-
135-
is Parcelable -> {
136-
intent.putExtra(paramName, paramsValue)
137-
}
138-
}
58+
intent.putValue(paramName,paramsValue)
13959
return this
14060
}
14161

62+
fun getBundle():Bundle?{
63+
return intent.extras
64+
}
65+
14266
/**
14367
* 有相关路由页面才会返回 Intent 否则返回 null
14468
*/
@@ -153,16 +77,40 @@ object ModuleRoute {
15377
}
15478

15579
/**
156-
*
80+
* 有相关路由页面才会返回 Intent 否则返回 null
81+
* 需要 [ModuleRoute].[setApplication] 来初始化 application.
82+
*/
83+
fun getIntent(): Intent? {
84+
val app = application
85+
?: throw IllegalArgumentException("请调用 ModuleRoute.setApplication 来初始化 application.")
86+
return getIntent(app as Context)
87+
}
88+
89+
/**
90+
* 跳转页面
15791
*/
15892
fun go(context: Context) {
93+
if (context !is Activity){
94+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
95+
}
96+
15997
val clazzInfo = getClassInfo()
16098
clazzInfo?.goRouterClazz?.goByPath(path, paramsMap, true,clazzInfo.pathInfo) {
16199
intent.setClass(context, clazzInfo.pathInfo.clazz)
162100
context.startActivity(intent)
163101
}
164102
}
165103

104+
/**
105+
* 跳转页面,需要 [ModuleRoute].[setApplication] 来初始化 application.
106+
*/
107+
fun go() {
108+
val app = application
109+
?: throw IllegalArgumentException("请调用 ModuleRoute.setApplication 来初始化 application.")
110+
go(app as Context)
111+
}
112+
113+
166114
/**
167115
* 根据路径信息获取到对应的 [Class] 类
168116
*/
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.flyjingfish.module_communication_route.utils
2+
3+
import android.content.Intent
4+
import android.os.Bundle
5+
import android.os.Parcelable
6+
import java.io.Serializable
7+
8+
fun <T> Intent.putValue(paramName: String, paramsValue: T) {
9+
val intent: Intent = this
10+
when (paramsValue) {
11+
is Char -> {
12+
intent.putExtra(paramName, paramsValue)
13+
}
14+
15+
is CharArray -> {
16+
intent.putExtra(paramName, paramsValue)
17+
}
18+
19+
is Byte -> {
20+
intent.putExtra(paramName, paramsValue)
21+
}
22+
23+
is ByteArray -> {
24+
intent.putExtra(paramName, paramsValue)
25+
}
26+
27+
is Short -> {
28+
intent.putExtra(paramName, paramsValue)
29+
}
30+
31+
is ShortArray -> {
32+
intent.putExtra(paramName, paramsValue)
33+
}
34+
35+
is Int -> {
36+
intent.putExtra(paramName, paramsValue)
37+
}
38+
39+
is IntArray -> {
40+
intent.putExtra(paramName, paramsValue)
41+
}
42+
43+
is Long -> {
44+
intent.putExtra(paramName, paramsValue)
45+
}
46+
47+
is LongArray -> {
48+
intent.putExtra(paramName, paramsValue)
49+
}
50+
51+
is Float -> {
52+
intent.putExtra(paramName, paramsValue)
53+
}
54+
55+
is FloatArray -> {
56+
intent.putExtra(paramName, paramsValue)
57+
}
58+
59+
is Double -> {
60+
intent.putExtra(paramName, paramsValue)
61+
}
62+
63+
is DoubleArray -> {
64+
intent.putExtra(paramName, paramsValue)
65+
}
66+
67+
is Boolean -> {
68+
intent.putExtra(paramName, paramsValue)
69+
}
70+
71+
is BooleanArray -> {
72+
intent.putExtra(paramName, paramsValue)
73+
}
74+
75+
is String -> {
76+
intent.putExtra(paramName, paramsValue)
77+
}
78+
79+
is Array<*> -> {
80+
if (paramsValue.isArrayOf<Parcelable>()) {
81+
intent.putExtra(paramName, paramsValue as Array<out Parcelable>)
82+
} else if (paramsValue.isArrayOf<CharSequence>()) {
83+
intent.putExtra(paramName, paramsValue as Array<out CharSequence>)
84+
} else if (paramsValue.isArrayOf<String>()) {
85+
intent.putExtra(paramName, paramsValue as Array<out String>)
86+
}
87+
}
88+
89+
is Bundle -> {
90+
intent.putExtra(paramName, paramsValue)
91+
}
92+
93+
is Serializable -> {
94+
intent.putExtra(paramName, paramsValue)
95+
}
96+
97+
is Parcelable -> {
98+
intent.putExtra(paramName, paramsValue)
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)