From dd7a339a4390e10e9d47fd4b73bb1c04eaa0ac6a Mon Sep 17 00:00:00 2001 From: Kevin G Christiano Date: Thu, 26 Mar 2020 10:46:23 -0400 Subject: [PATCH] correction --- .../MoleculeModelProtocol.swift | 6 +++++ .../Items/DropDownFilterTableViewCell.swift | 5 +--- .../Items/DropDownListItemModel.swift | 24 +++++++++++++------ 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/MVMCoreUI/Models/ModelProtocols/MoleculeModelProtocol.swift b/MVMCoreUI/Models/ModelProtocols/MoleculeModelProtocol.swift index 913c3bd0..dda19b35 100644 --- a/MVMCoreUI/Models/ModelProtocols/MoleculeModelProtocol.swift +++ b/MVMCoreUI/Models/ModelProtocols/MoleculeModelProtocol.swift @@ -1,6 +1,12 @@ import Foundation +public enum MolecularError: Swift.Error { + case error(String) + case countImbalance(String) +} + + public protocol MoleculeModelProtocol: ModelProtocol { var moleculeName: String { get } var backgroundColor: Color? { get set } diff --git a/MVMCoreUI/Molecules/Items/DropDownFilterTableViewCell.swift b/MVMCoreUI/Molecules/Items/DropDownFilterTableViewCell.swift index 307f64c9..aa581ba1 100644 --- a/MVMCoreUI/Molecules/Items/DropDownFilterTableViewCell.swift +++ b/MVMCoreUI/Molecules/Items/DropDownFilterTableViewCell.swift @@ -29,10 +29,7 @@ import UIKit guard newValue != oldValue, let self = self, let index = self.dropDown.pickerData.firstIndex(of: newValue), - let model = self.listItemModel as? DropDownListItemModel, - let dropListItemJSON = (self.listItemModel as? DropDownListItemModel).toJSON(), - let json2d = dropListItemJSON.optionalArrayForKey("molecules") as? [[[AnyHashable: Any]]], - !json2d.isEmpty && !(json2d.first?.isEmpty ?? false) + let model = self.listItemModel as? DropDownListItemModel else { return } if self.previousIndex != NSNotFound { diff --git a/MVMCoreUI/Molecules/Items/DropDownListItemModel.swift b/MVMCoreUI/Molecules/Items/DropDownListItemModel.swift index 071014d3..deb2374b 100644 --- a/MVMCoreUI/Molecules/Items/DropDownListItemModel.swift +++ b/MVMCoreUI/Molecules/Items/DropDownListItemModel.swift @@ -8,6 +8,7 @@ import Foundation + @objcMembers public class DropDownListItemModel: ListItemModel, MoleculeModelProtocol { //-------------------------------------------------- // MARK: - Properties @@ -17,6 +18,10 @@ import Foundation public var molecules: [[ListItemModelProtocol & MoleculeModelProtocol]] public var dropDown: ItemDropdownEntryFieldModel + //-------------------------------------------------- + // MARK: - Methods + //-------------------------------------------------- + /// Defaults to set public override func setDefaults() { super.setDefaults() @@ -25,20 +30,24 @@ import Foundation style = "sectionFooter" } -// public func verify() throws { -// guard dropDown.options.count != molecules.count else { -// throw -// } -// } + //-------------------------------------------------- + // MARK: - Functions + //-------------------------------------------------- + + public class func verify(dropdown: ItemDropdownEntryFieldModel, molecules: [[ListItemModelProtocol & MoleculeModelProtocol]]) throws { + guard dropdown.options.count == molecules.count else { + throw MolecularError.countImbalance("dropdown.options.count is not equal to molecules.count") + } + } //-------------------------------------------------- // MARK: - Initializer //-------------------------------------------------- - public init(molecules: [[ListItemModelProtocol & MoleculeModelProtocol]], dropDown: ItemDropdownEntryFieldModel) { + public init(molecules: [[ListItemModelProtocol & MoleculeModelProtocol]], dropDown: ItemDropdownEntryFieldModel) throws { self.molecules = molecules self.dropDown = dropDown -// try verify() + try Self.verify(dropdown: dropDown, molecules: molecules) super.init() } @@ -60,6 +69,7 @@ import Foundation let typeContainer = try decoder.container(keyedBy: CodingKeys.self) molecules = try typeContainer.decodeModels2DIfPresent(codingKey: .molecules) ?? [[]] dropDown = try typeContainer.decode(ItemDropdownEntryFieldModel.self, forKey: .dropDown) + try Self.verify(dropdown: dropDown, molecules: molecules) try super.init(from: decoder) }