Skip to content

Commit

Permalink
Support multi router Destination
Browse files Browse the repository at this point in the history
  • Loading branch information
0xZhangKe committed Aug 5, 2023
1 parent 8dafe17 commit 99f6d03
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class KRouterVisitor(
existsFile.outputStream().use { ServicesFiles.writeServiceFile(services, it) }
} else {
environment.codeGenerator.createNewFile(
dependencies = Dependencies(aggregating = false, serviceClassDeclaration.containingFile!!),
dependencies = Dependencies(aggregating = true, serviceClassDeclaration.containingFile!!),
packageName = "",
fileName = resourceFileName,
extensionName = "",
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/com/zhangke/krouter/Destination.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import kotlin.reflect.KClass
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
annotation class Destination(
val router: String,
vararg val router: String,
val type: KClass<*> = Unit::class,
)
16 changes: 6 additions & 10 deletions core/src/main/java/com/zhangke/krouter/ZZZKRouterInternalUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,16 @@ object ZZZKRouterInternalUtil {
): Any? {
val routerUri = URI.create(router).baseUri
val service = serviceClassList.firstOrNull {
val serviceRouter = getRouterFromClassAnnotation(it::class)
if (serviceRouter.isNullOrEmpty().not()) {
val serviceUri = URI.create(serviceRouter!!).baseUri
serviceUri == routerUri
} else {
false
}
getRouterFromClassAnnotation(it::class).map { route ->
URI.create(route).baseUri
}.contains(routerUri)
}
return service
}

private fun getRouterFromClassAnnotation(targetClass: KClass<*>): String? {
val routerAnnotation = targetClass.findAnnotation<Destination>() ?: return null
return routerAnnotation.router
private fun getRouterFromClassAnnotation(targetClass: KClass<*>): Array<String> {
val routerAnnotation = targetClass.findAnnotation<Destination>() ?: return emptyArray()
return arrayOf(*routerAnnotation.router)
}

fun getFilledRouterService(router: String, service: Any): Any {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import com.zhangke.krouter.sample.core.Screen

fun main() {
KRouter.route<Screen>("screen/home?name=zhangke")?.content()
KRouter.route<Screen>("scree/home/detail?name=zhangke")?.content()
KRouter.route<Screen>("krouter://sample.com/screen/home?name=zhangke")?.content()
KRouter.route<Screen>("screen/home/detail?name=zhangke")?.content()
KRouter.route<Screen>("screen/profile?name=zhangke")?.content()
KRouter.route<Screen>("screen/profile/detail?name=zhangke")?.content()
KRouter.route<Screen>("screen/setting")?.content()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.zhangke.krouter.Destination
import com.zhangke.krouter.Router
import com.zhangke.krouter.sample.core.Screen

@Destination(router = "scree/home/detail")
@Destination("screen/home/detail")
class HomeDetailScreen(@Router val router: String = "") : Screen {

override fun content() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.zhangke.krouter.Destination
import com.zhangke.krouter.Router
import com.zhangke.krouter.sample.core.Screen

@Destination("screen/home")
@Destination("screen/home", "krouter://sample.com/screen/home")
class HomeScreen(@Router val router: String = "") : Screen {

override fun content() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package com.zhangke.krouter.sample.home
import com.zhangke.krouter.Destination
import com.zhangke.krouter.sample.core.Screen

@Destination(router = "scree/profile/detail")
@Destination("screen/profile/detail")
class ProfileDetailScreen: Screen
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package com.zhangke.krouter.sample.setting
import com.zhangke.krouter.Destination
import com.zhangke.krouter.sample.core.Screen

@Destination(router = "screen/setting/detail")
@Destination("screen/setting/detail")
class SettingsDetailScreen : Screen

0 comments on commit 99f6d03

Please sign in to comment.