Skip to content

Commit ae94a47

Browse files
committed
Add export button type
1 parent 517b3db commit ae94a47

File tree

3 files changed

+79
-32
lines changed

3 files changed

+79
-32
lines changed

RELEASE_NOTES.md

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ SwiftUIKit makes its best effort to honor semver, but breaking changes can occur
44

55

66

7+
## 5.1.4
8+
9+
### ✨ Features
10+
11+
* `ButtonType` has a several new types.
12+
13+
14+
715
## 5.1.3
816

917
### ✨ Features

Sources/SwiftUIKit/Buttons/StandardButtonType.swift Sources/SwiftUIKit/Buttons/ButtonType.swift

+47-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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

1111
public 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.
3343
public 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+
4466
public 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-
*/

Sources/SwiftUIKit/Resources/Localizable.xcstrings

+24-14
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,6 @@
2626
}
2727
}
2828
},
29-
"Button.AddFavorite" : {
30-
"localizations" : {
31-
"en" : {
32-
"stringUnit" : {
33-
"state" : "translated",
34-
"value" : "Add Favorite"
35-
}
36-
}
37-
}
38-
},
3929
"Button.AddToFavorites" : {
4030
"localizations" : {
4131
"en" : {
@@ -126,6 +116,26 @@
126116
}
127117
}
128118
},
119+
"Button.Export" : {
120+
"localizations" : {
121+
"en" : {
122+
"stringUnit" : {
123+
"state" : "translated",
124+
"value" : "Export"
125+
}
126+
}
127+
}
128+
},
129+
"Button.Like" : {
130+
"localizations" : {
131+
"en" : {
132+
"stringUnit" : {
133+
"state" : "translated",
134+
"value" : "Like"
135+
}
136+
}
137+
}
138+
},
129139
"Button.OK" : {
130140
"localizations" : {
131141
"en" : {
@@ -146,22 +156,22 @@
146156
}
147157
}
148158
},
149-
"Button.RemoveFavorite" : {
159+
"Button.RemoveFromFavorites" : {
150160
"localizations" : {
151161
"en" : {
152162
"stringUnit" : {
153163
"state" : "translated",
154-
"value" : "Remove Favorite"
164+
"value" : "Remove From Favorites"
155165
}
156166
}
157167
}
158168
},
159-
"Button.RemoveFromFavorites" : {
169+
"Button.RemoveLike" : {
160170
"localizations" : {
161171
"en" : {
162172
"stringUnit" : {
163173
"state" : "translated",
164-
"value" : "Remove From Favorites"
174+
"value" : "Remove Like"
165175
}
166176
}
167177
}

0 commit comments

Comments
 (0)