This commit is contained in:
Suresh, Kamlesh 2020-07-15 19:04:29 -04:00
parent b3a2511b96
commit b076ad3316
3 changed files with 62 additions and 18 deletions

View File

@ -31,7 +31,7 @@ public extension MVMCoreAlertObject {
let alert = MVMCoreAlertObject(popupAlertWithTitle: alertJson.optionalStringForKey(KeyTitle), let alert = MVMCoreAlertObject(popupAlertWithTitle: alertJson.optionalStringForKey(KeyTitle),
message: alertJson.optionalStringForKey(KeyMessage), message: alertJson.optionalStringForKey(KeyMessage),
actions: actionsForAlert, actions: actionsForAlert,
isGreedy: true) isGreedy: false)
return alert return alert
} }
} }

View File

@ -8,6 +8,7 @@
import UIKit import UIKit
public class AlertButtonModel: Codable { public class AlertButtonModel: Codable {
public var title: String public var title: String
public var action: ActionModelProtocol public var action: ActionModelProtocol
@ -27,16 +28,18 @@ public class AlertButtonModel: Codable {
required public init(from decoder: Decoder) throws { required public init(from decoder: Decoder) throws {
let typeContainer = try decoder.container(keyedBy: CodingKeys.self) let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
title = try typeContainer.decode(String.self, forKey: .title) 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) action = try typeContainer.decodeModel(codingKey: .action)
} }
open func encode(to encoder: Encoder) throws { open func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self) var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(title, forKey: .title) 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) try container.encodeModel(action, forKey: .action)
} }
} }

View File

@ -12,31 +12,72 @@ enum AlertActionError: Error {
case notAnAlertAction case notAnAlertAction
} }
extension UIAlertAction.Style: Codable { extension UIAlertAction.Style: RawRepresentable {
public init(from decoder: Decoder) throws { public init(from decoder: Decoder) throws {
let typeContainer = try decoder.singleValueContainer() let typeContainer = try decoder.singleValueContainer()
let int = try typeContainer.decode(Int.self) 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 throw AlertActionError.notAnAlertAction
} }
self = alignment self = style
} }
public func encode(to encoder: Encoder) throws { public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer() var container = encoder.singleValueContainer()
try container.encode(rawValueInt) try container.encode(rawValueString)
} }
var rawValueInt: Int { init?(rawValue: String) {
switch self { switch rawValue {
case .default: case "default":
return 0 self = .default
case .cancel: case "cancel":
return 1 self = .cancel
case .destructive: case "destructive":
return 2 self = .destructive
@unknown default: default:
return 0 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"
}
}
} }