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

Fix sheet presentation in < iOS 14.5 #36

Closed
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions Sources/FlowStacks/Backport.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Foundation
import SwiftUI

struct Backport<Content> {
let content: Content
}

extension View {
var backport: Backport<Self> { Backport(content: self) }
}
8 changes: 3 additions & 5 deletions Sources/FlowStacks/Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,19 @@ indirect enum Node<Screen, V: View>: View {
onDismiss: onDismiss,
content: { next }
)
.cover(
.backport.cover(
isPresented: coverBinding,
onDismiss: onDismiss,
content: { next }
)
} else {
let asSheet = next?.route?.style.isSheet ?? false
screenView
.background(
NavigationLink(destination: next, isActive: pushBinding, label: EmptyView.init)
.hidden()
)
.present(
asSheet: asSheet,
isPresented: asSheet ? sheetBinding : coverBinding,
.backport.present(
isPresented: sheetBinding,
onDismiss: onDismiss,
content: { next }
)
Expand Down
101 changes: 52 additions & 49 deletions Sources/FlowStacks/View+cover.swift
Original file line number Diff line number Diff line change
@@ -1,54 +1,57 @@
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<Content: View>(isPresented: Binding<Bool>, 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 {

/// 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<Content: View>(asSheet: Bool, isPresented: Binding<Bool>, onDismiss: (() -> Void)? = nil, @ViewBuilder content: @escaping () -> Content) -> some View {
if asSheet {
self.sheet(
isPresented: isPresented,
onDismiss: nil,
content: content
)
} else {
self.cover(
isPresented: isPresented,
onDismiss: nil,
content: content
)
}
}
/// A shim for presenting a full-screen cover that falls back on a sheet presentation on platforms
/// where fullScreenCover is unavailable.
@ViewBuilder
func cover<Content: View>(isPresented: Binding<Bool>, 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<Content: View>(isPresented: Binding<Bool>, onDismiss: (() -> Void)? = nil, @ViewBuilder content contentBuilder: @escaping () -> Content) -> some View {
if #available(iOS 14.5, *) {
content.sheet(
isPresented: isPresented,
onDismiss: onDismiss,
content: contentBuilder
)
} else {
content.background(
EmptyView()
.sheet(
isPresented: isPresented,
onDismiss: onDismiss,
content: contentBuilder
)
)
}
}
}