-
Notifications
You must be signed in to change notification settings - Fork 1
/
NotificareNotification.swift
264 lines (227 loc) · 10.2 KB
/
NotificareNotification.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
//
// Copyright (c) 2020 Notificare. All rights reserved.
//
import NotificareUtilitiesKit
public struct NotificareNotification: Codable, Equatable {
public let partial: Bool
public let id: String
public let type: String
public let time: Date
public let title: String?
public let subtitle: String?
public let message: String
public let content: [Content]
public let actions: [Action]
public let attachments: [Attachment]
@NotificareExtraEquatable public private(set) var extra: [String: Any]
public let targetContentIdentifier: String?
public init(partial: Bool, id: String, type: String, time: Date, title: String?, subtitle: String?, message: String, content: [NotificareNotification.Content], actions: [NotificareNotification.Action], attachments: [NotificareNotification.Attachment], extra: [String: Any], targetContentIdentifier: String?) {
self.partial = partial
self.id = id
self.type = type
self.time = time
self.title = title
self.subtitle = subtitle
self.message = message
self.content = content
self.actions = actions
self.attachments = attachments
self.extra = extra
self.targetContentIdentifier = targetContentIdentifier
}
public enum NotificationType: String {
case none = "re.notifica.notification.None"
case alert = "re.notifica.notification.Alert"
case inAppBrowser = "re.notifica.notification.InAppBrowser"
case webView = "re.notifica.notification.WebView"
case url = "re.notifica.notification.URL"
case urlResolver = "re.notifica.notification.URLResolver"
case urlScheme = "re.notifica.notification.URLScheme"
case image = "re.notifica.notification.Image"
case video = "re.notifica.notification.Video"
case map = "re.notifica.notification.Map"
case rate = "re.notifica.notification.Rate"
case passbook = "re.notifica.notification.Passbook"
case store = "re.notifica.notification.Store"
}
public struct Content: Codable, Equatable {
public let type: String
@NotificareExtraEquatable public private(set) var data: Any
public init(type: String, data: Any) {
self.type = type
self.data = data
}
}
public struct Action: Codable, Equatable {
public let type: String
public let label: String
public let target: String?
public let keyboard: Bool
public let camera: Bool
public let destructive: Bool?
public let icon: Icon?
public init(type: String, label: String, target: String?, keyboard: Bool, camera: Bool, destructive: Bool?, icon: NotificareNotification.Action.Icon?) {
self.type = type
self.label = label
self.target = target
self.keyboard = keyboard
self.camera = camera
self.destructive = destructive
self.icon = icon
}
public enum ActionType: String {
case app = "re.notifica.action.App"
case browser = "re.notifica.action.Browser"
case callback = "re.notifica.action.Callback"
case custom = "re.notifica.action.Custom"
case mail = "re.notifica.action.Mail"
case sms = "re.notifica.action.SMS"
case telephone = "re.notifica.action.Telephone"
case inAppBrowser = "re.notifica.action.InAppBrowser"
@available(*, deprecated, message: "The WebView action type becomes a backwards compatible alias. Use the InAppBrowser action type instead.", renamed: "inAppBrowser")
case webView = "re.notifica.action.WebView"
}
public struct Icon: Codable, Equatable {
public let android: String?
public let ios: String?
public let web: String?
public init(android: String?, ios: String?, web: String?) {
self.android = android
self.ios = ios
self.web = web
}
}
}
public struct Attachment: Codable, Equatable {
public let mimeType: String
public let uri: String
public init(mimeType: String, uri: String) {
self.mimeType = mimeType
self.uri = uri
}
}
}
// Identifiable: NotificareNotification
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
extension NotificareNotification: Identifiable {}
// JSON: NotificareNotification
extension NotificareNotification {
public func toJson() throws -> [String: Any] {
let data = try JSONEncoder.notificare.encode(self)
return try JSONSerialization.jsonObject(with: data, options: []) as! [String: Any]
}
public static func fromJson(json: [String: Any]) throws -> NotificareNotification {
let data = try JSONSerialization.data(withJSONObject: json, options: [])
return try JSONDecoder.notificare.decode(NotificareNotification.self, from: data)
}
}
// Codable: NotificareNotification
extension NotificareNotification {
internal enum CodingKeys: String, CodingKey {
case partial
case id
case type
case time
case title
case subtitle
case message
case content
case actions
case attachments
case extra
case targetContentIdentifier
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
partial = try container.decodeIfPresent(Bool.self, forKey: .partial) ?? false
id = try container.decode(String.self, forKey: .id)
type = try container.decode(String.self, forKey: .type)
time = try container.decode(Date.self, forKey: .time)
title = try container.decodeIfPresent(String.self, forKey: .title)
subtitle = try container.decodeIfPresent(String.self, forKey: .subtitle)
message = try container.decode(String.self, forKey: .message)
content = try container.decode([Content].self, forKey: .content)
actions = try container.decode([Action].self, forKey: .actions)
attachments = try container.decode([Attachment].self, forKey: .attachments)
let decodedExtra = try container.decode(NotificareAnyCodable.self, forKey: .extra)
extra = decodedExtra.value as! [String: Any]
targetContentIdentifier = try container.decodeIfPresent(String.self, forKey: .targetContentIdentifier)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(partial, forKey: .partial)
try container.encode(id, forKey: .id)
try container.encode(type, forKey: .type)
try container.encode(time, forKey: .time)
try container.encodeIfPresent(title, forKey: .title)
try container.encodeIfPresent(subtitle, forKey: .subtitle)
try container.encode(message, forKey: .message)
try container.encode(content, forKey: .content)
try container.encode(actions, forKey: .actions)
try container.encode(attachments, forKey: .attachments)
try container.encode(NotificareAnyCodable(extra), forKey: .extra)
try container.encode(targetContentIdentifier, forKey: .targetContentIdentifier)
}
}
// JSON: NotificareNotification.Content
extension NotificareNotification.Content {
public func toJson() throws -> [String: Any] {
let data = try JSONEncoder.notificare.encode(self)
return try JSONSerialization.jsonObject(with: data, options: []) as! [String: Any]
}
public static func fromJson(json: [String: Any]) throws -> NotificareNotification.Content {
let data = try JSONSerialization.data(withJSONObject: json, options: [])
return try JSONDecoder.notificare.decode(NotificareNotification.Content.self, from: data)
}
}
// Codable: NotificareNotification.Content
extension NotificareNotification.Content {
internal enum CodingKeys: String, CodingKey {
case type
case data
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
type = try container.decode(String.self, forKey: .type)
let decoded = try container.decode(NotificareAnyCodable.self, forKey: .data)
data = decoded.value
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(type, forKey: .type)
try container.encode(NotificareAnyCodable(data), forKey: .data)
}
}
// JSON: NotificareNotification.Action
extension NotificareNotification.Action {
public func toJson() throws -> [String: Any] {
let data = try JSONEncoder.notificare.encode(self)
return try JSONSerialization.jsonObject(with: data, options: []) as! [String: Any]
}
public static func fromJson(json: [String: Any]) throws -> NotificareNotification.Action {
let data = try JSONSerialization.data(withJSONObject: json, options: [])
return try JSONDecoder.notificare.decode(NotificareNotification.Action.self, from: data)
}
}
// JSON: NotificareNotification.Action.Icon
extension NotificareNotification.Action.Icon {
public func toJson() throws -> [String: Any] {
let data = try JSONEncoder.notificare.encode(self)
return try JSONSerialization.jsonObject(with: data, options: []) as! [String: Any]
}
public static func fromJson(json: [String: Any]) throws -> NotificareNotification.Action.Icon {
let data = try JSONSerialization.data(withJSONObject: json, options: [])
return try JSONDecoder.notificare.decode(NotificareNotification.Action.Icon.self, from: data)
}
}
// JSON: NotificareNotification.Attachment
extension NotificareNotification.Attachment {
public func toJson() throws -> [String: Any] {
let data = try JSONEncoder.notificare.encode(self)
return try JSONSerialization.jsonObject(with: data, options: []) as! [String: Any]
}
public static func fromJson(json: [String: Any]) throws -> NotificareNotification.Attachment {
let data = try JSONSerialization.data(withJSONObject: json, options: [])
return try JSONDecoder.notificare.decode(NotificareNotification.Attachment.self, from: data)
}
}