diff --git a/MVMCore/MVMCore/AlertHandling/MVMCoreAlertObject+Swift.swift b/MVMCore/MVMCore/AlertHandling/MVMCoreAlertObject+Swift.swift index 37e8e98..166ca65 100644 --- a/MVMCore/MVMCore/AlertHandling/MVMCoreAlertObject+Swift.swift +++ b/MVMCore/MVMCore/AlertHandling/MVMCoreAlertObject+Swift.swift @@ -31,7 +31,7 @@ public extension MVMCoreAlertObject { let alert = MVMCoreAlertObject(popupAlertWithTitle: alertJson.optionalStringForKey(KeyTitle), message: alertJson.optionalStringForKey(KeyMessage), actions: actionsForAlert, - isGreedy: true) + isGreedy: false) return alert } } diff --git a/MVMCore/MVMCore/Models/ActionType/AlertModel.swift b/MVMCore/MVMCore/Models/ActionType/AlertModel.swift index 5365401..3f002e9 100644 --- a/MVMCore/MVMCore/Models/ActionType/AlertModel.swift +++ b/MVMCore/MVMCore/Models/ActionType/AlertModel.swift @@ -8,6 +8,7 @@ import UIKit + public class AlertButtonModel: Codable { public var title: String public var action: ActionModelProtocol @@ -27,16 +28,18 @@ public class AlertButtonModel: Codable { 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 + + if let style = try? typeContainer.decodeIfPresent(String.self, forKey: .style) { + self.style = UIAlertAction.Style.getStyle(for: 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.encode(UIAlertAction.Style.getStyleString(for: style), forKey: .style) try container.encodeModel(action, forKey: .action) } } diff --git a/MVMCore/MVMCore/Models/ActionType/UIAlertActionStyle+Codable.swift b/MVMCore/MVMCore/Models/ActionType/UIAlertActionStyle+Codable.swift index b2d9428..a461e82 100644 --- a/MVMCore/MVMCore/Models/ActionType/UIAlertActionStyle+Codable.swift +++ b/MVMCore/MVMCore/Models/ActionType/UIAlertActionStyle+Codable.swift @@ -12,31 +12,72 @@ enum AlertActionError: Error { case notAnAlertAction } -extension UIAlertAction.Style: Codable { +extension UIAlertAction.Style: RawRepresentable { + 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 { + guard let style = UIAlertAction.Style(rawValue: int) else { throw AlertActionError.notAnAlertAction } - self = alignment + self = style } public func encode(to encoder: Encoder) throws { var container = encoder.singleValueContainer() - try container.encode(rawValueInt) + try container.encode(rawValueString) } - var rawValueInt: Int { - switch self { - case .default: - return 0 - case .cancel: - return 1 - case .destructive: - return 2 - @unknown default: - return 0 + init?(rawValue: String) { + switch rawValue { + case "default": + self = .default + case "cancel": + self = .cancel + case "destructive": + self = .destructive + default: + self = .default } } + + var rawValueString: String { + switch self { + case .default: + return "default" + case .cancel: + return "cancel" + case .destructive: + return "destructive" + @unknown default: + return "default" + } + } + + public static func getStyle(for string: String) -> UIAlertAction.Style { + switch string { + case "default": + return .default + case "cancel": + return .cancel + case "destructive": + return .destructive + default: + return .default + } + } + + public static func getStyleString(for alignment: UIAlertAction.Style) -> String { + switch alignment { + case .default: + return "default" + case .cancel: + return "cancel" + case .destructive: + return "destructive" + default: + return "default" + } + } + }