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: dispatch loading failed event #805

Merged
merged 3 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 0 additions & 14 deletions Sources/MessagingInApp/Gist/EngineWeb/EngineRoute.swift

This file was deleted.

7 changes: 7 additions & 0 deletions Sources/MessagingInApp/Gist/EngineWeb/EngineWeb.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ protocol EngineWebInstance: AutoMockable {

public class EngineWeb: NSObject, EngineWebInstance {
private let logger: Logger = DIGraphShared.shared.logger
private let inAppMessageManager: InAppMessageManager = DIGraphShared.shared.inAppMessageManager
private let _currentMessage: Message?
mrehan27 marked this conversation as resolved.
Show resolved Hide resolved
private var _currentRoute = ""
private var _timeoutTimer: Timer?
private var _elapsedTimer = ElapsedTimer()
Expand All @@ -42,6 +44,8 @@ public class EngineWeb: NSObject, EngineWebInstance {
}

init(configuration: EngineWebConfiguration, state: InAppMessageState) {
self._currentMessage = state.currentMessageState.message

super.init()

_elapsedTimer.start(title: "Engine render for message: \(configuration.messageId)")
Expand Down Expand Up @@ -91,6 +95,9 @@ public class EngineWeb: NSObject, EngineWebInstance {
@objc
func forcedTimeout() {
logger.info("Timeout triggered, triggering message error.")
if let message = _currentMessage {
inAppMessageManager.dispatch(action: .engineAction(action: .messageLoadingFailed(message: message)))
}
delegate?.error()
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/MessagingInApp/Gist/Managers/MessageManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class MessageManager: EngineWebDelegate {
instanceId: message.instanceId,
endpoint: state.environment.networkSettings.engineAPI,
messageId: message.messageId,
properties: message.toEngineRoute().properties
properties: message.properties.mapValues { AnyEncodable($0) }
mrehan27 marked this conversation as resolved.
Show resolved Hide resolved
)

self.engine = engineWebProvider.getEngineWebInstance(configuration: engineWebConfiguration, state: state)
Expand Down Expand Up @@ -221,7 +221,7 @@ class MessageManager: EngineWebDelegate {

func error() {
logger.error("Error loading message with id: \(currentMessage.messageId)")
inAppMessageManager.dispatch(action: .engineAction(action: .error(message: currentMessage)))
inAppMessageManager.dispatch(action: .engineAction(action: .messageLoadingFailed(message: currentMessage)))
}

func routeLoaded(route: String) {
Expand Down
49 changes: 11 additions & 38 deletions Sources/MessagingInApp/Gist/Managers/Models/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,28 @@ public class GistProperties {
}

public class Message {
public let instanceId = UUID().uuidString.lowercased()
public let queueId: String?
public let priority: Int?
public let messageId: String
public let gistProperties: GistProperties

var properties = [String: Any]()
let instanceId = UUID().uuidString.lowercased()
let queueId: String?
let priority: Int?
let messageId: String
let gistProperties: GistProperties
let properties: [String: Any]

public var isEmbedded: Bool {
gistProperties.elementId != nil
}

public init(
messageId: String,
queueId: String? = nil,
priority: Int? = nil,
messageId: String,
properties: [String: Any]? = nil,
gistProperties: GistProperties? = nil
properties: [String: Any]?
) {
self.messageId = messageId
self.queueId = queueId
self.priority = priority
self.messageId = messageId
self.gistProperties = gistProperties ?? Message.parseGistProperties(from: properties)
if let props = properties {
self.properties = props
}
}

public convenience init(messageId: String, properties: [String: Any]?) {
self.init(
queueId: properties?["queueId"] as? String,
priority: properties?["priority"] as? Int,
messageId: messageId,
gistProperties: Message.parseGistProperties(from: properties?["gist"] as? [String: Any])
)
self.gistProperties = Message.parseGistProperties(from: properties?["gist"] as? [String: Any])
self.properties = properties ?? [:]
}

private static func parseGistProperties(from gist: [String: Any]?) -> GistProperties {
Expand All @@ -75,20 +62,6 @@ public class Message {
persistent: persistent
)
}

public func addProperty(key: String, value: Any) {
properties[key] = AnyEncodable(value)
}

func toEngineRoute() -> EngineRoute {
let engineRoute = EngineRoute(route: messageId)
properties.keys.forEach { key in
if let value = properties[key] {
engineRoute.addProperty(key: key, value: value)
}
}
return engineRoute
}
}

extension Message {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ struct UserQueueResponse {

func toMessage() -> Message {
Message(
messageId: messageId,
queueId: queueId,
priority: priority,
messageId: messageId,
properties: properties
)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/MessagingInApp/Gist/Views/GistView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class GistView: UIView {

override public func removeFromSuperview() {
super.removeFromSuperview()
if let message = message {
if let _ = message {
DIGraphShared.shared.gist.dismissMessage()
}
}
Expand Down
1 change: 0 additions & 1 deletion Sources/MessagingInApp/State/InAppMessageAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ enum InAppMessageAction: Equatable, Action {
enum EngineAction: Equatable {
case tap(message: Message, route: String, name: String, action: String)
case messageLoadingFailed(message: Message)
case error(message: Message)
}

// swiftlint:disable cyclomatic_complexity
Expand Down
3 changes: 0 additions & 3 deletions Sources/MessagingInApp/State/InAppMessageMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,6 @@ func messageEventCallbacksMiddleware(delegate: GistDelegate) -> InAppMessageMidd

case .messageLoadingFailed(let message):
delegate.messageError(message: message)

case .error(let message):
delegate.messageError(message: message)
}

default:
Expand Down
9 changes: 8 additions & 1 deletion Sources/MessagingInApp/State/InAppMessageReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,15 @@ private func reducer(action: InAppMessageAction, state: InAppMessageState) -> In
case .dismissMessage(let message, _, _):
return state.copy(currentMessageState: .dismissed(message: message))

case .engineAction(let engineAction):
switch engineAction {
case .tap:
return state
case .messageLoadingFailed(let message):
return state.copy(currentMessageState: .dismissed(message: message))
}

case .embedMessage,
.engineAction,
.reportError:
return state

Expand Down
Loading