Skip to content

Commit 2a7c2e8

Browse files
authored
Merge pull request #651 from Iterable/omni-cg-master-5730
Set Read and Remove Message callbacks MOB - 5730
2 parents 5d8c4fb + 47f3fe8 commit 2a7c2e8

File tree

4 files changed

+95
-16
lines changed

4 files changed

+95
-16
lines changed

swift-sdk/Internal/EmptyInAppManager.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Foundation
66
import UIKit
77

88
class EmptyInAppManager: IterableInternalInAppManagerProtocol {
9+
910
func start() -> Pending<Bool, Error> {
1011
Fulfill<Bool, Error>(value: true)
1112
}
@@ -34,14 +35,24 @@ class EmptyInAppManager: IterableInternalInAppManagerProtocol {
3435

3536
func remove(message _: IterableInAppMessage) {}
3637

38+
func remove(message _: IterableInAppMessage, successHandler _: OnSuccessHandler?, failureHandler _: OnFailureHandler?) {}
39+
3740
func remove(message _: IterableInAppMessage, location _: InAppLocation) {}
3841

42+
func remove(message _: IterableInAppMessage, location _: InAppLocation, successHandler _: OnSuccessHandler?, failureHandler _: OnFailureHandler?) {}
43+
3944
func remove(message _: IterableInAppMessage, location _: InAppLocation, source _: InAppDeleteSource) {}
4045

46+
func remove(message _: IterableInAppMessage, location _: InAppLocation, source _: InAppDeleteSource, successHandler _: OnSuccessHandler?, failureHandler _: OnFailureHandler?) {}
47+
4148
func remove(message _: IterableInAppMessage, location _: InAppLocation, source _: InAppDeleteSource, inboxSessionId _: String?) {}
4249

50+
func remove(message _: IterableInAppMessage, location _: InAppLocation, source _: InAppDeleteSource, inboxSessionId _: String?, successHandler _: OnSuccessHandler?, failureHandler _: OnFailureHandler?) {}
51+
4352
func set(read _: Bool, forMessage _: IterableInAppMessage) {}
4453

54+
func set(read _: Bool, forMessage _: IterableInAppMessage, successHandler _: OnSuccessHandler?, failureHandler _: OnFailureHandler?) {}
55+
4556
func getMessage(withId _: String) -> IterableInAppMessage? {
4657
nil
4758
}

swift-sdk/Internal/InAppManager.swift

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,24 @@ protocol IterableInternalInAppManagerProtocol: IterableInAppManagerProtocol, InA
1919
/// - parameter inboxSessionId: The ID of the inbox session that the message originates from.
2020
func handleClick(clickedUrl url: URL?, forMessage message: IterableInAppMessage, location: InAppLocation, inboxSessionId: String?)
2121

22+
2223
/// - parameter message: The message to remove.
2324
/// - parameter location: The location from where this message was shown. `inbox` or `inApp`.
2425
/// - parameter source: The source of deletion `inboxSwipe` or `deleteButton`.`
2526
/// - parameter inboxSessionId: The ID of the inbox session that the message originates from.
2627
func remove(message: IterableInAppMessage, location: InAppLocation, source: InAppDeleteSource, inboxSessionId: String?)
28+
29+
/// - parameter message: The message to remove.
30+
/// - parameter location: The location from where this message was shown. `inbox` or `inApp`.
31+
/// - parameter source: The source of deletion `inboxSwipe` or `deleteButton`.`
32+
/// - parameter inboxSessionId: The ID of the inbox session that the message originates from.
33+
/// - parameter successHandler: The callback which returns `success.
34+
/// - parameter failureHandler: The callback which returns `failure.
35+
func remove(message: IterableInAppMessage, location: InAppLocation, source: InAppDeleteSource, inboxSessionId: String?, successHandler: OnSuccessHandler?, failureHandler: OnFailureHandler?)
2736
}
2837

2938
class InAppManager: NSObject, IterableInternalInAppManagerProtocol {
39+
3040
init(requestHandler: RequestHandlerProtocol,
3141
deviceMetadata: DeviceMetadata,
3242
fetcher: InAppFetcherProtocol,
@@ -124,26 +134,44 @@ class InAppManager: NSObject, IterableInternalInAppManagerProtocol {
124134
func remove(message: IterableInAppMessage, location: InAppLocation) {
125135
ITBInfo()
126136

127-
removePrivate(message: message, location: location)
137+
remove(message: message, location: location, successHandler: nil, failureHandler: nil)
138+
}
139+
140+
func remove(message: IterableInAppMessage, location: InAppLocation, successHandler: OnSuccessHandler? = nil, failureHandler: OnFailureHandler? = nil) {
141+
removePrivate(message: message, location: location, successHandler: successHandler, failureHandler: failureHandler)
128142
}
129143

130144
func remove(message: IterableInAppMessage, location: InAppLocation, source: InAppDeleteSource) {
131-
ITBInfo()
145+
remove(message: message, location: location, source: source, successHandler: nil, failureHandler: nil)
146+
}
147+
148+
func remove(message: IterableInAppMessage, location: InAppLocation, source: InAppDeleteSource, successHandler: OnSuccessHandler? = nil, failureHandler: OnFailureHandler? = nil) {
132149

133-
removePrivate(message: message, location: location, source: source)
150+
removePrivate(message: message, location: location, source: source, successHandler: successHandler, failureHandler: failureHandler)
134151
}
135152

136-
func remove(message: IterableInAppMessage, location: InAppLocation, source: InAppDeleteSource, inboxSessionId: String? = nil) {
153+
func remove(message: IterableInAppMessage, location: InAppLocation, source: InAppDeleteSource, inboxSessionId: String?) {
137154
ITBInfo()
138155

139-
removePrivate(message: message, location: location, source: source, inboxSessionId: inboxSessionId)
156+
remove(message: message, location: location, source: source, inboxSessionId: inboxSessionId, successHandler: nil, failureHandler: nil)
157+
}
158+
159+
func remove(message: IterableInAppMessage, location: InAppLocation, source: InAppDeleteSource, inboxSessionId: String? = nil, successHandler: OnSuccessHandler? = nil, failureHandler: OnFailureHandler? = nil) {
160+
removePrivate(message: message, location: location, source: source, inboxSessionId: inboxSessionId, successHandler: successHandler, failureHandler: failureHandler)
140161
}
141162

142163
func set(read: Bool, forMessage message: IterableInAppMessage) {
164+
set(read: read, forMessage: message, successHandler: nil, failureHandler: nil)
165+
}
166+
167+
func set(read: Bool, forMessage message: IterableInAppMessage, successHandler: OnSuccessHandler? = nil, failureHandler: OnFailureHandler? = nil) {
143168
updateMessage(message, read: read).onSuccess { [weak self] _ in
169+
successHandler?([:])
144170
self?.callbackQueue.async { [weak self] in
145171
self?.notificationCenter.post(name: .iterableInboxChanged, object: self, userInfo: nil)
146172
}
173+
}.onError { [weak self] _ in
174+
failureHandler?(self?.description, nil)
147175
}
148176
}
149177

@@ -185,8 +213,12 @@ class InAppManager: NSObject, IterableInternalInAppManagerProtocol {
185213

186214
func remove(message: IterableInAppMessage) {
187215
ITBInfo()
188-
189-
removePrivate(message: message)
216+
217+
remove(message: message, successHandler: nil, failureHandler: nil)
218+
}
219+
220+
func remove(message: IterableInAppMessage, successHandler: OnSuccessHandler?, failureHandler: OnFailureHandler?) {
221+
removePrivate(message: message, location: .inApp, source: nil, successHandler: successHandler, failureHandler: failureHandler)
190222
}
191223

192224
// MARK: - Private/Internal
@@ -462,16 +494,17 @@ class InAppManager: NSObject, IterableInternalInAppManagerProtocol {
462494
private func removePrivate(message: IterableInAppMessage,
463495
location: InAppLocation = .inApp,
464496
source: InAppDeleteSource? = nil,
465-
inboxSessionId: String? = nil) {
497+
inboxSessionId: String? = nil,
498+
successHandler: OnSuccessHandler? = nil,
499+
failureHandler: OnFailureHandler? = nil) {
466500
ITBInfo()
467-
468501
updateMessage(message, didProcessTrigger: true, consumed: true)
469502
requestHandler?.inAppConsume(message: message,
470503
location: location,
471504
source: source,
472505
inboxSessionId: inboxSessionId,
473-
onSuccess: nil,
474-
onFailure: nil)
506+
onSuccess: successHandler,
507+
onFailure: failureHandler)
475508
callbackQueue.async { [weak self] in
476509
self?.notificationCenter.post(name: .iterableInboxChanged, object: self, userInfo: nil)
477510
}

swift-sdk/IterableInAppManagerProtocol.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,49 @@ import Foundation
2828
/// Note that this callback is called in addition to calling `IterableCustomActionDelegate` or `IterableUrlDelegate` on the button action.
2929
@objc(showMessage:consume:callbackBlock:) func show(message: IterableInAppMessage, consume: Bool, callback: ITBURLCallback?)
3030

31+
3132
/// - parameter message: The message to remove.
3233
@objc(removeMessage:) func remove(message: IterableInAppMessage)
3334

35+
/// - parameter message: The message to remove.
36+
/// - parameter successHandler: The callback which returns `success.
37+
/// - parameter failureHandler: The callback which returns `failure.
38+
@objc(removeMessage:successHandler:failureHandler:) func remove(message: IterableInAppMessage, successHandler: OnSuccessHandler?, failureHandler: OnFailureHandler?)
39+
40+
3441
/// - parameter message: The message to remove.
3542
/// - parameter location: The location from where this message was shown. `inbox` or `inApp`.
3643
@objc(removeMessage:location:) func remove(message: IterableInAppMessage, location: InAppLocation)
3744

45+
/// - parameter message: The message to remove.
46+
/// - parameter location: The location from where this message was shown. `inbox` or `inApp`.
47+
/// - parameter successHandler: The callback which returns `success.
48+
/// - parameter failureHandler: The callback which returns `failure.
49+
@objc(removeMessage:location:successHandler:failureHandler:) func remove(message: IterableInAppMessage, location: InAppLocation, successHandler: OnSuccessHandler?, failureHandler: OnFailureHandler?)
50+
51+
3852
/// - parameter message: The message to remove.
3953
/// - parameter location: The location from where this message was shown. `inbox` or `inApp`.
4054
/// - parameter source: The source of deletion `inboxSwipe` or `deleteButton`.`
4155
@objc(removeMessage:location:source:) func remove(message: IterableInAppMessage, location: InAppLocation, source: InAppDeleteSource)
4256

57+
/// - parameter message: The message to remove.
58+
/// - parameter location: The location from where this message was shown. `inbox` or `inApp`.
59+
/// - parameter source: The source of deletion `inboxSwipe` or `deleteButton`.`
60+
/// - parameter successHandler: The callback which returns `success.
61+
/// - parameter failureHandler: The callback which returns `failure.
62+
@objc(removeMessage:location:source:successHandler:failureHandler:) func remove(message: IterableInAppMessage, location: InAppLocation, source: InAppDeleteSource, successHandler: OnSuccessHandler?, failureHandler: OnFailureHandler?)
63+
4364
/// - parameter read: Whether this inbox message was read
4465
/// - parameter message: The inbox message
4566
@objc(setRead:forMessage:) func set(read: Bool, forMessage message: IterableInAppMessage)
4667

68+
/// - parameter read: Whether this inbox message was read
69+
/// - parameter message: The inbox message
70+
/// - parameter successHandler: The callback which returns `success.
71+
/// - parameter failureHandler: The callback which returns `failure.
72+
@objc(setRead:forMessage:successHandler:failureHandler:) func set(read: Bool, forMessage message: IterableInAppMessage, successHandler: OnSuccessHandler?, failureHandler: OnFailureHandler?)
73+
4774
/// - parameter id: The id of the message
4875
/// - returns: IterableInAppMessage with the id, if it exists.
4976
@objc(getMessageWithId:) func getMessage(withId id: String) -> IterableInAppMessage?

tests/unit-tests/InboxTests.swift

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,12 @@ class InboxTests: XCTestCase {
162162
let mockInAppFetcher = MockInAppFetcher()
163163
let config = IterableConfig()
164164
config.logDelegate = AllLogDelegate()
165-
165+
166166
let internalAPI = InternalIterableAPI.initializeForTesting(
167167
config: config,
168168
inAppFetcher: mockInAppFetcher
169169
)
170-
170+
171171
let payload = """
172172
{"inAppMessages":
173173
[
@@ -190,19 +190,27 @@ class InboxTests: XCTestCase {
190190
]
191191
}
192192
""".toJsonDict()
193-
193+
194194
mockInAppFetcher.mockInAppPayloadFromServer(internalApi: internalAPI, payload).onSuccess { _ in
195195
let messages = internalAPI.inAppManager.getInboxMessages()
196196
XCTAssertEqual(messages.count, 2)
197197

198-
internalAPI.inAppManager.remove(message: messages[0], location: .inbox, source: .inboxSwipe)
198+
let messageToRemove = messages[0]
199+
internalAPI.inAppManager.remove(
200+
message: messageToRemove,
201+
location: .inbox,
202+
source: .inboxSwipe,
203+
successHandler: { _ in },
204+
failureHandler: { _, _ in }
205+
)
206+
199207
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
200208
let newMessages = internalAPI.inAppManager.getInboxMessages()
201209
XCTAssertEqual(newMessages.count, 1)
202210
expectation1.fulfill()
203211
}
204212
}
205-
213+
206214
wait(for: [expectation1], timeout: testExpectationTimeout)
207215
}
208216

0 commit comments

Comments
 (0)