diff --git a/MVMCoreUI/Molecules/Items/DropDownFilterTableViewCell.swift b/MVMCoreUI/Molecules/Items/DropDownFilterTableViewCell.swift index 0826b4b5..ed2294a5 100644 --- a/MVMCoreUI/Molecules/Items/DropDownFilterTableViewCell.swift +++ b/MVMCoreUI/Molecules/Items/DropDownFilterTableViewCell.swift @@ -40,7 +40,9 @@ import UIKit for moleculeList in molecules { var json1d = [[AnyHashable: Any]]() for molecule in moleculeList { - json1d.append((molecule as? ListItemModel)!.toJSON()!) + if let moleculeDictionary = (molecule as? ListItemModel)?.toJSON() { + json1d.append(moleculeDictionary) + } } json2d.append(json1d) } diff --git a/MVMCoreUI/Templates/MoleculeListTemplate.swift b/MVMCoreUI/Templates/MoleculeListTemplate.swift index 159589eb..75ef7d2f 100644 --- a/MVMCoreUI/Templates/MoleculeListTemplate.swift +++ b/MVMCoreUI/Templates/MoleculeListTemplate.swift @@ -9,10 +9,10 @@ import UIKit open class MoleculeListTemplate: ThreeLayerTableViewController, TemplateProtocol { - + public var moleculesInfo: [(identifier: String, class: AnyClass, molecule: ListItemModelProtocol)]? var observer: NSKeyValueObservation? - + public var templateModel: ListPageTemplateModel? @objc public override func parsePageJSON() throws { try parseTemplateJSON() @@ -31,11 +31,11 @@ open class MoleculeListTemplate: ThreeLayerTableViewController, TemplateProtocol } } } - + open override func viewForTop() -> UIView { guard let headerModel = templateModel?.header, let molecule = MVMCoreUIMoleculeMappingObject.shared()?.createMolecule(headerModel, delegateObject() as? MVMCoreUIDelegateObject, false) else { - return super.viewForTop() + return super.viewForTop() } // Temporary, Default the horizontal padding @@ -48,7 +48,7 @@ open class MoleculeListTemplate: ThreeLayerTableViewController, TemplateProtocol override open func viewForBottom() -> UIView { guard let footerModel = templateModel?.footer, let molecule = MVMCoreUIMoleculeMappingObject.shared()?.createMolecule(footerModel, delegateObject() as? MVMCoreUIDelegateObject, false) else { - return super.viewForBottom() + return super.viewForBottom() } return molecule } @@ -97,7 +97,7 @@ open class MoleculeListTemplate: ThreeLayerTableViewController, TemplateProtocol open override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { guard let moleculeInfo = moleculesInfo?[indexPath.row], let cell = tableView.dequeueReusableCell(withIdentifier: moleculeInfo.identifier) else { - return UITableViewCell() + return UITableViewCell() } let delegate = delegateObject() as? MVMCoreUIDelegateObject let moleculeCell = cell as? MVMCoreUIMoleculeViewProtocol @@ -145,17 +145,15 @@ 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 listItemModel = try? JSONDecoder().decode(ListItemModel.self, from: data!) -  let jsonData = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) - let json = String(data: data!, encoding: String.Encoding.utf8) - tmpMolecules.append(listItemModel!) + if let data = try? JSONSerialization.data(withJSONObject: molecule), let listItemModel = try? JSONDecoder().decode(ListItemModel.self, from: data) { + tmpMolecules.append(listItemModel) + } } DispatchQueue.main.async { @@ -180,9 +178,9 @@ open class MoleculeListTemplate: ThreeLayerTableViewController, TemplateProtocol var tmpMolecules = [ListItemModelProtocol]() molecules.forEach { molecule in - let data = try? JSONSerialization.data(withJSONObject: molecule) - let listItemModel = try? JSONDecoder().decode(ListItemModel.self, from: data!) - tmpMolecules.append(listItemModel!) + if let data = try? JSONSerialization.data(withJSONObject: molecule), let listItemModel = try? JSONDecoder().decode(ListItemModel.self, from: data) { + tmpMolecules.append(listItemModel) + } } var indexPaths: [IndexPath] = [] @@ -202,21 +200,21 @@ open class MoleculeListTemplate: ThreeLayerTableViewController, TemplateProtocol 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 { - guard let indexPath = self.tableView?.indexPath(for: sender) else { return } - var indexPaths: [IndexPath] = [] - for molecule in molecules { - if let info = self.getMoleculeInfo(with: molecule) { - 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() - } + 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: molecule) { + 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 func removeMolecules(_ molecules: [ListItemModelProtocol], sender: UITableViewCell, animation: UITableView.RowAnimation) { @@ -241,7 +239,7 @@ open class MoleculeListTemplate: ThreeLayerTableViewController, TemplateProtocol guard let listItem = listItem, let moleculeClass = MVMCoreUIMoleculeMappingObject.shared()?.getMoleculeClass(listItem), let moleculeName = (moleculeClass as? ModelMoleculeViewProtocol.Type)?.nameForReuse(listItem, delegateObject() as? MVMCoreUIDelegateObject) ?? listItem.moleculeName else { - return nil + return nil } return (moleculeName, moleculeClass, listItem) }