Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: get WebView for inline message and add to Inline View to display #717

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions Sources/MessagingInApp/Gist/Gist.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import UIKit

protocol GistInstance: AutoMockable {
var siteId: String { get }
func showMessage(_ message: Message, position: MessagePosition) -> Bool
}

public class Gist: GistInstance, GistDelegate {
var messageQueueManager: MessageQueueManager = DIGraphShared.shared.messageQueueManager
var shownModalMessageQueueIds: Set<String> = [] // all modal messages that have been shown in the app already.
private var messageManagers: [MessageManager] = []
private var messageManagers: [ModalMessageManager] = []
public var siteId: String = ""
public var dataCenter: String = ""

Expand All @@ -29,8 +30,9 @@
Logger.instance.enabled = logging
messageQueueManager.setup(skipQueueCheck: false)

// Initialising Gist web with an empty message to fetch fonts and other assets.
_ = Gist.shared.getMessageView(Message(messageId: ""))
// To finish initializing of Gist, we want to fetch fonts and other assets for HTML in-app messages.
// To do that, we try to display a message with an empty message id.
_ = InlineMessageManager(siteId: self.siteId, message: Message(messageId: ""))
}

// MARK: User
Expand Down Expand Up @@ -76,11 +78,6 @@
showMessage(message, position: .center)
}

public func getMessageView(_ message: Message) -> GistView {
let messageManager = createMessageManager(siteId: siteId, message: message)
return messageManager.getMessageView()
}

public func dismissMessage(instanceId: String? = nil, completionHandler: (() -> Void)? = nil) {
if let id = instanceId, let messageManager = messageManager(instanceId: id) {
messageManager.removePersistentMessage()
Expand Down Expand Up @@ -144,18 +141,18 @@

// Message Manager

private func createMessageManager(siteId: String, message: Message) -> MessageManager {
let messageManager = MessageManager(siteId: siteId, message: message)
private func createMessageManager(siteId: String, message: Message) -> ModalMessageManager {
let messageManager = ModalMessageManager(siteId: siteId, message: message)

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

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Gist.swift#L144-L145

Added lines #L144 - L145 were not covered by tests
messageManager.delegate = self
messageManagers.append(messageManager)
return messageManager
}

private func getModalMessageManager() -> MessageManager? {
messageManagers.first(where: { !$0.isMessageEmbed })
private func getModalMessageManager() -> ModalMessageManager? {
messageManagers.first

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

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Gist.swift#L151-L152

Added lines #L151 - L152 were not covered by tests
}

func messageManager(instanceId: String) -> MessageManager? {
func messageManager(instanceId: String) -> ModalMessageManager? {

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

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Gist.swift#L155

Added line #L155 was not covered by tests
messageManagers.first(where: { $0.currentMessage.instanceId == instanceId })
}

Expand Down
53 changes: 53 additions & 0 deletions Sources/MessagingInApp/Gist/Managers/InlineMessageManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import Foundation

// Callbacks specific to inline message events.
protocol InlineMessageManagerDelegate: AnyObject {
func sizeChanged(width: CGFloat, height: CGFloat)
}

/**
Class that implements the business logic for a inline message being displayed. Handle when action buttons are clicked, render the HTML message, and get callbacks for inline message events.

Usage:
```
let inlineMessageManager = InlineMessageManager(siteId: "", message: message)
inlineMessageManager.inlineMessageDelegate = self // Get callbacks for inline message events.
inlineMessageManager.inlineMessageView // View that displays the in-app web message
```
*/
class InlineMessageManager: MessageManager {
var inlineMessageView: GistView? {
let view = super.gistView
view?.delegate = self
return view
}

weak var inlineMessageDelegate: InlineMessageManagerDelegate?

override func onReplaceMessage(newMessageToShow: Message) {
// Not yet implemented. Planned in future update.

Check warning on line 28 in Sources/MessagingInApp/Gist/Managers/InlineMessageManager.swift

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Managers/InlineMessageManager.swift#L27-L28

Added lines #L27 - L28 were not covered by tests
}

override func onDoneLoadingMessage(routeLoaded: String, onComplete: @escaping () -> Void) {
// The Inline View is responsible for making the in-app message visible in the UI. No logic needed in the manager.
onComplete()

Check warning on line 33 in Sources/MessagingInApp/Gist/Managers/InlineMessageManager.swift

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Managers/InlineMessageManager.swift#L31-L33

Added lines #L31 - L33 were not covered by tests
}

override func onDeepLinkOpened() {
// Do not do anything. Continue showing the in-app message.

Check warning on line 37 in Sources/MessagingInApp/Gist/Managers/InlineMessageManager.swift

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Managers/InlineMessageManager.swift#L36-L37

Added lines #L36 - L37 were not covered by tests
}

override func onCloseAction() {
// Not yet implemented. Planned in future update.

Check warning on line 41 in Sources/MessagingInApp/Gist/Managers/InlineMessageManager.swift

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Managers/InlineMessageManager.swift#L40-L41

Added lines #L40 - L41 were not covered by tests
}
}

extension InlineMessageManager: GistViewDelegate {
func sizeChanged(message: Message, width: CGFloat, height: CGFloat) {
inlineMessageDelegate?.sizeChanged(width: width, height: height)

Check warning on line 47 in Sources/MessagingInApp/Gist/Managers/InlineMessageManager.swift

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Managers/InlineMessageManager.swift#L46-L47

Added lines #L46 - L47 were not covered by tests
}

func action(message: Message, currentRoute: String, action: String, name: String) {
// Action button handling is processed by the superclass. Ignore this callback and instead use one of the superclass event callback functions.

Check warning on line 51 in Sources/MessagingInApp/Gist/Managers/InlineMessageManager.swift

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Managers/InlineMessageManager.swift#L50-L51

Added lines #L50 - L51 were not covered by tests
}
}
117 changes: 48 additions & 69 deletions Sources/MessagingInApp/Gist/Managers/MessageManager.swift
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main logic of this PR. MessageManager contains a lot of code that's common between inline and modal messages. ModalMessageManager contains code I pulled out of MessageManager that's only used for modal messages.

Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
case close = "gist://close"
}

class MessageManager: EngineWebDelegate {
/**
Handles business logic for in-app message events such as loading messages and handling when action buttons are clicked.

This class is meant to be extended and not constructed directly. It holds the common logic between all in-app message types.

Usage:
* Extend class.
* Override any of the abstract functions in class to implement custom logic for when certain events happen. Depending on the type of message you are displaying, you may want to handle events differently.
*/
class MessageManager {
private var engine: EngineWeb?
private let siteId: String
private var messagePosition: MessagePosition = .top
private var messageLoaded = false
private var modalViewManager: ModalViewManager?
var isMessageEmbed = false
let currentMessage: Message
var gistView: GistView!
private var currentRoute: String
Expand All @@ -32,51 +37,49 @@
properties: message.toEngineRoute().properties
)

// When EngineWeb instance is constructed, it will begin the rendering process for the in-app message.
// This means that the message begins the process of loading.
// Start a timer that helps us determine how long a message took to load/render.
elapsedTimer.start(title: "Loading message with id: \(currentMessage.messageId)")
self.engine = EngineWeb(configuration: engineWebConfiguration)

if let engine = engine {
engine.delegate = self
self.gistView = GistView(message: currentMessage, engineView: engine.view)
}
}

func showMessage(position: MessagePosition) {
elapsedTimer.start(title: "Displaying modal for message: \(currentMessage.messageId)")
messagePosition = position
deinit {
engine?.cleanEngineWeb()
engine = nil
}

func getMessageView() -> GistView {
isMessageEmbed = true
return gistView
// MARK: event listeners that subclasses override to handle events.

// Called when close action button pressed.
func onCloseAction() {
// Expect subclass implements this.

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

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Managers/MessageManager.swift#L60-L61

Added lines #L60 - L61 were not covered by tests
}

private func loadModalMessage() {
if messageLoaded {
modalViewManager = ModalViewManager(gistView: gistView, position: messagePosition)
modalViewManager?.showModalView { [weak self] in
guard let self = self else { return }
self.delegate?.messageShown(message: self.currentMessage)
self.elapsedTimer.end()
}
}
// Called when a deep link action button was clicked in a message and the SDK opened the deep link.
func onDeepLinkOpened() {
// expect subclass implements this.

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

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Managers/MessageManager.swift#L65-L66

Added lines #L65 - L66 were not covered by tests
}

func dismissMessage(completionHandler: (() -> Void)? = nil) {
if let modalViewManager = modalViewManager {
modalViewManager.dismissModalView { [weak self] in
guard let self = self else { return }
self.delegate?.messageDismissed(message: self.currentMessage)
completionHandler?()
}
}
Comment on lines -64 to -70
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is code that should only be called for modal messages. So I moved this block into the Modal manager subclass.

// Called when the message has finished loading and the WebView is ready to display the message.
func onDoneLoadingMessage(routeLoaded: String, onComplete: @escaping () -> Void) {
// expect subclass implements this.

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

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Managers/MessageManager.swift#L70-L71

Added lines #L70 - L71 were not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am honestly not big fan of subclassing when there are different approaches that makes the code easier to follow.
MessageManager can simply be a protocol with two different implementations if needed. The actual implementations can use similar utility functions for the common logic.
Inheritance is a very strong relation and unless absolutely a must, we can almost always find simpler approaches

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

func removePersistentMessage() {
if currentMessage.gistProperties.persistent == true {
Logger.instance.debug(message: "Persistent message dismissed, logging view")
Gist.shared.logMessageView(message: currentMessage)
}
// Called when an action button is clicked and the action is to show a different in-app message.
func onReplaceMessage(newMessageToShow: Message) {
// subclass should implement

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

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Managers/MessageManager.swift#L75-L76

Added lines #L75 - L76 were not covered by tests
}
}

// The main logic of this class is being the delegate for the EngineWeb instance.
// This class's delegate responsibilities are to run the logic that's common to all types of in-app messages and call the event listeners that subclasses override.
extension MessageManager: EngineWebDelegate {
func bootstrapped() {
Logger.instance.debug(message: "Bourbon Engine bootstrapped")

Expand All @@ -86,7 +89,6 @@
}
}

// swiftlint:disable cyclomatic_complexity
func tap(name: String, action: String, system: Bool) {
Logger.instance.info(message: "Action triggered: \(action) with name: \(name)")
delegate?.action(message: currentMessage, currentRoute: currentRoute, action: action, name: name)
Expand All @@ -96,22 +98,15 @@
switch url.host {
case "close":
Logger.instance.info(message: "Dismissing from action: \(action)")
removePersistentMessage()
dismissMessage()
onCloseAction()

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L101 was not covered by tests
case "loadPage":
if let page = url.queryParameters?["url"],
let pageUrl = URL(string: page),
UIApplication.shared.canOpenURL(pageUrl) {
UIApplication.shared.open(pageUrl)
}
case "showMessage":
if currentMessage.isEmbedded {
showNewMessage(url: url)
} else {
dismissMessage {
self.showNewMessage(url: url)
}
}
showNewMessage(url: url)

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L109 was not covered by tests
default: break
}
} else {
Expand Down Expand Up @@ -140,24 +135,22 @@
UIApplication.shared.open(url) { handled in
if handled {
Logger.instance.info(message: "Dismissing from system action: \(action)")
self.dismissMessage()
self.onDeepLinkOpened()

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L138 was not covered by tests
} else {
Logger.instance.info(message: "System action not handled")
}
}
} else {
Logger.instance.info(message: "Handled by NSUserActivity")
dismissMessage()
onDeepLinkOpened()

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L145 was not covered by tests
}
}
}
}
}

// swiftlint:enable cyclomatic_complexity

// Check if
func continueNSUserActivity(webpageURL: URL) -> Bool {
// Check if deep link can be handled in the host app. By using NSUserActivity, our SDK can handle Universal Links.
private func continueNSUserActivity(webpageURL: URL) -> Bool {

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L153 was not covered by tests
guard #available(iOS 10.0, *) else {
return false
}
Expand All @@ -174,7 +167,7 @@
}

// The NSUserActivity.webpageURL property permits only specific URL schemes. This function exists to validate the scheme and prevent potential exceptions due to incompatible URL formats.
func isLinkValidNSUserActivityLink(_ url: URL) -> Bool {
private func isLinkValidNSUserActivityLink(_ url: URL) -> Bool {

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L170 was not covered by tests
guard let schemeOfUrl = url.scheme else {
return false
}
Expand Down Expand Up @@ -206,25 +199,12 @@

func routeLoaded(route: String) {
Logger.instance.info(message: "Message loaded with route: \(route)")

currentRoute = route
if route == currentMessage.messageId, !messageLoaded {
messageLoaded = true
if isMessageEmbed {
delegate?.messageShown(message: currentMessage)
} else {
if UIApplication.shared.applicationState == .active {
loadModalMessage()
} else {
Gist.shared.removeMessageManager(instanceId: currentMessage.instanceId)
}
}
}
Comment on lines -211 to -222
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is code that should only be called for modal messages. So I moved this block into the Modal manager subclass.

}

deinit {
engine?.cleanEngineWeb()
engine = nil
onDoneLoadingMessage(routeLoaded: currentRoute) {
self.delegate?.messageShown(message: self.currentMessage)
self.elapsedTimer.end()

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

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Managers/MessageManager.swift#L204-L206

Added lines #L204 - L206 were not covered by tests
}
}

private func showNewMessage(url: URL) {
Expand All @@ -236,9 +216,8 @@
let convertedProps = convertToDictionary(text: decodedString) {
properties = convertedProps
}

if let messageId = url.queryParameters?["messageId"] {
_ = Gist.shared.showMessage(Message(messageId: messageId, properties: properties))
onReplaceMessage(newMessageToShow: Message(messageId: messageId, properties: properties))

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L220 was not covered by tests
}
}

Expand Down
62 changes: 62 additions & 0 deletions Sources/MessagingInApp/Gist/Managers/ModalMessageManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import Foundation
import UIKit

/**
Class that implements the business logic for a modal message being displayed. Handle when action buttons are clicked, render the HTML message, and get callbacks for modal message events.
*/
class ModalMessageManager: MessageManager {
private var messageLoaded = false

Check warning on line 8 in Sources/MessagingInApp/Gist/Managers/ModalMessageManager.swift

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Managers/ModalMessageManager.swift#L8

Added line #L8 was not covered by tests
private var modalViewManager: ModalViewManager?
var messagePosition: MessagePosition = .top

Check warning on line 10 in Sources/MessagingInApp/Gist/Managers/ModalMessageManager.swift

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Managers/ModalMessageManager.swift#L10

Added line #L10 was not covered by tests

func showMessage(position: MessagePosition) {
messagePosition = position

Check warning on line 13 in Sources/MessagingInApp/Gist/Managers/ModalMessageManager.swift

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Managers/ModalMessageManager.swift#L12-L13

Added lines #L12 - L13 were not covered by tests
}

override func onDoneLoadingMessage(routeLoaded: String, onComplete: @escaping () -> Void) {
if routeLoaded == currentMessage.messageId, !messageLoaded {
messageLoaded = true

Check warning on line 18 in Sources/MessagingInApp/Gist/Managers/ModalMessageManager.swift

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Managers/ModalMessageManager.swift#L16-L18

Added lines #L16 - L18 were not covered by tests

if UIApplication.shared.applicationState == .active {
modalViewManager = ModalViewManager(gistView: gistView, position: messagePosition)
modalViewManager?.showModalView {
onComplete()

Check warning on line 23 in Sources/MessagingInApp/Gist/Managers/ModalMessageManager.swift

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Managers/ModalMessageManager.swift#L20-L23

Added lines #L20 - L23 were not covered by tests
}
} else {
Gist.shared.removeMessageManager(instanceId: currentMessage.instanceId)

Check warning on line 26 in Sources/MessagingInApp/Gist/Managers/ModalMessageManager.swift

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Managers/ModalMessageManager.swift#L25-L26

Added lines #L25 - L26 were not covered by tests
}
}
}

override func onDeepLinkOpened() {
dismissMessage()

Check warning on line 32 in Sources/MessagingInApp/Gist/Managers/ModalMessageManager.swift

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Managers/ModalMessageManager.swift#L31-L32

Added lines #L31 - L32 were not covered by tests
}

override func onCloseAction() {
removePersistentMessage()
dismissMessage()

Check warning on line 37 in Sources/MessagingInApp/Gist/Managers/ModalMessageManager.swift

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Managers/ModalMessageManager.swift#L35-L37

Added lines #L35 - L37 were not covered by tests
}

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 43 in Sources/MessagingInApp/Gist/Managers/ModalMessageManager.swift

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Managers/ModalMessageManager.swift#L40-L43

Added lines #L40 - L43 were not covered by tests
}
}

func dismissMessage(completionHandler: (() -> Void)? = nil) {
if let modalViewManager = modalViewManager {
modalViewManager.dismissModalView { [weak self] in
guard let self = self else { return }
self.delegate?.messageDismissed(message: self.currentMessage)
completionHandler?()

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

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Managers/ModalMessageManager.swift#L47-L52

Added lines #L47 - L52 were not covered by tests
}
}
}

override func onReplaceMessage(newMessageToShow: Message) {
dismissMessage {
_ = Gist.shared.showMessage(newMessageToShow)

Check warning on line 59 in Sources/MessagingInApp/Gist/Managers/ModalMessageManager.swift

View check run for this annotation

Codecov / codecov/patch

Sources/MessagingInApp/Gist/Managers/ModalMessageManager.swift#L57-L59

Added lines #L57 - L59 were not covered by tests
}
}
}
Loading
Loading