Skip to content

Commit

Permalink
Merge pull request #34 from futuredapp/feature/Update-coordinator-pop…
Browse files Browse the repository at this point in the history
…-functions

Coordinator update pop functions
  • Loading branch information
ssestak authored Jan 28, 2025
2 parents 617e4c5 + a3ebc1d commit 3c061d1
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions Sources/FuturedArchitecture/Architecture/Coordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public protocol Coordinator: ObservableObject {
associatedtype RootView: View
/// Views which might be presented by the container based on an instance of `Destination`.
associatedtype DestinationViews: View

/// `rootView` returns the coordinator's main view.
/// - Note: It is common pattern to provide a default "destination" view as the body of the *container* instead of
/// ``SwiftUI.EmptyView``. If you do so, remeber to always capture the `instance` of the *coordinator* weakly!
Expand All @@ -30,12 +30,12 @@ public protocol Coordinator: ObservableObject {
@MainActor
var modalCover: ModalCoverModel<Destination>? { get set }


/// This function provides an instance of a `View` (commonly a *Component*) for each possible state of the
/// container (the destination).
@ViewBuilder
func scene(for destination: Destination) -> DestinationViews

/// This is a delegate function called when a modal view presented by the *container* is dismissed.
/// - Note: Default empty implementation is provided.
func onModalDismiss()
Expand Down Expand Up @@ -114,12 +114,27 @@ public extension NavigationStackCoordinator {
/// - Parameter destination: Destination to be reached. If nil is passed, or such destionation
/// is not currently on the stack, all views are removed.
/// - Experiment: This API is in preview and subject to change.
/// - Bug: @mikolasstuchlik thinks, that dismissing *all* views when destination is not found is
/// confusing and might be source of bugs.
func pop(to destination: Destination?) {
func pop(to destination: Destination) {
Task { @MainActor in
let index = destination.flatMap(self.path.lastIndex(of:)) ?? self.path.startIndex
guard let index = self.path.lastIndex(of: destination) else {
assertionFailure("Destination not found on the stack")
return
}
self.path = Array(path[path.startIndex...index])
return
}
}

func popToRoot() {
Task { @MainActor in
path = []
}
}

func reset() {
Task { @MainActor in
path = []
modalCover = nil
}
}
}

0 comments on commit 3c061d1

Please sign in to comment.