diff --git a/app-scaffold/src/main/kotlin/catchup/app/ui/activity/MainActivity.kt b/app-scaffold/src/main/kotlin/catchup/app/ui/activity/MainActivity.kt index 47f693e2f..3a291ba1b 100644 --- a/app-scaffold/src/main/kotlin/catchup/app/ui/activity/MainActivity.kt +++ b/app-scaffold/src/main/kotlin/catchup/app/ui/activity/MainActivity.kt @@ -42,7 +42,7 @@ import catchup.base.ui.RootContent import catchup.base.ui.rememberSystemBarColorController import catchup.compose.CatchUpTheme import catchup.compose.LocalDisplayFeatures -import catchup.deeplink.DeepLinkHandler +import catchup.deeplink.Router import catchup.deeplink.parse import catchup.di.AppScope import catchup.di.android.ActivityKey @@ -77,7 +77,7 @@ constructor( private val circuit: Circuit, private val catchUpPreferences: CatchUpPreferences, private val rootContent: RootContent, - private val deepLinkHandler: DeepLinkHandler, + private val router: Router, appConfig: AppConfig, ) : AppCompatActivity() { @@ -144,9 +144,7 @@ constructor( CatchUpTheme(useDarkTheme = useDarkTheme, isDynamicColor = useDynamicTheme) { CircuitCompositionLocals(circuit) { ContentWithOverlays { - val stack = remember { - intent?.let(deepLinkHandler::parse) ?: persistentListOf(HomeScreen) - } + val stack = remember { intent?.let(router::parse) ?: persistentListOf(HomeScreen) } val backStack = rememberSaveableBackStack(stack) val navigator = rememberCircuitNavigator(backStack) val intentAwareNavigator = diff --git a/libraries/deeplinking/src/main/kotlin/catchup/deeplink/DeepLinkHandler.kt b/libraries/deeplinking/src/main/kotlin/catchup/deeplink/Router.kt similarity index 94% rename from libraries/deeplinking/src/main/kotlin/catchup/deeplink/DeepLinkHandler.kt rename to libraries/deeplinking/src/main/kotlin/catchup/deeplink/Router.kt index 9bc51c240..07c5bf7c1 100644 --- a/libraries/deeplinking/src/main/kotlin/catchup/deeplink/DeepLinkHandler.kt +++ b/libraries/deeplinking/src/main/kotlin/catchup/deeplink/Router.kt @@ -20,7 +20,7 @@ import timber.log.Timber * * @see parse for primary documentation. */ -interface DeepLinkHandler { +interface Router { /** * Parses an [HttpUrl] into a list of [Screen]s that can be used as a backstack. * @@ -43,7 +43,7 @@ interface DeepLinkHandler { * * Only used if the [Intent.getAction] is [Intent.ACTION_VIEW]. */ -fun DeepLinkHandler.parse(intent: Intent): ImmutableList? { +fun Router.parse(intent: Intent): ImmutableList? { /* Example intent: adb shell am start \ @@ -65,9 +65,9 @@ private fun Uri.toHttpUrl(): HttpUrl? { @ContributesBinding(AppScope::class) @SingleIn(AppScope::class) -class DeepLinkHandlerImpl +class RouterImpl @Inject -constructor(private val routes: @JvmSuppressWildcards Map) : DeepLinkHandler { +constructor(private val routes: @JvmSuppressWildcards Map) : Router { override fun parse(url: HttpUrl): ImmutableList { val queryParams = url.queryParameterNames.associateWith { url.queryParameterValues(it) }.toImmutableMap() diff --git a/libraries/deeplinking/src/main/kotlin/catchup/deeplink/RoutingDSL.kt b/libraries/deeplinking/src/main/kotlin/catchup/deeplink/RoutingDSL.kt new file mode 100644 index 000000000..9a4c0ebab --- /dev/null +++ b/libraries/deeplinking/src/main/kotlin/catchup/deeplink/RoutingDSL.kt @@ -0,0 +1,76 @@ +package catchup.deeplink + +import com.slack.circuit.runtime.screen.Screen +import kotlinx.collections.immutable.ImmutableMap + +@DslMarker annotation class RoutingDSLMarker + +class RouteRequest( + val path: String, + val pathParams: ImmutableMap, + val queryParams: ImmutableMap>, +) + +@RoutingDSLMarker +interface RoutBuilder { + fun route(path: String = "", block: RoutBuilder.(RouteRequest) -> Screen): Screen + + fun route(path: Regex, block: RoutBuilder.(RouteRequest) -> Screen): Screen +} + +@RoutingDSLMarker +class RoutingBuilder : RoutBuilder { + class RoutBuilderImpl : RoutBuilder { + override fun route(path: String, block: RoutBuilder.(RouteRequest) -> Screen): Screen { + TODO() + } + + override fun route(path: Regex, block: RoutBuilder.(RouteRequest) -> Screen): Screen { + TODO("Not yet implemented") + } + } + + override fun route(path: String, block: RoutBuilder.(RouteRequest) -> Screen): Screen { + TODO() + } + + override fun route(path: Regex, block: RoutBuilder.(RouteRequest) -> Screen): Screen { + TODO("Not yet implemented") + } + + fun build(): Router { + TODO() + } +} + +class RouterImplV2 { + // TODO path params + // check PATH_TRAVERSAL from Retrofit + // /hello + // /order/shipment + // /user/{login} + // /user/* + // /user/{login?} + // /user/{...} + // /user/{param...} + // Regex("/.+/hello") + // (?pattern) + // Regex("""(?\d+)/hello""") +} + +fun routing(block: RoutingBuilder.() -> Unit): Router { + // TODO + val builder = RoutingBuilder() + block(builder) + return builder.build() +} + +fun example() { + routing { + route("/home") { request -> TODO() } + route("/users/{id}") { request -> + val id = request.pathParams["id"] + TODO() + } + } +}