-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/#16 runtime navcontroller navigate #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c7715ee
chore: add navigation testing dependency to support navigation compon…
angryPodo 6fa6f83
chore: add kotlin serialization plugin to build configuration
angryPodo 6ed7ffd
fix: add NavigationFailed error class to WispError for better runtime…
angryPodo f276644
fix: enhance Wisp class with navigation and route resolution logic, a…
angryPodo 2c54106
refactor: update WispRegistry interface to use createRoute and getRou…
angryPodo d931c8e
add: implement extension function for NavController to navigate using…
angryPodo fa203fc
fix: add null check and exception for route creation and argument bun…
angryPodo 75177c4
fix: lint check
angryPodo 00c226e
refactor: update WispRegistry to generate route factory map and patte…
angryPodo 41db122
fix: remove getRoutePattern method from WispRegistrySpec interface to…
angryPodo 5281474
refactor: remove getRoutePattern function from WispRegistryGenerator …
angryPodo a58f2c9
fix: simplify navigateTo extension and route handling in Wisp.kt
angryPodo 05181e6
fix: lint check
angryPodo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 42 additions & 32 deletions
74
wisp-processor/src/main/java/com/angrypodo/wisp/generator/WispRegistryGenerator.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,64 +1,74 @@ | ||
| package com.angrypodo.wisp.generator | ||
|
|
||
| import com.angrypodo.wisp.WispClassName.GENERATED_PACKAGE | ||
| import com.angrypodo.wisp.WispClassName.ROUTE_FACTORY | ||
| import com.angrypodo.wisp.WispClassName | ||
| import com.angrypodo.wisp.model.RouteInfo | ||
| import com.squareup.kotlinpoet.ANY | ||
| import com.squareup.kotlinpoet.CodeBlock | ||
| import com.squareup.kotlinpoet.FileSpec | ||
| import com.squareup.kotlinpoet.FunSpec | ||
| import com.squareup.kotlinpoet.KModifier | ||
| import com.squareup.kotlinpoet.MAP | ||
| import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy | ||
| import com.squareup.kotlinpoet.PropertySpec | ||
| import com.squareup.kotlinpoet.SET | ||
| import com.squareup.kotlinpoet.STRING | ||
| import com.squareup.kotlinpoet.TypeSpec | ||
|
|
||
| internal object WispRegistryGenerator { | ||
| private const val REGISTRY_NAME = "WispRegistry" | ||
| private const val FACTORIES_PROPERTY_NAME = "factories" | ||
| private const val GET_FACTORY_FUN_NAME = "getRouteFactory" | ||
| private const val GET_PATTERNS = "getPatterns" | ||
| internal class WispRegistryGenerator { | ||
|
|
||
| private val registryName = "WispRegistry" | ||
| private val factoriesPropertyName = "factories" | ||
|
|
||
| fun generate(routes: List<RouteInfo>): FileSpec { | ||
| val mapType = MAP.parameterizedBy(STRING, ROUTE_FACTORY) | ||
| val factoriesProperty = buildFactoriesProperty(routes) | ||
|
|
||
| val registryObject = TypeSpec.objectBuilder(registryName) | ||
| .addSuperinterface(WispClassName.WISP_REGISTRY_SPEC) | ||
| .addModifiers(KModifier.PUBLIC) | ||
| .addProperty(factoriesProperty) | ||
| .addFunction(buildCreateRouteFun(factoriesProperty)) | ||
| .build() | ||
|
|
||
| return FileSpec.builder(WispClassName.GENERATED_PACKAGE, registryName) | ||
| .addType(registryObject) | ||
| .build() | ||
| } | ||
|
|
||
| private fun buildFactoriesProperty(routes: List<RouteInfo>): PropertySpec { | ||
| val mapType = MAP.parameterizedBy(STRING, WispClassName.ROUTE_FACTORY) | ||
| val initializerBlock = CodeBlock.builder() | ||
| .add("mapOf(\n") | ||
| .indent() | ||
|
|
||
| routes.forEach { route -> | ||
| initializerBlock.add("%S to %T,\n", route.wispPath, route.factoryClassName) | ||
| } | ||
|
|
||
| initializerBlock.unindent().add(")") | ||
|
|
||
| val factoriesProperty = PropertySpec.builder(FACTORIES_PROPERTY_NAME, mapType) | ||
| return PropertySpec.builder(factoriesPropertyName, mapType) | ||
| .addModifiers(KModifier.PRIVATE) | ||
| .initializer(initializerBlock.build()) | ||
| .build() | ||
| } | ||
|
|
||
| val getFactoryFun = FunSpec.builder(GET_FACTORY_FUN_NAME) | ||
| .addModifiers(KModifier.INTERNAL) | ||
| .addParameter("path", STRING) | ||
| .returns(ROUTE_FACTORY.copy(nullable = true)) | ||
| .addStatement("return %N[path]", factoriesProperty) | ||
| .build() | ||
|
|
||
| val getPatternsFun = FunSpec.builder(GET_PATTERNS) | ||
| private fun buildCreateRouteFun(factoriesProperty: PropertySpec): FunSpec { | ||
| return FunSpec.builder("createRoute") | ||
| .addModifiers(KModifier.OVERRIDE) | ||
| .returns(SET.parameterizedBy(STRING)) | ||
| .addStatement("return %N.keys", factoriesProperty) | ||
| .build() | ||
|
|
||
| val registryObject = TypeSpec.objectBuilder(REGISTRY_NAME) | ||
| .addModifiers(KModifier.INTERNAL) | ||
| .addProperty(factoriesProperty) | ||
| .addFunction(getFactoryFun) | ||
| .build() | ||
|
|
||
| return FileSpec.builder(GENERATED_PACKAGE, REGISTRY_NAME) | ||
| .addType(registryObject) | ||
| .addParameter("path", STRING) | ||
| .returns(ANY.copy(nullable = true)) | ||
| .addCode( | ||
| CodeBlock.builder() | ||
| .beginControlFlow("for (pattern in %N.keys)", factoriesProperty) | ||
| .addStatement( | ||
| "val params = %T.match(path, pattern)", | ||
| WispClassName.WISP_URI_MATCHER | ||
| ) | ||
| .beginControlFlow("if (params != null)") | ||
| .addStatement("val factory = %N[pattern]", factoriesProperty) | ||
| .addStatement("return factory?.create(params)") | ||
| .endControlFlow() | ||
| .endControlFlow() | ||
| .addStatement("return null") | ||
| .build() | ||
| ) | ||
| .build() | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
wisp-runtime/src/main/java/com/angrypodo/wisp/runtime/NavControllerWisp.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package com.angrypodo.wisp.runtime | ||
|
|
||
| import android.net.Uri | ||
| import androidx.navigation.NavController | ||
|
|
||
| /** | ||
| * URI를 분석하고 즉시 백스택을 새로 구성하여 탐색하는 최종 사용자용 API입니다. | ||
| * 내부적으로 `Wisp.getDefaultInstance()`를 호출하여 모든 작업을 위임합니다. | ||
| * | ||
| * @param uri 딥링크 URI | ||
| * @throws WispError.ParsingFailed URI 파싱에 실패한 경우 | ||
| * @throws WispError.UnknownPath `WispRegistry`에 등록되지 않은 경로가 포함된 경우 | ||
| * @throws WispError.NavigationFailed 내비게이션 실행에 실패한 경우 | ||
| * @throws IllegalStateException `Wisp.initialize()`가 먼저 호출되지 않은 경우 | ||
| */ | ||
| fun NavController.navigateTo(uri: Uri) { | ||
| val wisp = Wisp.getDefaultInstance() | ||
| val routes = wisp.resolveRoutes(uri) | ||
| wisp.navigateTo(this, routes) | ||
| } |
60 changes: 47 additions & 13 deletions
60
wisp-runtime/src/main/java/com/angrypodo/wisp/runtime/Wisp.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,68 @@ | ||
| package com.angrypodo.wisp.runtime | ||
|
|
||
| import android.net.Uri | ||
| import androidx.navigation.NavController | ||
| import androidx.navigation.NavGraph.Companion.findStartDestination | ||
|
|
||
| /** | ||
| * Wisp 라이브러리의 핵심 로직을 수행하고, 내비게이션 기능을 실행하는 클래스입니다. | ||
| */ | ||
| class Wisp( | ||
| private val registry: WispRegistrySpec, | ||
| private val parser: WispUriParser = DefaultWispUriParser() | ||
| ) { | ||
|
|
||
| /** | ||
| * URI를 분석하여 @Serializable 라우트 객체의 리스트로 변환합니다. | ||
| * @throws WispError.UnknownPath 등록되지 않은 경로가 포함된 경우 | ||
| */ | ||
| fun resolveRoutes(uri: Uri): List<Any> { | ||
| val inputUris = parser.parse(uri) | ||
|
|
||
| return inputUris.map { inputUri -> | ||
| createRouteObject(inputUri) | ||
| val paths = parser.parse(uri) | ||
| return paths.map { path -> | ||
| registry.createRoute(path) ?: throw WispError.UnknownPath(path) | ||
| } | ||
| } | ||
|
|
||
| private fun createRouteObject(inputUri: String): Any { | ||
| val allPatterns = registry.getPatterns() | ||
| /** | ||
| * 주어진 라우트 객체 리스트를 사용하여 백스택을 새로 구성하고 탐색합니다. | ||
| * NavController.navigate를 순차적으로 호출하여 백스택을 구성합니다. | ||
| */ | ||
| fun navigateTo(navController: NavController, routes: List<Any>) { | ||
| if (routes.isEmpty()) return | ||
|
|
||
| for (pattern in allPatterns) { | ||
| val params = WispUriMatcher.match(inputUri, pattern) | ||
| try { | ||
| val firstRoute = routes.first() | ||
| navController.navigate(firstRoute) { | ||
| popUpTo(navController.graph.findStartDestination().id) { | ||
| inclusive = true | ||
| } | ||
| launchSingleTop = true | ||
| } | ||
|
|
||
| if (params != null) { | ||
| val factory = registry.getRouteFactory(pattern) | ||
| ?: throw WispError.UnknownPath(pattern) | ||
| routes.drop(1).forEach { route -> | ||
| navController.navigate(route) | ||
| } | ||
| } catch (e: Exception) { | ||
| throw WispError.NavigationFailed( | ||
| reason = e::class.simpleName ?: "Unknown", | ||
| detail = e.message | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| private var instance: Wisp? = null | ||
|
|
||
| return factory.create(params) | ||
| fun initialize(registry: WispRegistrySpec) { | ||
| if (instance == null) { | ||
| instance = Wisp(registry) | ||
| } | ||
| } | ||
|
|
||
| throw WispError.UnknownPath(inputUri) | ||
| internal fun getDefaultInstance(): Wisp { | ||
| return instance ?: throw IllegalStateException( | ||
| "Wisp.initialize() must be called first in your Application class." | ||
| ) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
wisp-runtime/src/main/java/com/angrypodo/wisp/runtime/WispRegistrySpec.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,5 @@ | ||
| package com.angrypodo.wisp.runtime | ||
|
|
||
| interface WispRegistrySpec { | ||
| fun getRouteFactory(routePattern: String): RouteFactory? | ||
| fun getPatterns(): Set<String> | ||
| fun createRoute(path: String): Any? | ||
| } |
2 changes: 1 addition & 1 deletion
2
wisp-runtime/src/main/java/com/angrypodo/wisp/runtime/WispUriMatcher.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
여기서 경쟁 상태가 일어날.. 가능성은 없겠죠? 멀티 스레드 관련해서 잘 아는 편이 아니라서 여쭤봅니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
일단 추후에 고민 해보겠습니다...지금대로면 크게 가능성은 없다고 생각해요!