Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coordinator update pop functions #34

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
}