Skip to content

Commit

Permalink
Fix possible desync of navigation if listener is null (#9)
Browse files Browse the repository at this point in the history
* Fix possible desync of navigation if listener is null

* Handle push return value on iOS

* Add return value for push

* api dump

---------

Co-authored-by: Hugo Lefrancois <[email protected]>
  • Loading branch information
nlangheit and hugolefrancois authored Apr 17, 2024
1 parent fd86091 commit 0934317
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
4 changes: 2 additions & 2 deletions navigation/common/api/navigation.api
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public abstract class com/mirego/pilot/navigation/PilotNavigationListener {
public fun <init> ()V
public abstract fun pop ()V
public abstract fun popTo (Lcom/mirego/pilot/navigation/PilotNavigationRoute;Z)V
public abstract fun push (Lcom/mirego/pilot/navigation/PilotNavigationRoute;)V
public abstract fun push (Lcom/mirego/pilot/navigation/PilotNavigationRoute;)Z
}

public abstract class com/mirego/pilot/navigation/PilotNavigationManager {
Expand Down Expand Up @@ -70,7 +70,7 @@ public class com/mirego/pilot/navigation/compose/PilotNavControllerNavigationLis
public fun <init> (Landroidx/navigation/NavController;)V
public fun pop ()V
public fun popTo (Lcom/mirego/pilot/navigation/PilotNavigationRoute;Z)V
public fun push (Lcom/mirego/pilot/navigation/PilotNavigationRoute;)V
public fun push (Lcom/mirego/pilot/navigation/PilotNavigationRoute;)Z
}

public final class com/mirego/pilot/navigation/compose/PilotNavigationHelpersKt {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ public open class PilotNavControllerNavigationListener<ROUTE : PilotNavigationRo
private val navController: NavController,
) : PilotNavigationListener<ROUTE>() {

override fun push(route: ROUTE) {
override fun push(route: ROUTE): Boolean {
navController.navigate(route.navRoute)
return true
}

override fun pop() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ public open class DefaultPilotNavigationManager<ROUTE : PilotNavigationRoute, AC
parentNavigationManager.push(route, locally = locally)
return@launch
}

internalRouteList.add(route)
listener?.push(route)
listener?.let {
if (it.push(route)) {
internalRouteList.add(route)
}
}
}
}

Expand Down Expand Up @@ -75,9 +77,14 @@ public open class DefaultPilotNavigationManager<ROUTE : PilotNavigationRoute, AC

if (index != -1 && internalRouteList.isNotEmpty()) {
val effectiveIndex = index + if (inclusive) 0 else 1
internalRouteList.removeAll(internalRouteList.takeLast(internalRouteList.size - effectiveIndex))
val elementsToRemove = internalRouteList.takeLast(internalRouteList.size - effectiveIndex)
if (callListener) {
listener?.popTo(navigationItem, inclusive = inclusive)
listener?.let {
internalRouteList.removeAll(elementsToRemove)
listener?.popTo(navigationItem, inclusive = inclusive)
}
} else {
internalRouteList.removeAll(elementsToRemove)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public abstract class PilotNavigationManager<ROUTE : PilotNavigationRoute, ACTIO
}

public abstract class PilotNavigationListener<ROUTE : PilotNavigationRoute> {
public abstract fun push(route: ROUTE)
public abstract fun push(route: ROUTE): Boolean
public abstract fun pop()
public abstract fun popTo(route: ROUTE, inclusive: Boolean)
}
Expand Down
6 changes: 3 additions & 3 deletions navigation/ios/NavigationState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ class NavigationState<
actionListener.startListening()
}

override func push(route: PilotNavigationRoute) {
override func push(route: PilotNavigationRoute) -> Bool {
guard let buildNavigation else { fatalError("buildNavigation not set")}
guard let route = route as? Route else { fatalError("Invalid route type")}
guard let navigation = buildNavigation(currentStack(), route) else {
navigationManager?.popToId(uniqueId: route.uniqueId, inclusive: true)
return
return false
}

dedounceNavigation { [weak self] in
guard let self else { return }
top().child = NavigationState(navigation: navigation, route: route)
}
return true
}

override func popTo(route: PilotNavigationRoute, inclusive: Bool) {
Expand Down

0 comments on commit 0934317

Please sign in to comment.