-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtensionsAndFunctions.swift
525 lines (449 loc) · 13.9 KB
/
ExtensionsAndFunctions.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
//
// ExtensionsAndFunctions.swift
// Ravens
//
// Created by Eric de Quartel on 15/01/2024.
//
import Foundation
import SwiftUI
import AudioToolbox
import SFSafeSymbols
let showView = false
//let showView = true
// werkhoven
// let longitude = 5.243376
// let latitude = 52.023861
// gouda en span nederland
let latitude = 52.013077-0.2
let longitude = 4.713450+0.1
let latitudeDelta = 4.5
let longitudeDelta = 3.0
func cleanName(_ name: String) -> String {
var cleanName = name.replacingOccurrences(of: " ", with: "_")
let charactersToRemove = Set(["!", "?", "."])
for character in charactersToRemove {
cleanName = cleanName.replacingOccurrences(of: String(character), with: "")
}
return cleanName
}
func formatCurrentDate(value: Date) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let currentDate = value
return dateFormatter.string(from: currentDate)
}
var selectedInBetween = "waarneming.nl"
func endPoint(value: String = "") -> String {
if value != "" {
selectedInBetween = value
}
return "https://"+selectedInBetween+"/api/v1/"
}
let bundleIdentifier = "ravens.app.bundle.identifier"
let VOMap = "map"
let VOMapFavorite = "favorite map"
let VOAreas = "areas"
extension Color {
static let obsBlueButterfly = Color(red: 76/255, green: 253/255, blue: 251/255) //4CFDFB
static let obsGreenSpider = Color(red: 31/255, green: 94/255, blue: 37/255) //1F5E25
static let obsGreenFlower = Color(red: 203/255, green: 252/255, blue: 69/255) //CBFC45
static let obsGreenEagle = Color(red: 29/255, green: 148/255, blue: 49/255) //1D9431
static let obsBackground = Color(red: 106/255, green: 227/255, blue: 136/255) //6AE388
static let obsStar = Color(red: 29/255, green: 148/255, blue: 49/255) //1D9431
static let obsInformation = Color(red: 0/255, green: 0/255, blue: 255/255) //blue
static let obsObserver = Color(red: 255/255, green: 0/255, blue: 0/255) //red
static let obsBookmark = Color(red: 29/255, green: 148/255, blue: 49/255) //green
static let obsArea = Color(red: 255/255, green: 165/255, blue: 0/255) //orange
static let obsObservation = Color(red: 89/255, green: 173/255, blue: 195/255) //lightblue
static let obsShareLink = Color(red: 0.5, green: 0.5, blue: 0.5) // gray
static let obsToolbar = Color(red: 89/255, green: 173/255, blue: 195/255) //lightblue
}
let obsStrDutchOrange = "f7b731"
let SFShareLink = SFSymbol.squareAndArrowUp
let SFInformation = SFSymbol.infoCircle
let SFArea = SFSymbol.map
let SFAreaFill = SFSymbol.mapFill
let SFSpecies = SFSymbol.star
let SFSpeciesFill = SFSymbol.starFill
let SFObservation = SFSymbol.binocularsFill
let SFObserver = SFSymbol.person
let SFObserverFill = SFSymbol.personFill
let SFObserverPlus = SFSymbol.personFillBadgePlus
let SFObserverMin = SFSymbol.personFillBadgeMinus
// func GroupColor(value: Int) -> Color {
// switch value {
// case 0: return .gray // all
// case 1: return .green // birds
// case 2: return .blue // mammals
// case 3: return .orange // reptiles and amfibian
// case 5: return .yellow // butterflies
// case 6: return .pink // dragonflies
// case 7: return .purple // molluscs
// case 8: return .brown // moth and micros
// case 9: return .gray // fish
// case 10: return .obsBackground // plants
// case 11: return .white // fungi
// case 12: return .indigo // mosses and lichen
// case 13: return .mint // geleedpotigen
// case 14: return .teal // locusts and crickets
// case 15: return .cyan // wantsen cicaden en plantenluizen
//// case 16: return .olive // bugs
//// case 17: return .lavender // bees wasp ants
//// case 18: return .amber // flies and mossies
//// case 19: return .coral // algea
//// case 20: return .peach // overige invertabrates
//// case 30: return .red // verstoringen
// default:
// return .gray //You can provide a default color or handle other cases as needed
// }
//}
func rarityColor(value: Int) -> Color {
switch value {
case 0:
return .gray //onbekend
case 1:
return .green //algemeen
case 2:
return .blue //vrij algemeen
case 3:
return .orange //rare
case 4:
return .red //very rare
default:
return .gray //You can provide a default color or handle other cases as needed
}
}
func speciesColor(value: Int) -> Color {
switch value {
case 0:
return .gray //onbekend
case 1:
return .green //birds
case 2:
return .blue //vrij algemeen
case 3:
return .orange //rare
case 4:
return .red //very rare
default:
return .gray //You can provide a default color or handle other cases as needed
}
}
extension AnyTransition {
static var flipView: AnyTransition {
AnyTransition.scale
.combined(with: .opacity)
.combined(with: .asymmetric(insertion: .opacity, removal: .scale))
}
}
extension View {
@ViewBuilder
func applyIf<T: View>(_ condition: Bool, apply: (Self) -> T) -> some View {
if condition {
apply(self)
} else {
self
}
}
func shadowedStyle() -> some View {
self
.shadow(color: .black.opacity(0.08), radius: 2, x: 0, y: 0)
.shadow(color: .black.opacity(0.16), radius: 24, x: 0, y: 0)
}
func customButtonStyle(
foreground: Color = .black,
background: Color = .white
) -> some View {
self.buttonStyle(
ExampleButtonStyle(
foreground: foreground,
background: background
)
)
}
#if os(iOS)
func cornerRadius(_ radius: CGFloat, corners: UIRectCorner) -> some View {
clipShape(RoundedCorner(radius: radius, corners: corners))
}
#endif
}
private struct ExampleButtonStyle: ButtonStyle {
let foreground: Color
let background: Color
func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.opacity(configuration.isPressed ? 0.45 : 1)
.foregroundColor(configuration.isPressed ? foreground.opacity(0.55) : foreground)
.background(configuration.isPressed ? background.opacity(0.55) : background)
}
}
#if os(iOS)
struct RoundedCorner: Shape {
var radius: CGFloat = .infinity
var corners: UIRectCorner = .allCorners
func path(in rect: CGRect) -> Path {
let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
return Path(path.cgPath)
}
}
#endif
class Constants {
static let privacyPolicy = """
Lorem ipsum
Maecenas eget felis in lacus
"""
}
struct Triangle: Shape {
func path(in rect: CGRect) -> Path {
var path = Path()
// Define the points for the triangle
let topPoint = CGPoint(x: rect.midX, y: rect.minY)
let bottomLeftPoint = CGPoint(x: rect.minX, y: rect.maxY)
let bottomRightPoint = CGPoint(x: rect.maxX, y: rect.maxY)
// Move to the top point of the triangle
path.move(to: topPoint)
// Add lines to the bottom left and bottom right points to complete the triangle
path.addLine(to: bottomLeftPoint)
path.addLine(to: bottomRightPoint)
// Close the path to form a closed triangle
path.closeSubpath()
return path
}
}
extension Font {
static var customTiny: Font {
return .system(size: 10)
}
static var customSmall: Font {
return .system(size: 12)
}
static var customMedium: Font {
return .system(size: 16)
}
static var customLarge: Font {
return .system(size: 20)
}
static var customExtraLarge: Font {
return .system(size: 24)
}
}
func deleteAllFiles() {
let documentsDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
let fileManager = FileManager.default
do {
let files = try fileManager.contentsOfDirectory(atPath: documentsDirectoryPath as String)
for file in files {
let filePath = documentsDirectoryPath.appendingPathComponent(file)
try fileManager.removeItem(atPath: filePath)
}
} catch {
print("Error deleting files: \(error)")
}
}
func calculateLocalStorageSize() -> String {
let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
let fileManager = FileManager.default
var totalSize: UInt64 = 0
do {
let files = try fileManager.contentsOfDirectory(atPath: documentDirectoryPath as String)
for file in files {
let filePath = documentDirectoryPath.appendingPathComponent(file)
let fileAttributes = try fileManager.attributesOfItem(atPath: filePath)
if let fileSize = fileAttributes[FileAttributeKey.size] as? UInt64 {
totalSize += fileSize
}
}
} catch {
print("Error getting file attributes: \(error)")
}
// Convert total size in bytes to MB
let sizeInMB = Double(totalSize) / 1024.0 / 1024.0
return String(format: "%.2f MB", sizeInMB)
}
extension Color {
init(hex: String) {
let scanner = Scanner(string: hex)
var rgbValue: UInt64 = 0
scanner.scanHexInt64(&rgbValue)
let red = (rgbValue & 0xff0000) >> 16
let green = (rgbValue & 0xff00) >> 8
let blue = rgbValue & 0xff
self.init(red: Double(red) / 0xff, green: Double(green) / 0xff, blue: Double(blue) / 0xff)
}
}
extension View {
func topLeft() -> some View {
self
.modifier(TopLeftModifier())
}
}
extension View {
func topRight() -> some View {
self
.modifier(TopRightModifier())
}
}
struct TopLeftModifier: ViewModifier {
func body(content: Content) -> some View {
VStack {
HStack {
content
.padding([.top, .leading], 10)
Spacer() // Pushes the view to the left
}
Spacer() // Pushes the view to the top
}
}
}
struct TopRightModifier: ViewModifier {
func body(content: Content) -> some View {
VStack {
HStack {
Spacer() // Pushes the view to the right
content
.padding([.top, .trailing], 10)
}
Spacer() // Pushes the view to the top
}
}
}
extension VStack {
func footnoteGrayStyle() -> some View {
self
.foregroundColor(.gray)
.font(.footnote)
.italic()
.lineLimit(1)
.truncationMode(.tail)
}
}
extension Text {
func footnoteGrayStyle() -> some View {
self
// .foregroundColor(.black)
.font(.footnote)
// .italic()
.lineLimit(1)
.truncationMode(.tail)
}
}
struct RoundButtonStyle: ViewModifier {
var iconName: String
var backgroundColor: Color
var foregroundColor: Color
var shadowRadius: CGFloat
func body(content: Content) -> some View {
content
.font(.title) // Adjust the size of the image
.foregroundColor(foregroundColor) // Color of the icon
.padding() // Padding around the icon
.background(Circle() // Creates a circular background
.fill(backgroundColor) // Background color of the circle
.shadow(radius: shadowRadius)) // Shadow with a radius
}
}
// Extension to apply the modifier more easily
extension View {
func roundButtonStyle(
iconName: String,
backgroundColor: Color,
foregroundColor: Color,
shadowRadius: CGFloat) -> some View {
self.modifier(
RoundButtonStyle(
iconName: iconName,
backgroundColor: backgroundColor,
foregroundColor: foregroundColor,
shadowRadius: shadowRadius))
}
}
struct RunOnceModifier: ViewModifier {
@State private var hasRun = false
let action: () -> Void
func body(content: Content) -> some View {
content
.onAppear {
if !hasRun {
action()
hasRun = true
}
}
}
}
extension View {
func onAppearOnce(perform action: @escaping () -> Void) -> some View {
self.modifier(RunOnceModifier(action: action))
}
}
struct HorizontalLine: View {
var body: some View {
Rectangle()
.frame(height: 0.4)
.foregroundColor(.gray)
}
}
func vibrate() {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
}
// make Int directly Identifiable
extension Int: @retroactive Identifiable {
public var id: Int { self }
}
extension String: @retroactive Identifiable {
public var id: String { self }
}
extension Image {
func uniformSize(color: Color? = nil) -> some View {
self
.resizable() // Makes the image resizable
.aspectRatio(contentMode: .fit) // Maintains aspect ratio
.frame(width: 24, height: 24) // Sets the uniform siz#
.padding(4) // Adds padding around the image
.overlay(
RoundedRectangle(cornerRadius: 4) // Adds a rounded rectangle border
.stroke(Color.blue, lineWidth: 1)
)
}
}
extension View {
func islandBackground(cornerRadius: CGFloat = 8, shadowRadius: CGFloat = 2) -> some View {
self
.background(
RoundedRectangle(cornerRadius: cornerRadius)
.fill(Color.gray.opacity(0.1)) // Light gray background with opacity
.shadow(radius: shadowRadius)
)
.clipShape(RoundedRectangle(cornerRadius: cornerRadius))
}
}
struct ImageWithOverlay: View {
var systemName: String
var value: Bool
var body: some View {
ZStack {
Image(systemName: systemName)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 24, height: 24)
if !value {
Image(systemSymbol: .lineDiagonal)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 24, height: 24)
}
}
}
}
struct ImageWithOverlay_Previews: PreviewProvider {
static var previews: some View {
ImageWithOverlay(systemName: "star", value: false)
}
}
//struct ClearButtonStyle: ButtonStyle {
// func makeBody(configuration: Configuration) -> some View {
// configuration.label
//// .padding()
// .background(Color.clear) // Clear background
// .foregroundColor(.blue) // Customize text color
// }
//}