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

Default fragment destination #15

Merged
merged 1 commit into from
Mar 20, 2024
Merged
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
17 changes: 15 additions & 2 deletions core/src/main/kotlin/dev/hotwire/core/config/Hotwire.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,7 +17,11 @@ object Hotwire {
List<BridgeComponentFactory<BridgeComponent>> = emptyList()
private set

internal var registeredFragmentDestinations: List<KClass<out Fragment>> = emptyList()
internal var registeredFragmentDestinations:
List<KClass<out Fragment>> = listOf(
HotwireWebFragment::class,
HotwireWebBottomSheetFragment::class
)
private set

internal var router = Router(listOf(
Expand Down Expand Up @@ -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<out Fragment> = HotwireWebFragment::class

/**
* Register fragment destinations that can be navigated to. Every possible
* destination must be provided here.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
)
}
Expand Down Expand Up @@ -94,12 +93,6 @@ internal class TurboNavGraphBuilder(
}
}

private fun KClass<out Any>.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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
*
Expand All @@ -14,4 +17,12 @@ package dev.hotwire.core.turbo.nav
@MustBeDocumented
annotation class TurboNavGraphDestination(
val uri: String
)
) {
companion object {
internal fun from(klass: KClass<out Any>): TurboNavGraphDestination {
return requireNotNull(klass.findAnnotation()) {
"A TurboNavGraphDestination annotation is required for the destination: ${klass.simpleName}"
}
}
}
}
1 change: 1 addition & 0 deletions demo/src/main/assets/json/configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions demo/src/main/kotlin/dev/hotwire/demo/DemoApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class DemoApplication : Application() {
)
)

// Set the default fragment destination
Hotwire.defaultFragmentDestination = WebFragment::class

// Register fragment destinations
Hotwire.registerFragmentDestinations(listOf(
WebFragment::class,
Expand Down