Skip to content

Commit

Permalink
feat: fetch in-app messages on new profile identification (#794)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrehan27 authored Aug 21, 2024
1 parent 46bda24 commit 13032c6
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions Sources/MessagingInApp/Gist/EngineWeb/EngineWeb.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class EngineWeb: NSObject {
}

public func cleanEngineWeb() {
Logger.instance.debug(message: "Cleaning EngineWeb")
webView.removeFromSuperview()
webView.configuration.userContentController.removeAllUserScripts()
webView.configuration.userContentController.removeScriptMessageHandler(forName: "gist")
Expand Down
1 change: 1 addition & 0 deletions Sources/MessagingInApp/Gist/Gist.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class Gist: GistDelegate {

public func setUserToken(_ userToken: String) {
UserManager().setUserToken(userToken: userToken)
messageQueueManager.fetchUserMessagesFromRemoteQueue()
}

public func clearUserToken() {
Expand Down
6 changes: 6 additions & 0 deletions Sources/MessagingInApp/Gist/Managers/MessageManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class MessageManager: EngineWebDelegate {
}

private func loadModalMessage() {
Logger.instance.debug(message: "Loading modal message with id: \(currentMessage.messageId), messageLoaded: \(messageLoaded)")

if messageLoaded {
modalViewManager = ModalViewManager(gistView: gistView, position: messagePosition)
modalViewManager?.showModalView { [weak self] in
Expand All @@ -61,6 +63,8 @@ class MessageManager: EngineWebDelegate {
}

func dismissMessage(completionHandler: (() -> Void)? = nil) {
Logger.instance.debug(message: "Dismissing message with id: \(currentMessage.messageId)")

if let modalViewManager = modalViewManager {
modalViewManager.dismissModalView { [weak self] in
guard let self = self else { return }
Expand All @@ -71,6 +75,8 @@ class MessageManager: EngineWebDelegate {
}

func removePersistentMessage() {
Logger.instance.debug(message: "Removing persistent message with id: \(currentMessage.messageId)")

if currentMessage.gistProperties.persistent == true {
Logger.instance.debug(message: "Persistent message dismissed, logging view")
Gist.shared.logMessageView(message: currentMessage)
Expand Down
11 changes: 11 additions & 0 deletions Sources/MessagingInApp/Gist/Managers/MessageQueueManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class MessageQueueManager {
queueTimer?.invalidate()
queueTimer = nil

Logger.instance.debug(message: "Scheduling fetch messages after interval \(interval) with skipQueueCheck \(skipQueueCheck)")
queueTimer = Timer.scheduledTimer(
timeInterval: interval,
target: self,
Expand All @@ -23,6 +24,7 @@ class MessageQueueManager {
// Since on app launch there's a short period where the applicationState is still set to "background"
// We wait 1 second for the app to become active before checking for messages.
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
Logger.instance.info(message: "Fetching user messages on setup")
self.fetchUserMessages()
}
}
Expand Down Expand Up @@ -61,11 +63,17 @@ class MessageQueueManager {

private func addMessageToLocalStore(message: Message) {
guard let queueId = message.queueId else {
Logger.instance.debug(message: "Message has no queueId, skipping.")
return
}
localMessageStore.updateValue(message, forKey: queueId)
}

func fetchUserMessagesFromRemoteQueue() {
Logger.instance.debug(message: "Fetching user messages from remote queue")
fetchUserMessages()
}

@objc
private func fetchUserMessages() {
if UIApplication.shared.applicationState != .background {
Expand All @@ -78,9 +86,11 @@ class MessageQueueManager {
Logger.instance.info(message: "No changes to remote queue")
case .success(let responses):
guard let responses else {
Logger.instance.error(message: "No responses found in remote queue")
return
}
// To prevent us from showing expired / revoked messages, clear user messages from local queue.
Logger.instance.info(message: "Clearing local store with \(self.localMessageStore.count) messages")
self.clearUserMessagesFromLocalStore()
Logger.instance.info(message: "Gist queue service found \(responses.count) new messages")
for queueMessage in responses {
Expand Down Expand Up @@ -128,6 +138,7 @@ class MessageQueueManager {
Gist.shared.embedMessage(message: message, elementId: elementId)
return
} else {
Logger.instance.info(message: "Showing message with position \(position)")
_ = Gist.shared.showMessage(message, position: position)
}
}
Expand Down
4 changes: 4 additions & 0 deletions Sources/MessagingInApp/Gist/Managers/ModalViewManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class ModalViewManager {
}

func showModalView(completionHandler: @escaping () -> Void) {
Logger.instance.debug(message: "Showing modal view")
viewController.view.isHidden = true
window = getUIWindow()
window.rootViewController = viewController
Expand All @@ -43,13 +44,15 @@ class ModalViewManager {
UIView.animate(withDuration: 0.1, delay: 0, options: [.curveEaseIn], animations: {
self.viewController.view.backgroundColor = UIColor.black.withAlphaComponent(0.2)
}, completion: nil)
Logger.instance.debug(message: "Modal view shown")
completionHandler()
})

viewController.view.isHidden = false
}

func dismissModalView(completionHandler: @escaping () -> Void) {
Logger.instance.debug(message: "Dismissing modal view")
var finalPosition: CGFloat = 0
switch position {
case .top:
Expand All @@ -69,6 +72,7 @@ class ModalViewManager {
self.window?.isHidden = false
self.viewController.removeFromParent()
self.window = nil
Logger.instance.debug(message: "Modal view dismissed")
completionHandler()
})
})
Expand Down
2 changes: 2 additions & 0 deletions Sources/MessagingInApp/Gist/Managers/UserManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ class UserManager {
}

func setUserToken(userToken: String) {
Logger.instance.info(message: "User token set: \(userToken)")
defaults.set(userToken, forKey: userTokenKey)
}

func clearUserToken() {
Logger.instance.info(message: "User token cleared")
defaults.removeObject(forKey: userTokenKey)
}
}
1 change: 1 addition & 0 deletions Sources/MessagingInApp/MessagingInAppImplementation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class MessagingInAppImplementation: MessagingInAppInstance {
}

func initialize() {
logger.debug("initializing in-app messaging with region: \(region)")
inAppProvider.initialize(siteId: siteId, region: region, delegate: self, enableLogging: logLevel == .debug)

// if identifier is already present, set the userToken again so in case if the customer was already identified and
Expand Down

0 comments on commit 13032c6

Please sign in to comment.