Skip to content

Commit b6afefb

Browse files
Merge pull request #461 from Iterable/tapash/mob-2448-refactor-consts
[MOB-2448] - Refactor consts
2 parents eac7ba2 + 27aaa7f commit b6afefb

32 files changed

+525
-613
lines changed

swift-sdk/Constants.swift

+103-129
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ enum Endpoint {
1212
static let links = linksHostName + "/"
1313
}
1414

15-
public enum Const {
16-
public static let apiPath = "/api/"
15+
enum Const {
16+
static let apiPath = "/api/"
1717

1818
static let deepLinkRegex = "/a/[a-zA-Z0-9]+"
1919
static let href = "href"
@@ -43,10 +43,10 @@ public enum Const {
4343
static let getRemoteConfiguration = "mobile/getRemoteConfiguration"
4444
}
4545

46-
public enum UserDefaults {
46+
public enum UserDefault {
4747
static let payloadKey = "itbl_payload_key"
4848
static let attributionInfoKey = "itbl_attribution_info_key"
49-
public static let emailKey = "itbl_email"
49+
static let emailKey = "itbl_email"
5050
static let userIdKey = "itbl_userid"
5151
static let authTokenKey = "itbl_auth_token"
5252
static let ddlChecked = "itbl_ddl_checked"
@@ -74,109 +74,95 @@ public enum Const {
7474
}
7575
}
7676

77-
public protocol JsonKeyValueRepresentable {
78-
var key: JsonKeyRepresentable { get }
79-
var value: JsonValueRepresentable { get }
80-
}
81-
82-
public struct JsonKeyValue: JsonKeyValueRepresentable {
83-
public let key: JsonKeyRepresentable
84-
public let value: JsonValueRepresentable
85-
}
86-
87-
public protocol JsonKeyRepresentable {
88-
var jsonKey: String { get }
89-
}
90-
91-
public enum JsonKey: String, JsonKeyRepresentable {
92-
case email
93-
case userId
94-
case currentEmail
95-
case currentUserId
96-
case newEmail
97-
case emailListIds
98-
case unsubscribedChannelIds
99-
case unsubscribedMessageTypeIds
100-
case subscribedMessageTypeIds
101-
case preferUserId
102-
103-
case mergeNestedObjects
104-
105-
case inboxMetadata
106-
case inboxTitle = "title"
107-
case inboxSubtitle = "subtitle"
108-
case inboxIcon = "icon"
109-
110-
case inboxExpiresAt = "expiresAt"
111-
case inboxCreatedAt = "createdAt"
112-
113-
case inAppMessageContext = "messageContext"
114-
115-
case campaignId
116-
case templateId
117-
case messageId
118-
case inboxSessionId
119-
120-
case saveToInbox
121-
case silentInbox
122-
case inAppLocation = "location"
123-
case clickedUrl
124-
case read
125-
case priorityLevel
126-
127-
case inboxSessionStart
128-
case inboxSessionEnd
129-
case startTotalMessageCount
130-
case startUnreadMessageCount
131-
case endTotalMessageCount
132-
case endUnreadMessageCount
133-
case impressions
134-
case closeAction
135-
case deleteAction
136-
137-
case url
138-
139-
case device
140-
case token
141-
case dataFields
142-
case deviceInfo
143-
case identifierForVendor
144-
case deviceId
145-
case localizedModel
146-
case model
147-
case userInterfaceIdiom
148-
case systemName
149-
case systemVersion
150-
case platform
151-
case appPackageName
152-
case appVersion
153-
case appBuild
154-
case applicationName
155-
case eventName
156-
case actionIdentifier
157-
case userText
158-
case appAlreadyRunning
159-
160-
case html
161-
162-
case iterableSdkVersion
163-
164-
case notificationsEnabled
165-
166-
case contentType = "Content-Type"
167-
168-
public enum ActionButton {
77+
enum JsonKey {
78+
static let email = "email"
79+
static let userId = "userId"
80+
static let currentEmail = "currentEmail"
81+
static let currentUserId = "currentUserId"
82+
static let newEmail = "newEmail"
83+
static let emailListIds = "emailListIds"
84+
static let unsubscribedChannelIds = "unsubscribedChannelIds"
85+
static let unsubscribedMessageTypeIds = "unsubscribedMessageTypeIds"
86+
static let subscribedMessageTypeIds = "subscribedMessageTypeIds"
87+
static let preferUserId = "preferUserId"
88+
89+
static let mergeNestedObjects = "mergeNestedObjects"
90+
91+
static let inboxMetadata = "inboxMetadata"
92+
static let inboxTitle = "title"
93+
static let inboxSubtitle = "subtitle"
94+
static let inboxIcon = "icon"
95+
96+
static let inboxExpiresAt = "expiresAt"
97+
static let inboxCreatedAt = "createdAt"
98+
99+
static let inAppMessageContext = "messageContext"
100+
101+
static let campaignId = "campaignId"
102+
static let templateId = "templateId"
103+
static let messageId = "messageId"
104+
static let inboxSessionId = "inboxSessionId"
105+
106+
static let saveToInbox = "saveToInbox"
107+
static let silentInbox = "silentInbox"
108+
static let inAppLocation = "location"
109+
static let clickedUrl = "clickedUrl"
110+
static let read = "read"
111+
static let priorityLevel = "priorityLevel"
112+
113+
static let inboxSessionStart = "inboxSessionStart"
114+
static let inboxSessionEnd = "inboxSessionEnd"
115+
static let startTotalMessageCount = "startTotalMessageCount"
116+
static let startUnreadMessageCount = "startUnreadMessageCount"
117+
static let endTotalMessageCount = "endTotalMessageCount"
118+
static let endUnreadMessageCount = "endUnreadMessageCount"
119+
static let impressions = "impressions"
120+
static let closeAction = "closeAction"
121+
static let deleteAction = "deleteAction"
122+
123+
static let url = "url"
124+
125+
static let device = "device"
126+
static let token = "token"
127+
static let dataFields = "dataFields"
128+
static let deviceInfo = "deviceInfo"
129+
static let identifierForVendor = "identifierForVendor"
130+
static let deviceId = "deviceId"
131+
static let localizedModel = "localizedModel"
132+
static let model = "model"
133+
static let userInterfaceIdiom = "userInterfaceIdiom"
134+
static let systemName = "systemName"
135+
static let systemVersion = "systemVersion"
136+
static let platform = "platform"
137+
static let appPackageName = "appPackageName"
138+
static let appVersion = "appVersion"
139+
static let appBuild = "appBuild"
140+
static let applicationName = "applicationName"
141+
static let eventName = "eventName"
142+
static let actionIdentifier = "actionIdentifier"
143+
static let userText = "userText"
144+
static let appAlreadyRunning = "appAlreadyRunning"
145+
146+
static let html = "html"
147+
148+
static let iterableSdkVersion = "iterableSdkVersion"
149+
150+
static let notificationsEnabled = "notificationsEnabled"
151+
152+
static let contentType = "Content-Type"
153+
154+
enum ActionButton {
169155
static let identifier = "identifier"
170156
static let action = "action"
171157
}
172158

173-
public enum Commerce {
159+
enum Commerce {
174160
static let items = "items"
175161
static let total = "total"
176162
static let user = "user"
177163
}
178164

179-
public enum Device {
165+
enum Device {
180166
static let localizedModel = "localizedModel"
181167
static let vendorId = "identifierForVendor"
182168
static let model = "model"
@@ -185,7 +171,7 @@ public enum JsonKey: String, JsonKeyRepresentable {
185171
static let userInterfaceIdiom = "userInterfaceIdiom"
186172
}
187173

188-
public enum Header {
174+
enum Header {
189175
static let apiKey = "Api-Key"
190176
static let sdkVersion = "SDK-Version"
191177
static let sdkPlatform = "SDK-Platform"
@@ -194,11 +180,11 @@ public enum JsonKey: String, JsonKeyRepresentable {
194180
static let requestProcessor = "SDK-Request-Processor"
195181
}
196182

197-
public enum Body {
183+
enum Body {
198184
static let createdAt = "createdAt"
199185
}
200186

201-
public enum InApp {
187+
enum InApp {
202188
static let trigger = "trigger"
203189
static let type = "type"
204190
static let contentType = "contentType"
@@ -212,60 +198,48 @@ public enum JsonKey: String, JsonKeyRepresentable {
212198
static let content = "content"
213199
}
214200

215-
public enum Payload {
201+
enum Payload {
216202
static let metadata = "itbl"
217203
static let actionButtons = "actionButtons"
218204
static let defaultAction = "defaultAction"
219205
}
220206

221-
public enum Response {
207+
enum Response {
222208
static let iterableCode = "code"
223209
}
224210

225-
public enum JWT {
211+
enum JWT {
226212
static let exp = "exp"
227213
}
228-
229-
public var jsonKey: String {
230-
rawValue
231-
}
232214
}
233215

234-
public protocol JsonValueRepresentable {
235-
var jsonValue: Any { get }
236-
}
216+
enum JsonValue {
217+
static let applicationJson = "application/json"
218+
static let apnsSandbox = "APNS_SANDBOX"
219+
static let apnsProduction = "APNS"
220+
static let iOS = "iOS"
221+
static let bearer = "Bearer"
237222

238-
public enum JsonValue: String, JsonValueRepresentable {
239-
case applicationJson = "application/json"
240-
case apnsSandbox = "APNS_SANDBOX"
241-
case apnsProduction = "APNS"
242-
case iOS
243-
case bearer = "Bearer"
244-
245-
public enum ActionIdentifier {
223+
enum ActionIdentifier {
246224
static let pushOpenDefault = "default"
247225
}
248226

249-
public enum DeviceIdiom {
227+
enum DeviceIdiom {
250228
static let pad = "Pad"
251229
static let phone = "Phone"
252230
static let carPlay = "CarPlay"
253231
static let tv = "TV"
254232
static let unspecified = "Unspecified"
255233
}
256234

257-
public enum Code {
235+
enum Code {
258236
static let badApiKey = "BadApiKey"
259237
static let invalidJwtPayload = "InvalidJwtPayload"
260238
}
261-
262-
public var jsonStringValue: String {
263-
rawValue
264-
}
265-
266-
public var jsonValue: Any {
267-
rawValue
268-
}
239+
}
240+
241+
public protocol JsonValueRepresentable {
242+
var jsonValue: Any { get }
269243
}
270244

271245
@objc public enum InAppLocation: Int, JsonValueRepresentable {

swift-sdk/Internal/ClassExtensions.swift

+10-38
Original file line numberDiff line numberDiff line change
@@ -24,52 +24,24 @@ extension Array where Element: Comparable {
2424
}
2525

2626
extension Dictionary where Key == AnyHashable, Value == Any {
27-
func getValue(for key: JsonKey) -> Any? {
28-
self[key.jsonKey]
29-
}
30-
31-
func getValue(for key: JsonKeyRepresentable) -> Any? {
32-
self[key.jsonKey]
33-
}
34-
35-
func getStringValue(for key: JsonKey, withDefault default: String? = nil) -> String? {
36-
getValue(for: key) as? String ?? `default`
37-
}
38-
39-
func getStringValue(for key: JsonKeyRepresentable, withDefault default: String? = nil) -> String? {
40-
getValue(for: key) as? String ?? `default`
41-
}
42-
43-
func getIntValue(for key: JsonKey) -> Int? {
44-
getValue(for: key) as? Int
45-
}
46-
47-
func getIntValue(for key: JsonKeyRepresentable) -> Int? {
48-
getValue(for: key) as? Int
27+
func getStringValue(for key: AnyHashable, withDefault default: String? = nil) -> String? {
28+
self[key] as? String ?? `default`
4929
}
5030

51-
func getDoubleValue(for key: JsonKey) -> Double? {
52-
getValue(for: key) as? Double
31+
func getIntValue(for key: AnyHashable) -> Int? {
32+
self[key] as? Int
5333
}
5434

55-
func getDoubleValue(for key: JsonKeyRepresentable) -> Double? {
56-
getValue(for: key) as? Double
35+
func getDoubleValue(for key: AnyHashable) -> Double? {
36+
self[key] as? Double
5737
}
5838

59-
func getBoolValue(for key: JsonKey) -> Bool? {
60-
getValue(for: key).flatMap ( Self.parseBool(_:) )
39+
func getBoolValue(for key: AnyHashable) -> Bool? {
40+
self[key].flatMap ( Self.parseBool(_:) )
6141
}
6242

63-
func getBoolValue(for key: JsonKeyRepresentable) -> Bool? {
64-
getValue(for: key).flatMap ( Self.parseBool(_:) )
65-
}
66-
67-
mutating func setValue(for key: JsonKey, value: JsonValueRepresentable?) {
68-
self[key.jsonKey] = value?.jsonValue
69-
}
70-
71-
mutating func setValue(for key: JsonKeyRepresentable, value: JsonValueRepresentable?) {
72-
self[key.jsonKey] = value?.jsonValue
43+
mutating func setValue(for key: AnyHashable, value: JsonValueRepresentable?) {
44+
self[key] = value?.jsonValue
7345
}
7446

7547
private static func parseBool(_ any: Any?) -> Bool? {

0 commit comments

Comments
 (0)