-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
BurntModule.swift
268 lines (216 loc) Β· 5.43 KB
/
BurntModule.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
import ExpoModulesCore
import SPIndicator
import SPAlert
enum AlertPreset: String, Enumerable {
case done
case error
case heart
case spinner
case custom
func toSPAlertIconPreset(_ options: AlertOptions?) throws -> SPAlertIconPreset {
switch self {
case .done:
return .done
case .error:
return .error
case .heart:
return .heart
case .spinner:
return .spinner
case .custom:
guard let image = UIImage.init( systemName: options?.icon?.name ?? "swift") else {
throw BurntError.invalidSystemName
}
return .custom((image.withTintColor(options?.icon?.color ?? .systemBlue, renderingMode: .alwaysOriginal)))
}
}
}
enum AlertHaptic: String, Enumerable {
case success
case warning
case error
case none
func toSPAlertHaptic() -> SPAlertHaptic {
switch self {
case .success:
return .success
case .warning:
return .warning
case .error:
return .error
case .none:
return .none
}
}
}
struct AlertLayout: Record {
@Field
var iconSize: IconSize?
}
struct AlertOptions: Record {
@Field
var title: String = ""
@Field
var message: String?
@Field
var preset: AlertPreset = AlertPreset.done
@Field
var duration: TimeInterval?
@Field
var shouldDismissByTap: Bool = true
@Field
var haptic: AlertHaptic = .none
@Field
var layout: AlertLayout?
@Field
var icon: Icon? = nil
}
struct Icon: Record {
@Field
var name: String? = nil
@Field
var color: UIColor = .systemGray
}
struct IconSize: Record {
@Field
var width: Int
@Field
var height: Int
}
struct ToastMargins: Record {
@Field
var top: CGFloat?
@Field
var left: CGFloat?
@Field
var bottom: CGFloat?
@Field
var right: CGFloat?
}
struct ToastLayout: Record {
@Field
var iconSize: IconSize?
@Field
var margins: ToastMargins?
}
struct ToastOptions: Record {
@Field
var title: String = ""
@Field
var message: String?
@Field
var preset: ToastPreset = ToastPreset.done
@Field
var duration: TimeInterval?
@Field
var layout: ToastLayout?
@Field
var shouldDismissByDrag: Bool = true
@Field
var haptic: ToastHaptic = .none
@Field
var from: ToastPresentSide = .top
@Field
var icon: Icon? = nil
}
enum ToastHaptic: String, Enumerable {
case success
case warning
case error
case none
func toSPIndicatorHaptic() -> SPIndicatorHaptic {
switch self {
case .success:
return .success
case .warning:
return .warning
case .error:
return .error
case .none:
return .none
}
}
}
enum BurntError: Error {
case invalidSystemName
}
enum ToastPreset: String, Enumerable {
case done
case error
case none
case custom
func toSPIndicatorPreset(_ options: ToastOptions?) throws -> SPIndicatorIconPreset? {
switch self {
case .done:
return .done
case .error:
return .error
case .none:
return .none
case .custom:
guard let image = UIImage.init( systemName: options?.icon?.name ?? "swift") else {
throw BurntError.invalidSystemName
}
return .custom(image.withTintColor(options?.icon?.color ?? .systemBlue, renderingMode: .alwaysOriginal))
}
}
}
enum ToastPresentSide: String, Enumerable {
case top
case bottom
func toSPIndicatorPresentSide() -> SPIndicatorPresentSide {
switch self {
case .top:
return .top
case .bottom:
return .bottom
}
}
}
public class BurntModule: Module {
public func definition() -> ModuleDefinition {
Name("Burnt")
AsyncFunction("toastAsync") { (options: ToastOptions) -> Void in
var preset: SPIndicatorIconPreset?
do {
preset = try options.preset.toSPIndicatorPreset(options)
} catch {
log.error("Burnt Toast error: \(error)")
}
let view = (preset != nil) ? SPIndicatorView(title: options.title, message: options.message, preset: preset ?? .done): SPIndicatorView(title: options.title, message: options.message)
if let duration = options.duration {
view.duration = duration
}
if let icon = options.layout?.iconSize {
view.layout.iconSize = .init(width: icon.width, height: icon.height)
}
view.dismissByDrag = options.shouldDismissByDrag
view.presentSide = options.from.toSPIndicatorPresentSide();
view.present(haptic: options.haptic.toSPIndicatorHaptic())
}.runOnQueue(.main)
AsyncFunction("alertAsync") { (options: AlertOptions) -> Void in
var preset: SPAlertIconPreset?
do {
preset = try options.preset.toSPAlertIconPreset(options)
} catch {
log.error("Burnt Alert error: \(error)")
}
let view = SPAlertView(
title: options.title,
message: options.message,
preset: preset ?? .done)
if let duration = options.duration {
view.duration = duration
}
view.dismissByTap = options.shouldDismissByTap
if let icon = options.layout?.iconSize {
view.layout.iconSize = .init(width: icon.width, height: icon.height)
}
view.present(
haptic: options.haptic.toSPAlertHaptic())
}.runOnQueue(.main)
AsyncFunction("dismissAllAlertsAsync") {
return SPAlert.dismiss()
}.runOnQueue(.main)
}
}