From 47b28f297fb64344230d119ebba634d9c3b37285 Mon Sep 17 00:00:00 2001 From: Kyle Matthew Hedden Date: Mon, 24 Oct 2022 17:33:23 -0400 Subject: [PATCH 1/3] Create an alertObject generator from a direct alertModel. Cleanup model initialization interface. --- .../Alerts/MVMCoreAlertObject+Swift.swift | 35 +++++++++++++++++++ .../Atomic/Actions/ActionAlertHandler.swift | 11 ++++-- MVMCoreUI/Atomic/Actions/AlertModel.swift | 5 +-- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/MVMCoreUI/Alerts/MVMCoreAlertObject+Swift.swift b/MVMCoreUI/Alerts/MVMCoreAlertObject+Swift.swift index c2c5d50f..ea9dfd98 100644 --- a/MVMCoreUI/Alerts/MVMCoreAlertObject+Swift.swift +++ b/MVMCoreUI/Alerts/MVMCoreAlertObject+Swift.swift @@ -9,6 +9,41 @@ public extension MVMCoreAlertObject { + static func alertObject(from alertModel: AlertModel, actions: [UIAlertAction]? = nil, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?, error: AutoreleasingUnsafeMutablePointer?) -> MVMCoreAlertObject? { + + let actionsForAlert = actions ?? generateActions(from: alertModel.alertActions, additionalData: additionalData, delegateObject: delegateObject) + + let alertObject = MVMCoreAlertObject(popupAlertWithTitle: alertModel.title, + message: alertModel.message, + actions: actionsForAlert, + isGreedy: false) + + alertObject?.alertStyle = alertModel.style + alertObject?.pageJson = alertModel.analyticsData + + return alertObject + } + + static func generateActions(from buttonModels: [AlertButtonModel], additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalHandling: ((AlertButtonModel, UIAlertAction)->())? = nil) -> [UIAlertAction] { + return buttonModels.map { alertButtonModel in + let alertAction = UIAlertAction(title: alertButtonModel.title, style: alertButtonModel.style) { action in + Task(priority: .userInitiated) { + do { + try await MVMCoreUIActionHandler.shared()?.handleAction( + with: alertButtonModel.action, + additionalData: additionalData, + delegateObject: delegateObject + ) + } catch { + + } + additionalHandling?(alertButtonModel, action) + } + } + return alertAction + } + } + @objc static func alertObjectWith(action actionJson: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?, error: AutoreleasingUnsafeMutablePointer?) -> MVMCoreAlertObject? { guard let alertJson = actionJson?.optionalDictionaryForKey("alert"), diff --git a/MVMCoreUI/Atomic/Actions/ActionAlertHandler.swift b/MVMCoreUI/Atomic/Actions/ActionAlertHandler.swift index 0f4a11fa..67ed6156 100644 --- a/MVMCoreUI/Atomic/Actions/ActionAlertHandler.swift +++ b/MVMCoreUI/Atomic/Actions/ActionAlertHandler.swift @@ -25,7 +25,14 @@ open class ActionAlertHandler: MVMCoreJSONActionHandlerProtocol { } open func execute(with model: ActionModelProtocol, delegateObject: DelegateObject?, additionalData: [AnyHashable : Any]?) async throws { - let json = try MVMCoreActionHandler.convertActionToJSON(model) - try await performAction(with: json, model: model, delegateObject: delegateObject, additionalData: additionalData) + guard let model = model as? ActionAlertModel else { return } + var error: MVMCoreErrorObject? = nil + guard let alertObject = MVMCoreAlertObject.alertObject(from: model.alert, additionalData: additionalData, delegateObject: delegateObject, error: &error) else { + throw MVMCoreError.errorObject(error!) + } + (delegateObject?.actionDelegate as? MVMCoreUIActionDelegateProtocol)?.willShowPopup(with: alertObject, alertJson: (try? MVMCoreActionHandler.convertActionToJSON(model)) ?? [:]) + _ = await MainActor.run { + MVMCoreAlertHandler.shared()?.showAlert(with: alertObject) + } } } diff --git a/MVMCoreUI/Atomic/Actions/AlertModel.swift b/MVMCoreUI/Atomic/Actions/AlertModel.swift index 76bfb273..6825898c 100644 --- a/MVMCoreUI/Atomic/Actions/AlertModel.swift +++ b/MVMCoreUI/Atomic/Actions/AlertModel.swift @@ -23,7 +23,7 @@ public class AlertButtonModel: Codable { // MARK: - Initializer //-------------------------------------------------- - public init(_ title: String,_ action: ActionModelProtocol,_ style: UIAlertAction.Style = .default) { + public init(_ title: String, _ action: ActionModelProtocol, style: UIAlertAction.Style = .default) { self.title = title self.action = action self.style = style @@ -76,10 +76,11 @@ public class AlertModel: Codable { // MARK: - Properties //-------------------------------------------------- - public init(_ title: String,_ message: String,_ alertActions: [AlertButtonModel]) { + public init(title: String, message: String, alertActions: [AlertButtonModel], style: UIAlertController.Style = .alert) { self.title = title self.message = message self.alertActions = alertActions + self.style = style } //-------------------------------------------------- From 0da94465b369adc4a0ba632d1b19651f9adaf2f0 Mon Sep 17 00:00:00 2001 From: Kyle Matthew Hedden Date: Thu, 27 Oct 2022 17:49:49 -0400 Subject: [PATCH 2/3] allow action delegate to perform the actiopn --- MVMCoreUI/Alerts/MVMCoreAlertObject+Swift.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MVMCoreUI/Alerts/MVMCoreAlertObject+Swift.swift b/MVMCoreUI/Alerts/MVMCoreAlertObject+Swift.swift index ea9dfd98..0adae2e2 100644 --- a/MVMCoreUI/Alerts/MVMCoreAlertObject+Swift.swift +++ b/MVMCoreUI/Alerts/MVMCoreAlertObject+Swift.swift @@ -29,7 +29,7 @@ public extension MVMCoreAlertObject { let alertAction = UIAlertAction(title: alertButtonModel.title, style: alertButtonModel.style) { action in Task(priority: .userInitiated) { do { - try await MVMCoreUIActionHandler.shared()?.handleAction( + try await (delegateObject?.actionDelegate as? ActionDelegateProtocol)?.performAction( with: alertButtonModel.action, additionalData: additionalData, delegateObject: delegateObject From 9ce2023eec8097a6f70cfcfbbb8fabec5573e1e3 Mon Sep 17 00:00:00 2001 From: Kyle Matthew Hedden Date: Fri, 28 Oct 2022 10:48:58 -0400 Subject: [PATCH 3/3] code review: allow try to throw on model decode --- MVMCoreUI/Atomic/Actions/ActionAlertHandler.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MVMCoreUI/Atomic/Actions/ActionAlertHandler.swift b/MVMCoreUI/Atomic/Actions/ActionAlertHandler.swift index 67ed6156..f3004f71 100644 --- a/MVMCoreUI/Atomic/Actions/ActionAlertHandler.swift +++ b/MVMCoreUI/Atomic/Actions/ActionAlertHandler.swift @@ -30,7 +30,7 @@ open class ActionAlertHandler: MVMCoreJSONActionHandlerProtocol { guard let alertObject = MVMCoreAlertObject.alertObject(from: model.alert, additionalData: additionalData, delegateObject: delegateObject, error: &error) else { throw MVMCoreError.errorObject(error!) } - (delegateObject?.actionDelegate as? MVMCoreUIActionDelegateProtocol)?.willShowPopup(with: alertObject, alertJson: (try? MVMCoreActionHandler.convertActionToJSON(model)) ?? [:]) + (delegateObject?.actionDelegate as? MVMCoreUIActionDelegateProtocol)?.willShowPopup(with: alertObject, alertJson: try MVMCoreActionHandler.convertActionToJSON(model)) _ = await MainActor.run { MVMCoreAlertHandler.shared()?.showAlert(with: alertObject) }