diff --git a/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireFragment.kt b/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireFragment.kt index f38ffc7..0825eb3 100644 --- a/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireFragment.kt +++ b/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireFragment.kt @@ -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 @@ -36,7 +33,6 @@ abstract class HotwireFragment : Fragment(), HotwireDestination { super.onViewCreated(view, savedInstanceState) delegate.onViewCreated() - observeModalResult() observeDialogResult() observeTheme() @@ -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() { @@ -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() {} @@ -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 { @@ -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 - } } diff --git a/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireFragmentDelegate.kt b/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireFragmentDelegate.kt index 1468b24..bd32542 100644 --- a/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireFragmentDelegate.kt +++ b/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireFragmentDelegate.kt @@ -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 @@ -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() } /** diff --git a/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebFragment.kt b/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebFragment.kt index de0c9a5..604e310 100644 --- a/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebFragment.kt +++ b/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebFragment.kt @@ -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 /** @@ -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() } /** @@ -79,10 +66,7 @@ open class HotwireWebFragment : HotwireFragment(), HotwireWebFragmentCallback { */ override fun onStartAfterDialogCancel() { super.onStartAfterDialogCancel() - - if (!delegate.sessionViewModel.modalResultExists) { - webDelegate.onStartAfterDialogCancel() - } + webDelegate.onStartAfterDialogCancel() } /** diff --git a/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebFragmentDelegate.kt b/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebFragmentDelegate.kt index e128a54..6245f81 100644 --- a/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebFragmentDelegate.kt +++ b/navigation-fragments/src/main/java/dev/hotwire/navigation/fragments/HotwireWebFragmentDelegate.kt @@ -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 diff --git a/navigation-fragments/src/main/java/dev/hotwire/navigation/navigator/Navigator.kt b/navigation-fragments/src/main/java/dev/hotwire/navigation/navigator/Navigator.kt index 0f65180..699bdac 100644 --- a/navigation-fragments/src/main/java/dev/hotwire/navigation/navigator/Navigator.kt +++ b/navigation-fragments/src/main/java/dev/hotwire/navigation/navigator/Navigator.kt @@ -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 + ) } } @@ -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( diff --git a/navigation-fragments/src/main/java/dev/hotwire/navigation/session/SessionViewModel.kt b/navigation-fragments/src/main/java/dev/hotwire/navigation/session/SessionViewModel.kt index 61d0807..a45a4b4 100644 --- a/navigation-fragments/src/main/java/dev/hotwire/navigation/session/SessionViewModel.kt +++ b/navigation-fragments/src/main/java/dev/hotwire/navigation/session/SessionViewModel.kt @@ -18,20 +18,6 @@ internal class SessionViewModel : ViewModel() { var visitOptions: SessionEvent? = 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> by lazy { - MutableLiveData>() - } - - /** - * 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. */ @@ -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. */