diff --git a/MVMCore/MVMCore/ActionHandling/ActionOpenUrlHandler.swift b/MVMCore/MVMCore/ActionHandling/ActionOpenUrlHandler.swift index 4961fa3..5ed9d2d 100644 --- a/MVMCore/MVMCore/ActionHandling/ActionOpenUrlHandler.swift +++ b/MVMCore/MVMCore/ActionHandling/ActionOpenUrlHandler.swift @@ -72,7 +72,7 @@ open class ActionOpenUrlHandler: MVMCoreJSONActionHandlerProtocol { // Try loading the app url first, otherwise fall back to browser url. if let appURL = model.appURL { do { - try await openURL(model: model, additionalData: additionalData, delegateObject: delegateObject) + try await ActionOpenUrlHandler.open(url: appURL) return } catch { // Log error and continue diff --git a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.swift b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.swift index a64d4f6..223ce20 100644 --- a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.swift +++ b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.swift @@ -89,10 +89,10 @@ public protocol MVMCoreJSONActionHandlerProtocol: MVMCoreActionHandlerProtocol { defer { MVMCoreActionHandler.log(string: "End Action: \(model.actionType)", additionalData: additionalData) } - let json = try additionalData?.removeValue(forKey: jsonKey) as? [AnyHashable : Any] ?? MVMCoreActionHandler.convertActionToJSON(model) + let json = try additionalData.removeValue(forKey: jsonKey) as? [AnyHashable : Any] ?? MVMCoreActionHandler.convertActionToJSON(model) // Log the action - delegateObject?.actionDelegate?.logAction?(withActionInformation: json, additionalData: additionalData) + logAction(with: json, additionalData: additionalData, delegateObject: delegateObject) do { let handlerType = try ModelRegistry.getHandler(model) as! MVMCoreActionHandlerProtocol.Type @@ -119,9 +119,9 @@ public protocol MVMCoreJSONActionHandlerProtocol: MVMCoreActionHandlerProtocol { // MARK: - Subclassables /// Subclass to log the action was fired. - open func logAction(with model: ActionModelProtocol, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) { + open func logAction(with JSON: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) { // Calls legacy log action function. - delegateObject?.actionDelegate?.logAction?(withActionInformation: model.toJSON(), additionalData: additionalData) + delegateObject?.actionDelegate?.logAction?(withActionInformation: JSON, additionalData: additionalData) } /// Logs the error. @@ -132,8 +132,8 @@ public protocol MVMCoreJSONActionHandlerProtocol: MVMCoreActionHandlerProtocol { // MARK: - Legacy Holdovers - static public func setUUID(additionalData: [AnyHashable: Any]?) -> [AnyHashable: Any]? { - guard getUUID(additionalData: additionalData) == nil else { return additionalData } + static public func setUUID(additionalData: [AnyHashable: Any]?, force: Bool = false) -> [AnyHashable: Any] { + if !force && getUUID(additionalData: additionalData) != nil { return additionalData! } return additionalData.dictionaryAdding(key: "Action-UUID", value: UUID().uuidString) } @@ -148,7 +148,6 @@ public protocol MVMCoreJSONActionHandlerProtocol: MVMCoreActionHandlerProtocol { /// Legacy handle action with json. @objc(handleActionWithDictionary:additionalData:delegateObject:) open func handleAction(with json: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) { - let additionalData = MVMCoreActionHandler.setUUID(additionalData: additionalData) let task = Task(priority: .userInitiated) { try Task.checkCancellation() do { @@ -156,12 +155,7 @@ public protocol MVMCoreJSONActionHandlerProtocol: MVMCoreActionHandlerProtocol { throw ModelRegistry.Error.keyNotFound } let model = try MVMCoreActionHandler.createModel(with: json, delegateObject: delegateObject) - if let closure = (delegateObject?.actionDelegate as? ActionDelegateProtocol)?.performAction { - // Allow newer delegates to handle calls from legacy functions - try await closure(model, additionalData, delegateObject) - } else { - try await handleAction(with: model, json: json, additionalData: additionalData, delegateObject: delegateObject) - } + try await handleAction(with: model, json: json, additionalData: additionalData, delegateObject: delegateObject) } catch { let actionType = json?.optionalStringForKey(KeyActionType) switch error { @@ -192,8 +186,15 @@ public protocol MVMCoreJSONActionHandlerProtocol: MVMCoreActionHandlerProtocol { /// Bridges the legacy json using functions and the new model using functions. open func handleAction(with model: ActionModelProtocol, json: [AnyHashable: Any], additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) async throws { try Task.checkCancellation() - let additionalData = additionalData.dictionaryAdding(key: jsonKey, value: json) - try await handleAction(with: model, additionalData: additionalData, delegateObject: delegateObject) + var additionalData = additionalData.dictionaryAdding(key: jsonKey, value: json) + additionalData = MVMCoreActionHandler.setUUID(additionalData: additionalData, force: true) + MVMCoreActionHandler.log(string: "JSON \(json)", additionalData: additionalData) + if let closure = (delegateObject?.actionDelegate as? ActionDelegateProtocol)?.performAction { + // Allow newer delegates to handle calls from legacy functions + try await closure(model, additionalData, delegateObject) + } else { + try await handleAction(with: model, additionalData: additionalData, delegateObject: delegateObject) + } } /// Subclass to handle and any actions where a handler was not registered. Checks with the delegate handlesUnknownAction function diff --git a/MVMCore/MVMCore/Utility/MVMError.swift b/MVMCore/MVMCore/Utility/MVMError.swift index ee1662b..e048840 100644 --- a/MVMCore/MVMCore/Utility/MVMError.swift +++ b/MVMCore/MVMCore/Utility/MVMError.swift @@ -8,7 +8,7 @@ import Foundation -protocol MVMError: LocalizedError, CustomNSError {} +public protocol MVMError: LocalizedError, CustomNSError {} extension MVMError { public var errorDescription: String? { return MVMCoreGetterUtility.hardcodedString(withKey: HardcodedErrorUnableToProcess) }