From 3af89dfdc64ce5102fb401cbe739fae0e1cdc62a Mon Sep 17 00:00:00 2001 From: Sumanth Nadigadda Date: Fri, 16 Jul 2021 02:14:56 +0530 Subject: [PATCH] Changes in CornerLabelModel to dynamically pick molecule's model --- .../LeftRightViews/CornerLabelsModel.swift | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/MVMCoreUI/Atomic/Molecules/LeftRightViews/CornerLabelsModel.swift b/MVMCoreUI/Atomic/Molecules/LeftRightViews/CornerLabelsModel.swift index 76a56178..58b661c6 100644 --- a/MVMCoreUI/Atomic/Molecules/LeftRightViews/CornerLabelsModel.swift +++ b/MVMCoreUI/Atomic/Molecules/LeftRightViews/CornerLabelsModel.swift @@ -8,7 +8,7 @@ import UIKit -public class CornerLabelsModel: MoleculeModelProtocol { +public class CornerLabelsModel: ParentMoleculeModelProtocol { public static var identifier: String = "cornerLabels" public var backgroundColor: Color? public var topLeftLabel: LabelModel? @@ -16,7 +16,10 @@ public class CornerLabelsModel: MoleculeModelProtocol { public var bottomLeftLabel: LabelModel? public var bottomRightLabel: LabelModel? public var molecule: MoleculeModelProtocol? - + public var children: [MoleculeModelProtocol] { + return [topLeftLabel, topRightLabel, bottomLeftLabel, bottomRightLabel].compactMap { (molecule: MoleculeModelProtocol?) in molecule } + } + init(with molecule: MoleculeModelProtocol?) { self.molecule = molecule } @@ -35,20 +38,20 @@ public class CornerLabelsModel: MoleculeModelProtocol { let typeContainer = try decoder.container(keyedBy: CodingKeys.self) backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) molecule = try typeContainer.decodeModelIfPresent(codingKey: .molecule) - topLeftLabel = try typeContainer.decodeIfPresent(LabelModel.self, forKey: .topLeftLabel) - topRightLabel = try typeContainer.decodeIfPresent(LabelModel.self, forKey: .topRightLabel) - bottomLeftLabel = try typeContainer.decodeIfPresent(LabelModel.self, forKey: .bottomLeftLabel) - bottomRightLabel = try typeContainer.decodeIfPresent(LabelModel.self, forKey: .bottomRightLabel) + topLeftLabel = try typeContainer.decodeMoleculeIfPresent(codingKey: .topLeftLabel) + topRightLabel = try typeContainer.decodeMoleculeIfPresent(codingKey: .topRightLabel) + bottomLeftLabel = try typeContainer.decodeMoleculeIfPresent(codingKey: .bottomLeftLabel) + bottomRightLabel = try typeContainer.decodeMoleculeIfPresent(codingKey: .bottomRightLabel) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor) try container.encodeModelIfPresent(molecule, forKey: .molecule) - try container.encodeIfPresent(topLeftLabel, forKey: .topLeftLabel) - try container.encodeIfPresent(topRightLabel, forKey: .topRightLabel) - try container.encodeIfPresent(bottomLeftLabel, forKey: .bottomLeftLabel) - try container.encodeIfPresent(bottomRightLabel, forKey: .bottomRightLabel) + try container.encodeModelIfPresent(topLeftLabel, forKey: .topLeftLabel) + try container.encodeModelIfPresent(topRightLabel, forKey: .topRightLabel) + try container.encodeModelIfPresent(bottomLeftLabel, forKey: .bottomLeftLabel) + try container.encodeModelIfPresent(bottomRightLabel, forKey: .bottomRightLabel) try container.encode(moleculeName, forKey: .moleculeName) } }