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

Add missing TurboSessionNavHostFragment API documentation #313

Merged
merged 1 commit into from
Feb 25, 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
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ class MainSessionNavHostFragment : TurboSessionNavHostFragment() {

override val startLocation = HOME_URL

override val registeredActivities: List<KClass<out AppCompatActivity>>
get() = listOf()

override val registeredFragments: List<KClass<out Fragment>>
get() = listOf(
WebFragment::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,38 @@ import dev.hotwire.turbo.views.TurboWebView
import kotlin.reflect.KClass

abstract class TurboSessionNavHostFragment : NavHostFragment() {
/**
* The name of the [TurboSession] instance, which is helpful for debugging
* purposes. This is arbitrary, but must be unique in your app.
*/
abstract val sessionName: String

/**
* The url of a starting location when your app starts up.
*/
abstract val startLocation: String

/**
* The location of your [TurboPathConfiguration] JSON file(s) to configure
* navigation rules.
*/
abstract val pathConfigurationLocation: TurboPathConfiguration.Location
abstract val registeredActivities: List<KClass<out AppCompatActivity>>

/**
* A list of registered fragments that can be navigated to. Every possible
* [dev.hotwire.turbo.fragments.TurboFragment] destination must be listed here.
*/
abstract val registeredFragments: List<KClass<out Fragment>>

/**
* A list of registered Activities that can be navigated to. This is optional.
*/
open val registeredActivities: List<KClass<out AppCompatActivity>> = emptyList()

/**
* The [TurboSession] instance that is shared with all destinations that are
* hosted inside this [TurboSessionNavHostFragment].
*/
lateinit var session: TurboSession
private set

Expand All @@ -34,14 +60,29 @@ abstract class TurboSessionNavHostFragment : NavHostFragment() {
onSessionCreated()
}

/**
* Called whenever the [TurboSession] instance has been (re)created. A new
* session is created whenever the [TurboSessionNavHostFragment] is created
* and whenever the WebView render process has been terminated and a new
* WebView instance is required.
*/
open fun onSessionCreated() {
session.pathConfiguration.load(pathConfigurationLocation)
}

/**
* Called whenever a new WebView instance needs to be (re)created. You can
* override this to provide your own [TurboWebView] subclass if you need
* custom behaviors.
*/
open fun onCreateWebView(context: Context): TurboWebView {
return TurboWebView(context, null)
}

/**
* Resets the [TurboSessionNavHostFragment] instance, it's [TurboSession]
* instance, and the entire navigation graph to its original starting point.
*/
fun reset(onReset: () -> Unit = {}) {
currentNavDestination.delegate().navigator.onNavigationVisit {
currentNavDestination.clearBackStack {
Expand All @@ -57,6 +98,9 @@ abstract class TurboSessionNavHostFragment : NavHostFragment() {
}
}

/**
* Retrieves the currently active [TurboNavDestination] on the backstack.
*/
val currentNavDestination: TurboNavDestination
get() = childFragmentManager.primaryNavigationFragment as TurboNavDestination?
?: throw IllegalStateException("No current destination found in NavHostFragment")
Expand Down
Loading