// // AlertWithData.swift // TuskerComponents // // Created by Shadowfacts on 11/9/22. // Copyright © 2022 Shadowfacts. All rights reserved. // import SwiftUI struct AlertWithData: ViewModifier { let title: LocalizedStringKey @Binding var data: Data? let actions: (Data) -> A let message: (Data) -> M private var isPresented: Binding { Binding(get: { data != nil }, set: { newValue in guard !newValue else { fatalError("Cannot set isPresented to true without data") } data = nil }) } init(title: LocalizedStringKey, data: Binding, @ViewBuilder actions: @escaping (Data) -> A, @ViewBuilder message: @escaping (Data) -> M) { self.title = title self._data = data self.actions = actions self.message = message } func body(content: Content) -> some View { content .alert(title, isPresented: isPresented, presenting: data, actions: actions, message: message) } } extension View { public func alertWithData(_ title: LocalizedStringKey, data: Binding, @ViewBuilder actions: @escaping (Data) -> A, @ViewBuilder message: @escaping (Data) -> M) -> some View { modifier(AlertWithData(title: title, data: data, actions: actions, message: message)) } }