Skip to content

Commit 739c766

Browse files
authored
Merge pull request #32 from HonQii/SwiftUI
add SwiftUI modifier
2 parents 0dcee61 + 4be44c6 commit 739c766

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,36 @@ alertView.present(duration: 1.5, haptic: .success, completion: nil)
130130

131131
You can remove duration and completion, its have default values.
132132

133+
### SwiftUI
134+
135+
Use like system alert:
136+
137+
* only show message tips
138+
139+
```swift
140+
Button("Show alert") {
141+
showAlert = true
142+
}.alert(isPresent: $showAlert, message: "this is message only")
143+
```
144+
145+
* show message, title, image and other configuration
146+
147+
```swift
148+
Button("Show alert") {
149+
showAlert = true
150+
}.alert(isPresent: $showAlert,
151+
title: "Alert title",
152+
message: "Alert message",
153+
duration: 2.0,
154+
dismissOnTap: false,
155+
present: .custom(UIImage(systemName: "heart")!),
156+
haptic: .success,
157+
layout: .init(),
158+
completion: {
159+
print("Alert is destory")
160+
})
161+
```
162+
133163
## Other Projects
134164

135165
#### [SPPermissions](https://github.com/ivanvorobei/SPPermissions)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//
2+
// SwiftUIView.swift
3+
//
4+
//
5+
// Created by Qi Hon on 2021/6/4.
6+
//
7+
8+
import SwiftUI
9+
10+
@available(iOS 13.0, *)
11+
extension View {
12+
public func alert(
13+
isPresent: Binding<Bool>,
14+
alertView: SPAlertView,
15+
duration: TimeInterval = 2.0,
16+
haptic: SPAlertHaptic = .none
17+
) -> some View {
18+
if isPresent.wrappedValue {
19+
let alertCompletion = alertView.completion
20+
let alertDismiss = {
21+
isPresent.wrappedValue = false
22+
alertCompletion?()
23+
}
24+
alertView.present(duration: duration, haptic: haptic, completion: alertDismiss)
25+
}
26+
return self
27+
}
28+
29+
public func alert(isPresent: Binding<Bool>,
30+
title: String = "",
31+
message: String? = nil,
32+
duration: TimeInterval = 2.0,
33+
dismissOnTap: Bool = true,
34+
present: SPAlertIconPreset = .done,
35+
haptic: SPAlertHaptic = .none,
36+
layout: SPAlertLayout = .init(),
37+
completion: (()-> Void)? = nil
38+
) -> some View {
39+
let alertView = SPAlertView(title: title, message: message, preset: present)
40+
alertView.dismissByTap = dismissOnTap
41+
alertView.layout = layout
42+
alertView.completion = completion
43+
44+
return alert(isPresent: isPresent, alertView: alertView, duration: duration, haptic: haptic)
45+
}
46+
47+
public func alert(isPresent: Binding<Bool>,
48+
message: String,
49+
duration: TimeInterval = 2.0,
50+
dismissOnTap: Bool = true,
51+
haptic: SPAlertHaptic = .none,
52+
completion: (()-> Void)? = nil
53+
) -> some View {
54+
let alertView = SPAlertView(message: message)
55+
alertView.dismissByTap = dismissOnTap
56+
alertView.completion = completion
57+
58+
return alert(isPresent: isPresent, alertView: alertView, duration: duration, haptic: haptic)
59+
}
60+
}

0 commit comments

Comments
 (0)