Skip to content

Commit

Permalink
updated files with the changes on gist
Browse files Browse the repository at this point in the history
  • Loading branch information
Shahroz16 committed Oct 18, 2023
1 parent 31935d8 commit 8efb7b4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 20 deletions.
30 changes: 19 additions & 11 deletions Sources/MessagingInApp/Gist/Gist.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ public class Gist: GistDelegate {
}

public func dismissMessage(instanceId: String? = nil, completionHandler: (() -> Void)? = nil) {
if let id = instanceId {
messageManager(instanceId: id)?.dismissMessage(completionHandler: completionHandler)
if let id = instanceId, let messageManager = messageManager(instanceId: id) {
messageManager.removePersistentMessage()
messageManager.dismissMessage(completionHandler: completionHandler)

Check warning on line 77 in Sources/MessagingInApp/Gist/Gist.swift

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Gist.swift#L75-L77

Added lines #L75 - L77 were not covered by tests
} else {
getModalMessageManager()?.dismissMessage(completionHandler: completionHandler)
}
Expand All @@ -83,15 +84,11 @@ public class Gist: GistDelegate {

public func messageShown(message: Message) {
Logger.instance.debug(message: "Message with route: \(message.messageId) shown")
messageQueueManager.removeMessageFromLocalStore(message: message)
let userToken = UserManager().getUserToken()
LogManager(siteId: siteId, dataCenter: dataCenter)
.logView(message: message, userToken: userToken) { response in
if case .failure(let error) = response {
Logger.instance.error(message:
"Failed to log view for message: \(message.messageId) with error: \(error)")
}
}
if message.gistProperties.persistent != true {
logMessageView(message: message)
} else {
Logger.instance.debug(message: "Persistent message shown, skipping logging view")
}

Check warning on line 91 in Sources/MessagingInApp/Gist/Gist.swift

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Gist.swift#L87-L91

Added lines #L87 - L91 were not covered by tests
delegate?.messageShown(message: message)
}

Expand All @@ -114,6 +111,17 @@ public class Gist: GistDelegate {
delegate?.embedMessage(message: message, elementId: elementId)
}

func logMessageView(message: Message) {
messageQueueManager.removeMessageFromLocalStore(message: message)
let userToken = UserManager().getUserToken()
LogManager(siteId: siteId, dataCenter: dataCenter)
.logView(message: message, userToken: userToken) { response in
if case .failure(let error) = response {
Logger.instance.error(message: "Failed to log view for message: \(message.messageId) with error: \(error)")
}
}
}

Check warning on line 123 in Sources/MessagingInApp/Gist/Gist.swift

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Gist.swift#L114-L123

Added lines #L114 - L123 were not covered by tests

// Message Manager

private func createMessageManager(siteId: String, message: Message) -> MessageManager {
Expand Down
8 changes: 8 additions & 0 deletions Sources/MessagingInApp/Gist/Managers/MessageManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ class MessageManager: EngineWebDelegate {
}
}

func removePersistentMessage() {
if currentMessage.gistProperties.persistent == true {
Logger.instance.debug(message: "Persistent message dismissed, logging view")
Gist.shared.logMessageView(message: currentMessage)
}
}

Check warning on line 78 in Sources/MessagingInApp/Gist/Managers/MessageManager.swift

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Managers/MessageManager.swift#L73-L78

Added lines #L73 - L78 were not covered by tests

func bootstrapped() {
Logger.instance.debug(message: "Bourbon Engine bootstrapped")

Expand All @@ -89,6 +96,7 @@ class MessageManager: EngineWebDelegate {
switch url.host {
case "close":
Logger.instance.info(message: "Dismissing from action: \(action)")
removePersistentMessage()

Check warning on line 99 in Sources/MessagingInApp/Gist/Managers/MessageManager.swift

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Managers/MessageManager.swift#L99

Added line #L99 was not covered by tests
dismissMessage()
case "loadPage":
if let page = url.queryParameters?["url"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class MessageQueueManager {
.fetchUserQueue(userToken: userToken, completionHandler: { response in
switch response {
case .success(let responses):
// To prevent us from showing expired / revoked messages, clear user messages from local queue.
self.clearUserMessagesFromLocalStore()

Check warning on line 60 in Sources/MessagingInApp/Gist/Managers/MessageQueueManager.swift

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Managers/MessageQueueManager.swift#L59-L60

Added lines #L59 - L60 were not covered by tests
Logger.instance.info(message: "Gist queue service found \(responses.count) new messages")
for queueMessage in responses {
let message = queueMessage.toMessage()
Expand Down
39 changes: 30 additions & 9 deletions Sources/MessagingInApp/Gist/Managers/Models/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ public class GistProperties {
public let elementId: String?
public let campaignId: String?
public let position: MessagePosition
public let persistent: Bool?

init(routeRule: String?, elementId: String?, campaignId: String?, position: MessagePosition) {
init(routeRule: String?, elementId: String?, campaignId: String?, position: MessagePosition, persistent: Bool?) {
self.routeRule = routeRule
self.elementId = elementId
self.position = position
self.campaignId = campaignId
self.persistent = persistent
}
}

Expand All @@ -28,27 +30,46 @@ public class Message {

public init(messageId: String) {
self.queueId = nil
self.gistProperties = GistProperties(routeRule: nil, elementId: nil, campaignId: nil, position: .center)
self.gistProperties = GistProperties(routeRule: nil, elementId: nil, campaignId: nil, position: .center, persistent: false)
self.messageId = messageId
}

init(queueId: String? = nil, messageId: String, properties: [String: Any]?) {
self.queueId = queueId
self.gistProperties = GistProperties(routeRule: nil, elementId: nil, campaignId: nil, position: .center)
self.gistProperties = GistProperties(routeRule: nil, elementId: nil, campaignId: nil, position: .center, persistent: false)
self.messageId = messageId

if let properties = properties {
self.properties = properties
if let gist = self.properties["gist"] as? [String: String] {
if let gist = self.properties["gist"] as? [String: Any] {
var messagePosition = MessagePosition.center
if let position = gist["position"], let positionValue = MessagePosition(rawValue: position) {
if let position = gist["position"] as? String,
let positionValue = MessagePosition(rawValue: position) {
messagePosition = positionValue
}
var routeRule: String?
if let routeRuleApple = gist["routeRuleApple"] as? String {
routeRule = routeRuleApple

Check warning on line 52 in Sources/MessagingInApp/Gist/Managers/Models/Message.swift

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Managers/Models/Message.swift#L52

Added line #L52 was not covered by tests
}
var elementId: String?
if let elementIdValue = gist["elementId"] as? String {
elementId = elementIdValue

Check warning on line 56 in Sources/MessagingInApp/Gist/Managers/Models/Message.swift

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Managers/Models/Message.swift#L56

Added line #L56 was not covered by tests
}
var campaignId: String?
if let campaignIdValue = gist["campaignId"] as? String {
campaignId = campaignIdValue
}
var persistent = false
if let persistentValue = gist["persistent"] as? Bool {
persistent = persistentValue

Check warning on line 64 in Sources/MessagingInApp/Gist/Managers/Models/Message.swift

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Managers/Models/Message.swift#L64

Added line #L64 was not covered by tests
}

self.gistProperties = GistProperties(
routeRule: gist["routeRuleApple"],
elementId: gist["elementId"],
campaignId: gist["campaignId"],
position: messagePosition
routeRule: routeRule,
elementId: elementId,
campaignId: campaignId,
position: messagePosition,
persistent: persistent
)
}
}
Expand Down

0 comments on commit 8efb7b4

Please sign in to comment.