From b093b2e6ac4b8c265d7d167517fe42a15744474d Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Mon, 27 May 2024 14:22:17 -0700 Subject: [PATCH] Leverage `alert(item:)` in `alert(_ state:)` --- Sources/SwiftUINavigation/Alert.swift | 40 +++++++--------- .../ConfirmationDialog.swift | 48 +++++++++---------- 2 files changed, 42 insertions(+), 46 deletions(-) diff --git a/Sources/SwiftUINavigation/Alert.swift b/Sources/SwiftUINavigation/Alert.swift index d3f0e10853..cfe8fced57 100644 --- a/Sources/SwiftUINavigation/Alert.swift +++ b/Sources/SwiftUINavigation/Alert.swift @@ -20,17 +20,15 @@ _ state: Binding?>, action handler: @escaping (Value?) -> Void = { (_: Never?) in } ) -> some View { - self.alert( - (state.wrappedValue?.title).map(Text.init) ?? Text(verbatim: ""), - isPresented: state.isPresent(), - presenting: state.wrappedValue, - actions: { - ForEach($0.buttons) { - Button($0, action: handler) - } - }, - message: { $0.message.map { Text($0) } } - ) + alert(item: state) { + Text($0.title) + } actions: { + ForEach($0.buttons) { + Button($0, action: handler) + } + } message: { + $0.message.map(Text.init) + } } /// Presents an alert from a binding to optional alert state. @@ -52,17 +50,15 @@ _ state: Binding?>, action handler: @escaping (Value?) async -> Void = { (_: Never?) async in } ) -> some View { - self.alert( - (state.wrappedValue?.title).map(Text.init) ?? Text(verbatim: ""), - isPresented: state.isPresent(), - presenting: state.wrappedValue, - actions: { - ForEach($0.buttons) { - Button($0, action: handler) - } - }, - message: { $0.message.map { Text($0) } } - ) + alert(item: state) { + Text($0.title) + } actions: { + ForEach($0.buttons) { + Button($0, action: handler) + } + } message: { + $0.message.map(Text.init) + } } } #endif // canImport(SwiftUI) diff --git a/Sources/SwiftUINavigation/ConfirmationDialog.swift b/Sources/SwiftUINavigation/ConfirmationDialog.swift index 6543e2b9f9..eae1840819 100644 --- a/Sources/SwiftUINavigation/ConfirmationDialog.swift +++ b/Sources/SwiftUINavigation/ConfirmationDialog.swift @@ -19,18 +19,18 @@ _ state: Binding?>, action handler: @escaping (Value?) -> Void = { (_: Never?) in } ) -> some View { - self.confirmationDialog( - state.wrappedValue.flatMap { Text($0.title) } ?? Text(verbatim: ""), - isPresented: state.isPresent(), - titleVisibility: state.wrappedValue.map { .init($0.titleVisibility) } ?? .automatic, - presenting: state.wrappedValue, - actions: { - ForEach($0.buttons) { - Button($0, action: handler) - } - }, - message: { $0.message.map { Text($0) } } - ) + confirmationDialog( + item: state, + titleVisibility: state.wrappedValue.map { .init($0.titleVisibility) } ?? .automatic + ) { + Text($0.title) + } actions: { + ForEach($0.buttons) { + Button($0, action: handler) + } + } message: { + $0.message.map(Text.init) + } } /// Presents a confirmation dialog from a binding to optional confirmation dialog state. @@ -53,18 +53,18 @@ _ state: Binding?>, action handler: @escaping (Value?) async -> Void = { (_: Never?) async in } ) -> some View { - self.confirmationDialog( - state.wrappedValue.flatMap { Text($0.title) } ?? Text(verbatim: ""), - isPresented: state.isPresent(), - titleVisibility: state.wrappedValue.map { .init($0.titleVisibility) } ?? .automatic, - presenting: state.wrappedValue, - actions: { - ForEach($0.buttons) { - Button($0, action: handler) - } - }, - message: { $0.message.map { Text($0) } } - ) + confirmationDialog( + item: state, + titleVisibility: state.wrappedValue.map { .init($0.titleVisibility) } ?? .automatic + ) { + Text($0.title) + } actions: { + ForEach($0.buttons) { + Button($0, action: handler) + } + } message: { + $0.message.map(Text.init) + } } } #endif // canImport(SwiftUI)