From ae353eae4b18e7961b767b30fa2af76a9fd40eb6 Mon Sep 17 00:00:00 2001 From: alexdremov Date: Thu, 4 May 2023 13:43:51 +0300 Subject: [PATCH] last view feaure --- Sources/PathPresenter/RoutingView.swift | 59 ++++++++++++++++++------- 1 file changed, 42 insertions(+), 17 deletions(-) diff --git a/Sources/PathPresenter/RoutingView.swift b/Sources/PathPresenter/RoutingView.swift index f64471d..ba9187d 100644 --- a/Sources/PathPresenter/RoutingView.swift +++ b/Sources/PathPresenter/RoutingView.swift @@ -46,16 +46,25 @@ public extension PathPresenter { PathPresenter views will try to occupy as much space as possible */ private let enforceFullScreen: Bool + + + /** + PathPresenter shows all views in path, this may lead to inapropriate .onAppear fire. + This option asks it to show only the last view + */ + private let showOnlyLast: Bool /** Init with external path state */ public init( path: Binding, - enforceFullScreen: Bool = true + enforceFullScreen: Bool = true, + showOnlyLast: Bool = false ) { self._path = path self.enforceFullScreen = enforceFullScreen + self.showOnlyLast = showOnlyLast } /** @@ -64,9 +73,10 @@ public extension PathPresenter { public init( path: Binding, enforceFullScreen: Bool = true, + showOnlyLast: Bool = false, @ViewBuilder rootView:() -> RootView ) { - self.init(path: path, enforceFullScreen: enforceFullScreen) + self.init(path: path, enforceFullScreen: enforceFullScreen, showOnlyLast: showOnlyLast) self.rootView = AnyView(rootView()) } @@ -76,6 +86,29 @@ public extension PathPresenter { public var pathBinding: Binding { $path } + + @ViewBuilder + private func getElementView(element elem: PathTypeView) -> some View { + switch elem { + case .plain(let view, hash: _, zIndex: let zIndex): + view + .zIndex(zIndex) + case .animated(let view, + transition: let transition, + animation: _, + hash: _, + zIndex: let zIndex): + view + .zIndex(zIndex) + .transition(transition) + case .sheet(let view, _, _): + view + } + } + + private func getLastNotSheet(content: [PathTypeView]) -> PathTypeView? { + content.last{!$0.isSheet} + } /** Constructs intrnal view structure @@ -89,21 +122,13 @@ public extension PathPresenter { rootView .zIndex(-1) } - ForEach(content, id: \.hashValue) { elem in - switch elem { - case .plain(let view, hash: _, zIndex: let zIndex): - view - .zIndex(zIndex) - case .animated(let view, - transition: let transition, - animation: _, - hash: _, - zIndex: let zIndex): - view - .zIndex(zIndex) - .transition(transition) - case .sheet(let view, _, _): - view + if showOnlyLast { + if let elem = getLastNotSheet(content: content) { + getElementView(element: elem) + } + } else { + ForEach(content, id: \.hashValue) { elem in + getElementView(element: elem) } } }