Skip to content

Commit

Permalink
iOS navigation: buildNavigation can now return nil to block the navig…
Browse files Browse the repository at this point in the history
…ation on iOS side
  • Loading branch information
hugolefrancois committed Apr 16, 2024
1 parent e39789c commit a23d930
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
4 changes: 2 additions & 2 deletions navigation/ios/Navigation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public extension View {
func pilotNavigation<ScreenData, Route: PilotNavigationRoute, Action: AnyObject, ScreenView: View, NavModifier: ViewModifier>(
navigationManager: PilotNavigationManager<Route, Action>,
@ViewBuilder buildView: @escaping (ScreenData) -> ScreenView,
buildNavigation: @escaping ([Route], Route) -> PilotNavigationType<ScreenData, NavModifier>,
buildNavigation: @escaping ([Route], Route) -> PilotNavigationType<ScreenData, NavModifier>?,
handleAction: ((Action) -> Void)? = nil
) -> some View {
modifier(
Expand All @@ -26,7 +26,7 @@ private struct NavigationModifier<ScreenData, Route: PilotNavigationRoute, Actio

init(
buildView: @escaping (ScreenData) -> ScreenView,
buildNavigation: @escaping ([Route], Route) -> PilotNavigationType<ScreenData, NavModifier>,
buildNavigation: @escaping ([Route], Route) -> PilotNavigationType<ScreenData, NavModifier>?,
handleAction: ((Action) -> Void)? = nil,
navigationManager: PilotNavigationManager<Route, Action>? = nil
) {
Expand Down
10 changes: 7 additions & 3 deletions navigation/ios/NavigationState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ class NavigationState<
@Published var child: NavigationStateTyped?
@Published var navigationDismissTriggered = false

private let buildNavigation: (([Route], Route) -> PilotNavigationType<ScreenData, NavModifier>)?
private let buildNavigation: (([Route], Route) -> PilotNavigationType<ScreenData, NavModifier>?)?
private let navigationManager: PilotNavigationManager<Route, Action>?
private let actionListener: ActionListener<Route, Action>
private var lastNavigationDate: Foundation.Date?

init(
navigation: PilotNavigationType<ScreenData, NavModifier>,
route: Route?,
buildNavigation: (([Route], Route) -> PilotNavigationType<ScreenData, NavModifier>)? = nil,
buildNavigation: (([Route], Route) -> PilotNavigationType<ScreenData, NavModifier>?)? = nil,
handleAction: ((Action) -> Void)? = nil,
navigationManager: PilotNavigationManager<Route, Action>? = nil
) {
Expand All @@ -49,10 +49,14 @@ class NavigationState<
override func push(route: PilotNavigationRoute) {
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
}

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

Expand Down

0 comments on commit a23d930

Please sign in to comment.