Skip to content

Commit

Permalink
Rename and move fragment delegate classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jayohms committed May 22, 2024
1 parent a7aa09c commit 82a09c6
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import dev.hotwire.core.R
import dev.hotwire.core.navigation.navigator.Navigator
import dev.hotwire.core.navigation.navigator.NavigatorHost
import dev.hotwire.core.turbo.config.title
import dev.hotwire.core.turbo.delegates.TurboFragmentDelegate
import dev.hotwire.core.turbo.nav.HotwireNavDestination
import dev.hotwire.core.turbo.nav.HotwireNavDialogDestination

Expand All @@ -23,12 +22,12 @@ import dev.hotwire.core.turbo.nav.HotwireNavDialogDestination
abstract class HotwireBottomSheetFragment : BottomSheetDialogFragment(),
HotwireNavDestination, HotwireNavDialogDestination {
override lateinit var navigator: Navigator
internal lateinit var delegate: TurboFragmentDelegate
internal lateinit var delegate: HotwireFragmentDelegate

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
navigator = (parentFragment as NavigatorHost).navigator
delegate = TurboFragmentDelegate(this)
delegate = HotwireFragmentDelegate(this)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -113,7 +112,7 @@ abstract class HotwireBottomSheetFragment : BottomSheetDialogFragment(),
return view?.findViewById(R.id.toolbar)
}

final override fun delegate(): TurboFragmentDelegate {
final override fun delegate(): HotwireFragmentDelegate {
return delegate
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import dev.hotwire.core.navigation.navigator.Navigator
import dev.hotwire.core.navigation.navigator.NavigatorHost
import dev.hotwire.core.turbo.config.context
import dev.hotwire.core.turbo.config.title
import dev.hotwire.core.turbo.delegates.TurboFragmentDelegate
import dev.hotwire.core.turbo.nav.HotwireNavDestination
import dev.hotwire.core.turbo.nav.TurboNavPresentationContext
import dev.hotwire.core.turbo.observers.HotwireWindowThemeObserver
Expand All @@ -24,12 +23,12 @@ import dev.hotwire.core.turbo.session.SessionModalResult
*/
abstract class HotwireFragment : Fragment(), HotwireNavDestination {
override lateinit var navigator: Navigator
internal lateinit var delegate: TurboFragmentDelegate
internal lateinit var delegate: HotwireFragmentDelegate

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
navigator = (parentFragment as NavigatorHost).navigator
delegate = TurboFragmentDelegate(this)
delegate = HotwireFragmentDelegate(this)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -125,7 +124,7 @@ abstract class HotwireFragment : Fragment(), HotwireNavDestination {
return view?.findViewById(R.id.toolbar)
}

final override fun delegate(): TurboFragmentDelegate {
final override fun delegate(): HotwireFragmentDelegate {
return delegate
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.hotwire.core.turbo.delegates
package dev.hotwire.core.navigation.fragments

import dev.hotwire.core.lib.logging.logEvent
import dev.hotwire.core.navigation.fragments.HotwireFragmentViewModel
import dev.hotwire.core.turbo.nav.HotwireNavDestination
import dev.hotwire.core.turbo.session.SessionModalResult
import dev.hotwire.core.turbo.session.SessionViewModel
Expand All @@ -13,7 +12,7 @@ import dev.hotwire.core.turbo.util.displayBackButtonAsCloseIcon
* to this class. Note: This class should not need to be used directly
* from within your app.
*/
class TurboFragmentDelegate(private val navDestination: HotwireNavDestination) {
class HotwireFragmentDelegate(private val navDestination: HotwireNavDestination) {
private val fragment = navDestination.fragment
private val location = navDestination.location
private val navigator = navDestination.navigator
Expand Down Expand Up @@ -51,15 +50,15 @@ class TurboFragmentDelegate(private val navDestination: HotwireNavDestination) {
}

/**
* Provides a hook to Turbo when the Fragment has been started again after a dialog has
* Provides a hook when the Fragment has been started again after a dialog has
* been dismissed/canceled and no result is passed back.
*/
fun onStartAfterDialogCancel() {
logEvent("fragment.onStartAfterDialogCancel", "location" to location)
}

/**
* Provides a hook to Turbo when a Fragment has been started again after receiving a
* Provides a hook when a Fragment has been started again after receiving a
* modal result. Will navigate if the result indicates it should.
*/
fun onStartAfterModalResult(result: SessionModalResult) {
Expand All @@ -70,7 +69,7 @@ class TurboFragmentDelegate(private val navDestination: HotwireNavDestination) {
}

/**
* Provides a hook to Turbo when the dialog has been canceled. If there is a modal
* Provides a hook when the dialog has been canceled. If there is a modal
* result, an event will be created in [SessionViewModel] that can be observed.
*/
fun onDialogCancel() {
Expand All @@ -81,7 +80,7 @@ class TurboFragmentDelegate(private val navDestination: HotwireNavDestination) {
}

/**
* Provides a hook to Turbo when the dialog has been dismissed.
* Provides a hook when the dialog has been dismissed.
*/
fun onDialogDismiss() {
logEvent("fragment.onDialogDismiss", "location" to location)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import android.view.ViewGroup
import androidx.activity.result.ActivityResultLauncher
import dev.hotwire.core.R
import dev.hotwire.core.bridge.BridgeDelegate
import dev.hotwire.core.turbo.delegates.TurboWebFragmentDelegate
import dev.hotwire.core.turbo.errors.VisitError
import dev.hotwire.core.turbo.nav.HotwireDestination
import dev.hotwire.core.turbo.util.TURBO_REQUEST_CODE_FILES
Expand All @@ -25,16 +24,16 @@ import dev.hotwire.core.turbo.views.TurboWebView
* For native bottom sheet fragments, refer to [HotwireBottomSheetFragment].
*/
@HotwireDestination(uri = "turbo://fragment/web/modal/sheet")
open class HotwireWebBottomSheetFragment : HotwireBottomSheetFragment(), TurboWebFragmentCallback {
private lateinit var webDelegate: TurboWebFragmentDelegate
open class HotwireWebBottomSheetFragment : HotwireBottomSheetFragment(), HotwireWebFragmentCallback {
private lateinit var webDelegate: HotwireWebFragmentDelegate

private val bridgeDelegate by lazy {
BridgeDelegate(location = location, destination = this)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
webDelegate = TurboWebFragmentDelegate(delegate, this, this)
webDelegate = HotwireWebFragmentDelegate(delegate, this, this)
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
Expand Down Expand Up @@ -99,7 +98,7 @@ open class HotwireWebBottomSheetFragment : HotwireBottomSheetFragment(), TurboWe
}

// ----------------------------------------------------------------------------
// TurboWebFragmentCallback interface
// HotwireWebFragmentCallback interface
// ----------------------------------------------------------------------------

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import android.view.ViewGroup
import androidx.activity.result.ActivityResultLauncher
import dev.hotwire.core.R
import dev.hotwire.core.bridge.BridgeDelegate
import dev.hotwire.core.turbo.delegates.TurboWebFragmentDelegate
import dev.hotwire.core.turbo.errors.VisitError
import dev.hotwire.core.turbo.nav.HotwireDestination
import dev.hotwire.core.turbo.session.SessionModalResult
Expand All @@ -25,16 +24,16 @@ import dev.hotwire.core.turbo.views.TurboWebView
* For native fragments, refer to [HotwireFragment].
*/
@HotwireDestination(uri = "turbo://fragment/web")
open class HotwireWebFragment : HotwireFragment(), TurboWebFragmentCallback {
private lateinit var webDelegate: TurboWebFragmentDelegate
open class HotwireWebFragment : HotwireFragment(), HotwireWebFragmentCallback {
private lateinit var webDelegate: HotwireWebFragmentDelegate

private val bridgeDelegate by lazy {
BridgeDelegate(location = location, destination = this)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
webDelegate = TurboWebFragmentDelegate(delegate, this, this)
webDelegate = HotwireWebFragmentDelegate(delegate, this, this)
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
Expand Down Expand Up @@ -117,7 +116,7 @@ open class HotwireWebFragment : HotwireFragment(), TurboWebFragmentCallback {
}

// ----------------------------------------------------------------------------
// TurboWebFragmentCallback interface
// HotwireWebFragmentCallback interface
// ----------------------------------------------------------------------------

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import dev.hotwire.core.turbo.views.TurboWebChromeClient
import dev.hotwire.core.turbo.views.TurboWebView

/**
* Callback interface to be implemented by a [TurboWebFragment],
* [TurboWebBottomSheetDialogFragment], or subclass.
* Callback interface to be implemented by a [HotwireWebFragment],
* [HotwireWebBottomSheetFragment], or subclass.
*/
interface TurboWebFragmentCallback {
interface HotwireWebFragmentCallback {
/**
* The TurboView instance located in the Fragment's view.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.hotwire.core.turbo.delegates
package dev.hotwire.core.navigation.fragments

import android.content.Intent
import android.graphics.Bitmap
Expand All @@ -11,7 +11,6 @@ import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.whenStateAtLeast
import dev.hotwire.core.turbo.config.pullToRefreshEnabled
import dev.hotwire.core.turbo.errors.VisitError
import dev.hotwire.core.navigation.fragments.TurboWebFragmentCallback
import dev.hotwire.core.turbo.nav.HotwireNavDestination
import dev.hotwire.core.turbo.session.SessionCallback
import dev.hotwire.core.turbo.session.SessionModalResult
Expand All @@ -29,10 +28,10 @@ import kotlin.random.Random
* Provides all the hooks for a web Fragment to delegate its lifecycle events
* to this class.
*/
internal class TurboWebFragmentDelegate(
private val delegate: TurboFragmentDelegate,
internal class HotwireWebFragmentDelegate(
private val delegate: HotwireFragmentDelegate,
private val navDestination: HotwireNavDestination,
private val callback: TurboWebFragmentCallback
private val callback: HotwireWebFragmentCallback
) : SessionCallback {

private val location = navDestination.location
Expand Down Expand Up @@ -85,7 +84,7 @@ internal class TurboWebFragmentDelegate(
}

/**
* Provides a hook to Turbo when a fragment has been started again after receiving a
* Provides a hook when a fragment has been started again after receiving a
* modal result. Will navigate if the result indicates it should.
*/
fun onStartAfterModalResult(result: SessionModalResult) {
Expand All @@ -96,17 +95,17 @@ internal class TurboWebFragmentDelegate(
}

/**
* Provides a hook to Turbo when the fragment has been started again after a dialog has
* Provides a hook when the fragment has been started again after a dialog has
* been dismissed/canceled and no result is passed back. Initializes all necessary views and
* executes the Turbo visit.
* executes the visit.
*/
fun onStartAfterDialogCancel() {
initNavigationVisit()
initWebChromeClient()
}

/**
* Provides a hook to Turbo when the dialog has been canceled. Detaches the WebView
* Provides a hook when the dialog has been canceled. Detaches the WebView
* before navigation.
*/
fun onDialogCancel() {
Expand All @@ -115,7 +114,7 @@ internal class TurboWebFragmentDelegate(
}

/**
* Provides a hook to Turbo when the dialog has been dismissed. Detaches the WebView
* Provides a hook when the dialog has been dismissed. Detaches the WebView
* before navigation.
*/
fun onDialogDismiss() {
Expand Down Expand Up @@ -145,7 +144,7 @@ internal class TurboWebFragmentDelegate(
}

/**
* Displays the error view that's implemented via [TurboWebFragmentCallback.createErrorView].
* Displays the error view that's implemented via [HotwireWebFragmentCallback.createErrorView].
*/
fun showErrorView(error: VisitError) {
turboView?.addErrorView(callback.createErrorView(error))
Expand Down Expand Up @@ -352,7 +351,7 @@ internal class TurboWebFragmentDelegate(
destinationIdentifier = identifier,
restoreWithCachedSnapshot = restoreWithCachedSnapshot,
reload = reload,
callback = this@TurboWebFragmentDelegate,
callback = this@HotwireWebFragmentDelegate,
options = options.copy(snapshotHTML = snapshot)
)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package dev.hotwire.core.turbo.delegates
package dev.hotwire.core.navigation.navigator

import androidx.annotation.IdRes
import androidx.fragment.app.Fragment
import dev.hotwire.core.navigation.navigator.NavigatorHost
import dev.hotwire.core.navigation.navigator.Navigator

/**
* A simplified delegate that can be used when a [NavigatorHost] is nested
Expand All @@ -15,10 +13,10 @@ import dev.hotwire.core.navigation.navigator.Navigator
*
* @property fragment The Fragment to bind this delegate to.
* @param navigatorHostId The resource ID of the [NavigatorHost]
* instance hosted in your Activity's layout resource.
* instance hosted in your Fragment's layout resource.
*/
@Suppress("unused", "MemberVisibilityCanBePrivate")
class TurboNestedFragmentDelegate(val fragment: Fragment, navigatorHostId: Int) {
class NestedNavigatorHostDelegate(val fragment: Fragment, navigatorHostId: Int) {
val navigatorHost by lazy { findNavigatorHost(navigatorHostId) }

val navigator: Navigator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import dev.hotwire.core.navigation.navigator.Navigator
import dev.hotwire.core.navigation.routing.Router
import dev.hotwire.core.turbo.config.PathConfigurationProperties
import dev.hotwire.core.turbo.config.context
import dev.hotwire.core.turbo.delegates.TurboFragmentDelegate
import dev.hotwire.core.turbo.delegates.TurboNestedFragmentDelegate
import dev.hotwire.core.navigation.fragments.HotwireFragmentDelegate
import dev.hotwire.core.navigation.navigator.NestedNavigatorHostDelegate
import dev.hotwire.core.turbo.visit.VisitAction

/**
Expand Down Expand Up @@ -71,7 +71,7 @@ interface HotwireNavDestination {
/**
* Gets the delegate instance that handles the Fragment's lifecycle events.
*/
fun delegate(): TurboFragmentDelegate
fun delegate(): HotwireFragmentDelegate

/**
* Returns the [Toolbar] used for navigation by the given view.
Expand Down Expand Up @@ -102,7 +102,7 @@ interface HotwireNavDestination {

/**
* Gets the navigator that will be used for navigating to `newLocation`. You should
* not have to override this, unless you're using a [TurboNestedFragmentDelegate] to provide
* not have to override this, unless you're using a [NestedNavigatorHostDelegate] to provide
* sub-navigation within your current Fragment destination and would like custom behavior.
*/
fun navigatorForNavigation(newLocation: String): Navigator {
Expand Down

0 comments on commit 82a09c6

Please sign in to comment.