Skip to content
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

Tinker with routing DSL #1469

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {

Expand Down Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -43,7 +43,7 @@ interface DeepLinkHandler {
*
* Only used if the [Intent.getAction] is [Intent.ACTION_VIEW].
*/
fun DeepLinkHandler.parse(intent: Intent): ImmutableList<Screen>? {
fun Router.parse(intent: Intent): ImmutableList<Screen>? {
/*
Example intent:
adb shell am start \
Expand All @@ -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<String, DeepLinkable>) : DeepLinkHandler {
constructor(private val routes: @JvmSuppressWildcards Map<String, DeepLinkable>) : Router {
override fun parse(url: HttpUrl): ImmutableList<Screen> {
val queryParams =
url.queryParameterNames.associateWith { url.queryParameterValues(it) }.toImmutableMap()
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String>,
val queryParams: ImmutableMap<String, List<String?>>,
)

@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")
// (?<name>pattern)
// Regex("""(?<id>\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()
}
}
}
Loading