comparison GeoQuiz/Helpers/Extensions.swift @ 26:425078c01194

refactor code
author Dennis C. M. <dennis@denniscm.com>
date Wed, 09 Nov 2022 10:30:01 +0100
parents
children
comparison
equal deleted inserted replaced
25:b3df0f5dc750 26:425078c01194
1 //
2 // Extensions.swift
3 // GeoQuiz
4 //
5 // Created by Dennis Concepción Martín on 7/11/22.
6 //
7
8 import Foundation
9 import SwiftUI
10
11 extension ShapeStyle where Self == Gradient {
12 static var main: Gradient {
13 Gradient(colors: [Color("MayaBlue"), Color("RoyalLightBlue")])
14 }
15
16 static var secondary: Gradient {
17 Gradient(colors: [Color("AtomicTangerine"), Color("ChinaPink")])
18 }
19
20 static var tertiary: Gradient {
21 Gradient(colors: [Color("PinkLavender"), Color("BlueBell")])
22 }
23
24 static var quaternary: Gradient {
25 Gradient(colors: [Color("MaizeCrayola"), Color("MiddleRed")])
26 }
27 }
28
29 extension ShapeStyle where Self == Color {
30 static var atomicTangerine: Color {
31 Color("AtomicTangerine")
32 }
33
34 static var blueBell: Color {
35 Color("BlueBell")
36 }
37
38 static var chinaPink: Color {
39 Color("ChinaPink")
40 }
41
42 static var maizeCrayola: Color {
43 Color("MaizeCrayola")
44 }
45
46 static var mayaBlue: Color {
47 Color("MayaBlue")
48 }
49
50 static var middleRed: Color {
51 Color("MiddleRed")
52 }
53
54 static var pinkLavender: Color {
55 Color("PinkLavender")
56 }
57
58 static var royalLightBlue: Color {
59 Color("RoyalLightBlue")
60 }
61 }
62
63 extension Formatter {
64 static let withSeparator: NumberFormatter = {
65 let formatter = NumberFormatter()
66 formatter.numberStyle = .decimal
67 return formatter
68 }()
69 }
70
71 extension Int {
72 var formattedWithSeparator: String {
73 return Formatter.withSeparator.string(for: self) ?? ""
74 }
75 }
76
77 extension Bundle {
78 func decode<T: Codable>(_ file: String) -> T {
79 guard let url = self.url(forResource: file, withExtension: nil) else {
80 fatalError("Failed to locate \(file) in bundle.")
81 }
82
83 guard let data = try? Data(contentsOf: url) else {
84 fatalError("Failed to load \(file) from bundle.")
85 }
86
87 let decoder = JSONDecoder()
88
89 guard let loaded = try? decoder.decode(T.self, from: data) else {
90 fatalError("Failed to decode \(file) from bundle.")
91 }
92
93 return loaded
94 }
95 }