Skip to content

Commit ef2c76c

Browse files
authored
Merge pull request #442 from Iterable/MOB-2419-in-app-priority
[MOB-2419] in-app priority
2 parents 9bf4739 + 2fe8a52 commit ef2c76c

File tree

12 files changed

+161
-14
lines changed

12 files changed

+161
-14
lines changed

swift-sdk.xcodeproj/project.pbxproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
5531CDAE22A9C992000D05E2 /* ClassExtensionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5531CDAD22A9C992000D05E2 /* ClassExtensionsTests.swift */; };
1717
5536781F2576FF9000DB3652 /* IterableUtilTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5536781E2576FF9000DB3652 /* IterableUtilTests.swift */; };
1818
556FB1EA244FAF6A00EDF6BD /* InAppPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 556FB1E9244FAF6A00EDF6BD /* InAppPresenter.swift */; };
19+
55705A6925A78BF3009BDEE2 /* InAppPriorityTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55705A6825A78BF3009BDEE2 /* InAppPriorityTests.swift */; };
1920
557AE6BF24A56E5E00B57750 /* Auth.swift in Sources */ = {isa = PBXBuildFile; fileRef = 557AE6BE24A56E5E00B57750 /* Auth.swift */; };
2021
5585DF8F22A73390000A32B9 /* IterableInboxViewControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5585DF8E22A73390000A32B9 /* IterableInboxViewControllerTests.swift */; };
2122
55B3119B251015CF0056E4FC /* AuthManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55298B222501A5AB00190BAE /* AuthManager.swift */; };
@@ -367,6 +368,7 @@
367368
5531CDAD22A9C992000D05E2 /* ClassExtensionsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClassExtensionsTests.swift; sourceTree = "<group>"; };
368369
5536781E2576FF9000DB3652 /* IterableUtilTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IterableUtilTests.swift; sourceTree = "<group>"; };
369370
556FB1E9244FAF6A00EDF6BD /* InAppPresenter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InAppPresenter.swift; sourceTree = "<group>"; };
371+
55705A6825A78BF3009BDEE2 /* InAppPriorityTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = InAppPriorityTests.swift; path = "tests/swift-sdk-swift-tests/in-app-tests/InAppPriorityTests.swift"; sourceTree = SOURCE_ROOT; };
370372
557AE6BE24A56E5E00B57750 /* Auth.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Auth.swift; sourceTree = "<group>"; };
371373
5585DF8E22A73390000A32B9 /* IterableInboxViewControllerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IterableInboxViewControllerTests.swift; sourceTree = "<group>"; };
372374
5585DF9022A877E6000A32B9 /* IterableInboxViewControllerUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IterableInboxViewControllerUITests.swift; sourceTree = "<group>"; };
@@ -687,12 +689,13 @@
687689
557059B125A691D1009BDEE2 /* in-app-tests */ = {
688690
isa = PBXGroup;
689691
children = (
692+
ACC3FD9D2536D7A30004A2E0 /* InAppFilePersistenceTests.swift */,
690693
AC750A49234CD67900561902 /* InAppHelperTests.swift */,
691694
AC6FDD8B20F56309005D811E /* InAppParsingTests.swift */,
692695
55B37FED229F59290042F13A /* InAppPersistenceTests.swift */,
693696
55CC257A2462064F00A77FD5 /* InAppPresenterTests.swift */,
697+
55705A6825A78BF3009BDEE2 /* InAppPriorityTests.swift */,
694698
ACA8D1A821965B7D001B1332 /* InAppTests.swift */,
695-
ACC3FD9D2536D7A30004A2E0 /* InAppFilePersistenceTests.swift */,
696699
);
697700
path = "in-app-tests";
698701
sourceTree = "<group>";
@@ -1815,6 +1818,7 @@
18151818
AC89661E2124FBCE0051A6CD /* IterableAutoRegistrationTests.swift in Sources */,
18161819
ACA8D1A921965B7D001B1332 /* InAppTests.swift in Sources */,
18171820
00B6FACC210E8484007535CF /* APNSTypeCheckerTests.swift in Sources */,
1821+
55705A6925A78BF3009BDEE2 /* InAppPriorityTests.swift in Sources */,
18181822
AC8F35A2239806B500302994 /* InboxViewControllerViewModelTests.swift in Sources */,
18191823
AC995F9A2166EEB50099A184 /* CommonMocks.swift in Sources */,
18201824
5585DF8F22A73390000A32B9 /* IterableInboxViewControllerTests.swift in Sources */,

swift-sdk/Constants.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ public enum Const {
5656
static let payloadExpiration = 24
5757
static let attributionInfoExpiration = 24
5858
}
59+
60+
enum PriorityLevel {
61+
static let critical = 100.0
62+
static let high = 200.0
63+
static let medium = 300.0
64+
static let low = 400.0
65+
66+
static let unassigned = 300.5
67+
}
5968
}
6069

6170
public protocol JsonKeyValueRepresentable {
@@ -106,6 +115,7 @@ public enum JsonKey: String, JsonKeyRepresentable {
106115
case inAppLocation = "location"
107116
case clickedUrl
108117
case read
118+
case priorityLevel
109119

110120
case inboxSessionStart
111121
case inboxSessionEnd

swift-sdk/Internal/InAppManager+Functions.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ struct MessagesProcessor {
6969
}
7070

7171
private func getFirstProcessableTriggeredMessage() -> IterableInAppMessage? {
72-
messagesMap.values.filter(MessagesProcessor.isProcessableTriggeredMessage).first
72+
messagesMap.values
73+
.filter(MessagesProcessor.isProcessableTriggeredMessage)
74+
.sorted { $0.priorityLevel < $1.priorityLevel }
75+
.first
7376
}
7477

7578
private static func isProcessableTriggeredMessage(_ message: IterableInAppMessage) -> Bool {

swift-sdk/Internal/InAppManager.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class InAppManager: NSObject, IterableInternalInAppManagerProtocol {
219219
.map { self.processMergedMessages(appIsReady: appIsReady, mergeMessagesResult: $0) }
220220
}
221221

222-
// messages are new messages coming from the server
222+
/// `messages` are new messages coming from the server
223223
private func mergeMessages(_ messages: [IterableInAppMessage]) -> MergeMessagesResult {
224224
MessagesObtainedHandler(messagesMap: messagesMap, messages: messages).handle()
225225
}
@@ -231,7 +231,7 @@ class InAppManager: NSObject, IterableInternalInAppManagerProtocol {
231231
messagesMap = mergeMessagesResult.messagesMap
232232
}
233233

234-
// track in app delivery
234+
// track in-app delivery
235235
mergeMessagesResult.deliveredMessages.forEach {
236236
_ = apiClient?.track(inAppDelivery: InAppMessageContext.from(message: $0, location: nil))
237237
}

swift-sdk/Internal/InAppMessageParser.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ struct InAppMessageParser {
103103
let createdAt = parseTime(withKey: .inboxCreatedAt, fromJson: json)
104104
let expiresAt = parseTime(withKey: .inboxExpiresAt, fromJson: json)
105105
let read = json[JsonKey.read.jsonKey] as? Bool ?? false
106+
let priorityLevel = json[JsonKey.priorityLevel.jsonKey] as? Double ?? Const.PriorityLevel.unassigned
106107

107108
return .success(IterableInAppMessage(messageId: messageId,
108109
campaignId: campaignId,
@@ -113,7 +114,8 @@ struct InAppMessageParser {
113114
saveToInbox: saveToInbox,
114115
inboxMetadata: inboxMetadata,
115116
customPayload: customPayload,
116-
read: read))
117+
read: read,
118+
priorityLevel: priorityLevel))
117119
}
118120

119121
private static func parseTime(withKey key: JsonKey, fromJson json: [AnyHashable: Any]) -> Date? {

swift-sdk/Internal/InAppPersistence.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ extension IterableInAppMessage: Codable {
234234
case read
235235
case trigger
236236
case content
237+
case priorityLevel
237238
}
238239

239240
enum ContentCodingKeys: String, CodingKey {
@@ -265,6 +266,7 @@ extension IterableInAppMessage: Codable {
265266

266267
let trigger = (try? container.decode(IterableInAppTrigger.self, forKey: .trigger)) ?? .undefinedTrigger
267268
let content = IterableInAppMessage.decodeContent(from: container)
269+
let priorityLevel = (try? container.decode(Double.self, forKey: .priorityLevel)) ?? Const.PriorityLevel.unassigned
268270

269271
self.init(messageId: messageId,
270272
campaignId: campaignId,
@@ -275,7 +277,8 @@ extension IterableInAppMessage: Codable {
275277
saveToInbox: saveToInbox,
276278
inboxMetadata: inboxMetadata,
277279
customPayload: customPayload,
278-
read: read)
280+
read: read,
281+
priorityLevel: priorityLevel)
279282

280283
self.didProcessTrigger = didProcessTrigger
281284
self.consumed = consumed
@@ -294,6 +297,7 @@ extension IterableInAppMessage: Codable {
294297
try? container.encode(didProcessTrigger, forKey: .didProcessTrigger)
295298
try? container.encode(consumed, forKey: .consumed)
296299
try? container.encode(read, forKey: .read)
300+
try? container.encode(priorityLevel, forKey: .priorityLevel)
297301

298302
if let inboxMetadata = inboxMetadata {
299303
try? container.encode(inboxMetadata, forKey: .inboxMetadata)

swift-sdk/Internal/RequestProcessorUtil.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct RequestProcessorUtil {
2424
} else if error.httpStatusCode == 401, error.iterableCode == JsonValue.Code.badApiKey {
2525
ITBError(error.reason)
2626
}
27-
27+
2828
if let onFailure = onFailure {
2929
onFailure(error.reason, error.data)
3030
} else {

swift-sdk/IterableMessaging.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ public extension Notification.Name {
194194
saveToInbox && trigger.type == .never
195195
}
196196

197+
/// the urgency level of this message (nil will be treated as `unassigned` when displaying this message)
198+
public var priorityLevel: Double
199+
197200
// MARK: - Private/Internal
198201

199202
init(messageId: String,
@@ -205,7 +208,8 @@ public extension Notification.Name {
205208
saveToInbox: Bool = false,
206209
inboxMetadata: IterableInboxMetadata? = nil,
207210
customPayload: [AnyHashable: Any]? = nil,
208-
read: Bool = false) {
211+
read: Bool = false,
212+
priorityLevel: Double = Const.PriorityLevel.unassigned) {
209213
self.messageId = messageId
210214
self.campaignId = campaignId
211215
self.trigger = trigger
@@ -216,5 +220,6 @@ public extension Notification.Name {
216220
self.inboxMetadata = inboxMetadata
217221
self.customPayload = customPayload
218222
self.read = read
223+
self.priorityLevel = priorityLevel
219224
}
220225
}

tests/common/CommonExtensions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ extension IterableAPI {
126126
localStorage: LocalStorageProtocol = MockLocalStorage(),
127127
inAppFetcher: InAppFetcherProtocol = MockInAppFetcher(),
128128
inAppDisplayer: InAppDisplayerProtocol = MockInAppDisplayer(),
129-
inAppPersister: InAppPersistenceProtocol = MockInAppPesister(),
129+
inAppPersister: InAppPersistenceProtocol = MockInAppPersister(),
130130
urlOpener: UrlOpenerProtocol = MockUrlOpener(),
131131
applicationStateProvider: ApplicationStateProviderProtocol = UIApplication.shared,
132132
notificationCenter: NotificationCenterProtocol = NotificationCenter.default,
@@ -169,7 +169,7 @@ extension IterableAPIInternal {
169169
localStorage: LocalStorageProtocol = MockLocalStorage(),
170170
inAppFetcher: InAppFetcherProtocol = MockInAppFetcher(),
171171
inAppDisplayer: InAppDisplayerProtocol = MockInAppDisplayer(),
172-
inAppPersister: InAppPersistenceProtocol = MockInAppPesister(),
172+
inAppPersister: InAppPersistenceProtocol = MockInAppPersister(),
173173
urlOpener: UrlOpenerProtocol = MockUrlOpener(),
174174
applicationStateProvider: ApplicationStateProviderProtocol = UIApplication.shared,
175175
notificationCenter: NotificationCenterProtocol = NotificationCenter.default,

tests/common/CommonMocks.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ class MockNotificationCenter: NotificationCenterProtocol {
458458
private var observers = [Observer]()
459459
}
460460

461-
class MockInAppPesister: InAppPersistenceProtocol {
461+
class MockInAppPersister: InAppPersistenceProtocol {
462462
private var messages = [IterableInAppMessage]()
463463

464464
func getMessages() -> [IterableInAppMessage] {

0 commit comments

Comments
 (0)