/
View.swift
421 lines (382 loc) · 12.8 KB
/
View.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import SwiftUI
import Combine
@available(iOS 13.0, macOS 10.15, *)
public extension View {
/// Fill parent view
/// - Parameter alignment: alignment
/// - Returns: View with filled frame
func fillParent(alignment: Alignment = .center) -> some View {
self
.frame(
minWidth: 0,
maxWidth: .infinity,
minHeight: 0,
maxHeight: .infinity,
alignment: alignment
)
}
/// Fill parent's width
/// - Returns: view
func fillWidth() -> some View {
return self.frame(minWidth: 0, maxWidth: .infinity)
}
/// Fill parent's height
/// - Returns: view
func fillHeight() -> some View {
return self.frame(minHeight: 0, maxHeight: .infinity)
}
/// Similar to distribuation fill on `UIStackView`, take up all availabe space of parent
/// - Parameters:
/// - flexibleAxis: Axis to fill
/// - alignment: Alignment
/// - Returns: View
public func flexibleFill(
_ flexibleAxis: Axis.Set = [.horizontal, .vertical],
alignment: Alignment = .center
) -> some View {
return frame(
maxWidth: flexibleAxis.contains(.horizontal) ? .infinity : nil,
maxHeight: flexibleAxis.contains(.vertical) ? .infinity : nil,
alignment: alignment)
}
/// If a condition is true, transform and return a new view
/// - Parameters:
/// - condition: condition
/// - transform: transformation if true
/// - Returns: new view
@ViewBuilder
func `if`<TrueContent: View>(_ condition: Bool, transform: (Self) -> TrueContent) -> some View {
if condition {
transform(self)
} else {
self
}
}
/// If a condition is true, transform and return a new view
/// - Parameters:
/// - condition: condition
/// - ifTransform: true case transform
/// - elseTransform: else case transform
/// - Returns: new view
@ViewBuilder
func `if`<TrueContent: View, FalseContent: View>(
_ condition: Bool,
if ifTransform: (Self) -> TrueContent,
else elseTransform: (Self) -> FalseContent
) -> some View {
if condition {
ifTransform(self)
} else {
elseTransform(self)
}
}
/// Conditionally apply a modifier to the view based on if the optional exists
/// - Parameters:
/// - value: Optional value
/// - modifier: Modifier to run on value if it exists
/// - Returns: Modified self if optional has a value
@ViewBuilder
func `ifLet`<Content: View, Value>(
_ value: Value?,
_ modifier: (Self, Value) -> Content) -> some View {
if let value = value {
modifier(self, value)
} else {
self
}
}
/// Embed Self in `NavigationView`
/// - Returns: New view with `NavigationView` parent
func embedInNavigationView() -> some View {
NavigationView { self }
}
/// Embed Self in `ScrollView`
/// - Returns: New view with `ScrollView` parent
func embedInScrollView(_ axis: Axis.Set = .vertical,
showsIndicators: Bool = false) -> some View {
ScrollView(axis, showsIndicators: showsIndicators) { self }
}
/// Embed Self in `TabView`
/// - Parameters:
/// - selection: selection binding
/// - paged: page style or not (`PageTabViewStyle`)
/// - pageIndicators: show page indicators, default .always
/// - Returns: View
@available(iOS 14.0, *)
func embedInTabView<Selection: Hashable>(
_ selection: Binding<Selection>,
paged: Bool = true,
pageIndicators: PageTabViewStyle.IndexDisplayMode = .always
) -> some View {
TabView(selection: selection) { self }
.if(paged) { (tabView) in
return tabView
.tabViewStyle(PageTabViewStyle(indexDisplayMode: pageIndicators))
}
}
/**
```
ForEach(0 ..< 5) { item in
Rectangle()
.foregroundColor(.random())
}.embedInVerticalPagingTabView($selection)
```
*/
/// Embed Self in vertically paging `TabView`
/// - Parameters:
/// - selection: selection binding
/// - pageIndicators: show page indicators, default .always
/// - Returns: View
@available(iOS 14.0, *)
func embedInVerticalPagingTabView<Selection: Hashable>(
_ selection: Binding<Selection>,
pageIndicators: PageTabViewStyle.IndexDisplayMode = .never
) -> some View {
GeometryReader { proxy in
TabView(selection: selection) {
self
.rotationEffect(.degrees(-90)) // Rotate content
.frame(
width: proxy.localWidth,
height: proxy.localHeight
)
}
.frame(
width: proxy.localHeight,
height: proxy.localWidth
)
.rotationEffect(.degrees(90), anchor: .topLeading) // Rotate TabView
.offset(x: proxy.localWidth) // Offset back into screens bounds
.tabViewStyle(
PageTabViewStyle(indexDisplayMode: pageIndicators)
)
}
}
/// Round view with specific corners
/// - Parameters:
/// - radius: radius
/// - corners: corners to round
/// - Returns: new rounded view
func cornerRadius(_ radius: CGFloat, corners: UIRectCorner) -> some View {
clipShape(RoundedCorner(radius: radius, corners: corners))
}
/// Erase view to `AnyView`
/// - Returns: new view
func eraseToAnyView() -> AnyView {
AnyView(self)
}
/// Hides the view conditionally
/// - Parameter hidden: is hidden
/// - Returns: hidden view but not drawn on screen
@ViewBuilder
func hide(if hidden: Bool) -> some View {
switch hidden {
case true: self.hidden()
case false: self
}
}
/// Loading shimmer animation on view
/// - Parameters:
/// - isActive: is shimmer animation active
/// - speed: speed, default: 0.15
/// - angle: angle of animation, default: 70º
/// - Returns: View
func shimmer(
isActive: Bool = true,
speed: Double = 0.15,
angle: Angle = .init(degrees: 70)
) -> some View {
let view = ShimmerModifier(
isActive: isActive,
speed: speed,
angle: angle
)
return self.modifier(view)
}
/// Add animating circles to view
/// - Parameters:
/// - isActive: is animation active
/// - circleColor: circle foreground color
/// - Returns: View
func circleMotion(
isActive: Bool = true,
circleColor: Color = .gray
) -> some View {
let view = CircleMotionModifier(
isActive: isActive,
circleColor: circleColor)
return self.modifier(view)
}
/// Overlay border on view
/// - Parameters:
/// - content: ShapeStyle view
/// - width: border width
/// - cornerRadius: corner radius
/// - Returns: view
func addBorder<S>(_ content: S, width: CGFloat = 1, cornerRadius: CGFloat) -> some View where S: ShapeStyle {
return overlay(RoundedRectangle(cornerRadius: cornerRadius)
.strokeBorder(content, lineWidth: width))
}
/// Addd a `LinearGradient` overlayed on View
/// - Parameters:
/// - colors: Colors
/// - start: Start, default top
/// - end: End, default bottom
/// - Returns: View
func gradientOverlay(
colors: Color...,
start: UnitPoint = .top,
end: UnitPoint = .bottom) -> some View {
overlay(
LinearGradient(
gradient: Gradient(colors: colors),
startPoint: start, endPoint: end)
)
}
/// Addd a `LinearGradient` background on View
/// - Parameters:
/// - colors: Colors
/// - start: Start, default top
/// - end: End, default bottom
/// - Returns: View
func gradientBackground(
colors: Color...,
start: UnitPoint = .top,
end: UnitPoint = .bottom) -> some View {
background(
LinearGradient(
gradient: Gradient(colors: colors),
startPoint: start, endPoint: end)
)
}
/// Expand safe area on the background of the view
/// - Parameters:
/// - backgroundView: view to ignore safe area
/// - safeAreaRegions: safe area regions
/// - edges: edges
/// - Returns: View
@available(iOS 14.0, *)
func safeArea<Background: View>(
withBackground backgroundView: Background,
safeAreaRegions: SafeAreaRegions = .all,
edges: Edge.Set = .all
) -> some View {
self.background(
backgroundView.ignoresSafeArea(
safeAreaRegions,
edges: edges
)
)
}
/// Set Navigation bar background color and text
/// - Parameters:
/// - background: background color
/// - text: text color
/// - Returns: View
func navigationBarColors(background: UIColor, text: UIColor) -> some View {
self.modifier(
NavigationBarModifier(backgroundColor: background, textColor: text)
)
}
/// Container that doesn’t leak outside the safe area, only the elements in its background
/// - Parameters:
/// - alignment: alignment
/// - content: content
/// - Returns: View
func background<Content: View>(
alignment: Alignment = .center,
@ViewBuilder content: () -> Content
) -> some View {
self.background(ZStack(alignment: alignment) { content() }, alignment: alignment)
}
/// Hide keyboard
func hideKeyboard() {
DispatchQueue.main.async {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
}
/// Receive keyboard status updates
/// - Parameter isVisible: is keyboard visible
/// - Returns: View
func keyboardState(isVisible: Binding<Bool>) -> some View {
self.modifier(KeyboardStateModifier(isVisible))
}
/// Debug print
/// - Parameter vars: item to print
/// - Returns: View
func debugPrint(_ vars: Any...) -> some View {
for v in vars { dprint(v) }
return self
}
/// Debug action
/// - Parameter closure: action
/// - Returns: View
func debugAction(_ closure: () -> Void) -> Self {
#if DEBUG
closure()
#endif
return self
}
/// Debug visual modifier
/// - Parameter modifier: View modifier
/// - Returns: View
func debugModifier<T: View>(_ modifier: (Self) -> T) -> some View {
#if DEBUG
return modifier(self)
#else
return self
#endif
}
/// Debug only border
/// - Parameters:
/// - color: color
/// - width: width
/// - Returns: View
func debugBorder(_ color: Color = .red, width: CGFloat = 1) -> some View {
debugModifier {
$0.border(color, width: width)
}
}
/// Debug only background
/// - Parameter color: color
/// - Returns: View
func debugBackground(_ color: Color = .red) -> some View {
debugModifier {
$0.background(color)
}
}
/// Debug print view representation
/// - Returns: Self after printing
func debug() -> Self {
print(Mirror(reflecting: self).subjectType)
return self
}
/// Repeat View n times
/// - Parameter value: repeat upto not including
/// - Returns: View
func times(_ value: UInt) -> some View {
ForEach(0..<Int(value)) { _ in self }
}
/// Overlay View on content size
/// - Parameter content: content size
/// - Returns: View
func useSize<Content: View>(of content: @autoclosure () -> Content) -> some View {
return content().hidden().overlay(self)
}
/// Subscribe and blind ouput to View property
/// - Parameters:
/// - publisher: publisher
/// - binding: binding
/// - Returns: View
func onReceive<P: Publisher>(_ publisher: P, assignTo binding: Binding<P.Output>) -> some View where P.Failure == Never {
onReceive(publisher) { binding.wrappedValue = $0 }
}
/// Subscribe and blind optional ouput to View property
/// - Parameters:
/// - publisher: publisher
/// - binding: binding
/// - Returns: View
func onReceive<P: Publisher>(_ publisher: P, assignTo binding: Binding<P.Output?>) -> some View where P.Failure == Never {
onReceive(publisher) { binding.wrappedValue = $0 }
}
}