-
Notifications
You must be signed in to change notification settings - Fork 3
/
ActionButton.swift
320 lines (277 loc) · 8 KB
/
ActionButton.swift
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//
// GenericButton.swift
// PovioKit
//
// Created by Arlind Dushi on 3/17/22.
// Copyright © 2024 Povio Inc. All rights reserved.
//
import SwiftUI
public struct ActionButton: View {
@ObservedObject private var properties = ActionButtonViewModel()
public init() {}
public init(action: @escaping (() -> Void)) {
properties.actionHandler = action
}
public enum Background {
case plain(Color)
case linearGradient(LinearGradient)
case radialGradient(RadialGradient)
case angularGradient(AngularGradient)
}
public var body: some View {
Button(action: {
properties.actionHandler?()
}) {
VStack {
Spacer()
.frame(minHeight: 1)
HStack{
leftView
Spacer()
HStack {
leftTitleView
Text(title)
.font(properties.font)
rightTitleView
}
Spacer()
rightView
}
Spacer()
.frame(minHeight: 1)
}
.background(backgroundView)
.foregroundColor(properties.textColor)
.cornerRadius(getCornerRadius(for: properties.cornerRadius))
.overlay(
RoundedRectangle(cornerRadius: getCornerRadius(for: properties.cornerRadius))
.stroke(properties.borderColor, lineWidth: properties.borderWidth)
)
}
.buttonStyle(.plain)
}
}
// MARK: - ActionButtonViewModel
private extension ActionButton {
class ActionButtonViewModel: ObservableObject {
@Published var title: String = "Button"
@Published var font: Font = .system(size: 16)
@Published var textColor: Color = .white
@Published var cornerRadius: ActionButton.CornerRadiusType = .rounded
@Published var backgroundType = ActionButton.Background.plain(.blue)
@Published var borderColor: Color = .clear
@Published var borderWidth: CGFloat = 0
@Published var leftImage: ActionButton.ExtraImage?
@Published var rightImage: ActionButton.ExtraImage?
@Published var titleLeftImage: ActionButton.ExtraImage?
@Published var titleRightImage: ActionButton.ExtraImage?
@Published var actionHandler: (() -> Void)?
}
}
// MARK: - Public Properties
public extension ActionButton {
struct ExtraImage {
let image: Image
let size: CGSize
public init(image: Image, size: CGSize) {
self.image = image
self.size = size
}
}
enum CornerRadiusType {
case rounded
case custom(CGFloat)
}
}
// MARK: - Builder Pattern Methods
public extension ActionButton {
func title(_ title: String) -> ActionButton {
properties.title = title
return self
}
func font(_ font: Font) -> ActionButton {
properties.font = font
return self
}
func textColor(_ textColor: Color) -> ActionButton {
properties.textColor = textColor
return self
}
func cornerRadius(_ cornerRadius: ActionButton.CornerRadiusType) -> ActionButton {
properties.cornerRadius = cornerRadius
return self
}
func backgroundType(_ backgroundType: ActionButton.Background) -> ActionButton {
properties.backgroundType = backgroundType
return self
}
func borderColor(_ borderColor: Color) -> ActionButton {
properties.borderColor = borderColor
return self
}
func borderWidth(_ borderWidth: CGFloat) -> ActionButton {
properties.borderWidth = borderWidth
return self
}
func leftImage(_ leftImage: ActionButton.ExtraImage) -> ActionButton {
properties.leftImage = leftImage
return self
}
func rightImage(_ rightImage: ActionButton.ExtraImage) -> ActionButton {
properties.rightImage = rightImage
return self
}
func titleLeftImage(_ titleLeftImage: ActionButton.ExtraImage) -> ActionButton {
properties.titleLeftImage = titleLeftImage
return self
}
func titleRightImage(_ titleRightImage: ActionButton.ExtraImage) -> ActionButton {
properties.titleRightImage = titleRightImage
return self
}
}
// MARK: - Access to properties from UIKit
public extension ActionButton {
var title: String {
get { properties.title }
set { properties.title = newValue}
}
var font: Font {
get { properties.font }
set { properties.font = newValue}
}
var textColor: Color {
get { properties.textColor }
set { properties.textColor = newValue}
}
var cornerRadius: ActionButton.CornerRadiusType {
get { properties.cornerRadius }
set { properties.cornerRadius = newValue}
}
var backgroundType: ActionButton.Background {
get { properties.backgroundType }
set { properties.backgroundType = newValue}
}
var borderColor: Color {
get { properties.borderColor }
set { properties.borderColor = newValue}
}
var borderWidth: CGFloat {
get { properties.borderWidth }
set { properties.borderWidth = newValue}
}
var leftImage: ActionButton.ExtraImage? {
get { properties.leftImage }
set { properties.leftImage = newValue}
}
var rightImage: ActionButton.ExtraImage? {
get { properties.rightImage }
set { properties.rightImage = newValue}
}
var titleLeftImage: ActionButton.ExtraImage? {
get { properties.titleLeftImage }
set { properties.titleLeftImage = newValue}
}
var titleRightImage: ActionButton.ExtraImage? {
get { properties.titleRightImage }
set { properties.titleRightImage = newValue}
}
func addAction(_ action: @escaping () -> Void) {
properties.actionHandler = action
}
}
// MARK: - Helper Methods
private extension ActionButton {
@ViewBuilder
private var leftTitleView: some View {
properties
.titleLeftImage?
.image
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: properties.titleLeftImage?.size.width,
height: properties.titleLeftImage?.size.height)
}
@ViewBuilder
private var rightTitleView: some View {
properties
.titleRightImage?
.image
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: properties.titleRightImage?.size.width,
height: properties.titleRightImage?.size.height)
}
@ViewBuilder
private var leftView: some View {
let leftImage = properties.leftImage
let rightImage = properties.rightImage
switch (leftImage, rightImage) {
case (.some(let leftImage), _):
leftImage
.image
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: leftImage.size.width,
height: leftImage.size.height)
.padding(.leading, 10)
case (.none, .some(let rightImage)):
Rectangle()
.fill(Color.clear)
.frame(width: rightImage.size.width,
height: rightImage.size.height)
.padding(.leading, 10)
case (.none, .none):
EmptyView()
}
}
@ViewBuilder
private var rightView: some View {
let leftImage = properties.leftImage
let rightImage = properties.rightImage
switch (leftImage, rightImage) {
case (_, .some(let image)):
image
.image
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: image.size.width,
height: image.size.height)
.padding(.trailing, 10)
case (.some(let leftImage) ,.none):
Rectangle()
.fill(Color.clear)
.frame(width: leftImage.size.width,
height: leftImage.size.height)
.padding(.trailing, 10)
case (.none, .none):
EmptyView()
}
}
@ViewBuilder
private var backgroundView: some View {
switch properties.backgroundType {
case .plain(let backgroundColor):
backgroundColor
case let .linearGradient(gradient):
gradient
case let .angularGradient(gradient):
gradient
case let .radialGradient(gradient):
gradient
}
}
private func getCornerRadius(for cornerType: CornerRadiusType) -> CGFloat {
switch cornerType {
case .rounded:
return .infinity
case .custom(let cornerRadius):
return cornerRadius
}
}
}
struct ActionButton_Previews: PreviewProvider {
static var previews: some View {
ActionButton()
}
}