From d17ab73d34f6d67bacc29b68ee341fccf89e684f Mon Sep 17 00:00:00 2001 From: panxi Date: Thu, 19 Dec 2019 14:02:52 -0500 Subject: [PATCH] update encode decode method --- MVMCoreUI/Atoms/Views/CaretViewModel.swift | 5 +-- .../Atoms/Views/LabelModel/LabelModel.swift | 2 +- MVMCoreUI/Atoms/Views/ProgressBar.swift | 2 +- MVMCoreUI/Models/Extensions/ModelHelper.swift | 8 ++-- .../Models/Molecules/CarouselItemModel.swift | 10 ++--- .../Models/Molecules/CarouselModel.swift | 42 ++++++++----------- .../Molecules/DropDownListItemModel.swift | 4 +- MVMCoreUI/Models/Molecules/FooterModel.swift | 16 +++---- MVMCoreUI/Models/Molecules/HeaderModel.swift | 20 ++++----- .../Molecules/MoleculeStackItemModel.swift | 13 +++--- .../Models/Molecules/MoleculeStackModel.swift | 21 ++++------ .../Template/ListPageTemplateModel.swift | 2 +- MVMCoreUI/Molecules/Items/ListItemModel.swift | 5 ++- 13 files changed, 61 insertions(+), 89 deletions(-) diff --git a/MVMCoreUI/Atoms/Views/CaretViewModel.swift b/MVMCoreUI/Atoms/Views/CaretViewModel.swift index 37d095d8..5a8998db 100644 --- a/MVMCoreUI/Atoms/Views/CaretViewModel.swift +++ b/MVMCoreUI/Atoms/Views/CaretViewModel.swift @@ -10,11 +10,8 @@ import Foundation @objcMembers public class CaretViewModel: MoleculeProtocol { - public static var identifier: String { - get{ return "caretView" } - } + public static var identifier: String = "caretView" public var backgroundColor: String? - public var strokeColor: String? public var isHidden: Bool? public var isOpaque: Bool? diff --git a/MVMCoreUI/Atoms/Views/LabelModel/LabelModel.swift b/MVMCoreUI/Atoms/Views/LabelModel/LabelModel.swift index d2c7947f..2b87186f 100644 --- a/MVMCoreUI/Atoms/Views/LabelModel/LabelModel.swift +++ b/MVMCoreUI/Atoms/Views/LabelModel/LabelModel.swift @@ -57,7 +57,7 @@ import Foundation self.fontName = try typeContainer.decodeIfPresent(String.self, forKey: .fontName) self.fontSize = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .fontSize) self.textAlignment = try typeContainer.decodeIfPresent(String.self, forKey: .textAlignment) - self.attributes = try typeContainer.decodeArrayIfPresent(codingKey: .attributes, typeCodingKey: AttributeTypeKey.type) as? [LabelAttributeModel] + self.attributes = try typeContainer.decodeModelsIfPresent(codingKey: .attributes, typeCodingKey: AttributeTypeKey.type) as? [LabelAttributeModel] self.html = try typeContainer.decodeIfPresent(String.self, forKey: .html) self.hero = try typeContainer.decodeIfPresent(Int.self, forKey: .hero) self.makeWholeViewClickable = try typeContainer.decodeIfPresent(Bool.self, forKey: .makeWholeViewClickable) diff --git a/MVMCoreUI/Atoms/Views/ProgressBar.swift b/MVMCoreUI/Atoms/Views/ProgressBar.swift index 0a29d62a..b640e4c7 100644 --- a/MVMCoreUI/Atoms/Views/ProgressBar.swift +++ b/MVMCoreUI/Atoms/Views/ProgressBar.swift @@ -57,7 +57,7 @@ import Foundation } isRounded = progressBarModel.isRounded ?? false thickness = progressBarModel.thickness ?? 8 - progress = (progressBarModel.percentage )/100.0 + progress = (progressBarModel.percentage)/100.0 if let progressColor = progressBarModel.progressColor { progressTintColor = UIColor.mfGet(forHex: progressColor) } diff --git a/MVMCoreUI/Models/Extensions/ModelHelper.swift b/MVMCoreUI/Models/Extensions/ModelHelper.swift index b4d0b1e2..fab9bd5f 100644 --- a/MVMCoreUI/Models/Extensions/ModelHelper.swift +++ b/MVMCoreUI/Models/Extensions/ModelHelper.swift @@ -14,21 +14,21 @@ extension KeyedDecodingContainer where Key : CodingKey { } public func decode(codingKey: KeyedDecodingContainer.Key) throws -> MoleculeProtocol { - return try decode(codingKey: codingKey, typeCodingKey: TypeCodingKey.moleculeName) + return try decodeModel(codingKey: codingKey, typeCodingKey: TypeCodingKey.moleculeName) } public func decodeIfPresent(codingKey: KeyedDecodingContainer.Key) throws -> MoleculeProtocol? { - return try decodeIfPresent(codingKey: codingKey, typeCodingKey: TypeCodingKey.moleculeName) + return try decodeModelIfPresent(codingKey: codingKey, typeCodingKey: TypeCodingKey.moleculeName) } public func decodeArray(codingKey: KeyedDecodingContainer.Key) throws -> [MoleculeProtocol] { - guard let models = try decodeArray(codingKey: codingKey, typeCodingKey: TypeCodingKey.moleculeName) as? [MoleculeProtocol] else { + guard let models = try decodeModels(codingKey: codingKey, typeCodingKey: TypeCodingKey.moleculeName) as? [MoleculeProtocol] else { throw ModelRegistry.Error.decoderError } return models } public func decodeArrayIfPresent(codingKey: KeyedDecodingContainer.Key) throws -> [MoleculeProtocol]? { - return try decodeArrayIfPresent(codingKey: codingKey, typeCodingKey: TypeCodingKey.moleculeName) as? [MoleculeProtocol] + return try decodeModelsIfPresent(codingKey: codingKey, typeCodingKey: TypeCodingKey.moleculeName) as? [MoleculeProtocol] } } diff --git a/MVMCoreUI/Models/Molecules/CarouselItemModel.swift b/MVMCoreUI/Models/Molecules/CarouselItemModel.swift index 58aa737f..f2392131 100644 --- a/MVMCoreUI/Models/Molecules/CarouselItemModel.swift +++ b/MVMCoreUI/Models/Molecules/CarouselItemModel.swift @@ -13,10 +13,6 @@ import Foundation public static var identifier: String = "carouselItem" public var backgroundColor: String? - public init(backgroundColor: String?) { - self.backgroundColor = backgroundColor - } - enum CodingKeys: String, CodingKey { case moleculeName case molecule @@ -25,13 +21,13 @@ import Foundation required public init(from decoder: Decoder) throws { let typeContainer = try decoder.container(keyedBy: CodingKeys.self) - self.backgroundColor = try typeContainer.decode(String.self, forKey: .backgroundColor) + backgroundColor = try typeContainer.decode(String.self, forKey: .backgroundColor) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(moleculeName, forKey: .moleculeName) - try container.encode(backgroundColor, forKey: .backgroundColor) - try container.encodeIfPresent(self.molecule, forKey: .molecule) + try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor) + try container.encodeModelIfPresent(molecule, forKey: .molecule) } } diff --git a/MVMCoreUI/Models/Molecules/CarouselModel.swift b/MVMCoreUI/Models/Molecules/CarouselModel.swift index 60f2b27c..08bddafd 100644 --- a/MVMCoreUI/Models/Molecules/CarouselModel.swift +++ b/MVMCoreUI/Models/Molecules/CarouselModel.swift @@ -21,16 +21,8 @@ import UIKit public var itemAlignment: String? public var pagingMolecule: PagingMoleculeProtocol? - public init(molecules: [CarouselItemModel], spacing: Float?, border: Bool?, loop: Bool?, height: Float?, itemWidthPercent: Float?, itemAlignment: String?, pagingMolecule: PagingMoleculeProtocol?, backgroundColor: String?){ + public init(molecules: [CarouselItemModel]){ self.molecules = molecules - self.spacing = spacing - self.border = border - self.loop = loop - self.height = height - self.itemWidthPercent = itemWidthPercent - self.itemAlignment = itemAlignment - self.pagingMolecule = pagingMolecule - self.backgroundColor = backgroundColor } enum CodingKeys: String, CodingKey { @@ -47,26 +39,26 @@ import UIKit required public init(from decoder: Decoder) throws { let typeContainer = try decoder.container(keyedBy: CodingKeys.self) - self.molecules = try typeContainer.decode([CarouselItemModel].self, forKey: .molecules) - self.spacing = try typeContainer.decode(Float.self, forKey: .spacing) - self.border = try typeContainer.decode(Bool.self, forKey: .border) - self.loop = try typeContainer.decode(Bool.self, forKey: .loop) - self.height = try typeContainer.decode(Float.self, forKey: .height) - self.itemWidthPercent = try typeContainer.decode(Float.self, forKey: .itemWidthPercent) - self.itemAlignment = try typeContainer.decode(String.self, forKey: .itemAlignment) - self.pagingMolecule = try typeContainer.decodeIfPresent(codingKey: .pagingMolecule) as? PagingMoleculeProtocol + molecules = try typeContainer.decode([CarouselItemModel].self, forKey: .molecules) + spacing = try typeContainer.decodeIfPresent(Float.self, forKey: .spacing) + border = try typeContainer.decodeIfPresent(Bool.self, forKey: .border) + loop = try typeContainer.decodeIfPresent(Bool.self, forKey: .loop) + height = try typeContainer.decodeIfPresent(Float.self, forKey: .height) + itemWidthPercent = try typeContainer.decodeIfPresent(Float.self, forKey: .itemWidthPercent) + itemAlignment = try typeContainer.decodeIfPresent(String.self, forKey: .itemAlignment) + pagingMolecule = try typeContainer.decodeIfPresent(codingKey: .pagingMolecule) as? PagingMoleculeProtocol } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(moleculeName, forKey: .moleculeName) + try container.encodeIfPresent(moleculeName, forKey: .moleculeName) try container.encode(molecules, forKey: .molecules) - try container.encode(spacing, forKey: .spacing) - try container.encode(border, forKey: .border) - try container.encode(loop, forKey: .loop) - try container.encode(height, forKey: .height) - try container.encode(itemWidthPercent, forKey: .itemWidthPercent) - try container.encode(itemAlignment, forKey: .itemAlignment) - try container.encodeIfPresent(self.pagingMolecule, forKey: .pagingMolecule) + try container.encodeIfPresent(spacing, forKey: .spacing) + try container.encodeIfPresent(border, forKey: .border) + try container.encodeIfPresent(loop, forKey: .loop) + try container.encodeIfPresent(height, forKey: .height) + try container.encodeIfPresent(itemWidthPercent, forKey: .itemWidthPercent) + try container.encodeIfPresent(itemAlignment, forKey: .itemAlignment) + try container.encodeModelIfPresent(pagingMolecule, forKey: .pagingMolecule) } } diff --git a/MVMCoreUI/Models/Molecules/DropDownListItemModel.swift b/MVMCoreUI/Models/Molecules/DropDownListItemModel.swift index 7a7770df..0bac18c6 100644 --- a/MVMCoreUI/Models/Molecules/DropDownListItemModel.swift +++ b/MVMCoreUI/Models/Molecules/DropDownListItemModel.swift @@ -31,14 +31,14 @@ import Foundation required public init(from decoder: Decoder) throws { let typeContainer = try decoder.container(keyedBy: CodingKeys.self) self.molecules = try typeContainer.decode([[ListItemModel]].self, forKey: .molecules) - self.separator = try typeContainer.decode(LineModel.self, forKey: .separator) + self.separator = try typeContainer.decodeIfPresent(LineModel.self, forKey: .separator) self.backgroundColor = try typeContainer.decodeIfPresent(String.self, forKey: .backgroundColor) self.dropDown = try typeContainer.decode(DropDownModel.self, forKey: .dropDown) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(moleculeName, forKey: .moleculeName) + try container.encodeIfPresent(moleculeName, forKey: .moleculeName) try container.encode(molecules, forKey: .molecules) try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor) try container.encode(dropDown, forKey: .dropDown) diff --git a/MVMCoreUI/Models/Molecules/FooterModel.swift b/MVMCoreUI/Models/Molecules/FooterModel.swift index 95a45422..52b21212 100644 --- a/MVMCoreUI/Models/Molecules/FooterModel.swift +++ b/MVMCoreUI/Models/Molecules/FooterModel.swift @@ -15,29 +15,23 @@ import Foundation public var backgroundColor: String? public var molecule: MoleculeProtocol? - public init(molecule: MoleculeProtocol?, backgroundColor: String?){ - self.molecule = molecule - self.backgroundColor = backgroundColor - } - enum CodingKeys: String, CodingKey { case moleculeName case molecule case backgroundColor - case separator } required public init(from decoder: Decoder) throws { let typeContainer = try decoder.container(keyedBy: CodingKeys.self) - self.moleculeName = try typeContainer.decode(String.self, forKey: .moleculeName) - self.backgroundColor = try typeContainer.decodeIfPresent(String.self, forKey: .backgroundColor) - self.molecule = try typeContainer.decodeIfPresent(codingKey: .molecule) + moleculeName = try typeContainer.decodeIfPresent(String.self, forKey: .moleculeName) + backgroundColor = try typeContainer.decodeIfPresent(String.self, forKey: .backgroundColor) + molecule = try typeContainer.decodeIfPresent(codingKey: .molecule) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(moleculeName, forKey: .moleculeName) - try container.encodeIfPresent(self.molecule, forKey: .molecule) + try container.encodeIfPresent(moleculeName, forKey: .moleculeName) + try container.encodeModelIfPresent(self.molecule, forKey: .molecule) try container.encodeIfPresent(self.backgroundColor, forKey: .backgroundColor) } } diff --git a/MVMCoreUI/Models/Molecules/HeaderModel.swift b/MVMCoreUI/Models/Molecules/HeaderModel.swift index adbfca47..45f511e0 100644 --- a/MVMCoreUI/Models/Molecules/HeaderModel.swift +++ b/MVMCoreUI/Models/Molecules/HeaderModel.swift @@ -15,12 +15,6 @@ import Foundation public var molecule: MoleculeProtocol? public var seperator: LineModel? - public init(molecule: MoleculeProtocol?, backgroundColor: String?, seperator: LineModel?){ - self.molecule = molecule - self.backgroundColor = backgroundColor - self.seperator = seperator - } - enum CodingKeys: String, CodingKey { case moleculeName case molecule @@ -30,17 +24,17 @@ import Foundation required public init(from decoder: Decoder) throws { let typeContainer = try decoder.container(keyedBy: CodingKeys.self) - self.moleculeName = try typeContainer.decode(String.self, forKey: .moleculeName) - self.backgroundColor = try typeContainer.decodeIfPresent(String.self, forKey: .backgroundColor) - self.molecule = try typeContainer.decodeIfPresent(codingKey: .molecule) - self.seperator = try typeContainer.decodeIfPresent(LineModel.self, forKey: .separator) + moleculeName = try typeContainer.decode(String.self, forKey: .moleculeName) + backgroundColor = try typeContainer.decodeIfPresent(String.self, forKey: .backgroundColor) + molecule = try typeContainer.decodeIfPresent(codingKey: .molecule) + seperator = try typeContainer.decodeIfPresent(LineModel.self, forKey: .separator) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(moleculeName, forKey: .moleculeName) - try container.encodeIfPresent(self.molecule, forKey: .molecule) - try container.encodeIfPresent(self.backgroundColor, forKey: .backgroundColor) - try container.encodeIfPresent(self.seperator, forKey: .separator) + try container.encodeModelIfPresent(molecule, forKey: .molecule) + try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor) + try container.encodeIfPresent(seperator, forKey: .separator) } } diff --git a/MVMCoreUI/Models/Molecules/MoleculeStackItemModel.swift b/MVMCoreUI/Models/Molecules/MoleculeStackItemModel.swift index 1a7e6f73..0818630f 100644 --- a/MVMCoreUI/Models/Molecules/MoleculeStackItemModel.swift +++ b/MVMCoreUI/Models/Molecules/MoleculeStackItemModel.swift @@ -52,12 +52,11 @@ import Foundation public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(moleculeName, forKey: .moleculeName) - try container.encodeIfPresent(self.molecule, forKey: .molecule) - - try container.encode(spacing, forKey: .spacing) - try container.encode(percentage, forKey: .percentage) - try container.encode(verticalAlignment, forKey: .verticalAlignment) - try container.encode(horizontalAlignment, forKey: .horizontalAlignment) - try container.encode(gone, forKey: .gone) + try container.encodeModelIfPresent(self.molecule, forKey: .molecule) + try container.encodeIfPresent(spacing, forKey: .spacing) + try container.encodeIfPresent(percentage, forKey: .percentage) + try container.encodeIfPresent(verticalAlignment, forKey: .verticalAlignment) + try container.encodeIfPresent(horizontalAlignment, forKey: .horizontalAlignment) + try container.encodeIfPresent(gone, forKey: .gone) } } diff --git a/MVMCoreUI/Models/Molecules/MoleculeStackModel.swift b/MVMCoreUI/Models/Molecules/MoleculeStackModel.swift index 9c462339..296c26d6 100644 --- a/MVMCoreUI/Models/Molecules/MoleculeStackModel.swift +++ b/MVMCoreUI/Models/Molecules/MoleculeStackModel.swift @@ -16,31 +16,28 @@ import Foundation public var axis: String? public var spacing: Float? - public init(axis: String?, molecules: [MoleculeStackItemModel]?, spacing: Float?) { - self.axis = axis - self.molecules = molecules - self.spacing = spacing - } - enum CodingKeys: String, CodingKey { case moleculeName case molecules case axis case spacing + case backgroundColor } required public init(from decoder: Decoder) throws { let typeContainer = try decoder.container(keyedBy: CodingKeys.self) - self.molecules = try typeContainer.decodeIfPresent([MoleculeStackItemModel].self, forKey: .molecules) - self.axis = try typeContainer.decodeIfPresent(String.self, forKey: .axis) - self.spacing = try typeContainer.decodeIfPresent(Float.self, forKey: .spacing) + molecules = try typeContainer.decodeIfPresent([MoleculeStackItemModel].self, forKey: .molecules) + axis = try typeContainer.decodeIfPresent(String.self, forKey: .axis) + spacing = try typeContainer.decodeIfPresent(Float.self, forKey: .spacing) + backgroundColor = try typeContainer.decode(String.self, forKey: .backgroundColor) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(moleculeName, forKey: .moleculeName) + try container.encodeIfPresent(moleculeName, forKey: .moleculeName) try container.encodeIfPresent(molecules, forKey: .molecules) - try container.encode(axis, forKey: .axis) - try container.encode(spacing, forKey: .spacing) + try container.encodeIfPresent(axis, forKey: .axis) + try container.encodeIfPresent(spacing, forKey: .spacing) + try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor) } } diff --git a/MVMCoreUI/Models/Template/ListPageTemplateModel.swift b/MVMCoreUI/Models/Template/ListPageTemplateModel.swift index fa8b96a2..ffeff803 100644 --- a/MVMCoreUI/Models/Template/ListPageTemplateModel.swift +++ b/MVMCoreUI/Models/Template/ListPageTemplateModel.swift @@ -54,7 +54,7 @@ import Foundation try container.encode(pageType, forKey: .pageType) try container.encode(screenHeading, forKey: .screenHeading) - try container.encodeArray(molecules, forKey: .molecules) + try container.encodeModels(molecules, forKey: .molecules) try container.encodeIfPresent(isAtomicTabs, forKey: .isAtomicTabs) try container.encodeIfPresent(header, forKey: .header) try container.encodeIfPresent(footer, forKey: .footer) diff --git a/MVMCoreUI/Molecules/Items/ListItemModel.swift b/MVMCoreUI/Molecules/Items/ListItemModel.swift index 5ae9e7fa..cfb1165b 100644 --- a/MVMCoreUI/Molecules/Items/ListItemModel.swift +++ b/MVMCoreUI/Molecules/Items/ListItemModel.swift @@ -20,6 +20,7 @@ import Foundation enum CodingKeys: String, CodingKey { case moleculeName case molecule + case backgroundColor case action case hideArrow case separator @@ -29,6 +30,7 @@ import Foundation required public init(from decoder: Decoder) throws { let typeContainer = try decoder.container(keyedBy: CodingKeys.self) molecule = try typeContainer.decodeIfPresent(codingKey: .molecule) + backgroundColor = try typeContainer.decodeIfPresent(String.self, forKey: .backgroundColor) action = try typeContainer.decodeIfPresent(ActionModel.self, forKey: .action) hideArrow = try typeContainer.decodeIfPresent(Bool.self, forKey: .hideArrow) separator = try typeContainer.decodeIfPresent(LineModel.self, forKey: .separator) @@ -38,7 +40,8 @@ import Foundation public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(moleculeName, forKey: .moleculeName) - try container.encodeIfPresent(molecule, forKey: .molecule) + try container.encodeModelIfPresent(molecule, forKey: .molecule) + try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor) try container.encodeIfPresent(action, forKey: .action) try container.encodeIfPresent(hideArrow, forKey: .hideArrow) try container.encodeIfPresent(separator, forKey: .separator)