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 1e01055..9197047 100644 --- a/core/src/main/kotlin/dev/hotwire/core/config/Hotwire.kt +++ b/core/src/main/kotlin/dev/hotwire/core/config/Hotwire.kt @@ -4,8 +4,10 @@ import android.content.Context import androidx.fragment.app.Fragment import dev.hotwire.core.bridge.BridgeComponent import dev.hotwire.core.bridge.BridgeComponentFactory -import dev.hotwire.core.navigation.routing.BrowserRoute +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.Router import dev.hotwire.core.turbo.config.TurboPathConfiguration import kotlin.reflect.KClass @@ -15,7 +17,11 @@ object Hotwire { List> = emptyList() private set - internal var registeredFragmentDestinations: List> = emptyList() + internal var registeredFragmentDestinations: + List> = listOf( + HotwireWebFragment::class, + HotwireWebBottomSheetFragment::class + ) private set internal var router = Router(listOf( @@ -59,6 +65,13 @@ object Hotwire { registeredBridgeComponentFactories = factories } + /** + * The default fragment destination for web requests. If you have not + * loaded a path configuration with a matching rule and a `uri` available + * for all possible paths, this destination will be used as the default. + */ + var defaultFragmentDestination: KClass = HotwireWebFragment::class + /** * Register fragment destinations that can be navigated to. Every possible * destination must be provided here. diff --git a/core/src/main/kotlin/dev/hotwire/core/turbo/config/TurboPathConfiguration.kt b/core/src/main/kotlin/dev/hotwire/core/turbo/config/TurboPathConfiguration.kt index 24774a2..ba62d2a 100644 --- a/core/src/main/kotlin/dev/hotwire/core/turbo/config/TurboPathConfiguration.kt +++ b/core/src/main/kotlin/dev/hotwire/core/turbo/config/TurboPathConfiguration.kt @@ -3,7 +3,10 @@ package dev.hotwire.core.turbo.config import android.annotation.SuppressLint import android.content.Context import android.net.Uri +import androidx.core.net.toUri import com.google.gson.annotations.SerializedName +import dev.hotwire.core.config.Hotwire +import dev.hotwire.core.turbo.nav.TurboNavGraphDestination import dev.hotwire.core.turbo.nav.TurboNavPresentation import dev.hotwire.core.turbo.nav.TurboNavPresentationContext import dev.hotwire.core.turbo.nav.TurboNavQueryStringPresentation @@ -131,10 +134,11 @@ val TurboPathConfigurationProperties.context: TurboNavPresentationContext } val TurboPathConfigurationProperties.uri: Uri - get() = Uri.parse(get("uri")) + get() = get("uri")?.toUri() ?: + TurboNavGraphDestination.from(Hotwire.defaultFragmentDestination).uri.toUri() val TurboPathConfigurationProperties.fallbackUri: Uri? - get() = get("fallback_uri")?.let { Uri.parse(it) } + get() = get("fallback_uri")?.toUri() val TurboPathConfigurationProperties.title: String? get() = get("title") diff --git a/core/src/main/kotlin/dev/hotwire/core/turbo/nav/TurboNavGraphBuilder.kt b/core/src/main/kotlin/dev/hotwire/core/turbo/nav/TurboNavGraphBuilder.kt index 49515b3..2b951e7 100644 --- a/core/src/main/kotlin/dev/hotwire/core/turbo/nav/TurboNavGraphBuilder.kt +++ b/core/src/main/kotlin/dev/hotwire/core/turbo/nav/TurboNavGraphBuilder.kt @@ -13,7 +13,6 @@ import dev.hotwire.core.turbo.config.TurboPathConfiguration import dev.hotwire.core.turbo.config.uri import java.util.UUID import kotlin.reflect.KClass -import kotlin.reflect.full.findAnnotation import kotlin.reflect.full.isSubclassOf internal class TurboNavGraphBuilder( @@ -35,7 +34,7 @@ internal class TurboNavGraphBuilder( val fragmentDestinations = registeredFragments.map { FragmentDestination( route = currentRoute.also { currentRoute++ }.toString(), - uri = it.turboAnnotation().uri.toUri(), + uri = TurboNavGraphDestination.from(it).uri.toUri(), kClass = it ) } @@ -94,12 +93,6 @@ internal class TurboNavGraphBuilder( } } - private fun KClass.turboAnnotation(): TurboNavGraphDestination { - return requireNotNull(findAnnotation()) { - "A TurboNavGraphDestination annotation is required for the destination: ${this.simpleName}" - } - } - // Modified from AndroidX FragmentNavigatorDestinationBuilder extensions private inline fun NavGraphBuilder.fragment( route: String, diff --git a/core/src/main/kotlin/dev/hotwire/core/turbo/nav/TurboNavGraphDestination.kt b/core/src/main/kotlin/dev/hotwire/core/turbo/nav/TurboNavGraphDestination.kt index 31c7b1b..b8969dd 100644 --- a/core/src/main/kotlin/dev/hotwire/core/turbo/nav/TurboNavGraphDestination.kt +++ b/core/src/main/kotlin/dev/hotwire/core/turbo/nav/TurboNavGraphDestination.kt @@ -1,5 +1,8 @@ package dev.hotwire.core.turbo.nav +import kotlin.reflect.KClass +import kotlin.reflect.full.findAnnotation + /** * Annotation for each Fragment that will be registered as a navigation destination. * @@ -14,4 +17,12 @@ package dev.hotwire.core.turbo.nav @MustBeDocumented annotation class TurboNavGraphDestination( val uri: String -) +) { + companion object { + internal fun from(klass: KClass): TurboNavGraphDestination { + return requireNotNull(klass.findAnnotation()) { + "A TurboNavGraphDestination annotation is required for the destination: ${klass.simpleName}" + } + } + } +} diff --git a/demo/src/main/assets/json/configuration.json b/demo/src/main/assets/json/configuration.json index 6436ba9..b1c31f6 100644 --- a/demo/src/main/assets/json/configuration.json +++ b/demo/src/main/assets/json/configuration.json @@ -59,6 +59,7 @@ "/numbers/[0-9]+$" ], "properties": { + "context": "modal", "uri": "turbo://fragment/numbers/sheet", "title": "Number", "description": "This is a native bottom sheet fragment" diff --git a/demo/src/main/kotlin/dev/hotwire/demo/DemoApplication.kt b/demo/src/main/kotlin/dev/hotwire/demo/DemoApplication.kt index d622e62..b471c3d 100644 --- a/demo/src/main/kotlin/dev/hotwire/demo/DemoApplication.kt +++ b/demo/src/main/kotlin/dev/hotwire/demo/DemoApplication.kt @@ -41,6 +41,9 @@ class DemoApplication : Application() { ) ) + // Set the default fragment destination + Hotwire.defaultFragmentDestination = WebFragment::class + // Register fragment destinations Hotwire.registerFragmentDestinations(listOf( WebFragment::class,