diff --git a/core/src/main/kotlin/dev/hotwire/core/config/Hotwire.kt b/core/src/main/kotlin/dev/hotwire/core/config/Hotwire.kt index d8fac7d..81ae0b2 100644 --- a/core/src/main/kotlin/dev/hotwire/core/config/Hotwire.kt +++ b/core/src/main/kotlin/dev/hotwire/core/config/Hotwire.kt @@ -6,8 +6,8 @@ import dev.hotwire.core.bridge.BridgeComponent import dev.hotwire.core.bridge.BridgeComponentFactory import dev.hotwire.core.navigation.fragments.HotwireWebBottomSheetFragment import dev.hotwire.core.navigation.fragments.HotwireWebFragment -import dev.hotwire.core.navigation.routing.AppNavigationRoute -import dev.hotwire.core.navigation.routing.BrowserRoute +import dev.hotwire.core.navigation.routing.AppNavigationRouteDecisionHandler +import dev.hotwire.core.navigation.routing.BrowserRouteDecisionHandler import dev.hotwire.core.navigation.routing.Router import dev.hotwire.core.turbo.config.PathConfiguration import kotlin.reflect.KClass @@ -25,8 +25,8 @@ object Hotwire { private set internal var router = Router(listOf( - AppNavigationRoute(), - BrowserRoute() + AppNavigationRouteDecisionHandler(), + BrowserRouteDecisionHandler() )) val config: HotwireConfig = HotwireConfig() @@ -45,11 +45,11 @@ object Hotwire { } /** - * Registers the [Router.Route] instances that determine whether to route location + * Registers the [Router.RouteDecisionHandler] instances that determine whether to route location * urls within in-app navigation or with alternative custom behaviors. */ - fun registerRoutes(routes: List) { - router = Router(routes) + fun registerRouteDecisionHandlers(decisionHandlers: List) { + router = Router(decisionHandlers) } /** diff --git a/core/src/main/kotlin/dev/hotwire/core/navigation/navigator/Navigator.kt b/core/src/main/kotlin/dev/hotwire/core/navigation/navigator/Navigator.kt index 0cdf97f..ede1bb7 100644 --- a/core/src/main/kotlin/dev/hotwire/core/navigation/navigator/Navigator.kt +++ b/core/src/main/kotlin/dev/hotwire/core/navigation/navigator/Navigator.kt @@ -95,7 +95,7 @@ class Navigator( extras: FragmentNavigator.Extras? = null ) { - if (getRouteResult(location) == Router.RouteResult.STOP) { + if (getRouteDecision(location) == Router.Decision.CANCEL) { return } @@ -350,15 +350,15 @@ class Navigator( return currentDestination.navigatorForNavigation(location).navController } - private fun getRouteResult(location: String): Router.RouteResult { - val result = currentDestination.route(location) + private fun getRouteDecision(location: String): Router.Decision { + val decision = currentDestination.decideRoute(location) logEvent( - "routeResult", + "routeDecision", "location" to location, - "result" to result + "decision" to decision ) - return result + return decision } private fun navOptions(location: String, action: VisitAction): NavOptions { diff --git a/core/src/main/kotlin/dev/hotwire/core/navigation/routing/AppNavigationRoute.kt b/core/src/main/kotlin/dev/hotwire/core/navigation/routing/AppNavigationRouteDecisionHandler.kt similarity index 83% rename from core/src/main/kotlin/dev/hotwire/core/navigation/routing/AppNavigationRoute.kt rename to core/src/main/kotlin/dev/hotwire/core/navigation/routing/AppNavigationRouteDecisionHandler.kt index d3d5dd6..c770f78 100644 --- a/core/src/main/kotlin/dev/hotwire/core/navigation/routing/AppNavigationRoute.kt +++ b/core/src/main/kotlin/dev/hotwire/core/navigation/routing/AppNavigationRouteDecisionHandler.kt @@ -4,10 +4,10 @@ import androidx.core.net.toUri import dev.hotwire.core.navigation.activities.HotwireActivity import dev.hotwire.core.navigation.navigator.NavigatorConfiguration -class AppNavigationRoute : Router.Route { +class AppNavigationRouteDecisionHandler : Router.RouteDecisionHandler { override val name = "app-navigation" - override val result = Router.RouteResult.NAVIGATE + override val decision = Router.Decision.NAVIGATE override fun matches( location: String, diff --git a/core/src/main/kotlin/dev/hotwire/core/navigation/routing/BrowserRoute.kt b/core/src/main/kotlin/dev/hotwire/core/navigation/routing/BrowserRouteDecisionHandler.kt similarity index 83% rename from core/src/main/kotlin/dev/hotwire/core/navigation/routing/BrowserRoute.kt rename to core/src/main/kotlin/dev/hotwire/core/navigation/routing/BrowserRouteDecisionHandler.kt index e5200de..f59e04a 100644 --- a/core/src/main/kotlin/dev/hotwire/core/navigation/routing/BrowserRoute.kt +++ b/core/src/main/kotlin/dev/hotwire/core/navigation/routing/BrowserRouteDecisionHandler.kt @@ -7,10 +7,10 @@ import dev.hotwire.core.lib.logging.logError import dev.hotwire.core.navigation.activities.HotwireActivity import dev.hotwire.core.navigation.navigator.NavigatorConfiguration -class BrowserRoute : Router.Route { +class BrowserRouteDecisionHandler : Router.RouteDecisionHandler { override val name = "browser" - override val result = Router.RouteResult.STOP + override val decision = Router.Decision.CANCEL override fun matches( location: String, @@ -29,7 +29,7 @@ class BrowserRoute : Router.Route { try { activity.startActivity(intent) } catch (e: ActivityNotFoundException) { - logError("BrowserRoute", e) + logError("BrowserRouteDecisionHandler", e) } } } diff --git a/core/src/main/kotlin/dev/hotwire/core/navigation/routing/BrowserTabRoute.kt b/core/src/main/kotlin/dev/hotwire/core/navigation/routing/BrowserTabRouteDecisionHandler.kt similarity index 91% rename from core/src/main/kotlin/dev/hotwire/core/navigation/routing/BrowserTabRoute.kt rename to core/src/main/kotlin/dev/hotwire/core/navigation/routing/BrowserTabRouteDecisionHandler.kt index a154d20..a1cdc62 100644 --- a/core/src/main/kotlin/dev/hotwire/core/navigation/routing/BrowserTabRoute.kt +++ b/core/src/main/kotlin/dev/hotwire/core/navigation/routing/BrowserTabRouteDecisionHandler.kt @@ -8,10 +8,10 @@ import dev.hotwire.core.navigation.activities.HotwireActivity import dev.hotwire.core.navigation.navigator.NavigatorConfiguration import dev.hotwire.core.turbo.util.colorFromThemeAttr -class BrowserTabRoute : Router.Route { +class BrowserTabRouteDecisionHandler : Router.RouteDecisionHandler { override val name = "browser-tab" - override val result = Router.RouteResult.STOP + override val decision = Router.Decision.CANCEL override fun matches( location: String, diff --git a/core/src/main/kotlin/dev/hotwire/core/navigation/routing/Router.kt b/core/src/main/kotlin/dev/hotwire/core/navigation/routing/Router.kt index 66fa7a0..6e9c48d 100644 --- a/core/src/main/kotlin/dev/hotwire/core/navigation/routing/Router.kt +++ b/core/src/main/kotlin/dev/hotwire/core/navigation/routing/Router.kt @@ -3,34 +3,35 @@ package dev.hotwire.core.navigation.routing import dev.hotwire.core.lib.logging.logEvent import dev.hotwire.core.navigation.activities.HotwireActivity import dev.hotwire.core.navigation.navigator.NavigatorConfiguration -import dev.hotwire.core.navigation.routing.Router.Route +import dev.hotwire.core.navigation.routing.Router.RouteDecisionHandler /** * Routes location urls within in-app navigation or with custom behaviors - * provided in [Route] instances. + * provided in [RouteDecisionHandler] instances. */ -class Router(private val routes: List) { +class Router(private val decisionHandlers: List) { /** - * An interface to implement to provide custom route behaviors in your app. + * An interface to implement to provide custom route decision handling + * behaviors in your app. */ - interface Route { + interface RouteDecisionHandler { /** - * The route name used in debug logging. + * The decision handler name used in debug logging. */ val name: String /** - * To permit in-app navigation when the location matches this route, - * return [RouteResult.NAVIGATE]. To prevent in-app navigation return - * [RouteResult.STOP]. + * To permit in-app navigation when the location matches this decision + * handler, return [Decision.NAVIGATE]. To prevent in-app navigation + * return [Decision.CANCEL]. */ - val result: RouteResult + val decision: Decision /** - * Determines whether the location matches this route. Use your own custom - * rules based on the location's domain, protocol, path, or any other - * factors. + * Determines whether the location matches this decision handler. Use + * your own custom rules based on the location's domain, protocol, + * path, or any other factors. */ fun matches( location: String, @@ -48,7 +49,7 @@ class Router(private val routes: List) { ) } - enum class RouteResult { + enum class Decision { /** * Permit in-app navigation with your app's domain urls. */ @@ -57,27 +58,27 @@ class Router(private val routes: List) { /** * Prevent in-app navigation. Always use this for external domain urls. */ - STOP + CANCEL } - internal fun route( + internal fun decideRoute( location: String, configuration: NavigatorConfiguration, activity: HotwireActivity - ): RouteResult { - routes.forEach { route -> - if (route.matches(location, configuration)) { - logEvent("routeMatch", listOf( - "route" to route.name, + ): Decision { + decisionHandlers.forEach { handler -> + if (handler.matches(location, configuration)) { + logEvent("handlerMatch", listOf( + "handler" to handler.name, "location" to location )) - route.handle(location, configuration, activity) - return route.result + handler.handle(location, configuration, activity) + return handler.decision } } - logEvent("noRouteForLocation", location) - return RouteResult.STOP + logEvent("noHandlerForLocation", location) + return Decision.CANCEL } } diff --git a/core/src/main/kotlin/dev/hotwire/core/turbo/nav/HotwireNavDestination.kt b/core/src/main/kotlin/dev/hotwire/core/turbo/nav/HotwireNavDestination.kt index 0fb38ea..797ba13 100644 --- a/core/src/main/kotlin/dev/hotwire/core/turbo/nav/HotwireNavDestination.kt +++ b/core/src/main/kotlin/dev/hotwire/core/turbo/nav/HotwireNavDestination.kt @@ -115,12 +115,12 @@ interface HotwireNavDestination { /** * Determines whether the new location should be routed within in-app navigation from the - * current destination. By default, the registered [Router.Route] instances are used to + * current destination. By default, the registered [Router.RouteDecisionHandler] instances are used to * determine routing logic. You can override the global behavior for a specific destination, - * but it's recommend to use dedicated [Router.Route] instances for routing logic. + * but it's recommend to use dedicated [Router.RouteDecisionHandler] instances for routing logic. */ - fun route(newLocation: String): Router.RouteResult { - return Hotwire.router.route( + fun decideRoute(newLocation: String): Router.Decision { + return Hotwire.router.decideRoute( location = newLocation, configuration = navigator.configuration, activity = fragment.requireActivity() as HotwireActivity diff --git a/core/src/test/kotlin/dev/hotwire/core/navigation/routing/AppNavigationRouteTest.kt b/core/src/test/kotlin/dev/hotwire/core/navigation/routing/AppNavigationRouteDecisionHandlerTest.kt similarity index 84% rename from core/src/test/kotlin/dev/hotwire/core/navigation/routing/AppNavigationRouteTest.kt rename to core/src/test/kotlin/dev/hotwire/core/navigation/routing/AppNavigationRouteDecisionHandlerTest.kt index 9f91c38..1bdf276 100644 --- a/core/src/test/kotlin/dev/hotwire/core/navigation/routing/AppNavigationRouteTest.kt +++ b/core/src/test/kotlin/dev/hotwire/core/navigation/routing/AppNavigationRouteDecisionHandlerTest.kt @@ -7,8 +7,8 @@ import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner @RunWith(RobolectricTestRunner::class) -class AppNavigationRouteTest { - private val route = AppNavigationRoute() +class AppNavigationRouteDecisionHandlerTest { + private val route = AppNavigationRouteDecisionHandler() private val config = NavigatorConfiguration( name = "test", startLocation = "https://my.app.com", @@ -17,7 +17,7 @@ class AppNavigationRouteTest { @Test fun `matching result navigates`() { - assertEquals(Router.RouteResult.NAVIGATE, route.result) + assertEquals(Router.Decision.NAVIGATE, route.decision) } @Test diff --git a/core/src/test/kotlin/dev/hotwire/core/navigation/routing/BrowserTabRouteTest.kt b/core/src/test/kotlin/dev/hotwire/core/navigation/routing/BrowserRouteDecisionHandlerTest.kt similarity index 86% rename from core/src/test/kotlin/dev/hotwire/core/navigation/routing/BrowserTabRouteTest.kt rename to core/src/test/kotlin/dev/hotwire/core/navigation/routing/BrowserRouteDecisionHandlerTest.kt index 07b372b..3cd450a 100644 --- a/core/src/test/kotlin/dev/hotwire/core/navigation/routing/BrowserTabRouteTest.kt +++ b/core/src/test/kotlin/dev/hotwire/core/navigation/routing/BrowserRouteDecisionHandlerTest.kt @@ -7,8 +7,8 @@ import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner @RunWith(RobolectricTestRunner::class) -class BrowserTabRouteTest { - private val route = BrowserTabRoute() +class BrowserRouteDecisionHandlerTest { + private val route = BrowserRouteDecisionHandler() private val config = NavigatorConfiguration( name = "test", startLocation = "https://my.app.com", @@ -17,7 +17,7 @@ class BrowserTabRouteTest { @Test fun `matching result stops navigation`() { - assertEquals(Router.RouteResult.STOP, route.result) + assertEquals(Router.Decision.CANCEL, route.decision) } @Test diff --git a/core/src/test/kotlin/dev/hotwire/core/navigation/routing/BrowserRouteTest.kt b/core/src/test/kotlin/dev/hotwire/core/navigation/routing/BrowserTabRouteDecisionHandlerTest.kt similarity index 85% rename from core/src/test/kotlin/dev/hotwire/core/navigation/routing/BrowserRouteTest.kt rename to core/src/test/kotlin/dev/hotwire/core/navigation/routing/BrowserTabRouteDecisionHandlerTest.kt index d8089f3..6a49227 100644 --- a/core/src/test/kotlin/dev/hotwire/core/navigation/routing/BrowserRouteTest.kt +++ b/core/src/test/kotlin/dev/hotwire/core/navigation/routing/BrowserTabRouteDecisionHandlerTest.kt @@ -7,8 +7,8 @@ import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner @RunWith(RobolectricTestRunner::class) -class BrowserRouteTest { - private val route = BrowserRoute() +class BrowserTabRouteDecisionHandlerTest { + private val route = BrowserTabRouteDecisionHandler() private val config = NavigatorConfiguration( name = "test", startLocation = "https://my.app.com", @@ -17,7 +17,7 @@ class BrowserRouteTest { @Test fun `matching result stops navigation`() { - assertEquals(Router.RouteResult.STOP, route.result) + assertEquals(Router.Decision.CANCEL, route.decision) } @Test diff --git a/demo/src/main/kotlin/dev/hotwire/demo/DemoApplication.kt b/demo/src/main/kotlin/dev/hotwire/demo/DemoApplication.kt index 4ec6337..5875ee6 100644 --- a/demo/src/main/kotlin/dev/hotwire/demo/DemoApplication.kt +++ b/demo/src/main/kotlin/dev/hotwire/demo/DemoApplication.kt @@ -5,8 +5,8 @@ import dev.hotwire.core.BuildConfig import dev.hotwire.core.bridge.BridgeComponentFactory import dev.hotwire.core.bridge.KotlinXJsonConverter import dev.hotwire.core.config.Hotwire -import dev.hotwire.core.navigation.routing.AppNavigationRoute -import dev.hotwire.core.navigation.routing.BrowserTabRoute +import dev.hotwire.core.navigation.routing.AppNavigationRouteDecisionHandler +import dev.hotwire.core.navigation.routing.BrowserTabRouteDecisionHandler import dev.hotwire.core.turbo.config.PathConfiguration import dev.hotwire.demo.bridge.FormComponent import dev.hotwire.demo.bridge.MenuComponent @@ -59,10 +59,10 @@ class DemoApplication : Application() { BridgeComponentFactory("overflow-menu", ::OverflowMenuComponent) )) - // Register routes - Hotwire.registerRoutes(listOf( - AppNavigationRoute(), - BrowserTabRoute() + // Register route decision handlers + Hotwire.registerRouteDecisionHandlers(listOf( + AppNavigationRouteDecisionHandler(), + BrowserTabRouteDecisionHandler() )) // Set configuration options