Cotton code
128 lines · 5.0 KB · SwiftRaw
// The one way a screen asks — over a binding.//// Every question a screen asks before an act is `confirmDecision`: the torn// slip, the sentence the product wrote, Cancel in pencil and the act in ink or// red. Not `.alert` and not `.confirmationDialog` — a system alert speaks in the// platform's voice with the platform's buttons, and a screen that reached for// one was a screen off the paper (`ConfirmSweepTests`). `WorkConfirm`, the Work// lane's own copy, folded in here.//// This form takes the screen's own BINDING, so the host writes the question// and the act and nothing about closing it:////   • Cancel, and a tap on the dimmed screen, set the binding false.//   • Confirm CLOSES THEN ACTS when `isLoading` is left out — what a host that//     reports the result in a toast wants.//   • Pass `isLoading` and the HOST owns dismissal: both verbs stay inert while//     it is true (`ConfirmDecisionDialog` holds that), and the host closes the//     question when the write settles. That is `WorkConfirm`'s `busy`, kept.import SwiftUIenum ConfirmDecisionClosing {    /// Whether a confirm closes the question itself: only when the host did    /// not ask to show the write in flight.    static func closesOnConfirm(isLoading: Bool?) -> Bool { isLoading == nil }    /// The confirm as the dialog runs it: close (or not), then the act.    static func confirm(isLoading: Bool?, close: () -> Void, act: () -> Void) {        if closesOnConfirm(isLoading: isLoading) { close() }        act()    }}extension View {    /// Ask a question that cannot be taken back, over this screen, presented    /// by the screen's own binding.    func confirmDecision(        isPresented: Binding<Bool>,        title: String,        message: String,        confirmLabel: String,        cancelLabel: String? = nil,        tone: ConfirmDecisionTone = .danger,        isLoading: Bool? = nil,        identifier: String? = nil,        onConfirm: @escaping () -> Void    ) -> some View {        confirmDecision(            isPresented: isPresented.wrappedValue,            title: title,            message: message,            confirmLabel: confirmLabel,            cancelLabel: cancelLabel,            tone: tone,            isLoading: isLoading ?? false,            identifier: identifier,            onConfirm: {                ConfirmDecisionClosing.confirm(                    isLoading: isLoading,                    close: { isPresented.wrappedValue = false },                    act: onConfirm                )            },            onCancel: { isPresented.wrappedValue = false }        )    }    /// The same question, presented while `item` is non-nil — the act gets    /// the item it was asked about, taken BEFORE the question closes.    func confirmDecision<Item>(        item: Binding<Item?>,        title: (Item) -> String,        message: (Item) -> String,        confirmLabel: (Item) -> String,        cancelLabel: String? = nil,        tone: ConfirmDecisionTone = .danger,        identifier: String? = nil,        onConfirm: @escaping (Item) -> Void    ) -> some View {        let held = item.wrappedValue        return confirmDecision(            isPresented: Binding(                get: { item.wrappedValue != nil },                set: { if !$0 { item.wrappedValue = nil } }            ),            title: held.map(title) ?? "",            message: held.map(message) ?? "",            confirmLabel: held.map(confirmLabel) ?? "",            cancelLabel: cancelLabel,            tone: tone,            identifier: identifier,            onConfirm: { if let held { onConfirm(held) } }        )    }}extension View {    /// Ask how far a write to a record that repeats reaches, while `item` is    /// non-nil: "Only this one" or "Every one" (or "Every one" alone), each    /// handed the item it was asked about BEFORE the question closes.    func scopeDecision<Item>(        item: Binding<Item?>,        question: (Item) -> ScopeQuestion,        identifier: String,        onChoose: @escaping (Item, WriteScope) -> Void    ) -> some View {        let held = item.wrappedValue        let asked = held.map(question)        func choose(_ scope: WriteScope) {            item.wrappedValue = nil            if let held { onChoose(held, scope) }        }        return overlay {            if let asked {                ConfirmDecisionDialog(                    title: asked.title, message: asked.message,                    confirmLabel: asked.offersThis ? ScopeQuestion.onlyThis : ScopeQuestion.everyOne,                    tone: asked.destructive ? .danger : .primary, identifier: identifier,                    onConfirm: { choose(asked.offersThis ? .this : .all) },                    onCancel: { item.wrappedValue = nil }                )                .other(asked.offersThis ? ScopeQuestion.everyOne : nil) { choose(.all) }                .transition(.opacity)            }        }        .animation(.easeInOut(duration: 0.15), value: held != nil)    }}