Skip to content

Commit

Permalink
Fix event regressions (#3099)
Browse files Browse the repository at this point in the history
* Fix event regressions

* Pending message id

* Pager completed test

* left align
  • Loading branch information
rlepinski authored May 16, 2024
1 parent f096377 commit cb82eb6
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ final class ThomasDisplayListener: ThomasDelegate {
private let timer: ActiveTimerProtocol
private let tracker: ThomasPagerTracker
private var onDismiss: (@MainActor @Sendable (DisplayResult) -> Void)?
private var completedPagers: Set<String> = Set()

init(
analytics: InAppMessageAnalyticsProtocol,
Expand Down Expand Up @@ -125,6 +126,16 @@ final class ThomasDisplayListener: ThomasDelegate {
),
layoutContext: layoutContext
)

if pagerInfo.completed, !completedPagers.contains(pagerInfo.identifier) {
completedPagers.insert(pagerInfo.identifier)
analytics.recordEvent(
InAppPagerCompletedEvent(
pagerInfo: pagerInfo
),
layoutContext: layoutContext
)
}
}

func onPageGesture(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ final class LegacyInAppMessaging: LegacyInAppMessagingProtocol, @unchecked Senda
cleanUpOldData()
}

var pendingMessageId: String? {
var pendingMessageID: String? {
get {
return dataStore.string(forKey: Keys.CurrentStorage.pendingMessageIds.rawValue)
}
Expand Down Expand Up @@ -98,30 +98,45 @@ final class LegacyInAppMessaging: LegacyInAppMessagingProtocol, @unchecked Senda
return
}

if let pending = self.pendingMessageId {
do {
try await self.automationEngine.cancelSchedules(identifiers: [pending])
} catch {
AirshipLogger.debug("Failed to cancel \(pending), \(error)")
if let pending = self.pendingMessageID {
if await self.scheduleExists(identifier: pending) {
AirshipLogger.debug("Pending in-app message replaced")
self.analytics.recordReplacedEvent(
scheduleID: pending,
replacementID: schedule.identifier
)
}

AirshipLogger.debug("Pending in-app message replaced")
self.analytics.recordReplacedEvent(
scheduleID: pending,
replacementID: schedule.identifier
)
await self.cancelSchedule(identifier: pending)
}

self.pendingMessageId = schedule.identifier
self.pendingMessageID = schedule.identifier

do {
try await self.automationEngine.upsertSchedules([schedule])
AirshipLogger.debug("LegacyInAppMessageManager - schedule is saved \(schedule)")
} catch {
AirshipLogger.error("Failed to schedule \(schedule)")
}
}


private func scheduleExists(identifier: String) async -> Bool {
do {
return try await automationEngine.getSchedule(identifier: identifier) != nil
} catch {
AirshipLogger.debug("Failed to query schedule \(identifier), \(error)")
return true
}
}

private func cancelSchedule(identifier: String) async {
do {
return try await automationEngine.cancelSchedules(identifiers: [identifier])
} catch {
AirshipLogger.debug("Failed to cancel schedule \(identifier), \(error)")
}
}

@MainActor
private func generateScheduleFor(message: LegacyInAppMessage) -> AutomationSchedule? {
let primaryColor = InAppMessageColor(hexColorString: message.primaryColor ?? Defaults.primaryColor)
Expand All @@ -133,10 +148,15 @@ final class LegacyInAppMessaging: LegacyInAppMessagingProtocol, @unchecked Senda
.map({ action in
return InAppMessageButtonInfo(
identifier: action.identifier,
label: InAppMessageTextInfo(text: action.title, color: primaryColor, alignment: .center),
label: InAppMessageTextInfo(
text: action.title,
color: primaryColor,
alignment: .left
),
actions: message.buttonActions?[action.identifier],
backgroundColor: secondaryColor,
borderRadius: Defaults.borderRadius)
borderRadius: Defaults.borderRadius
)
})

let displayContent = InAppMessageDisplayContent.Banner(
Expand Down Expand Up @@ -188,28 +208,24 @@ extension LegacyInAppMessaging: InternalLegacyInAppMessagingProtocol {

guard
userInfo.keys.contains(Keys.incomingMessageKey.rawValue),
let messageId = userInfo["_"] as? String,
messageId == self.pendingMessageId
let messageID = userInfo["_"] as? String,
messageID == self.pendingMessageID
else {
completionHandler()
return
}

self.pendingMessageId = nil
self.pendingMessageID = nil

Task {
do {
try await self.automationEngine.cancelSchedules(identifiers: [messageId])
} catch {
AirshipLogger.debug("LegacyInAppMessageManager: failed to cancel \(messageId), \(error)")
if await self.scheduleExists(identifier: messageID) {
AirshipLogger.debug("Pending in-app message replaced")
self.analytics.recordDirectOpenEvent(scheduleID: messageID)
}

AirshipLogger.debug("Pending in-app message replaced")
self.analytics.recordDirectOpenEvent(scheduleID: messageId)
await self.cancelSchedule(identifier: messageID)
completionHandler()
}


}

func receivedRemoteNotification(_ notification: [AnyHashable : Any],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,73 @@ class ThomasDisplayListenerTest: XCTestCase {
XCTAssertNil(self.result.value)
}

@MainActor
func testPagerCompleted() {

let pagerInfos = [
makePagerInfo(
pager: "foo",
page: 0,
pageCount: 1,
completed: false
),

makePagerInfo(
pager: "foo",
page: 1,
pageCount: 1,
completed: true
),

makePagerInfo(
pager: "foo",
page: 0,
pageCount: 1,
completed: true
),

makePagerInfo(
pager: "foo",
page: 1,
pageCount: 1,
completed: true
)
]

pagerInfos.forEach { info in
listener.onPageViewed(
pagerInfo: info,
layoutContext: self.layoutContext
)
}


verifyEvents(
[
(
InAppPageViewEvent(pagerInfo: pagerInfos[0], viewCount: 1),
self.layoutContext
),
(
InAppPageViewEvent(pagerInfo: pagerInfos[1], viewCount: 1),
self.layoutContext
),
(
InAppPagerCompletedEvent(pagerInfo: pagerInfos[1]),
self.layoutContext
),
(
InAppPageViewEvent(pagerInfo: pagerInfos[2], viewCount: 2),
self.layoutContext
),
(
InAppPageViewEvent(pagerInfo: pagerInfos[3], viewCount: 2),
self.layoutContext
)
]
)
}

@MainActor
func testPageGesture() {
self.timer.start()
Expand Down Expand Up @@ -436,13 +503,18 @@ class ThomasDisplayListenerTest: XCTestCase {
}
}

private func makePagerInfo(pager: String, page: Int) -> ThomasPagerInfo {
private func makePagerInfo(
pager: String,
page: Int,
pageCount: Int = 100,
completed: Bool = false
) -> ThomasPagerInfo {
return ThomasPagerInfo(
identifier: pager,
pageIndex: page,
pageIdentifier: "page-\(page)",
pageCount: 100,
completed: false
pageCount: pageCount,
completed: completed
)
}
}
Loading

0 comments on commit cb82eb6

Please sign in to comment.