Changes in CornerLabelModel to dynamically pick molecule's model

This commit is contained in:
Sumanth Nadigadda 2021-07-16 02:14:56 +05:30
parent edbe68429b
commit 3af89dfdc6

View File

@ -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)
}
}