|
| 1 | +import SwiftUI |
| 2 | + |
| 3 | +// |
| 4 | +// POSSheet - Wraps default SwiftUI .sheet() modifiers to support sheet presentation detection within POS. |
| 5 | +// |
| 6 | +// Usage: Replace .sheet() with .posSheet() and inject POSSheetManager at the POS root. |
| 7 | +// Sheet presentation can be detected via POSSheetManager.isPresented. |
| 8 | +// |
| 9 | + |
| 10 | +// MARK: - Sheet Detection Infrastructure |
| 11 | + |
| 12 | +final class POSSheetManager: ObservableObject { |
| 13 | + @Published private(set) var isPresented: Bool = false |
| 14 | + private var presentedSheets: Set<String> = [] |
| 15 | + |
| 16 | + func registerSheetPresented(id: String) { |
| 17 | + presentedSheets.insert(id) |
| 18 | + updateState() |
| 19 | + } |
| 20 | + |
| 21 | + func registerSheetDismissed(id: String) { |
| 22 | + presentedSheets.remove(id) |
| 23 | + updateState() |
| 24 | + } |
| 25 | + |
| 26 | + private func updateState() { |
| 27 | + isPresented = !presentedSheets.isEmpty |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// MARK: - Individual Sheet Modifiers |
| 32 | + |
| 33 | +struct POSSheetViewModifier<SheetContent: View>: ViewModifier { |
| 34 | + @EnvironmentObject var sheetManager: POSSheetManager |
| 35 | + @Binding var isPresented: Bool |
| 36 | + let onDismiss: (() -> Void)? |
| 37 | + let sheetContent: () -> SheetContent |
| 38 | + |
| 39 | + @State private var sheetId = UUID().uuidString |
| 40 | + |
| 41 | + func body(content: Content) -> some View { |
| 42 | + content |
| 43 | + .sheet(isPresented: $isPresented, onDismiss: onDismiss, content: sheetContent) |
| 44 | + .onChange(of: isPresented) { newValue in |
| 45 | + if newValue { |
| 46 | + sheetManager.registerSheetPresented(id: sheetId) |
| 47 | + } else { |
| 48 | + sheetManager.registerSheetDismissed(id: sheetId) |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +struct POSSheetViewModifierForItem<Item: Identifiable & Equatable, SheetContent: View>: ViewModifier { |
| 55 | + @EnvironmentObject var sheetManager: POSSheetManager |
| 56 | + @Binding var item: Item? |
| 57 | + let onDismiss: (() -> Void)? |
| 58 | + let sheetContent: (Item) -> SheetContent |
| 59 | + |
| 60 | + @State private var sheetId = UUID().uuidString |
| 61 | + |
| 62 | + func body(content: Content) -> some View { |
| 63 | + content |
| 64 | + .sheet(item: $item, onDismiss: onDismiss, content: sheetContent) |
| 65 | + .onChange(of: item) { newItem in |
| 66 | + let newValue = newItem != nil |
| 67 | + if newValue { |
| 68 | + sheetManager.registerSheetPresented(id: sheetId) |
| 69 | + } else { |
| 70 | + sheetManager.registerSheetDismissed(id: sheetId) |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// MARK: - View Modifiers |
| 77 | + |
| 78 | +extension View { |
| 79 | + /// Shows a sheet with automatic detection of presentation. |
| 80 | + /// |
| 81 | + /// This works exactly like the native .sheet() modifier but automatically tracks |
| 82 | + /// presentation state. |
| 83 | + /// |
| 84 | + /// This will only work in a view hierarchy containing a `POSSheetManager` environment object. |
| 85 | + /// |
| 86 | + /// - Parameters: |
| 87 | + /// - isPresented: Binding to control when the sheet is shown. |
| 88 | + /// - onDismiss: Optional closure executed when the sheet is dismissed. |
| 89 | + /// - content: Content to show in the sheet |
| 90 | + /// - Returns: a modified view which can show the sheet content specified, when applicable. |
| 91 | + func posSheet<SheetContent: View>( |
| 92 | + isPresented: Binding<Bool>, |
| 93 | + onDismiss: (() -> Void)? = nil, |
| 94 | + @ViewBuilder content: @escaping () -> SheetContent |
| 95 | + ) -> some View { |
| 96 | + self.modifier( |
| 97 | + POSSheetViewModifier( |
| 98 | + isPresented: isPresented, |
| 99 | + onDismiss: onDismiss, |
| 100 | + sheetContent: content |
| 101 | + ) |
| 102 | + ) |
| 103 | + } |
| 104 | + |
| 105 | + /// Shows a sheet with automatic detection of presentation. |
| 106 | + /// |
| 107 | + /// This works exactly like the native .sheet(item:) modifier but automatically tracks |
| 108 | + /// presentation state. |
| 109 | + /// |
| 110 | + /// This will only work in a view hierarchy containing a `POSSheetManager` environment object. |
| 111 | + /// |
| 112 | + /// - Parameters: |
| 113 | + /// - item: Binding to control when the sheet is shown. When non-nil, the item is used to build the content. |
| 114 | + /// - onDismiss: Optional closure executed when the sheet is dismissed. |
| 115 | + /// - content: Content to show in the sheet |
| 116 | + /// - Returns: a modified view which can show the sheet content specified, when applicable. |
| 117 | + func posSheet<Item: Identifiable & Equatable, SheetContent: View>( |
| 118 | + item: Binding<Item?>, |
| 119 | + onDismiss: (() -> Void)? = nil, |
| 120 | + @ViewBuilder content: @escaping (Item) -> SheetContent |
| 121 | + ) -> some View { |
| 122 | + self.modifier( |
| 123 | + POSSheetViewModifierForItem( |
| 124 | + item: item, |
| 125 | + onDismiss: onDismiss, |
| 126 | + sheetContent: content |
| 127 | + ) |
| 128 | + ) |
| 129 | + } |
| 130 | +} |
0 commit comments