From 3cfe5aa4237a1f6ce6bb0d05aa9c53f49a21f0da Mon Sep 17 00:00:00 2001 From: "Suresh, Kamlesh" Date: Thu, 16 Jan 2020 16:47:53 -0500 Subject: [PATCH] StackModel --- MVMCoreUI/Organisms/StackModel.swift | 29 +++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/MVMCoreUI/Organisms/StackModel.swift b/MVMCoreUI/Organisms/StackModel.swift index b647b791..deea2457 100644 --- a/MVMCoreUI/Organisms/StackModel.swift +++ b/MVMCoreUI/Organisms/StackModel.swift @@ -12,11 +12,38 @@ import Foundation public static var identifier: String = "simpleStack" public var backgroundColor: Color? public var molecules: [StackItemModel] - @Axis public var axis: NSLayoutConstraint.Axis = .vertical + public var axis: NSLayoutConstraint.Axis = .vertical public var spacing: CGFloat = 16.0 public var useStackSpacingBeforeFirstItem = false public init(molecules: [StackItemModel]) { self.molecules = molecules } + + enum StackCodingKeys: String, CodingKey { + case moleculeName + case backgroundColor + case molecules + case axis + case spacing + } + + required public init(from decoder: Decoder) throws { + let typeContainer = try decoder.container(keyedBy: StackCodingKeys.self) + + molecules = try typeContainer.decode([StackItemModel].self, forKey: .molecules) + if let axisString = try typeContainer.decodeIfPresent(String.self, forKey: .axis), let optionalAxis = NSLayoutConstraint.Axis(rawValue: axisString) { + axis = optionalAxis + } + if let spacing = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .spacing) { + self.spacing = spacing + } + } + + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: StackCodingKeys.self) + try container.encodeIfPresent(molecules, forKey: .molecules) + try container.encodeIfPresent(axis.rawValueString, forKey: .axis) + try container.encodeIfPresent(spacing, forKey: .spacing) + } }