Skip to content

Commit

Permalink
Merge pull request #226 from hotwired/modal-backstack
Browse files Browse the repository at this point in the history
Allow multiple modal destinations on the backstack
  • Loading branch information
jayohms authored Apr 29, 2022
2 parents 0d8aeab + 66fdd56 commit 5dfab8e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
10 changes: 6 additions & 4 deletions turbo/src/main/kotlin/dev/hotwire/turbo/nav/TurboNavRule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@ internal class TurboNavRule(
val currentLocation = checkNotNull(controller.currentBackStackEntry.location)
val currentProperties = pathConfiguration.properties(currentLocation)
val currentPresentationContext = currentProperties.context
val currentDestination = checkNotNull(controller.currentDestination)
val isAtStartDestination = controller.previousBackStackEntry == null

// New destination
val newLocation = location
val newProperties = pathConfiguration.properties(newLocation)
val newPresentationContext = newProperties.context
val newVisitOptions = visitOptions
val newBundle = bundle.withNavArguments()
val newExtras = extras
val newProperties = pathConfiguration.properties(newLocation)
val newPresentationContext = newProperties.context
val newQueryStringPresentation = newProperties.queryStringPresentation
val newPresentation = newPresentation()
val newNavigationMode = newNavigationMode()
Expand Down Expand Up @@ -126,7 +125,10 @@ internal class TurboNavRule(

private fun Bundle?.withNavArguments(): Bundle {
val bundle = this ?: bundleOf()
return bundle.apply { putString("location", newLocation) }
return bundle.apply {
putString("location", newLocation)
putSerializable("presentation-context", newPresentationContext)
}
}

private val NavBackStackEntry?.location: String?
Expand Down
21 changes: 19 additions & 2 deletions turbo/src/main/kotlin/dev/hotwire/turbo/nav/TurboNavigator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev.hotwire.turbo.nav

import android.os.Bundle
import androidx.fragment.app.DialogFragment
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavController
import androidx.navigation.NavOptions
import androidx.navigation.fragment.FragmentNavigator
Expand Down Expand Up @@ -155,11 +156,21 @@ internal class TurboNavigator(private val navDestination: TurboNavDestination) {
// underlying fragment is still active and will receive the
// result immediately. This allows the modal result flow to
// behave exactly like full screen fragments.
rule.controller.popBackStack(rule.currentDestination.id, true)
do {
rule.controller.popBackStack()
} while (
rule.controller.currentBackStackEntry.isModalContext
)

sendModalResult(rule)
} else {
sendModalResult(rule)
rule.controller.popBackStack(rule.currentDestination.id, true)

do {
rule.controller.popBackStack()
} while (
rule.controller.currentBackStackEntry.isModalContext
)
}
}
}
Expand Down Expand Up @@ -263,6 +274,12 @@ internal class TurboNavigator(private val navDestination: TurboNavDestination) {
)
}

private val NavBackStackEntry?.isModalContext: Boolean
get() {
val context = this?.arguments?.getSerializable("presentation-context")
return context as? TurboNavPresentationContext == TurboNavPresentationContext.MODAL
}

private fun logEvent(event: String, vararg params: Pair<String, Any>) {
val attributes = params.toMutableList().apply {
add(0, "session" to session.sessionName)
Expand Down

0 comments on commit 5dfab8e

Please sign in to comment.