Skip to content

Commit

Permalink
Start removing the concept of "modal results" since it's no longer ne…
Browse files Browse the repository at this point in the history
…cessary with a single navigator instance per host
  • Loading branch information
jayohms committed Nov 27, 2024
1 parent 71f8cdc commit 29b3f67
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ import android.os.Bundle
import android.view.View
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import dev.hotwire.core.turbo.config.context
import dev.hotwire.core.turbo.config.title
import dev.hotwire.core.turbo.nav.PresentationContext
import dev.hotwire.navigation.R
import dev.hotwire.navigation.destinations.HotwireDestination
import dev.hotwire.navigation.navigator.Navigator
import dev.hotwire.navigation.navigator.NavigatorHost
import dev.hotwire.navigation.observers.HotwireWindowThemeObserver
import dev.hotwire.navigation.session.SessionModalResult

/**
* The base class from which all "standard" native Fragments (non-dialogs) in a
Expand All @@ -36,7 +33,6 @@ abstract class HotwireFragment : Fragment(), HotwireDestination {
super.onViewCreated(view, savedInstanceState)
delegate.onViewCreated()

observeModalResult()
observeDialogResult()
observeTheme()

Expand Down Expand Up @@ -77,10 +73,7 @@ abstract class HotwireFragment : Fragment(), HotwireDestination {

override fun onStart() {
super.onStart()

if (!delegate.sessionViewModel.modalResultExists) {
delegate.onStart()
}
delegate.onStart()
}

override fun onStop() {
Expand All @@ -92,22 +85,12 @@ abstract class HotwireFragment : Fragment(), HotwireDestination {
delegate.prepareNavigation(onReady)
}

/**
* Called when the Fragment has been started again after receiving a
* modal result. Will navigate if the result indicates it should.
*/
open fun onStartAfterModalResult(result: SessionModalResult) {
delegate.onStartAfterModalResult(result)
}

/**
* Called when the Fragment has been started again after a dialog has
* been dismissed/canceled and no result is passed back.
*/
open fun onStartAfterDialogCancel() {
if (!delegate.sessionViewModel.modalResultExists) {
delegate.onStartAfterDialogCancel()
}
delegate.onStartAfterDialogCancel()
}

override fun onBeforeNavigation() {}
Expand All @@ -129,16 +112,6 @@ abstract class HotwireFragment : Fragment(), HotwireDestination {
return delegate
}

private fun observeModalResult() {
if (shouldHandleModalResults()) {
delegate.sessionViewModel.modalResult.observe(viewLifecycleOwner) { event ->
event.getContentIfNotHandled()?.let {
onStartAfterModalResult(it)
}
}
}
}

private fun observeDialogResult() {
delegate.sessionViewModel.dialogResult.observe(viewLifecycleOwner) { event ->
event.getContentIfNotHandled()?.let {
Expand All @@ -165,9 +138,4 @@ abstract class HotwireFragment : Fragment(), HotwireDestination {
viewLifecycleOwner.lifecycle.addObserver(HotwireWindowThemeObserver(this))
}
}

private fun shouldHandleModalResults(): Boolean {
// Only handle modal results in non-modal contexts
return pathProperties.context != PresentationContext.MODAL
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package dev.hotwire.navigation.fragments

import dev.hotwire.navigation.logging.logEvent
import dev.hotwire.navigation.destinations.HotwireDestination
import dev.hotwire.navigation.logging.logEvent
import dev.hotwire.navigation.navigator.navigatorName
import dev.hotwire.navigation.session.SessionModalResult
import dev.hotwire.navigation.session.SessionViewModel
import dev.hotwire.navigation.util.displayBackButton
import dev.hotwire.navigation.util.displayBackButtonAsCloseIcon
Expand Down Expand Up @@ -65,26 +64,13 @@ class HotwireFragmentDelegate(private val navDestination: HotwireDestination) {
logEvent("fragment.onStartAfterDialogCancel", "location" to location)
}

/**
* 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) {
logEvent("fragment.onStartAfterModalResult", "location" to result.location, "options" to result.options)
if (result.shouldNavigate) {
navigator.route(result.location, result.options, result.bundle)
}
}

/**
* 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() {
logEvent("fragment.onDialogCancel", "location" to location)
if (!sessionViewModel.modalResultExists) {
sessionViewModel.sendDialogResult()
}
sessionViewModel.sendDialogResult()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import android.view.View
import android.view.ViewGroup
import androidx.activity.result.ActivityResultLauncher
import dev.hotwire.core.bridge.BridgeDelegate
import dev.hotwire.core.turbo.errors.VisitError
import dev.hotwire.core.files.util.HOTWIRE_REQUEST_CODE_FILES
import dev.hotwire.core.turbo.errors.VisitError
import dev.hotwire.core.turbo.webview.HotwireWebChromeClient
import dev.hotwire.core.turbo.webview.HotwireWebView
import dev.hotwire.navigation.R
import dev.hotwire.navigation.config.HotwireNavigation
import dev.hotwire.navigation.destinations.HotwireDestinationDeepLink
import dev.hotwire.navigation.session.SessionModalResult
import dev.hotwire.navigation.views.HotwireView

/**
Expand Down Expand Up @@ -58,19 +57,7 @@ open class HotwireWebFragment : HotwireFragment(), HotwireWebFragmentCallback {

override fun onStart() {
super.onStart()

if (!delegate.sessionViewModel.modalResultExists) {
webDelegate.onStart()
}
}

/**
* Called when the Fragment has been started again after receiving a
* modal result. Will navigate if the result indicates it should.
*/
override fun onStartAfterModalResult(result: SessionModalResult) {
super.onStartAfterModalResult(result)
webDelegate.onStartAfterModalResult(result)
webDelegate.onStart()
}

/**
Expand All @@ -79,10 +66,7 @@ open class HotwireWebFragment : HotwireFragment(), HotwireWebFragmentCallback {
*/
override fun onStartAfterDialogCancel() {
super.onStartAfterDialogCancel()

if (!delegate.sessionViewModel.modalResultExists) {
webDelegate.onStartAfterDialogCancel()
}
webDelegate.onStartAfterDialogCancel()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,6 @@ internal class HotwireWebFragmentDelegate(
initWebChromeClient()
}

/**
* 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) {
if (!result.shouldNavigate) {
initNavigationVisit()
initWebChromeClient()
}
}

/**
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,18 +264,14 @@ class Navigator(
)

navigateWhenReady {
val isDialog = currentDestination.fragment is HotwireDialogDestination
if (isDialog) {
// Pop the backstack before sending the modal result, since the
// underlying fragment is still active and will receive the
// result immediately. This allows the modal result flow to
// behave exactly like full screen fragments.
popModalsFromBackStack(rule)
sendModalResult(rule)
} else {
sendModalResult(rule)
popModalsFromBackStack(rule)
}
popModalsFromBackStack(rule)

// Perform the new navigation in the "default" context
route(
location = checkNotNull(rule.newModalResult?.location),
options = checkNotNull(rule.newModalResult?.options),
bundle = rule.newModalResult?.bundle
)
}
}

Expand All @@ -295,14 +291,6 @@ class Navigator(
rule.controller.popBackStack()
}

private fun sendModalResult(rule: NavigatorRule) {
// Save the modal result with VisitOptions so it can be retrieved
// by the previous destination when the backstack is popped.
currentDestination.delegate().sessionViewModel.sendModalResult(
checkNotNull(rule.newModalResult)
)
}

private fun replaceRootLocation(rule: NavigatorRule) {
if (rule.newDestination == null) {
logEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,6 @@ internal class SessionViewModel : ViewModel() {
var visitOptions: SessionEvent<VisitOptions>? = null
private set

/**
* A one-time event that can be observed to determine if a closing modal has returned a result
* to be proceed. Can only be consumed once.
*/
val modalResult: MutableLiveData<SessionEvent<SessionModalResult>> by lazy {
MutableLiveData<SessionEvent<SessionModalResult>>()
}

/**
* Convenience method to check if the modal result has already been consumed.
*/
val modalResultExists: Boolean
get() = modalResult.value?.hasBeenHandled == false

/**
* A one-time event that can be observed to determine when a dialog has been cancelled.
*/
Expand All @@ -46,13 +32,6 @@ internal class SessionViewModel : ViewModel() {
visitOptions = SessionEvent(options)
}

/**
* Wraps a modal result in a [SessionEvent] and updates the LiveData value.
*/
fun sendModalResult(result: SessionModalResult) {
modalResult.value = SessionEvent(result)
}

/**
* Wraps a dialog result in a [SessionEvent] and updates the LiveData value.
*/
Expand Down

0 comments on commit 29b3f67

Please sign in to comment.