Skip to content

Commit 4d391ab

Browse files
Return ShowInAppResult enum from IterableInAppManager.
1 parent 1ca68b3 commit 4d391ab

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

swift-sdk/Internal/IterableAPIInternal.swift

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -399,27 +399,30 @@ final class IterableAPIInternal : NSObject, PushTrackerProtocol {
399399
}
400400
}
401401

402-
func spawn(inAppNotification callbackBlock:ITEActionBlock?) {
403-
getInAppMessages(1).flatMap { IterableInAppManager.handleInApp(withPayload: $0, callbackBlock: callbackBlock) }.onSuccess {
404-
switch $0 {
405-
case .success(opened: let opened, messageId: let messageId):
406-
if opened {
407-
self.inAppConsume(messageId)
402+
@discardableResult func spawn(inAppNotification callbackBlock:ITEActionBlock?) -> Future<IterableInAppManager.ShowInAppResult> {
403+
return getInAppMessages(1)
404+
.map { IterableInAppManager.parseInApp(fromPayload: $0)}
405+
.flatMap { IterableInAppManager.showInApp(parseResult: $0, callbackBlock: callbackBlock)}
406+
.onSuccess {
407+
switch $0 {
408+
case .success(opened: let opened, messageId: let messageId):
409+
if opened {
410+
self.inAppConsume(messageId)
411+
}
412+
case .failure(reason: let reason, messageId: let messageId):
413+
if let messageId = messageId {
414+
self.inAppConsume(messageId)
415+
}
416+
ITBError(reason)
408417
}
409-
case .failure(reason: let reason, messageId: let messageId):
410-
if let messageId = messageId {
411-
self.inAppConsume(messageId)
412-
}
413-
ITBError(reason)
414418
}
415-
}
416419
}
417420

418-
func getInAppMessages(_ count: NSNumber) -> Future<SendRequestValue> {
421+
@discardableResult func getInAppMessages(_ count: NSNumber) -> Future<SendRequestValue> {
419422
return getInAppMessages(count, onSuccess: IterableAPIInternal.defaultOnSucess(identifier: "getMessages"), onFailure: IterableAPIInternal.defaultOnFailure(identifier: "getMessages"))
420423
}
421424

422-
func getInAppMessages(_ count: NSNumber, onSuccess: OnSuccessHandler?, onFailure: OnFailureHandler?) -> Future<SendRequestValue> {
425+
@discardableResult func getInAppMessages(_ count: NSNumber, onSuccess: OnSuccessHandler?, onFailure: OnFailureHandler?) -> Future<SendRequestValue> {
423426
guard email != nil || userId != nil else {
424427
ITBError("Both email and userId are nil")
425428
onFailure?("Both email and userId are nil", nil)

swift-sdk/Internal/IterableInAppManager.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,15 @@ class IterableInAppManager: NSObject {
170170
}
171171
}
172172

173-
enum HandleInAppResult {
173+
enum ShowInAppResult {
174174
case success(opened: Bool, messageId: String)
175175
case failure(reason: String, messageId: String?)
176176
}
177177

178-
static func handleInApp(withPayload payload: [AnyHashable : Any], callbackBlock:ITEActionBlock?) -> Future<HandleInAppResult> {
179-
let parseResult = parseInApp(fromPayload: payload)
178+
static func showInApp(parseResult: InAppParseResult, callbackBlock:ITEActionBlock?) -> Future<ShowInAppResult> {
180179
switch parseResult {
181180
case .success(let inAppDetails):
182-
let result = Promise<HandleInAppResult>()
181+
let result = Promise<ShowInAppResult>()
183182
let notificationMetadata = IterableNotificationMetadata.metadata(fromInAppOptions: inAppDetails.messageId)
184183

185184
DispatchQueue.main.async {
@@ -192,10 +191,11 @@ class IterableInAppManager: NSObject {
192191
}
193192
return result
194193
case .failure(let reason, let messageId):
195-
return Promise<HandleInAppResult>(value: .failure(reason: reason, messageId: messageId))
194+
return Promise<ShowInAppResult>(value: .failure(reason: reason, messageId: messageId))
196195
}
197196
}
198197

198+
199199
private static func getTopViewController() -> UIViewController? {
200200
guard let rootViewController = IterableUtil.rootViewController else {
201201
return nil
@@ -207,12 +207,12 @@ class IterableInAppManager: NSObject {
207207
return topViewController
208208
}
209209

210-
private enum InAppParseResult {
210+
enum InAppParseResult {
211211
case success(InAppDetails)
212212
case failure(reason: String, messageId: String?)
213213
}
214214

215-
private struct InAppDetails {
215+
struct InAppDetails {
216216
let edgeInsets: UIEdgeInsets
217217
let backgroundAlpha: Double
218218
let messageId: String
@@ -221,7 +221,7 @@ class IterableInAppManager: NSObject {
221221

222222
// Payload is what comes from Api
223223
// If successful you get InAppDetails
224-
private static func parseInApp(fromPayload payload: [AnyHashable : Any]) -> InAppParseResult {
224+
static func parseInApp(fromPayload payload: [AnyHashable : Any]) -> InAppParseResult {
225225
guard let dialogOptions = IterableInAppManager.getNextMessageFromPayload(payload) else {
226226
return .failure(reason: "No notifications found for inApp payload \(payload)", messageId: nil)
227227
}

0 commit comments

Comments
 (0)