From d5eb881a373537c82b5fe669b0768de1f166c66b Mon Sep 17 00:00:00 2001 From: taka-2120 Date: Fri, 21 Feb 2025 21:30:42 +0900 Subject: [PATCH 1/3] feat: enable using AlertController --- Sources/AlertPresentable/View+Alert.swift | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/Sources/AlertPresentable/View+Alert.swift b/Sources/AlertPresentable/View+Alert.swift index 682567a..3c6f922 100644 --- a/Sources/AlertPresentable/View+Alert.swift +++ b/Sources/AlertPresentable/View+Alert.swift @@ -14,27 +14,36 @@ public extension View { /// - parameter alertPresentable: A binding class (mostly view model) inherited `AlertPresentable` protocol. /// func alert(using alertPresentable: Binding) -> some View { - modifier(CommonAlert(alertPresentable: alertPresentable)) + modifier(CommonAlert(alertController: alertPresentable.alertController)) + } + + /// + /// Show native alert using a `AlertController`. + /// + /// - parameter alertController: A binding variable of `AlertController`. + /// + func alert(using alertController: Binding) -> some View { + modifier(CommonAlert(alertController: alertController)) } } -private struct CommonAlert: ViewModifier { - @Binding var alertPresentable: T +private struct CommonAlert: ViewModifier { + @Binding var alertController: AlertController func body(content: Content) -> some View { content - .alert(alertPresentable.alertController.title, isPresented: $alertPresentable.alertController.isPresented) { - ForEach(alertPresentable.alertController.actions) { action in + .alert(alertController.title, isPresented: $alertController.isPresented) { + ForEach(alertController.actions) { action in Button(action.label, role: action.role) { if let action = action.action { action() return } - alertPresentable.alertController.isPresented = false + alertController.isPresented = false } } } message: { - Text(alertPresentable.alertController.message) + Text(alertController.message) } } } From eb37873433ee1d9620d882720a98d52e58a2c607 Mon Sep 17 00:00:00 2001 From: taka-2120 Date: Fri, 21 Feb 2025 21:31:06 +0900 Subject: [PATCH 2/3] docs: update for using AlertController --- README.md | 87 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 53 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 6e3f405..6f8d964 100644 --- a/README.md +++ b/README.md @@ -50,44 +50,63 @@ Show native alerts from ViewModel with ease. ## Usage -1. Add `AlertPresentable` protocol to your class (like view model). +1. Setup `AlertController` - ```swift - import AlertPresentable + - Using View Model - @Observable - class YourClass: AlertPresentable { - ... - } - ``` - -2. Add a necessary variable to your class. - - ```swift - import AlertPresentable - - @Observable - class YourViewModel: AlertPresentable { - var alertController = AlertController() - } - ``` - -3. Add `alert` modifier to your view. - - ```swift - import AlertPresentable - - struct YourView: View { - @State private var viewModel = YourViewModel() - - var body: some View { - yourView - .alert(using: viewModel) + a. Add `AlertPresentable` protocol to your class (like view model). + + ```swift + import AlertPresentable + + @Observable + class YourClass: AlertPresentable { + ... + } + ``` + + b. Add a necessary variable to your class. + + ```swift + import AlertPresentable + + @Observable + class YourViewModel: AlertPresentable { + var alertController = AlertController() + } + ``` + + c. Add `alert` modifier to your view. + + ```swift + import AlertPresentable + + struct YourView: View { + @State private var viewModel = YourViewModel() + + var body: some View { + yourView + .alert(using: viewModel) + } + } + ``` + + - Using View Only (Above 1.1.0 or later) + + ```swift + import AlertPresentable + + struct YourView: View { + @State private var alertController = AlertController() + + var body: some View { + yourView + .alert(using: $alertController) + } } - } - ``` + ``` -4. Call `showAlert` function to show the alert. +2. Call `showAlert` function to show the alert. ```swift // Simplest alert (Show OK button) From 095ccf9b74ad9fa0caf3677f0701ce29717ca1f0 Mon Sep 17 00:00:00 2001 From: taka-2120 Date: Fri, 21 Feb 2025 21:34:45 +0900 Subject: [PATCH 3/3] chore: format import order --- Tests/AlertPresentableTests/AlertPresentableTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/AlertPresentableTests/AlertPresentableTests.swift b/Tests/AlertPresentableTests/AlertPresentableTests.swift index 044b176..4c41f24 100644 --- a/Tests/AlertPresentableTests/AlertPresentableTests.swift +++ b/Tests/AlertPresentableTests/AlertPresentableTests.swift @@ -1,5 +1,5 @@ -import Testing @testable import AlertPresentable +import Testing @Test func example() async throws { // Write your test here and use APIs like `#expect(...)` to check expected conditions.