Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 53 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 16 additions & 7 deletions Sources/AlertPresentable/View+Alert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,36 @@ public extension View {
/// - parameter alertPresentable: A binding class (mostly view model) inherited `AlertPresentable` protocol.
///
func alert(using alertPresentable: Binding<some AlertPresentable>) -> 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<AlertController>) -> some View {
modifier(CommonAlert(alertController: alertController))
}
}

private struct CommonAlert<T: AlertPresentable>: 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)
}
}
}
2 changes: 1 addition & 1 deletion Tests/AlertPresentableTests/AlertPresentableTests.swift
Original file line number Diff line number Diff line change
@@ -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.
Expand Down