From a3ebc1db4e27513933c0ac6135e694cfa4033f80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=CC=8Cimon=20S=CC=8Cesta=CC=81k?= Date: Fri, 13 Dec 2024 15:45:52 +0100 Subject: [PATCH] Coordinator update pop functions --- .../Architecture/Coordinator.swift | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/Sources/FuturedArchitecture/Architecture/Coordinator.swift b/Sources/FuturedArchitecture/Architecture/Coordinator.swift index e0eaa3c..400797a 100644 --- a/Sources/FuturedArchitecture/Architecture/Coordinator.swift +++ b/Sources/FuturedArchitecture/Architecture/Coordinator.swift @@ -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! @@ -30,12 +30,12 @@ public protocol Coordinator: ObservableObject { @MainActor var modalCover: ModalCoverModel? { 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() @@ -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 } } }