Create an alertObject generator from a direct alertModel. Cleanup model initialization interface.

This commit is contained in:
Kyle Matthew Hedden 2022-10-24 17:33:23 -04:00
parent 1dc367c463
commit 47b28f297f
3 changed files with 47 additions and 4 deletions

View File

@ -9,6 +9,41 @@
public extension MVMCoreAlertObject {
static func alertObject(from alertModel: AlertModel, actions: [UIAlertAction]? = nil, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?, error: AutoreleasingUnsafeMutablePointer<MVMCoreErrorObject?>?) -> 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<MVMCoreErrorObject?>?) -> MVMCoreAlertObject? {
guard let alertJson = actionJson?.optionalDictionaryForKey("alert"),

View File

@ -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)
}
}
}

View File

@ -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
}
//--------------------------------------------------