Merge branch 'feature/Decoders+Context' into 'develop'

updated models for use of Decoder Context

See merge request BPHV_MIPS/mvm_core_ui!851
This commit is contained in:
Pfeil, Scott Robert 2022-05-24 19:03:33 +00:00
commit a8bced9dbd
4 changed files with 17 additions and 15 deletions

View File

@ -198,6 +198,11 @@ open class ButtonModel: ButtonModelProtocol, MoleculeModelProtocol, FormGroupWat
title = try typeContainer.decode(String.self, forKey: .title)
action = try typeContainer.decodeModel(codingKey: .action)
if let style = decoder.context?.value(forKey: CodingKeys.style.stringValue) as? Styler.Button.Style{
self.style = style
setFacade(by: style)
}
if let style = try typeContainer.decodeIfPresent(Styler.Button.Style.self, forKey: .style) {
self.style = style
setFacade(by: style)

View File

@ -50,13 +50,17 @@ public class TwoButtonViewModel: ParentMoleculeModelProtocol {
required public init(from decoder: Decoder) throws {
let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
primaryButton = try typeContainer.decodeMoleculeIfPresent(codingKey: .primaryButton)
if primaryButton?.style == nil {
primaryButton?.style = .primary
//set context value for 'primary' style to be set for the primaryButton in case the
//property is not returned in the JSON and once decoded, this value is removed from the context
try decoder.setContext(value: Styler.Button.Style.primary, for: "style") {
self.primaryButton = try typeContainer.decodeMoleculeIfPresent(codingKey: .primaryButton)
}
secondaryButton = try typeContainer.decodeMoleculeIfPresent(codingKey: .secondaryButton)
if secondaryButton?.style == nil {
secondaryButton?.style = .secondary
//set context value for 'secondary' style to be set for the primaryButton in case the
//property is not returned in the JSON and once decoded, this value is removed from the context
try decoder.setContext(value: Styler.Button.Style.secondary, for: "style") {
self.secondaryButton = try typeContainer.decodeMoleculeIfPresent(codingKey: .secondaryButton)
}
}

View File

@ -33,11 +33,7 @@ public extension TemplateProtocol {
guard let pageJSON = json else { return }
let delegateObject = (self as? MVMCoreViewControllerProtocol)?.delegateObject?() as? MVMCoreUIDelegateObject
let data = try JSONSerialization.data(withJSONObject: pageJSON)
let decoder = JSONDecoder()
if let delegateObject = delegateObject {
// Add the delegate to access mid parsing if applicable.
try decoder.add(delegateObject: delegateObject)
}
let decoder = JSONDecoder.create(with: delegateObject)
templateModel = try decodeTemplate(using: decoder, from: data)
// Add additional required behaviors if applicable.

View File

@ -69,10 +69,7 @@ open class TopNotificationModel: Codable {
/// Decodes the top alert json to a model.
public static func decode(json: [AnyHashable: Any], delegateObject: MVMCoreUIDelegateObject?) throws -> Self {
let data = try JSONSerialization.data(withJSONObject: json)
let decoder = JSONDecoder()
if let delegateObject = delegateObject {
try decoder.add(delegateObject: delegateObject)
}
let decoder = JSONDecoder.create(with: delegateObject)
return try decoder.decode(self, from: data)
}