From 195d22ad87d6c0e2f94c706f2f25d01a050cad23 Mon Sep 17 00:00:00 2001 From: Alejandro Ruiz Ponce Date: Wed, 22 Jun 2022 13:28:16 +0200 Subject: [PATCH 1/7] Fix sheet presentation --- Sources/FlowStacks/View+cover.swift | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/Sources/FlowStacks/View+cover.swift b/Sources/FlowStacks/View+cover.swift index ce7eef0..bd137a3 100644 --- a/Sources/FlowStacks/View+cover.swift +++ b/Sources/FlowStacks/View+cover.swift @@ -38,11 +38,22 @@ extension View { @ViewBuilder func present(asSheet: Bool, isPresented: Binding, onDismiss: (() -> Void)? = nil, @ViewBuilder content: @escaping () -> Content) -> some View { if asSheet { - self.sheet( - isPresented: isPresented, - onDismiss: nil, - content: content - ) + if #available(iOS 14.5, *) { + self.sheet( + isPresented: isPresented, + onDismiss: nil, + content: content + ) + } else { + self.background( + EmptyView() + .sheet( + isPresented: isPresented, + onDismiss: nil, + content: content + ) + ) + } } else { self.cover( isPresented: isPresented, From b70df6333cfe06b26cdf272df7c8459f666b6190 Mon Sep 17 00:00:00 2001 From: Alejandro Ruiz Ponce Date: Wed, 22 Jun 2022 13:49:58 +0200 Subject: [PATCH 2/7] Add backport --- Sources/FlowStacks/Node.swift | 2 +- Sources/FlowStacks/View+cover.swift | 65 ++++++++++++++++------------- 2 files changed, 36 insertions(+), 31 deletions(-) diff --git a/Sources/FlowStacks/Node.swift b/Sources/FlowStacks/Node.swift index bb9412c..ce55c9e 100644 --- a/Sources/FlowStacks/Node.swift +++ b/Sources/FlowStacks/Node.swift @@ -117,7 +117,7 @@ indirect enum Node: View { NavigationLink(destination: next, isActive: pushBinding, label: EmptyView.init) .hidden() ) - .present( + .backport.present( asSheet: asSheet, isPresented: asSheet ? sheetBinding : coverBinding, onDismiss: onDismiss, diff --git a/Sources/FlowStacks/View+cover.swift b/Sources/FlowStacks/View+cover.swift index bd137a3..88172df 100644 --- a/Sources/FlowStacks/View+cover.swift +++ b/Sources/FlowStacks/View+cover.swift @@ -31,35 +31,40 @@ extension View { } #endif } +} - /// NOTE: On iOS 14.4 and below, a bug prevented multiple sheet/fullScreenCover modifiers being chained - /// on the same view, so we conditionally add the sheet/cover modifiers as a workaround. See - /// https://developer.apple.com/documentation/ios-ipados-release-notes/ios-ipados-14_5-release-notes - @ViewBuilder - func present(asSheet: Bool, isPresented: Binding, onDismiss: (() -> Void)? = nil, @ViewBuilder content: @escaping () -> Content) -> some View { - if asSheet { - if #available(iOS 14.5, *) { - self.sheet( - isPresented: isPresented, - onDismiss: nil, - content: content - ) - } else { - self.background( - EmptyView() - .sheet( - isPresented: isPresented, - onDismiss: nil, - content: content - ) - ) - } - } else { - self.cover( - isPresented: isPresented, - onDismiss: nil, - content: content - ) - } - } +public extension Backport where Content: View { + + /// NOTE: On iOS 14.4 and below, a bug prevented multiple sheet/fullScreenCover modifiers being chained + /// on the same view, so we conditionally add the sheet/cover modifiers as a workaround. See + /// https://developer.apple.com/documentation/ios-ipados-release-notes/ios-ipados-14_5-release-notes + @ViewBuilder + func present(asSheet: Bool, isPresented: Binding, onDismiss: (() -> Void)? = nil, @ViewBuilder content contentBuilder: @escaping () -> Content) -> some View { + + if #available(iOS 14.5, *) { + content.sheet( + isPresented: isPresented, + onDismiss: nil, + content: contentBuilder + ) + } else { + content.background( + EmptyView() + .sheet( + isPresented: isPresented, + onDismiss: nil, + content: contentBuilder + ) + ) + } + + } +} + +public struct Backport { + let content: Content +} + +public extension View { + var backport: Backport { Backport(content: self) } } From d978c5239e50d1fb570b9903e0bdae7072da21ef Mon Sep 17 00:00:00 2001 From: Alejandro Ruiz Ponce Date: Wed, 22 Jun 2022 13:53:38 +0200 Subject: [PATCH 3/7] Renaming --- Sources/FlowStacks/Node.swift | 2 +- Sources/FlowStacks/View+cover.swift | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/FlowStacks/Node.swift b/Sources/FlowStacks/Node.swift index ce55c9e..e06a7f0 100644 --- a/Sources/FlowStacks/Node.swift +++ b/Sources/FlowStacks/Node.swift @@ -117,7 +117,7 @@ indirect enum Node: View { NavigationLink(destination: next, isActive: pushBinding, label: EmptyView.init) .hidden() ) - .backport.present( + .backported.present( asSheet: asSheet, isPresented: asSheet ? sheetBinding : coverBinding, onDismiss: onDismiss, diff --git a/Sources/FlowStacks/View+cover.swift b/Sources/FlowStacks/View+cover.swift index 88172df..2ca2f49 100644 --- a/Sources/FlowStacks/View+cover.swift +++ b/Sources/FlowStacks/View+cover.swift @@ -33,7 +33,7 @@ extension View { } } -public extension Backport where Content: View { +public extension Backported where Content: View { /// NOTE: On iOS 14.4 and below, a bug prevented multiple sheet/fullScreenCover modifiers being chained /// on the same view, so we conditionally add the sheet/cover modifiers as a workaround. See @@ -61,10 +61,10 @@ public extension Backport where Content: View { } } -public struct Backport { +public struct Backported { let content: Content } public extension View { - var backport: Backport { Backport(content: self) } + var backported: Backported { Backported(content: self) } } From f81d809c2e8846ff59eabdd238e6bd5540664dbe Mon Sep 17 00:00:00 2001 From: Alejandro Ruiz Ponce Date: Wed, 22 Jun 2022 14:48:35 +0200 Subject: [PATCH 4/7] Reorganizing code --- Sources/FlowStacks/Backport.swift | 10 ++++ Sources/FlowStacks/Node.swift | 4 +- Sources/FlowStacks/View+cover.swift | 77 ++++++++++++++--------------- 3 files changed, 48 insertions(+), 43 deletions(-) create mode 100644 Sources/FlowStacks/Backport.swift diff --git a/Sources/FlowStacks/Backport.swift b/Sources/FlowStacks/Backport.swift new file mode 100644 index 0000000..c0c9d3b --- /dev/null +++ b/Sources/FlowStacks/Backport.swift @@ -0,0 +1,10 @@ +import Foundation +import SwiftUI + +struct Backport { + let content: Content +} + +extension View { + var backport: Backport { Backport(content: self) } +} diff --git a/Sources/FlowStacks/Node.swift b/Sources/FlowStacks/Node.swift index e06a7f0..27fe1ce 100644 --- a/Sources/FlowStacks/Node.swift +++ b/Sources/FlowStacks/Node.swift @@ -105,7 +105,7 @@ indirect enum Node: View { onDismiss: onDismiss, content: { next } ) - .cover( + .backport.cover( isPresented: coverBinding, onDismiss: onDismiss, content: { next } @@ -117,7 +117,7 @@ indirect enum Node: View { NavigationLink(destination: next, isActive: pushBinding, label: EmptyView.init) .hidden() ) - .backported.present( + .backport.present( asSheet: asSheet, isPresented: asSheet ? sheetBinding : coverBinding, onDismiss: onDismiss, diff --git a/Sources/FlowStacks/View+cover.swift b/Sources/FlowStacks/View+cover.swift index 2ca2f49..376f73d 100644 --- a/Sources/FlowStacks/View+cover.swift +++ b/Sources/FlowStacks/View+cover.swift @@ -1,46 +1,42 @@ import Foundation import SwiftUI -extension View { - /// A shim for presenting a full-screen cover that falls back on a sheet presentation on platforms - /// where fullScreenCover is unavailable. - @ViewBuilder - func cover(isPresented: Binding, onDismiss: (() -> Void)? = nil, @ViewBuilder content: @escaping () -> Content) -> some View { - #if os(macOS) - self - .sheet( - isPresented: isPresented, - onDismiss: onDismiss, - content: content - ) - #else - if #available(iOS 14.0, tvOS 14.0, macOS 99.9, *) { - self - .fullScreenCover( - isPresented: isPresented, - onDismiss: onDismiss, - content: content - ) - } else { - self - .sheet( - isPresented: isPresented, - onDismiss: onDismiss, - content: content - ) - } - #endif - } -} +extension Backport where Content: View { -public extension Backported where Content: View { + /// A shim for presenting a full-screen cover that falls back on a sheet presentation on platforms + /// where fullScreenCover is unavailable. + @ViewBuilder + func cover(isPresented: Binding, onDismiss: (() -> Void)? = nil, @ViewBuilder content contentBuilder: @escaping () -> Content) -> some View { + #if os(macOS) + self + content.sheet( + isPresented: isPresented, + onDismiss: onDismiss, + content: content + ) + #else + if #available(iOS 14.0, tvOS 14.0, macOS 99.9, *) { + content.fullScreenCover( + isPresented: isPresented, + onDismiss: onDismiss, + content: contentBuilder + ) + } else { + content.sheet( + isPresented: isPresented, + onDismiss: onDismiss, + content: contentBuilder + ) + } + #endif + } /// NOTE: On iOS 14.4 and below, a bug prevented multiple sheet/fullScreenCover modifiers being chained /// on the same view, so we conditionally add the sheet/cover modifiers as a workaround. See /// https://developer.apple.com/documentation/ios-ipados-release-notes/ios-ipados-14_5-release-notes @ViewBuilder func present(asSheet: Bool, isPresented: Binding, onDismiss: (() -> Void)? = nil, @ViewBuilder content contentBuilder: @escaping () -> Content) -> some View { - + if asSheet { if #available(iOS 14.5, *) { content.sheet( isPresented: isPresented, @@ -57,14 +53,13 @@ public extension Backported where Content: View { ) ) } - + } else { + cover( + isPresented: isPresented, + onDismiss: nil, + content: contentBuilder + ) + } } } -public struct Backported { - let content: Content -} - -public extension View { - var backported: Backported { Backported(content: self) } -} From 4a37430477522ede09c708ed0d9c3cf2d14f03a0 Mon Sep 17 00:00:00 2001 From: Alejandro Ruiz Ponce Date: Wed, 22 Jun 2022 14:52:46 +0200 Subject: [PATCH 5/7] Removing cover --- Sources/FlowStacks/View+cover.swift | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/Sources/FlowStacks/View+cover.swift b/Sources/FlowStacks/View+cover.swift index 376f73d..d2d3d92 100644 --- a/Sources/FlowStacks/View+cover.swift +++ b/Sources/FlowStacks/View+cover.swift @@ -36,7 +36,7 @@ extension Backport where Content: View { /// https://developer.apple.com/documentation/ios-ipados-release-notes/ios-ipados-14_5-release-notes @ViewBuilder func present(asSheet: Bool, isPresented: Binding, onDismiss: (() -> Void)? = nil, @ViewBuilder content contentBuilder: @escaping () -> Content) -> some View { - if asSheet { + if #available(iOS 14.5, *) { content.sheet( isPresented: isPresented, @@ -53,13 +53,7 @@ extension Backport where Content: View { ) ) } - } else { - cover( - isPresented: isPresented, - onDismiss: nil, - content: contentBuilder - ) - } + } } From ef374c1c6fd0fbf9a3cd6fbba08ee037ffe6c8d9 Mon Sep 17 00:00:00 2001 From: Alejandro Ruiz Ponce Date: Wed, 22 Jun 2022 14:58:27 +0200 Subject: [PATCH 6/7] Cleaning cover code --- Sources/FlowStacks/Node.swift | 4 +--- Sources/FlowStacks/View+cover.swift | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/Sources/FlowStacks/Node.swift b/Sources/FlowStacks/Node.swift index 27fe1ce..0a1a235 100644 --- a/Sources/FlowStacks/Node.swift +++ b/Sources/FlowStacks/Node.swift @@ -111,15 +111,13 @@ indirect enum Node: View { content: { next } ) } else { - let asSheet = next?.route?.style.isSheet ?? false screenView .background( NavigationLink(destination: next, isActive: pushBinding, label: EmptyView.init) .hidden() ) .backport.present( - asSheet: asSheet, - isPresented: asSheet ? sheetBinding : coverBinding, + isPresented: sheetBinding, onDismiss: onDismiss, content: { next } ) diff --git a/Sources/FlowStacks/View+cover.swift b/Sources/FlowStacks/View+cover.swift index d2d3d92..f742d5b 100644 --- a/Sources/FlowStacks/View+cover.swift +++ b/Sources/FlowStacks/View+cover.swift @@ -35,8 +35,7 @@ extension Backport where Content: View { /// on the same view, so we conditionally add the sheet/cover modifiers as a workaround. See /// https://developer.apple.com/documentation/ios-ipados-release-notes/ios-ipados-14_5-release-notes @ViewBuilder - func present(asSheet: Bool, isPresented: Binding, onDismiss: (() -> Void)? = nil, @ViewBuilder content contentBuilder: @escaping () -> Content) -> some View { - + func present(isPresented: Binding, onDismiss: (() -> Void)? = nil, @ViewBuilder content contentBuilder: @escaping () -> Content) -> some View { if #available(iOS 14.5, *) { content.sheet( isPresented: isPresented, @@ -53,7 +52,6 @@ extension Backport where Content: View { ) ) } - } } From 528c54f9f3b4044e5a491f40956bb9d81436f0f0 Mon Sep 17 00:00:00 2001 From: Alejandro Ruiz Ponce Date: Wed, 22 Jun 2022 15:37:23 +0200 Subject: [PATCH 7/7] Add onDismiss closures --- Sources/FlowStacks/View+cover.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/FlowStacks/View+cover.swift b/Sources/FlowStacks/View+cover.swift index f742d5b..bcaf945 100644 --- a/Sources/FlowStacks/View+cover.swift +++ b/Sources/FlowStacks/View+cover.swift @@ -39,7 +39,7 @@ extension Backport where Content: View { if #available(iOS 14.5, *) { content.sheet( isPresented: isPresented, - onDismiss: nil, + onDismiss: onDismiss, content: contentBuilder ) } else { @@ -47,7 +47,7 @@ extension Backport where Content: View { EmptyView() .sheet( isPresented: isPresented, - onDismiss: nil, + onDismiss: onDismiss, content: contentBuilder ) )