-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathSerialization.swift
More file actions
309 lines (257 loc) · 8.18 KB
/
Serialization.swift
File metadata and controls
309 lines (257 loc) · 8.18 KB
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
//
// Created by Tapash Majumder on 3/19/20.
// Copyright © 2020 Iterable. All rights reserved.
//
import Foundation
import IterableSDK
struct SerializationUtil {
static func dateToInt(date: Date) -> Int {
Int(date.timeIntervalSince1970 * 1000)
}
static func intToDate(int: Int) -> Date {
let seconds = Double(int) / 1000.0 // ms -> seconds
return Date(timeIntervalSince1970: seconds)
}
static func encodableToDictionary<T>(encodable: T) -> [String: Any]? where T: Encodable {
guard let data = try? JSONEncoder().encode(encodable) else {
return nil
}
return try? JSONSerialization.jsonObject(with: data, options: [.allowFragments])
as? [String: Any]
}
static func dictionaryToDecodable<T>(dict: [AnyHashable: Any]) -> T? where T: Decodable {
guard let data = try? JSONSerialization.data(withJSONObject: dict, options: []) else {
return nil
}
return try? JSONDecoder().decode(T.self, from: data)
}
}
extension IterableConfig {
static func from(dict: [AnyHashable: Any]?) -> IterableConfig {
let config = IterableConfig()
guard let dict = dict else {
return config
}
if let allowedProtocols = dict["allowedProtocols"] as? [String] {
config.allowedProtocols = allowedProtocols
}
if let pushIntegrationName = dict["pushIntegrationName"] as? String {
config.pushIntegrationName = pushIntegrationName
}
if let pushPlatform = dict["pushPlatform"] as? NSNumber {
switch pushPlatform {
case 0:
config.pushPlatform = .sandbox
case 1:
config.pushPlatform = .production
default:
config.pushPlatform = .auto
}
}
if let autoPushRegistration = dict["autoPushRegistration"] as? Bool {
config.autoPushRegistration = autoPushRegistration
}
if let inAppDisplayInterval = dict["inAppDisplayInterval"] as? Double {
config.inAppDisplayInterval = inAppDisplayInterval
}
if let expiringAuthTokenRefreshPeriod = dict["expiringAuthTokenRefreshPeriod"] as? TimeInterval
{
config.expiringAuthTokenRefreshPeriod = expiringAuthTokenRefreshPeriod
}
if let logLevelNumber = dict["logLevel"] as? NSNumber {
config.logDelegate = createLogDelegate(logLevelNumber: logLevelNumber)
}
if let useInMemoryStorageForInApp = dict["useInMemoryStorageForInApps"] as? Bool {
config.useInMemoryStorageForInApps = useInMemoryStorageForInApp
}
if let enableEmbeddedMessaging = dict["enableEmbeddedMessaging"] as? Bool {
config.enableEmbeddedMessaging = enableEmbeddedMessaging
}
if let dataRegion = dict["dataRegion"] as? NSNumber {
switch dataRegion {
case 0:
config.dataRegion = IterableDataRegion.US
case 1:
config.dataRegion = IterableDataRegion.EU
default:
config.dataRegion = IterableDataRegion.US
}
}
if let retryPolicyDict = dict["retryPolicy"] as? [AnyHashable: Any] {
if let maxRetry = retryPolicyDict["maxRetry"] as? Int,
let retryInterval = retryPolicyDict["retryInterval"] as? TimeInterval,
let retryBackoffString = retryPolicyDict["retryBackoff"] as? String
{
let retryBackoffType: RetryPolicy.BackoffType =
retryBackoffString == "EXPONENTIAL" ? .exponential : .linear
config.retryPolicy = RetryPolicy(
maxRetry: maxRetry, retryInterval: retryInterval, retryBackoff: retryBackoffType)
}
}
return config
}
private static func createLogDelegate(logLevelNumber: NSNumber) -> IterableLogDelegate {
DefaultLogDelegate(minLogLevel: LogLevel.from(number: logLevelNumber))
}
}
extension CommerceItem {
static func from(dict: [AnyHashable: Any]) -> CommerceItem? {
guard let id = dict["id"] as? String else {
return nil
}
guard let name = dict["name"] as? String else {
return nil
}
guard let price = dict["price"] as? NSNumber else {
return nil
}
guard let quantity = dict["quantity"] as? UInt else {
return nil
}
let sku = dict["sku"] as? String
let description = dict["description"] as? String
let url = dict["url"] as? String
let imageUrl = dict["imageUrl"] as? String
let categories = dict["categories"] as? [String]
let dataFields = dict["dataFields"] as? [AnyHashable: Any]
return CommerceItem(
id: id,
name: name,
price: price,
quantity: quantity,
sku: sku,
description: description,
url: url,
imageUrl: imageUrl,
categories: categories,
dataFields: dataFields)
}
}
extension IterableInAppTrigger {
func toDict() -> [AnyHashable: Any] {
var dict = [AnyHashable: Any]()
dict["type"] = self.type.rawValue
return dict
}
}
extension UIEdgeInsets {
func toDict() -> [AnyHashable: Any] {
var dict = [AnyHashable: Any]()
dict["top"] = top
dict["left"] = left
dict["bottom"] = bottom
dict["right"] = right
return dict
}
}
extension IterableInboxMetadata {
func toDict() -> [AnyHashable: Any]? {
var dict = [AnyHashable: Any]()
dict["title"] = title
dict["subtitle"] = subtitle
dict["icon"] = icon
return dict
}
}
extension IterableInAppMessage {
func toDict() -> [AnyHashable: Any] {
var dict = [AnyHashable: Any]()
dict["messageId"] = messageId
dict["campaignId"] = campaignId
dict["trigger"] = trigger.toDict()
dict["createdAt"] = createdAt.map(SerializationUtil.dateToInt(date:))
dict["expiresAt"] = expiresAt.map(SerializationUtil.dateToInt(date:))
dict["saveToInbox"] = saveToInbox
dict["inboxMetadata"] = inboxMetadata?.toDict() ?? nil
dict["customPayload"] = customPayload
dict["read"] = read
dict["priorityLevel"] = priorityLevel
return dict
}
}
extension IterableHtmlInAppContent {
func toDict() -> [AnyHashable: Any] {
var dict = [AnyHashable: Any]()
dict["type"] = type.rawValue
dict["edgeInsets"] = edgeInsets.toDict()
dict["html"] = html
return dict
}
}
extension InAppLocation {
static func from(number: NSNumber) -> InAppLocation {
if let value = number as? Int {
return InAppLocation(rawValue: value) ?? .inApp
} else {
return .inApp
}
}
}
extension InAppCloseSource {
static func from(number: NSNumber) -> InAppCloseSource? {
guard let value = number as? Int else {
return nil
}
return InAppCloseSource(rawValue: value)
}
}
extension InAppDeleteSource {
static func from(number: NSNumber) -> InAppDeleteSource? {
guard let value = number as? Int else {
return nil
}
return InAppDeleteSource(rawValue: value)
}
}
extension InAppShowResponse {
static func from(number: NSNumber) -> InAppShowResponse {
if let value = number as? Int {
return InAppShowResponse(rawValue: value) ?? .show
} else {
return .show
}
}
}
extension LogLevel {
static func from(number: NSNumber) -> LogLevel {
if let value = number as? Int {
return LogLevel(rawValue: value) ?? .info
} else {
return .info
}
}
}
extension InboxImpressionTracker.RowInfo {
static func from(dict: [AnyHashable: Any]) -> InboxImpressionTracker.RowInfo? {
guard let messageId = dict["messageId"] as? String else {
return nil
}
guard let silentInbox = dict["silentInbox"] as? Bool else {
return nil
}
let rowInfo = InboxImpressionTracker.RowInfo(messageId: messageId, silentInbox: silentInbox)
return rowInfo
}
static func rowInfos(from rows: [[AnyHashable: Any]]) -> [InboxImpressionTracker.RowInfo] {
return rows.compactMap(InboxImpressionTracker.RowInfo.from(dict:))
}
}
extension IterableEmbeddedMessage {
func toDict() -> [AnyHashable: Any] {
var dict = [AnyHashable: Any]()
// Serialize metadata (which is Codable)
if let metadataDict = SerializationUtil.encodableToDictionary(encodable: metadata) {
dict["metadata"] = metadataDict
}
// Serialize elements if present (which is Codable)
if let elements = elements,
let elementsDict = SerializationUtil.encodableToDictionary(encodable: elements) {
dict["elements"] = elementsDict
}
// Add payload directly
if let payload = payload {
dict["payload"] = payload
}
return dict
}
}