Skip to content

Commit

Permalink
Add missing TurboSessionNavHostFragment API documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jayohms committed Feb 25, 2024
1 parent b2f2aa4 commit 0630b68
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
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

0 comments on commit 0630b68

Please sign in to comment.