Skip to content

Commit

Permalink
save and restore previous backstack in order to 'undo' unwanted backs…
Browse files Browse the repository at this point in the history
…tack operations
  • Loading branch information
cmathew committed Feb 25, 2022
1 parent 38daf0d commit 15e8651
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,27 @@ public open class NavigationDelegate(
direction: Direction,
backStackOperation: (Deque<NavigationEvent>) -> MagellanTransition
) {
containerView?.setInterceptTouchEvents(true)
navigationPropagator.beforeNavigation()
val oldBackStack = backStack.map { it.navigable }
val oldBackStackCopy = ArrayDeque(backStack)

val transition = backStackOperation.invoke(backStack)
navigationRequestHandler?.let { navRequestHandler ->
val backstackCopy = ArrayDeque(backStack)
backStackOperation.invoke(backstackCopy)
// onNavigationRequested implementation determines whether nav operation should be skipped
if (backstackCopy.currentNavigable != null &&
navRequestHandler.onNavigationRequested(this, backstackCopy.currentNavigable!!)
if (backStack.currentNavigable != null &&
navRequestHandler.shouldOverrideNavigation(this, backStack.currentNavigable!!)
) {
backStack.clear()
backStack.addAll(oldBackStackCopy)
navRequestHandler.overrideNavigationRequest(this, backStack.currentNavigable!!)
return
}
}

containerView?.setInterceptTouchEvents(true)
navigationPropagator.beforeNavigation()
val from = navigateFrom(currentNavigable)
val oldBackStack = backStack.map { it.navigable }
val transition = backStackOperation.invoke(backStack)
val from = navigateFrom(oldBackStackCopy.currentNavigable)
val newBackStack = backStack.map { it.navigable }
findBackstackChangesAndUpdateStates(oldBackStack = oldBackStack, newBackStack = newBackStack)
val to = navigateTo(currentNavigable!!, direction)
val to = navigateTo(backStack.currentNavigable!!, direction)
navigationPropagator.afterNavigation()
animateAndRemove(from, to, direction, transition)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ import android.content.Context
import android.view.View

public interface NavigationRequestHandler {
public fun onNavigationRequested(
public fun shouldOverrideNavigation(
navigationDelegate: NavigationDelegate,
navigable: NavigableCompat
): Boolean

// what if this supplied a backstack operation, instead?
public fun overrideNavigationRequest(
navigationDelegate: NavigationDelegate,
navigable: NavigableCompat
)
}

public interface ViewTemplateApplier {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@ class SampleApplication : Application() {
.build()

Magellan.navigationRequestHandler = object : NavigationRequestHandler {
override fun onNavigationRequested(
override fun overrideNavigationRequest(
navigationDelegate: NavigationDelegate,
navigable: NavigableCompat
) {
navigationDelegate.goTo(UpdateAppStep())
}

override fun shouldOverrideNavigation(
navigationDelegate: NavigationDelegate,
navigable: NavigableCompat
): Boolean {
if (navigable is SuggestExhibitJourney) {
navigationDelegate.goTo(UpdateAppStep())
return true
}
return false
return navigable is SuggestExhibitJourney
}
}
}
Expand Down

0 comments on commit 15e8651

Please sign in to comment.