From 83d5721e683c8675c494b7e7d8c78d2584cd2bde Mon Sep 17 00:00:00 2001 From: Kevin G Christiano Date: Thu, 23 Jan 2020 19:59:05 -0500 Subject: [PATCH] temp fix for arch issue --- .../BaseDropdownEntryFieldModel.swift | 8 +++ .../DateDropdownEntryFieldModel.swift | 2 +- .../TextFields/DigitEntryFieldModel.swift | 4 +- .../Atoms/TextFields/EntryFieldModel.swift | 8 +-- .../TextFields/ItemDropdownEntryField.swift | 8 ++- .../ItemDropdownEntryFieldModel.swift | 26 +++++++- .../MFViewController+Model.swift | 8 +++ .../Items/DropDownFilterTableViewCell.swift | 36 +++++------ .../Items/DropDownListItemModel.swift | 35 ++++++++++- .../ModelMoleculeDelegateProtocol.swift | 8 ++- .../Templates/MoleculeListTemplate.swift | 59 +++++++++++++++++++ 11 files changed, 171 insertions(+), 31 deletions(-) diff --git a/MVMCoreUI/Atoms/TextFields/BaseDropdownEntryFieldModel.swift b/MVMCoreUI/Atoms/TextFields/BaseDropdownEntryFieldModel.swift index 3dffa669..1fefd42f 100644 --- a/MVMCoreUI/Atoms/TextFields/BaseDropdownEntryFieldModel.swift +++ b/MVMCoreUI/Atoms/TextFields/BaseDropdownEntryFieldModel.swift @@ -15,4 +15,12 @@ public override class var identifier: String { return "" } + + required public init(from decoder: Decoder) throws { + try super.init(from: decoder) + } + + public override func encode(to encoder: Encoder) throws { + try super.encode(to: encoder) + } } diff --git a/MVMCoreUI/Atoms/TextFields/DateDropdownEntryFieldModel.swift b/MVMCoreUI/Atoms/TextFields/DateDropdownEntryFieldModel.swift index c226660b..4e916268 100644 --- a/MVMCoreUI/Atoms/TextFields/DateDropdownEntryFieldModel.swift +++ b/MVMCoreUI/Atoms/TextFields/DateDropdownEntryFieldModel.swift @@ -38,6 +38,6 @@ public override func encode(to encoder: Encoder) throws { try super.encode(to: encoder) var container = encoder.container(keyedBy: CodingKeys.self) - try container.encodeIfPresent(dateFormat, forKey: .dateFormat) + try container.encode(dateFormat, forKey: .dateFormat) } } diff --git a/MVMCoreUI/Atoms/TextFields/DigitEntryFieldModel.swift b/MVMCoreUI/Atoms/TextFields/DigitEntryFieldModel.swift index 3b139e9b..46171e41 100644 --- a/MVMCoreUI/Atoms/TextFields/DigitEntryFieldModel.swift +++ b/MVMCoreUI/Atoms/TextFields/DigitEntryFieldModel.swift @@ -42,7 +42,7 @@ public override func encode(to encoder: Encoder) throws { try super.encode(to: encoder) var container = encoder.container(keyedBy: CodingKeys.self) - try container.encodeIfPresent(digits, forKey: .digits) - try container.encodeIfPresent(secureEntry, forKey: .secureEntry) + try container.encode(digits, forKey: .digits) + try container.encode(secureEntry, forKey: .secureEntry) } } diff --git a/MVMCoreUI/Atoms/TextFields/EntryFieldModel.swift b/MVMCoreUI/Atoms/TextFields/EntryFieldModel.swift index adb2b58c..14a57f4c 100644 --- a/MVMCoreUI/Atoms/TextFields/EntryFieldModel.swift +++ b/MVMCoreUI/Atoms/TextFields/EntryFieldModel.swift @@ -68,10 +68,10 @@ import Foundation try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor) try container.encodeIfPresent(title, forKey: .title) try container.encodeIfPresent(feedback, forKey: .feedback) - try container.encodeIfPresent(errorMessage, forKey: .errorMessage) - try container.encodeIfPresent(isEnabled, forKey: .isEnabled) - try container.encodeIfPresent(isLocked, forKey: .isLocked) - try container.encodeIfPresent(isSelected, forKey: .isSelected) + try container.encode(errorMessage, forKey: .errorMessage) + try container.encode(isEnabled, forKey: .isEnabled) + try container.encode(isLocked, forKey: .isLocked) + try container.encode(isSelected, forKey: .isSelected) try container.encodeIfPresent(fieldKey, forKey: .fieldKey) try container.encodeIfPresent(isValid, forKey: .isValid) } diff --git a/MVMCoreUI/Atoms/TextFields/ItemDropdownEntryField.swift b/MVMCoreUI/Atoms/TextFields/ItemDropdownEntryField.swift index 962ff89b..d1728b46 100644 --- a/MVMCoreUI/Atoms/TextFields/ItemDropdownEntryField.swift +++ b/MVMCoreUI/Atoms/TextFields/ItemDropdownEntryField.swift @@ -96,9 +96,11 @@ open class ItemDropdownEntryField: BaseDropdownEntryField { guard let model = model as? ItemDropdownEntryFieldModel else { return } - if let options = model.options { - pickerData = options - setPickerDelegates(delegate: self) + pickerData = model.options + setPickerDelegates(delegate: self) + + if let pickerView = pickerView { + self.pickerView(pickerView, didSelectRow: 0, inComponent: 0) } } } diff --git a/MVMCoreUI/Atoms/TextFields/ItemDropdownEntryFieldModel.swift b/MVMCoreUI/Atoms/TextFields/ItemDropdownEntryFieldModel.swift index a12d6b80..b913f976 100644 --- a/MVMCoreUI/Atoms/TextFields/ItemDropdownEntryFieldModel.swift +++ b/MVMCoreUI/Atoms/TextFields/ItemDropdownEntryFieldModel.swift @@ -15,5 +15,29 @@ return "dropDown" } - public var options: [String]? + public var options: [String] = [] + + //-------------------------------------------------- + // MARK: - Keys + //-------------------------------------------------- + + private enum CodingKeys: String, CodingKey { + case options + } + + //-------------------------------------------------- + // MARK: - Initializers + //-------------------------------------------------- + + required public init(from decoder: Decoder) throws { + try super.init(from: decoder) + let typeContainer = try decoder.container(keyedBy: CodingKeys.self) + options = try typeContainer.decodeIfPresent([String].self, forKey: .options) ?? [] + } + + public override func encode(to encoder: Encoder) throws { + try super.encode(to: encoder) + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(options, forKey: .options) + } } diff --git a/MVMCoreUI/BaseControllers/MFViewController+Model.swift b/MVMCoreUI/BaseControllers/MFViewController+Model.swift index f503d0b5..6cedf93c 100644 --- a/MVMCoreUI/BaseControllers/MFViewController+Model.swift +++ b/MVMCoreUI/BaseControllers/MFViewController+Model.swift @@ -33,6 +33,14 @@ extension MFViewController: MoleculeDelegateProtocol { @objc public func moleculeLayoutUpdated(_ molecule: UIView & MVMCoreUIMoleculeViewProtocol) { } + + @objc public func addMolecules(_ molecules: [[AnyHashable : Any]], sender: UITableViewCell, animation: UITableView.RowAnimation) { + // Do nothing + } + + @objc public func removeMolecules(_ molecules: [[AnyHashable : Any]], sender: UITableViewCell, animation: UITableView.RowAnimation) { + // Do nothing + } } public extension MFViewController { diff --git a/MVMCoreUI/Molecules/Items/DropDownFilterTableViewCell.swift b/MVMCoreUI/Molecules/Items/DropDownFilterTableViewCell.swift index bf67a5bc..bc8edf18 100644 --- a/MVMCoreUI/Molecules/Items/DropDownFilterTableViewCell.swift +++ b/MVMCoreUI/Molecules/Items/DropDownFilterTableViewCell.swift @@ -19,47 +19,49 @@ import UIKit var previousIndex = NSNotFound //-------------------------------------------------- - // MARK: - MFViewProtocol + // MARK: - Lifecycle //-------------------------------------------------- override public func setupView() { super.setupView() guard dropDown.superview == nil else { return } - - contentView.addSubview(dropDown) - NSLayoutConstraint.activate(Array(NSLayoutConstraint.pinView(toSuperview: dropDown, useMargins: true).values)) - + addMolecule(dropDown) dropDown.observeDropdownChange = { [weak self] oldValue, newValue in guard newValue != oldValue, let self = self, - let options = self.dropDown.json?.optionalArrayForKey("options") as? [NSString], - let index = options.firstIndex(of: newValue as NSString), - let molecules = self.dropDownListItemModel?.molecules else { return } + let index = self.dropDown.pickerData.firstIndex(of: newValue), + let molecules = self.dropDownListItemModel?.molecules + else { return } - if self.previousIndex != NSNotFound { - self.delegateObject?.moleculeDelegate?.removeMolecules(molecules[self.previousIndex], sender: self, animation: .fade) + var json2d = [[[AnyHashable: Any]]]() + + for moleculeList in molecules { + var json1d = [[AnyHashable: Any]]() + for molecule in moleculeList { + json1d.append((molecule as? ListItemModel)!.toJSON()!) + } + json2d.append(json1d) } - self.delegateObject?.moleculeDelegate?.addMolecules(molecules[index], sender: self, animation: .fade) + if self.previousIndex != NSNotFound { + self.delegateObject?.moleculeDelegate?.removeMolecules(json2d[self.previousIndex], sender: self, animation: .fade) + } + + self.delegateObject?.moleculeDelegate?.addMolecules(json2d[index], sender: self, animation: .fade) self.previousIndex = index } } - public override func updateView(_ size: CGFloat) { - super.updateView(size) - dropDown.updateView(size) - } - public override func setWithModel(_ model: MoleculeModelProtocol?, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) { dropDownListItemModel = model as? DropDownListItemModel self.delegateObject = delegateObject super.setWithModel(model, delegateObject, additionalData) + dropDown.setWithModel(dropDownListItemModel?.dropDown, delegateObject, additionalData) dropDown.observingTextFieldDelegate = delegateObject?.uiTextFieldDelegate as? ObservingTextFieldDelegate dropDown.uiTextFieldDelegate = delegateObject?.uiTextFieldDelegate - dropDown.setWithModel(dropDownListItemModel, delegateObject, additionalData) } public override func reset() { diff --git a/MVMCoreUI/Molecules/Items/DropDownListItemModel.swift b/MVMCoreUI/Molecules/Items/DropDownListItemModel.swift index 9b9a66f1..13af4278 100644 --- a/MVMCoreUI/Molecules/Items/DropDownListItemModel.swift +++ b/MVMCoreUI/Molecules/Items/DropDownListItemModel.swift @@ -9,6 +9,9 @@ import Foundation @objcMembers public class DropDownListItemModel: ContainerModel, ListItemModelProtocol { + //-------------------------------------------------- + // MARK: - Properties + //-------------------------------------------------- public static var identifier: String = "dropDownListItem" public var molecules: [[ListItemModelProtocol]] @@ -16,12 +19,37 @@ import Foundation public var backgroundColor: Color? public var line: LineModel? = LineModel(type: .none) public var hideArrow: Bool? = true + + /// Defaults to set + func setDefaults() { + if useHorizontalMargins == nil { + useHorizontalMargins = true + } + if useVerticalMargins == nil { + useVerticalMargins = true + } + if topMarginPadding == nil { + topMarginPadding = 24 + } + if bottomMarginPadding == nil { + bottomMarginPadding = 0 + } + } + //-------------------------------------------------- + // MARK: - Initializer + //-------------------------------------------------- + public init(molecules: [[ListItemModelProtocol]], dropDown: ItemDropdownEntryFieldModel) { self.molecules = molecules self.dropDown = dropDown super.init() + setDefaults() } + + //-------------------------------------------------- + // MARK: - Keys + //-------------------------------------------------- private enum CodingKeys: String, CodingKey { case molecules @@ -29,10 +57,14 @@ import Foundation case line case backgroundColor } + + //-------------------------------------------------- + // MARK: - Codec + //-------------------------------------------------- required public init(from decoder: Decoder) throws { let typeContainer = try decoder.container(keyedBy: CodingKeys.self) - molecules = try typeContainer.decodeMolecules2D(codingKey: .molecules) as! [[ListItemModelProtocol]] + molecules = try typeContainer.decodeMolecules2D(codingKey: .molecules) as? [[ListItemModelProtocol]] ?? [[]] dropDown = try typeContainer.decode(ItemDropdownEntryFieldModel.self, forKey: .dropDown) if let lineModel = try typeContainer.decodeIfPresent(LineModel.self, forKey: .line) { @@ -41,6 +73,7 @@ import Foundation backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor) try super.init(from: decoder) + setDefaults() } public override func encode(to encoder: Encoder) throws { diff --git a/MVMCoreUI/OtherHandlers/ModelMoleculeDelegateProtocol.swift b/MVMCoreUI/OtherHandlers/ModelMoleculeDelegateProtocol.swift index 11e8a154..8fe1caa5 100644 --- a/MVMCoreUI/OtherHandlers/ModelMoleculeDelegateProtocol.swift +++ b/MVMCoreUI/OtherHandlers/ModelMoleculeDelegateProtocol.swift @@ -31,15 +31,19 @@ extension MoleculeDelegateProtocol { public func moleculeLayoutUpdated(_ molecule: UIView & MVMCoreUIMoleculeViewProtocol) { // Do Nothing } + public func addMolecules(_ molecules: [[AnyHashable : Any]], sender: UITableViewCell, animation: UITableView.RowAnimation) { - // Do nothpublic ing + // Do nothing } + public func removeMolecules(_ molecules: [[AnyHashable : Any]], sender: UITableViewCell, animation: UITableView.RowAnimation) { // Do nothing } + public func addMolecules(_ molecules: [ListItemModelProtocol], sender: UITableViewCell, animation: UITableView.RowAnimation) { - // Do nothpublic ing + // Do nothing } + public func removeMolecules(_ molecules: [ListItemModelProtocol], sender: UITableViewCell, animation: UITableView.RowAnimation) { // Do nothing } diff --git a/MVMCoreUI/Templates/MoleculeListTemplate.swift b/MVMCoreUI/Templates/MoleculeListTemplate.swift index c21a770b..a7b8d6ba 100644 --- a/MVMCoreUI/Templates/MoleculeListTemplate.swift +++ b/MVMCoreUI/Templates/MoleculeListTemplate.swift @@ -146,6 +146,64 @@ open class MoleculeListTemplate: ThreeLayerTableViewController, TemplateProtocol } } + public override func addMolecules(_ molecules: [[AnyHashable: Any]], sender: UITableViewCell, animation: UITableView.RowAnimation) { + + var tmpMolecules = [ListItemModelProtocol]() + + molecules.forEach { molecule in + + let data = try? JSONSerialization.data(withJSONObject: molecule) + let decoder = JSONDecoder() + let listItemModel = try? decoder.decode(ListItemModel.self, from: data!) + + tmpMolecules.append(listItemModel!) + } + + DispatchQueue.main.async { + guard let indexPath = self.tableView?.indexPath(for: sender) else { return } + var indexPaths: [IndexPath] = [] + for molecule in molecules { + if let info = self.getMoleculeInfo(with: tmpMolecules as! ListItemModelProtocol) { + self.tableView?.register(info.class, forCellReuseIdentifier: info.identifier) + let index = indexPath.row + 1 + indexPaths.count + self.moleculesInfo?.insert(info, at: index) + indexPaths.append(IndexPath(row: index, section: 0)) + } + } + self.tableView?.insertRows(at: indexPaths, with: animation) + self.updateViewConstraints() + self.view.layoutIfNeeded() + } + } + + public override func removeMolecules(_ molecules: [[AnyHashable: Any]], sender: UITableViewCell, animation: UITableView.RowAnimation) { + + var tmpMolecules = [ListItemModelProtocol]() + + molecules.forEach { molecule in + + let data = try? JSONSerialization.data(withJSONObject: molecule) + let decoder = JSONDecoder() + let listItemModel = try? decoder.decode(ListItemModel.self, from: data!) + + tmpMolecules.append(listItemModel!) + } + + var indexPaths: [IndexPath] = [] + //TODO: cehck for molecule protocola eqality + for molecule in tmpMolecules { + if let removeIndex = moleculesInfo?.firstIndex(where: { (moleculeInfo) -> Bool in + return molecule.toJSONString() == moleculeInfo.molecule.toJSONString() + }) { + moleculesInfo?.remove(at: removeIndex) + indexPaths.append(IndexPath(row: removeIndex + indexPaths.count, section: 0)) + } + } + self.tableView?.deleteRows(at: indexPaths, with: animation) + self.updateViewConstraints() + self.view.layoutIfNeeded() + } + public func addMolecules(_ molecules: [ListItemModelProtocol], sender: UITableViewCell, animation: UITableView.RowAnimation) { // This dispatch is needed to fix a race condition that can occur if this function is called during the table setup. DispatchQueue.main.async { @@ -164,6 +222,7 @@ open class MoleculeListTemplate: ThreeLayerTableViewController, TemplateProtocol self.view.layoutIfNeeded() } } + public func removeMolecules(_ molecules: [ListItemModelProtocol], sender: UITableViewCell, animation: UITableView.RowAnimation) { var indexPaths: [IndexPath] = [] //TODO: cehck for molecule protocola eqality