Skip to content

Commit d41181b

Browse files
committed
完善库功能
1 parent 0e5ea1c commit d41181b

File tree

7 files changed

+127
-1
lines changed

7 files changed

+127
-1
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.flyjingfish.modulecommunication
33
import android.content.Context
44
import android.content.Intent
55
import android.os.Bundle
6+
import android.util.Log
67
import androidx.activity.ComponentActivity
78
import com.flyjingfish.login.LoginActivity
89
import com.flyjingfish.modulecommunication.databinding.ActivityMainBinding
@@ -22,5 +23,7 @@ class MainActivity : ComponentActivity() {
2223
binding.btnGoUser.setOnClickListener {
2324
startActivity(Intent(this,UserActivity::class.java))
2425
}
26+
27+
Log.e("MainActivity","------${Int::class.qualifiedName}")
2528
}
2629
}

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ symbol-processing-gradle-plugin = { group = "com.google.devtools.ksp", name = "s
3232
androidAop-core = { group = "io.github.FlyJingFish.AndroidAop", name = "android-aop-core" ,version.ref = "androidAopVersion" }
3333
androidAop-annotation = { group = "io.github.FlyJingFish.AndroidAop", name = "android-aop-annotation" ,version.ref = "androidAopVersion" }
3434
androidAop-ksp = { group = "io.github.FlyJingFish.AndroidAop", name = "android-aop-ksp" ,version.ref = "androidAopVersion" }
35+
kotlin-reflect = { group = "org.jetbrains.kotlin", name = "kotlin-reflect" ,version.ref = "org-jetbrains-kotlin-android" }
3536
[plugins]
3637
org-jetbrains-kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "org-jetbrains-kotlin-jvm" }
3738
com-android-library = { id = "com.android.library", version.ref = "agp" }
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.flyjingfish.user;
2+
3+
import androidx.appcompat.app.AppCompatActivity;
4+
5+
import com.flyjingfish.module_communication_annotation.Route;
6+
import com.flyjingfish.module_communication_annotation.RouteParams;
7+
8+
@Route(path = "user/UserActivity2")
9+
public class UserActivity2 extends AppCompatActivity {
10+
@RouteParams(name = "age")
11+
private int age;
12+
}

module-communication-annotation/src/main/java/com/flyjingfish/module_communication_annotation/bean/PathInfo.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.flyjingfish.module_communication_annotation.bean
22

3-
class PathInfo(val path: String, val clazz: Class<*>, val tag: Int,paramsInfo: MutableList<ParamsInfo>){
3+
class PathInfo(val path: String, val clazz: Class<*>, val tag: Int,val paramsInfo: MutableList<ParamsInfo>){
44

55
/**
66
* 当前页面的 tag 是否存在 [item]

module-communication-route/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ apply("$rootDir/gradle/android_base.gradle")
1414
apply("$rootDir/gradle/android_publish.gradle")
1515
dependencies {
1616
implementation(project(mapOf("path" to ":module-communication-annotation")))
17+
implementation(libs.gson)
1718
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ import android.app.Activity
44
import android.app.Application
55
import android.content.Context
66
import android.content.Intent
7+
import android.net.Uri
78
import android.os.Bundle
9+
import com.flyjingfish.module_communication_annotation.bean.ParamsInfo
810
import com.flyjingfish.module_communication_annotation.bean.PathInfo
911
import com.flyjingfish.module_communication_annotation.interfaces.BaseRouterClass
1012
import com.flyjingfish.module_communication_route.bean.ClassInfo
13+
import com.flyjingfish.module_communication_route.utils.Utils
1114
import com.flyjingfish.module_communication_route.utils.putValue
1215

1316
object ModuleRoute {
@@ -46,6 +49,24 @@ object ModuleRoute {
4649
*/
4750
fun builder(moduleName: String, path: String) = RouteBuilder(path,moduleName)
4851

52+
/**
53+
* 自动解析页面跳转信息
54+
*/
55+
fun builder(uri: Uri) :RouteBuilder? {
56+
val path = uri.path
57+
return if (path != null){
58+
val builder = builder(path)
59+
val pathInfo = builder.getInfoByPath()
60+
if (pathInfo != null){
61+
Utils.putPathParams(pathInfo,uri,builder)
62+
builder
63+
}else{
64+
null
65+
}
66+
}else null
67+
}
68+
69+
4970
class RouteBuilder(private val path: String, private val moduleName: String? = null) {
5071
private val intent = Intent()
5172
private val paramsMap = mutableMapOf<String, Any?>()
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.flyjingfish.module_communication_route.utils
2+
3+
import android.net.Uri
4+
import android.text.TextUtils
5+
import com.flyjingfish.module_communication_annotation.bean.ParamsInfo
6+
import com.flyjingfish.module_communication_annotation.bean.PathInfo
7+
import com.flyjingfish.module_communication_route.ModuleRoute
8+
import com.google.gson.Gson
9+
import java.util.Collections
10+
11+
internal object Utils {
12+
/**
13+
* 拆分查询参数
14+
*
15+
* @param rawUri raw uri
16+
* @return map with params
17+
*/
18+
fun splitQueryParameters(rawUri: Uri): Map<String, String?> {
19+
val query = rawUri.encodedQuery
20+
?: return emptyMap()
21+
val paramMap: MutableMap<String?, String> = LinkedHashMap()
22+
var start = 0
23+
do {
24+
val next = query.indexOf('&', start)
25+
val end = if (next == -1) query.length else next
26+
var separator = query.indexOf('=', start)
27+
if (separator > end || separator == -1) {
28+
separator = end
29+
}
30+
val name = query.substring(start, separator)
31+
if (!TextUtils.isEmpty(name)) {
32+
val value = if (separator == end) "" else query.substring(separator + 1, end)
33+
paramMap[Uri.decode(name)] = Uri.decode(value)
34+
}
35+
36+
// Move start to end of name.
37+
start = end + 1
38+
} while (start < query.length)
39+
return Collections.unmodifiableMap(paramMap)
40+
}
41+
fun putPathParams(pathInfo: PathInfo, rawUri: Uri, builder : ModuleRoute.RouteBuilder) {
42+
val paramsInfo: MutableList<ParamsInfo> = pathInfo.paramsInfo
43+
if (paramsInfo.isNotEmpty()) {
44+
val resultMap: Map<String, String?> = Utils.splitQueryParameters(rawUri)
45+
for (params in paramsInfo) {
46+
val paramValue = resultMap[params.name]
47+
paramValue?.let {
48+
putValue(params,it,builder)
49+
}
50+
}
51+
}
52+
}
53+
private fun putValue(paramsInfo: ParamsInfo,strValue : String,builder: ModuleRoute.RouteBuilder){
54+
when(paramsInfo.className){
55+
String::class.qualifiedName ,Char::class.qualifiedName->{
56+
builder.putValue(paramsInfo.name, strValue)
57+
}
58+
Byte::class.qualifiedName ->{
59+
builder.putValue(paramsInfo.name, strValue.toByte())
60+
}
61+
Short::class.qualifiedName ->{
62+
builder.putValue(paramsInfo.name, strValue.toShort())
63+
}
64+
Int::class.qualifiedName ->{
65+
builder.putValue(paramsInfo.name, strValue.toInt())
66+
}
67+
Long::class.qualifiedName ->{
68+
builder.putValue(paramsInfo.name, strValue.toLong())
69+
}
70+
Float::class.qualifiedName ->{
71+
builder.putValue(paramsInfo.name, strValue.toFloat())
72+
}
73+
Double::class.qualifiedName ->{
74+
builder.putValue(paramsInfo.name, strValue.toDouble())
75+
}
76+
Boolean::class.qualifiedName ->{
77+
builder.putValue(paramsInfo.name, strValue.toBoolean())
78+
}
79+
else ->{
80+
val gson = Gson()
81+
val clazz = Class.forName(paramsInfo.className)
82+
val data = gson.fromJson<Any>(strValue,clazz)
83+
builder.putValue(paramsInfo.name, data)
84+
}
85+
86+
}
87+
}
88+
}

0 commit comments

Comments
 (0)