1
1
//
2
- // Button+Standard .swift
2
+ // ButtonType .swift
3
3
// SwiftUIKit
4
4
//
5
5
// Created by Daniel Saidi on 2024-04-30.
@@ -10,18 +10,28 @@ import SwiftUI
10
10
11
11
public extension Button {
12
12
13
- /// Create a new ``StandardType``-based button.
13
+ /// Create a new ``ButtonType``-based button.
14
+ ///
15
+ /// You have to tint the icon separately for destructive
16
+ /// buttons like ``ButtonType/delete``. This can be done
17
+ /// automatically, by using `.labelStyle(.titleAndIcon)`.
14
18
init (
15
19
_ type: ButtonType ,
16
20
_ title: LocalizedStringKey ? = nil ,
17
21
_ icon: Image ? = nil ,
18
22
bundle: Bundle ? = nil ,
19
23
action: @escaping ( ) -> Void
20
24
) where Label == SwiftUI . Label < Text , Image ? > {
21
- self . init ( role: type. role, action: action) {
25
+ self . init (
26
+ role: type. role,
27
+ action: action
28
+ ) {
29
+ let bundle : Bundle ? = title == nil ? . module : bundle
30
+ let displayTitle = title ?? type. title
31
+ let icon = icon ?? type. image
22
32
Label (
23
- title: { Text ( title ?? type . title , bundle: title == nil ? . module : bundle) } ,
24
- icon: { icon ?? type . image }
33
+ title: { Text ( displayTitle , bundle: bundle) } ,
34
+ icon: { icon }
25
35
)
26
36
}
27
37
}
@@ -31,16 +41,28 @@ public extension Button {
31
41
/// This enum defines standard button types and provides
32
42
/// standard localized texts and icons.
33
43
public enum ButtonType : String , CaseIterable , Identifiable {
34
- case add, addFavorite , addToFavorites,
44
+ case add, addToFavorites,
35
45
cancel, call, copy,
36
46
delete, deselect, done,
37
- edit, email,
47
+ edit, email, export,
48
+ like,
38
49
ok,
39
50
paste,
40
- removeFavorite , removeFromFavorites ,
51
+ removeFromFavorites , removeLike ,
41
52
search, select, share
42
53
}
43
54
55
+ public extension ButtonType {
56
+
57
+ static func toggleFavorite( isFavorite: Bool ) -> ButtonType {
58
+ isFavorite ? . removeFromFavorites : . addToFavorites
59
+ }
60
+
61
+ static func toggleLike( isLiked: Bool ) -> ButtonType {
62
+ isLiked ? . removeLike : . like
63
+ }
64
+ }
65
+
44
66
public extension ButtonType {
45
67
46
68
var id : String { rawValue }
@@ -53,8 +75,7 @@ public extension ButtonType {
53
75
var imageName : String ? {
54
76
switch self {
55
77
case . add: " plus "
56
- case . addFavorite: " star.circle "
57
- case . addToFavorites: " star.circle "
78
+ case . addToFavorites: " star "
58
79
case . cancel: " xmark "
59
80
case . call: " phone "
60
81
case . copy: " doc.on.doc "
@@ -63,10 +84,12 @@ public extension ButtonType {
63
84
case . done: " checkmark "
64
85
case . edit: " pencil "
65
86
case . email: " envelope "
87
+ case . export: " square.and.arrow.up "
88
+ case . like: " heart "
66
89
case . ok: " checkmark "
67
90
case . paste: " clipboard "
68
- case . removeFavorite : " star.circle .fill "
69
- case . removeFromFavorites : " star.circle .fill"
91
+ case . removeFromFavorites : " star.fill "
92
+ case . removeLike : " heart .fill"
70
93
case . search: " magnifyingglass "
71
94
case . select: " checkmark.circle "
72
95
case . share: " square.and.arrow.up "
@@ -97,20 +120,21 @@ public extension ButtonType {
97
120
var title : LocalizedStringKey {
98
121
switch self {
99
122
case . add: " Button.Add "
100
- case . addFavorite: " Button.AddFavorite "
101
123
case . addToFavorites: " Button.AddToFavorites "
102
124
case . call: " Button.Call "
103
125
case . cancel: " Button.Cancel "
104
126
case . copy: " Button.Copy "
105
127
case . deselect: " Button.Deselect "
106
128
case . edit: " Button.Edit "
107
129
case . email: " Button.Email "
130
+ case . export: " Button.Export "
108
131
case . delete: " Button.Delete "
109
132
case . done: " Button.Done "
133
+ case . like: " Button.Like "
110
134
case . ok: " Button.OK "
111
135
case . paste: " Button.Paste "
112
- case . removeFavorite: " Button.RemoveFavorite "
113
136
case . removeFromFavorites: " Button.RemoveFromFavorites "
137
+ case . removeLike: " Button.RemoveLike "
114
138
case . search: " Button.Search "
115
139
case . select: " Button.Select "
116
140
case . share: " Button.Share "
@@ -140,20 +164,26 @@ public extension View {
140
164
}
141
165
}
142
166
143
- /*
167
+
144
168
#Preview {
145
169
146
170
@ViewBuilder
147
171
func buttons( ) -> some View {
148
172
Section {
149
- ForEach(Button.StandardType .allCases) { type in
173
+ ForEach ( ButtonType . allCases) { type in
150
174
Button ( type) { print ( " Tapped \( type. title) " ) }
151
175
}
152
176
}
177
+ Section {
178
+ Button ( . toggleFavorite( isFavorite: false ) ) { }
179
+ Button ( . toggleFavorite( isFavorite: true ) ) { }
180
+ Button ( . toggleLike( isLiked: false ) ) { }
181
+ Button ( . toggleLike( isLiked: true ) ) { }
182
+ }
153
183
}
154
184
155
185
return List {
156
- buttons()
186
+ buttons ( ) . labelStyle ( . titleAndIcon )
157
187
buttons ( ) . labelStyle ( . titleOnly)
158
188
buttons ( ) . labelStyle ( . iconOnly)
159
189
}
@@ -163,4 +193,3 @@ public extension View {
163
193
}
164
194
}
165
195
}
166
- */
0 commit comments