11//
2- // Button+Standard .swift
2+ // ButtonType .swift
33// SwiftUIKit
44//
55// Created by Daniel Saidi on 2024-04-30.
@@ -10,18 +10,28 @@ import SwiftUI
1010
1111public extension Button {
1212
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)`.
1418 init (
1519 _ type: ButtonType ,
1620 _ title: LocalizedStringKey ? = nil ,
1721 _ icon: Image ? = nil ,
1822 bundle: Bundle ? = nil ,
1923 action: @escaping ( ) -> Void
2024 ) 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
2232 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 }
2535 )
2636 }
2737 }
@@ -31,16 +41,28 @@ public extension Button {
3141/// This enum defines standard button types and provides
3242/// standard localized texts and icons.
3343public enum ButtonType : String , CaseIterable , Identifiable {
34- case add, addFavorite , addToFavorites,
44+ case add, addToFavorites,
3545 cancel, call, copy,
3646 delete, deselect, done,
37- edit, email,
47+ edit, email, export,
48+ like,
3849 ok,
3950 paste,
40- removeFavorite , removeFromFavorites ,
51+ removeFromFavorites , removeLike ,
4152 search, select, share
4253}
4354
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+
4466public extension ButtonType {
4567
4668 var id : String { rawValue }
@@ -53,8 +75,7 @@ public extension ButtonType {
5375 var imageName : String ? {
5476 switch self {
5577 case . add: " plus "
56- case . addFavorite: " star.circle "
57- case . addToFavorites: " star.circle "
78+ case . addToFavorites: " star "
5879 case . cancel: " xmark "
5980 case . call: " phone "
6081 case . copy: " doc.on.doc "
@@ -63,10 +84,12 @@ public extension ButtonType {
6384 case . done: " checkmark "
6485 case . edit: " pencil "
6586 case . email: " envelope "
87+ case . export: " square.and.arrow.up "
88+ case . like: " heart "
6689 case . ok: " checkmark "
6790 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"
7093 case . search: " magnifyingglass "
7194 case . select: " checkmark.circle "
7295 case . share: " square.and.arrow.up "
@@ -97,20 +120,21 @@ public extension ButtonType {
97120 var title : LocalizedStringKey {
98121 switch self {
99122 case . add: " Button.Add "
100- case . addFavorite: " Button.AddFavorite "
101123 case . addToFavorites: " Button.AddToFavorites "
102124 case . call: " Button.Call "
103125 case . cancel: " Button.Cancel "
104126 case . copy: " Button.Copy "
105127 case . deselect: " Button.Deselect "
106128 case . edit: " Button.Edit "
107129 case . email: " Button.Email "
130+ case . export: " Button.Export "
108131 case . delete: " Button.Delete "
109132 case . done: " Button.Done "
133+ case . like: " Button.Like "
110134 case . ok: " Button.OK "
111135 case . paste: " Button.Paste "
112- case . removeFavorite: " Button.RemoveFavorite "
113136 case . removeFromFavorites: " Button.RemoveFromFavorites "
137+ case . removeLike: " Button.RemoveLike "
114138 case . search: " Button.Search "
115139 case . select: " Button.Select "
116140 case . share: " Button.Share "
@@ -140,20 +164,26 @@ public extension View {
140164 }
141165}
142166
143- /*
167+
144168#Preview {
145169
146170 @ViewBuilder
147171 func buttons( ) -> some View {
148172 Section {
149- ForEach(Button.StandardType .allCases) { type in
173+ ForEach ( ButtonType . allCases) { type in
150174 Button ( type) { print ( " Tapped \( type. title) " ) }
151175 }
152176 }
177+ Section {
178+ Button ( . toggleFavorite( isFavorite: false ) ) { }
179+ Button ( . toggleFavorite( isFavorite: true ) ) { }
180+ Button ( . toggleLike( isLiked: false ) ) { }
181+ Button ( . toggleLike( isLiked: true ) ) { }
182+ }
153183 }
154184
155185 return List {
156- buttons()
186+ buttons ( ) . labelStyle ( . titleAndIcon )
157187 buttons ( ) . labelStyle ( . titleOnly)
158188 buttons ( ) . labelStyle ( . iconOnly)
159189 }
@@ -163,4 +193,3 @@ public extension View {
163193 }
164194 }
165195}
166- */
0 commit comments