Skip to content

Commit

Permalink
Merge pull request #651 from Iterable/omni-cg-master-5730
Browse files Browse the repository at this point in the history
Set Read and Remove Message callbacks MOB - 5730
  • Loading branch information
Ayyanchira committed Jul 7, 2023
2 parents 5d8c4fb + 47f3fe8 commit 2a7c2e8
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 16 deletions.
11 changes: 11 additions & 0 deletions swift-sdk/Internal/EmptyInAppManager.swift
Expand Up @@ -6,6 +6,7 @@ import Foundation
import UIKit

class EmptyInAppManager: IterableInternalInAppManagerProtocol {

func start() -> Pending<Bool, Error> {
Fulfill<Bool, Error>(value: true)
}
Expand Down Expand Up @@ -34,14 +35,24 @@ class EmptyInAppManager: IterableInternalInAppManagerProtocol {

func remove(message _: IterableInAppMessage) {}

func remove(message _: IterableInAppMessage, successHandler _: OnSuccessHandler?, failureHandler _: OnFailureHandler?) {}

func remove(message _: IterableInAppMessage, location _: InAppLocation) {}

func remove(message _: IterableInAppMessage, location _: InAppLocation, successHandler _: OnSuccessHandler?, failureHandler _: OnFailureHandler?) {}

func remove(message _: IterableInAppMessage, location _: InAppLocation, source _: InAppDeleteSource) {}

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

func remove(message _: IterableInAppMessage, location _: InAppLocation, source _: InAppDeleteSource, inboxSessionId _: String?) {}

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

func set(read _: Bool, forMessage _: IterableInAppMessage) {}

func set(read _: Bool, forMessage _: IterableInAppMessage, successHandler _: OnSuccessHandler?, failureHandler _: OnFailureHandler?) {}

func getMessage(withId _: String) -> IterableInAppMessage? {
nil
}
Expand Down
55 changes: 44 additions & 11 deletions swift-sdk/Internal/InAppManager.swift
Expand Up @@ -19,14 +19,24 @@ protocol IterableInternalInAppManagerProtocol: IterableInAppManagerProtocol, InA
/// - parameter inboxSessionId: The ID of the inbox session that the message originates from.
func handleClick(clickedUrl url: URL?, forMessage message: IterableInAppMessage, location: InAppLocation, inboxSessionId: String?)


/// - parameter message: The message to remove.
/// - parameter location: The location from where this message was shown. `inbox` or `inApp`.
/// - parameter source: The source of deletion `inboxSwipe` or `deleteButton`.`
/// - parameter inboxSessionId: The ID of the inbox session that the message originates from.
func remove(message: IterableInAppMessage, location: InAppLocation, source: InAppDeleteSource, inboxSessionId: String?)

/// - parameter message: The message to remove.
/// - parameter location: The location from where this message was shown. `inbox` or `inApp`.
/// - parameter source: The source of deletion `inboxSwipe` or `deleteButton`.`
/// - parameter inboxSessionId: The ID of the inbox session that the message originates from.
/// - parameter successHandler: The callback which returns `success.
/// - parameter failureHandler: The callback which returns `failure.
func remove(message: IterableInAppMessage, location: InAppLocation, source: InAppDeleteSource, inboxSessionId: String?, successHandler: OnSuccessHandler?, failureHandler: OnFailureHandler?)
}

class InAppManager: NSObject, IterableInternalInAppManagerProtocol {

init(requestHandler: RequestHandlerProtocol,
deviceMetadata: DeviceMetadata,
fetcher: InAppFetcherProtocol,
Expand Down Expand Up @@ -124,26 +134,44 @@ class InAppManager: NSObject, IterableInternalInAppManagerProtocol {
func remove(message: IterableInAppMessage, location: InAppLocation) {
ITBInfo()

removePrivate(message: message, location: location)
remove(message: message, location: location, successHandler: nil, failureHandler: nil)
}

func remove(message: IterableInAppMessage, location: InAppLocation, successHandler: OnSuccessHandler? = nil, failureHandler: OnFailureHandler? = nil) {
removePrivate(message: message, location: location, successHandler: successHandler, failureHandler: failureHandler)
}

func remove(message: IterableInAppMessage, location: InAppLocation, source: InAppDeleteSource) {
ITBInfo()
remove(message: message, location: location, source: source, successHandler: nil, failureHandler: nil)
}

func remove(message: IterableInAppMessage, location: InAppLocation, source: InAppDeleteSource, successHandler: OnSuccessHandler? = nil, failureHandler: OnFailureHandler? = nil) {

removePrivate(message: message, location: location, source: source)
removePrivate(message: message, location: location, source: source, successHandler: successHandler, failureHandler: failureHandler)
}

func remove(message: IterableInAppMessage, location: InAppLocation, source: InAppDeleteSource, inboxSessionId: String? = nil) {
func remove(message: IterableInAppMessage, location: InAppLocation, source: InAppDeleteSource, inboxSessionId: String?) {
ITBInfo()

removePrivate(message: message, location: location, source: source, inboxSessionId: inboxSessionId)
remove(message: message, location: location, source: source, inboxSessionId: inboxSessionId, successHandler: nil, failureHandler: nil)
}

func remove(message: IterableInAppMessage, location: InAppLocation, source: InAppDeleteSource, inboxSessionId: String? = nil, successHandler: OnSuccessHandler? = nil, failureHandler: OnFailureHandler? = nil) {
removePrivate(message: message, location: location, source: source, inboxSessionId: inboxSessionId, successHandler: successHandler, failureHandler: failureHandler)
}

func set(read: Bool, forMessage message: IterableInAppMessage) {
set(read: read, forMessage: message, successHandler: nil, failureHandler: nil)
}

func set(read: Bool, forMessage message: IterableInAppMessage, successHandler: OnSuccessHandler? = nil, failureHandler: OnFailureHandler? = nil) {
updateMessage(message, read: read).onSuccess { [weak self] _ in
successHandler?([:])
self?.callbackQueue.async { [weak self] in
self?.notificationCenter.post(name: .iterableInboxChanged, object: self, userInfo: nil)
}
}.onError { [weak self] _ in
failureHandler?(self?.description, nil)
}
}

Expand Down Expand Up @@ -185,8 +213,12 @@ class InAppManager: NSObject, IterableInternalInAppManagerProtocol {

func remove(message: IterableInAppMessage) {
ITBInfo()

removePrivate(message: message)

remove(message: message, successHandler: nil, failureHandler: nil)
}

func remove(message: IterableInAppMessage, successHandler: OnSuccessHandler?, failureHandler: OnFailureHandler?) {
removePrivate(message: message, location: .inApp, source: nil, successHandler: successHandler, failureHandler: failureHandler)
}

// MARK: - Private/Internal
Expand Down Expand Up @@ -462,16 +494,17 @@ class InAppManager: NSObject, IterableInternalInAppManagerProtocol {
private func removePrivate(message: IterableInAppMessage,
location: InAppLocation = .inApp,
source: InAppDeleteSource? = nil,
inboxSessionId: String? = nil) {
inboxSessionId: String? = nil,
successHandler: OnSuccessHandler? = nil,
failureHandler: OnFailureHandler? = nil) {
ITBInfo()

updateMessage(message, didProcessTrigger: true, consumed: true)
requestHandler?.inAppConsume(message: message,
location: location,
source: source,
inboxSessionId: inboxSessionId,
onSuccess: nil,
onFailure: nil)
onSuccess: successHandler,
onFailure: failureHandler)
callbackQueue.async { [weak self] in
self?.notificationCenter.post(name: .iterableInboxChanged, object: self, userInfo: nil)
}
Expand Down
27 changes: 27 additions & 0 deletions swift-sdk/IterableInAppManagerProtocol.swift
Expand Up @@ -28,22 +28,49 @@ import Foundation
/// Note that this callback is called in addition to calling `IterableCustomActionDelegate` or `IterableUrlDelegate` on the button action.
@objc(showMessage:consume:callbackBlock:) func show(message: IterableInAppMessage, consume: Bool, callback: ITBURLCallback?)


/// - parameter message: The message to remove.
@objc(removeMessage:) func remove(message: IterableInAppMessage)

/// - parameter message: The message to remove.
/// - parameter successHandler: The callback which returns `success.
/// - parameter failureHandler: The callback which returns `failure.
@objc(removeMessage:successHandler:failureHandler:) func remove(message: IterableInAppMessage, successHandler: OnSuccessHandler?, failureHandler: OnFailureHandler?)


/// - parameter message: The message to remove.
/// - parameter location: The location from where this message was shown. `inbox` or `inApp`.
@objc(removeMessage:location:) func remove(message: IterableInAppMessage, location: InAppLocation)

/// - parameter message: The message to remove.
/// - parameter location: The location from where this message was shown. `inbox` or `inApp`.
/// - parameter successHandler: The callback which returns `success.
/// - parameter failureHandler: The callback which returns `failure.
@objc(removeMessage:location:successHandler:failureHandler:) func remove(message: IterableInAppMessage, location: InAppLocation, successHandler: OnSuccessHandler?, failureHandler: OnFailureHandler?)


/// - parameter message: The message to remove.
/// - parameter location: The location from where this message was shown. `inbox` or `inApp`.
/// - parameter source: The source of deletion `inboxSwipe` or `deleteButton`.`
@objc(removeMessage:location:source:) func remove(message: IterableInAppMessage, location: InAppLocation, source: InAppDeleteSource)

/// - parameter message: The message to remove.
/// - parameter location: The location from where this message was shown. `inbox` or `inApp`.
/// - parameter source: The source of deletion `inboxSwipe` or `deleteButton`.`
/// - parameter successHandler: The callback which returns `success.
/// - parameter failureHandler: The callback which returns `failure.
@objc(removeMessage:location:source:successHandler:failureHandler:) func remove(message: IterableInAppMessage, location: InAppLocation, source: InAppDeleteSource, successHandler: OnSuccessHandler?, failureHandler: OnFailureHandler?)

/// - parameter read: Whether this inbox message was read
/// - parameter message: The inbox message
@objc(setRead:forMessage:) func set(read: Bool, forMessage message: IterableInAppMessage)

/// - parameter read: Whether this inbox message was read
/// - parameter message: The inbox message
/// - parameter successHandler: The callback which returns `success.
/// - parameter failureHandler: The callback which returns `failure.
@objc(setRead:forMessage:successHandler:failureHandler:) func set(read: Bool, forMessage message: IterableInAppMessage, successHandler: OnSuccessHandler?, failureHandler: OnFailureHandler?)

/// - parameter id: The id of the message
/// - returns: IterableInAppMessage with the id, if it exists.
@objc(getMessageWithId:) func getMessage(withId id: String) -> IterableInAppMessage?
Expand Down
18 changes: 13 additions & 5 deletions tests/unit-tests/InboxTests.swift
Expand Up @@ -162,12 +162,12 @@ class InboxTests: XCTestCase {
let mockInAppFetcher = MockInAppFetcher()
let config = IterableConfig()
config.logDelegate = AllLogDelegate()

let internalAPI = InternalIterableAPI.initializeForTesting(
config: config,
inAppFetcher: mockInAppFetcher
)

let payload = """
{"inAppMessages":
[
Expand All @@ -190,19 +190,27 @@ class InboxTests: XCTestCase {
]
}
""".toJsonDict()

mockInAppFetcher.mockInAppPayloadFromServer(internalApi: internalAPI, payload).onSuccess { _ in
let messages = internalAPI.inAppManager.getInboxMessages()
XCTAssertEqual(messages.count, 2)

internalAPI.inAppManager.remove(message: messages[0], location: .inbox, source: .inboxSwipe)
let messageToRemove = messages[0]
internalAPI.inAppManager.remove(
message: messageToRemove,
location: .inbox,
source: .inboxSwipe,
successHandler: { _ in },
failureHandler: { _, _ in }
)

DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
let newMessages = internalAPI.inAppManager.getInboxMessages()
XCTAssertEqual(newMessages.count, 1)
expectation1.fulfill()
}
}

wait(for: [expectation1], timeout: testExpectationTimeout)
}

Expand Down

0 comments on commit 2a7c2e8

Please sign in to comment.