blob: 6c39da8499a428eefa22f7f64f3e8a8991c01ac4 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
import SwiftUI
struct CollectionAlertsModifier: ViewModifier {
@Binding var isNewCollectionAlertPresented: Bool
@Binding var newCollectionName: String
@Binding var isCollectionErrorAlertPresented: Bool
let onCreate: (String) -> Void
func body(content: Content) -> some View {
content
.alert(
"New Collection",
isPresented: $isNewCollectionAlertPresented
) {
TextField("Collection Name", text: $newCollectionName)
Button("Cancel") {
newCollectionName = ""
isNewCollectionAlertPresented = false
}
Button("Create") {
if newCollectionName.isEmpty {
isCollectionErrorAlertPresented = true
} else {
onCreate(newCollectionName)
newCollectionName = ""
isNewCollectionAlertPresented = false
}
}
}
.alert(
"Error",
isPresented: $isCollectionErrorAlertPresented
) {
Button("OK", role: .cancel) { () }
} message: {
Text("Collection name cannot be empty.")
}
}
}
extension View {
func collectionAlerts(
isNewCollectionAlertPresented: Binding<Bool>,
newCollectionName: Binding<String>,
isCollectionErrorAlertPresented: Binding<Bool>,
onCreate: @escaping (String) -> Void
) -> some View {
modifier(
CollectionAlertsModifier(
isNewCollectionAlertPresented: isNewCollectionAlertPresented,
newCollectionName: newCollectionName,
isCollectionErrorAlertPresented: isCollectionErrorAlertPresented,
onCreate: onCreate
)
)
}
}
|