diff --git a/MVMCore/MVMCore.xcodeproj/project.pbxproj b/MVMCore/MVMCore.xcodeproj/project.pbxproj index da488a1..46b0ab0 100644 --- a/MVMCore/MVMCore.xcodeproj/project.pbxproj +++ b/MVMCore/MVMCore.xcodeproj/project.pbxproj @@ -23,6 +23,8 @@ /* Begin PBXBuildFile section */ 0184D3DB24B7D5A600A05369 /* ActionAlertModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0184D3DA24B7D5A600A05369 /* ActionAlertModel.swift */; }; 0184D3E324B8D0C600A05369 /* MVMCoreAlertObject+Swift.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0184D3E224B8D0C600A05369 /* MVMCoreAlertObject+Swift.swift */; }; + 0184D3FD24BE54A300A05369 /* UIAlertActionStyle+Codable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0184D3FC24BE54A300A05369 /* UIAlertActionStyle+Codable.swift */; }; + 0184D3FF24BE554A00A05369 /* AlertModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0184D3FE24BE554A00A05369 /* AlertModel.swift */; }; 01C851CF23CF7B260021F976 /* JSONMap.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01C851CE23CF7B260021F976 /* JSONMap.swift */; }; 01C851D123CF97FE0021F976 /* ActionBackModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01C851D023CF97FE0021F976 /* ActionBackModel.swift */; }; 01DF561421F90ADC00CC099B /* Dictionary+MFConvenience.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01DF561321F90ADC00CC099B /* Dictionary+MFConvenience.swift */; }; @@ -173,6 +175,8 @@ /* Begin PBXFileReference section */ 0184D3DA24B7D5A600A05369 /* ActionAlertModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ActionAlertModel.swift; sourceTree = ""; }; 0184D3E224B8D0C600A05369 /* MVMCoreAlertObject+Swift.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MVMCoreAlertObject+Swift.swift"; sourceTree = ""; }; + 0184D3FC24BE54A300A05369 /* UIAlertActionStyle+Codable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIAlertActionStyle+Codable.swift"; sourceTree = ""; }; + 0184D3FE24BE554A00A05369 /* AlertModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertModel.swift; sourceTree = ""; }; 01C851CE23CF7B260021F976 /* JSONMap.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONMap.swift; sourceTree = ""; }; 01C851D023CF97FE0021F976 /* ActionBackModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionBackModel.swift; sourceTree = ""; }; 01DF561321F90ADC00CC099B /* Dictionary+MFConvenience.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Dictionary+MFConvenience.swift"; sourceTree = ""; }; @@ -463,6 +467,7 @@ 946EE1BB237B691A0036751F /* ActionOpenPageModel.swift */, 01F2A03823A812DD00D954D8 /* ActionOpenUrlModel.swift */, 0184D3DA24B7D5A600A05369 /* ActionAlertModel.swift */, + 0184D3FC24BE54A300A05369 /* UIAlertActionStyle+Codable.swift */, 01F2A04B23A82B1B00D954D8 /* ActionCallModel.swift */, 01F2A04D23A82CF500D954D8 /* ActionPopupModel.swift */, 01C851D023CF97FE0021F976 /* ActionBackModel.swift */, @@ -471,6 +476,7 @@ 94C014D2242119E6005811A9 /* ActionPreviousSubmitModel.swift */, 94C014D424211AF0005811A9 /* ActionCancelModel.swift */, 94C014D824212360005811A9 /* ActionSettingModel.swift */, + 0184D3FE24BE554A00A05369 /* AlertModel.swift */, ); path = ActionType; sourceTree = ""; @@ -898,6 +904,7 @@ 01F2A03623A80A7300D954D8 /* ActionModelProtocol.swift in Sources */, AFBB96991FBA3A9A0008D868 /* MVMCoreAlertController.m in Sources */, 881D26941FCC9D180079C521 /* MVMCoreOperation.m in Sources */, + 0184D3FD24BE54A300A05369 /* UIAlertActionStyle+Codable.swift in Sources */, AFED77A41FCCA29400BAE689 /* MVMCoreViewControllerMappingObject.m in Sources */, 01C851CF23CF7B260021F976 /* JSONMap.swift in Sources */, 01F2A04C23A82B1B00D954D8 /* ActionCallModel.swift in Sources */, @@ -920,6 +927,7 @@ 30349BF21FCCA78A00546A1E /* MVMCoreSessionTimeHandler.m in Sources */, D2DEDCB923C6400600C44CC4 /* UnitInterval.swift in Sources */, 94C014D3242119E6005811A9 /* ActionPreviousSubmitModel.swift in Sources */, + 0184D3FF24BE554A00A05369 /* AlertModel.swift in Sources */, 8876D5E91FB50AB000EB2E3D /* NSArray+MFConvenience.m in Sources */, 946EE1B2237B5F260036751F /* JSONValue.swift in Sources */, AFBB96971FBA3A9A0008D868 /* MVMCorePresentViewControllerOperation.m in Sources */, diff --git a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.h b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.h index 26afbe2..74b93a4 100644 --- a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.h +++ b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.h @@ -50,9 +50,12 @@ extern NSString * _Nonnull const KeyActionTypeOpen; // Makes the previous request, needs the delegate for this - (void)previousSubmitAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegateObject:(nullable DelegateObject *)delegateObject; -// Shows a popup +// Shows a popup alert by grabbing the content from the page map. - (void)popupAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegateObject:(nullable DelegateObject *)delegateObject; +// Shows popup alert from the alert object in the action map. The actionType of the action is 'alert' +- (void)showAlert:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegateObject:(nullable DelegateObject *)delegateObject; + // Shows a top alert - (void)topAlertAction:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegateObject:(nullable DelegateObject *)delegateObject; diff --git a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.m b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.m index 1de9eff..807b5ac 100644 --- a/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.m +++ b/MVMCore/MVMCore/ActionHandling/MVMCoreActionHandler.m @@ -315,8 +315,8 @@ NSString * const KeyActionTypeOpen = @"openPage"; - (void)showAlert:(nullable NSDictionary *)actionInformation additionalData:(nullable NSDictionary *)additionalData delegateObject:(nullable DelegateObject *)delegateObject { - MVMCoreErrorObject *error = nil; - MVMCoreAlertObject *alertObject = [MVMCoreAlertObject alertObjectWithAction:actionInformation additionalData:additionalData delegateObject:delegateObject error:&error]; + MVMCoreErrorObject *error = nil; + MVMCoreAlertObject *alertObject = [MVMCoreAlertObject alertObjectWithAction:actionInformation additionalData:additionalData delegateObject:delegateObject error:&error]; if ([delegateObject.actionDelegate respondsToSelector:@selector(willShowPopupWithAlertObject:alertJson:)]) { [delegateObject.actionDelegate willShowPopupWithAlertObject:alertObject alertJson:actionInformation]; } diff --git a/MVMCore/MVMCore/AlertHandling/MVMCoreAlertObject+Swift.swift b/MVMCore/MVMCore/AlertHandling/MVMCoreAlertObject+Swift.swift index 0a0f846..37e8e98 100644 --- a/MVMCore/MVMCore/AlertHandling/MVMCoreAlertObject+Swift.swift +++ b/MVMCore/MVMCore/AlertHandling/MVMCoreAlertObject+Swift.swift @@ -13,30 +13,25 @@ public extension MVMCoreAlertObject { @objc static func alertObjectWith(action actionJson: [AnyHashable : Any]?, additionalData: [AnyHashable : Any]?, delegateObject: DelegateObject?, error: AutoreleasingUnsafeMutablePointer?) -> MVMCoreAlertObject? { guard let alertJson = actionJson?.optionalDictionaryForKey("alert"), + (alertJson.optionalStringForKey(KeyTitle) != nil || alertJson.optionalStringForKey(KeyMessage) != nil), let actionsList = alertJson.optionalArrayForKey("alertActions") as? [[AnyHashable : Any]] else { + error?.pointee = MVMCoreErrorObject(title: nil, message: MVMCoreGetterUtility.hardcodedString(withKey: HardcodedErrorUnableToProcess), code: ErrorCode.popupFailed.rawValue, domain: ErrorDomainNative, location: String(describing: self)) return nil } - var actionsForAlert:[UIAlertAction] = [] + var actionsForAlert: [UIAlertAction] = [] for actionJson in actionsList { let style = UIAlertAction.Style(rawValue: Int(actionJson.int32ForKey("style"))) ?? UIAlertAction.Style.default - let alertAction = UIAlertAction(title: actionJson.stringForkey(KeyTitle), style: style) { (action) in - MVMCoreActionHandler.shared()?.handleAction(with: actionJson.dictionaryForKey("action"), additionalData: additionalData, delegateObject: delegateObject) + let alertAction = UIAlertAction(title: actionJson.optionalStringForKey(KeyTitle), style: style) { (action) in + MVMCoreActionHandler.shared()?.handleAction(with: actionJson.optionalDictionaryForKey("action"), additionalData: additionalData, delegateObject: delegateObject) } actionsForAlert.append(alertAction) } - let alertTitle = alertJson.optionalStringForKey(KeyTitle) - let alertMessage = alertJson.optionalStringForKey(KeyMessage) - - if alertTitle != nil || alertMessage != nil { - let alert = MVMCoreAlertObject(popupAlertWithTitle: alertTitle, message: alertMessage, actions: actionsForAlert, isGreedy: true) - return alert - } - - if let errorObject = MVMCoreErrorObject(title: nil, message: MVMCoreGetterUtility.hardcodedString(withKey: HardcodedErrorUnableToProcess), code: ErrorCode.popupFailed.rawValue, domain: ErrorDomainNative, location: String(describing: self)) { - error?.pointee = errorObject - } - return nil + let alert = MVMCoreAlertObject(popupAlertWithTitle: alertJson.optionalStringForKey(KeyTitle), + message: alertJson.optionalStringForKey(KeyMessage), + actions: actionsForAlert, + isGreedy: true) + return alert } } diff --git a/MVMCore/MVMCore/Models/ActionType/ActionAlertModel.swift b/MVMCore/MVMCore/Models/ActionType/ActionAlertModel.swift index 5cdbd77..5893dc0 100644 --- a/MVMCore/MVMCore/Models/ActionType/ActionAlertModel.swift +++ b/MVMCore/MVMCore/Models/ActionType/ActionAlertModel.swift @@ -8,70 +8,6 @@ import UIKit -public class AlertButtonModel: Codable { - public var title: String - public var action: ActionModelProtocol - public var style: Int = 0 - public init(_ title: String,_ action: ActionModelProtocol,_ style: Int) { - self.title = title - self.action = action - self.style = style - } - - private enum CodingKeys: String, CodingKey { - case title - case action - case style - } - - required public init(from decoder: Decoder) throws { - let typeContainer = try decoder.container(keyedBy: CodingKeys.self) - title = try typeContainer.decode(String.self, forKey: .title) - if let style = try? typeContainer.decodeIfPresent(Int.self, forKey: .style) { - self.style = style - } - action = try typeContainer.decodeModel(codingKey: .action) - } - - open func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(title, forKey: .title) - try container.encode(style, forKey: .style) - try container.encodeModel(action, forKey: .action) - } -} - -public class AlertModel: Codable { - public var title: String - public var message: String - public var alertActions: [AlertButtonModel] - public init(_ title: String,_ message: String,_ alertActions: [AlertButtonModel]) { - self.title = title - self.message = message - self.alertActions = alertActions - } - - private enum CodingKeys: String, CodingKey { - case title - case message - case alertActions - } - - required public init(from decoder: Decoder) throws { - let typeContainer = try decoder.container(keyedBy: CodingKeys.self) - title = try typeContainer.decode(String.self, forKey: .title) - message = try typeContainer.decode(String.self, forKey: .message) - alertActions = try typeContainer.decode([AlertButtonModel].self, forKey: .alertActions) - } - - open func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(title, forKey: .title) - try container.encode(message, forKey: .message) - try container.encode(alertActions, forKey: .alertActions) - } -} - @objcMembers public class ActionAlertModel: ActionModelProtocol { public static var identifier: String = "alert" diff --git a/MVMCore/MVMCore/Models/ActionType/AlertModel.swift b/MVMCore/MVMCore/Models/ActionType/AlertModel.swift new file mode 100644 index 0000000..25c12f2 --- /dev/null +++ b/MVMCore/MVMCore/Models/ActionType/AlertModel.swift @@ -0,0 +1,73 @@ +// +// AlertModel.swift +// MVMCore +// +// Created by Suresh, Kamlesh on 7/14/20. +// Copyright © 2020 myverizon. All rights reserved. +// + +import UIKit + +public class AlertButtonModel: Codable { + public var title: String + public var action: ActionModelProtocol + public var style: UIAlertAction.Style = .default + public init(_ title: String,_ action: ActionModelProtocol,_ style: UIAlertAction.Style = .default) { + self.title = title + self.action = action + self.style = style + } + + private enum CodingKeys: String, CodingKey { + case title + case action + case style + } + + required public init(from decoder: Decoder) throws { + let typeContainer = try decoder.container(keyedBy: CodingKeys.self) + title = try typeContainer.decode(String.self, forKey: .title) +// if let style = try? typeContainer.decodeIfPresent(UIAlertAction.Style.self, forKey: .style) { +// self.style = style +// } + action = try typeContainer.decodeModel(codingKey: .action) + } + + open func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(title, forKey: .title) + try container.encode(style, forKey: .style) + try container.encodeModel(action, forKey: .action) + } +} + +public class AlertModel: Codable { + public var title: String + public var message: String + public var alertActions: [AlertButtonModel] + public init(_ title: String,_ message: String,_ alertActions: [AlertButtonModel]) { + self.title = title + self.message = message + self.alertActions = alertActions + } + + private enum CodingKeys: String, CodingKey { + case title + case message + case alertActions + } + + required public init(from decoder: Decoder) throws { + let typeContainer = try decoder.container(keyedBy: CodingKeys.self) + title = try typeContainer.decode(String.self, forKey: .title) + message = try typeContainer.decode(String.self, forKey: .message) + alertActions = try typeContainer.decode([AlertButtonModel].self, forKey: .alertActions) + } + + open func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(title, forKey: .title) + try container.encode(message, forKey: .message) + try container.encode(alertActions, forKey: .alertActions) + } +} diff --git a/MVMCore/MVMCore/Models/ActionType/UIAlertActionStyle+Codable.swift b/MVMCore/MVMCore/Models/ActionType/UIAlertActionStyle+Codable.swift new file mode 100644 index 0000000..b2d9428 --- /dev/null +++ b/MVMCore/MVMCore/Models/ActionType/UIAlertActionStyle+Codable.swift @@ -0,0 +1,42 @@ +// +// UIAlertActionStyle+Codable.swift +// MVMCore +// +// Created by Suresh, Kamlesh on 7/14/20. +// Copyright © 2020 myverizon. All rights reserved. +// + +import UIKit + +enum AlertActionError: Error { + case notAnAlertAction +} + +extension UIAlertAction.Style: Codable { + public init(from decoder: Decoder) throws { + let typeContainer = try decoder.singleValueContainer() + let int = try typeContainer.decode(Int.self) + guard let alignment = UIAlertAction.Style(rawValue: int) else { + throw AlertActionError.notAnAlertAction + } + self = alignment + } + + public func encode(to encoder: Encoder) throws { + var container = encoder.singleValueContainer() + try container.encode(rawValueInt) + } + + var rawValueInt: Int { + switch self { + case .default: + return 0 + case .cancel: + return 1 + case .destructive: + return 2 + @unknown default: + return 0 + } + } +}