update index functions

This commit is contained in:
Pfeil, Scott Robert 2020-03-13 13:31:33 -04:00
parent 907a617e1d
commit 7793bcfcb6
6 changed files with 37 additions and 128 deletions

View File

@ -391,13 +391,11 @@ open class ViewController: UIViewController, MVMCoreViewControllerProtocol, MVMC
return nil return nil
} }
// Test to see if needed. // Needed otherwise when subclassed, the extension gets called.
open func moleculeLayoutUpdated(_ molecule: UIView & MVMCoreUIMoleculeViewProtocol) {} open func moleculeLayoutUpdated(_ molecule: UIView & MVMCoreUIMoleculeViewProtocol) {}
open func addMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], sender: UITableViewCell, animation: UITableView.RowAnimation) {} open func getIndexPath(for molecule: ListItemModelProtocol & MoleculeModelProtocol) -> IndexPath? { return nil }
open func removeMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], sender: UITableViewCell, animation: UITableView.RowAnimation) {} open func addMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], indexPath: IndexPath, animation: UITableView.RowAnimation) {}
open func addMolecules(_ molecules: [[AnyHashable: Any]], indexPath: IndexPath, animation: UITableView.RowAnimation) {} open func removeMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], animation: UITableView.RowAnimation) {}
open func addMolecules(_ molecules: [[AnyHashable: Any]], sender: UITableViewCell, animation: UITableView.RowAnimation) {}
open func removeMolecules(_ molecules: [[AnyHashable: Any]], sender: UITableViewCell, animation: UITableView.RowAnimation) {}
// MARK: - UITextFieldDelegate (Check if this is still needed) // MARK: - UITextFieldDelegate (Check if this is still needed)
// To Remove TextFields Bug: Keyboard is not dismissing after reaching textfield max length limit // To Remove TextFields Bug: Keyboard is not dismissing after reaching textfield max length limit

View File

@ -32,18 +32,14 @@ import UIKit
override public func didSelectCell(at index: IndexPath, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable : Any]?) { override public func didSelectCell(at index: IndexPath, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable : Any]?) {
accordionButton.isSelected = !accordionButton.isSelected accordionButton.isSelected = !accordionButton.isSelected
accordionButton.setTitle(accordionButton.isSelected ? "-" : "+", for: .normal) accordionButton.setTitle(accordionButton.isSelected ? "-" : "+", for: .normal)
guard let model = accordionListItemModel else { guard let model = accordionListItemModel else { return }
return
}
guard let json = model.toJSON(),
let molecules = json.optionalArrayForKey("molecules") as? [[AnyHashable: Any]]
else { return }
if accordionButton.isSelected { if accordionButton.isSelected {
delegateObject?.moleculeDelegate?.addMolecules(molecules, sender: self, animation: .automatic) if let indexPath = delegateObject?.moleculeDelegate?.getIndexPath(for: model) {
delegateObject?.moleculeDelegate?.addMolecules(model.molecules, indexPath: indexPath, animation: .automatic)
}
} else { } else {
delegateObject?.moleculeDelegate?.removeMolecules(molecules, sender: self, animation: .automatic) delegateObject?.moleculeDelegate?.removeMolecules(model.molecules, animation: .automatic)
} }
if (accordionListItemModel?.hideLineWhenExpanded ?? false) && (self.bottomSeparatorView?.shouldBeVisible() ?? false) { if (accordionListItemModel?.hideLineWhenExpanded ?? false) && (self.bottomSeparatorView?.shouldBeVisible() ?? false) {

View File

@ -29,14 +29,16 @@ import UIKit
guard newValue != oldValue, guard newValue != oldValue,
let self = self, let self = self,
let index = self.dropDown.pickerData.firstIndex(of: newValue), let index = self.dropDown.pickerData.firstIndex(of: newValue),
let molecules2D = (self.listItemModel as? DropDownListItemModel)?.molecules let model = self.listItemModel as? DropDownListItemModel
else { return } else { return }
if self.previousIndex != NSNotFound { if self.previousIndex != NSNotFound {
self.delegateObject?.moleculeDelegate?.removeMolecules(molecules2D[self.previousIndex], sender: self, animation: .fade) self.delegateObject?.moleculeDelegate?.removeMolecules(model.molecules[self.previousIndex], animation: .fade)
} }
self.delegateObject?.moleculeDelegate?.addMolecules(molecules2D[index], sender: self, animation: .fade) if let indexPath = self.delegateObject?.moleculeDelegate?.getIndexPath(for: model) {
self.delegateObject?.moleculeDelegate?.addMolecules(model.molecules[index], indexPath: indexPath, animation: .fade)
}
self.previousIndex = index self.previousIndex = index
} }
} }

View File

@ -54,23 +54,19 @@ import UIKit
extension TabsTableViewCell: TopTabbarDelegate { extension TabsTableViewCell: TopTabbarDelegate {
public func shouldSelectItem(at index: Int, topTabbar: TopTabbar) -> Bool { public func shouldSelectItem(at index: Int, topTabbar: TopTabbar) -> Bool {
if let model = tabsListItemModel, if let model = tabsListItemModel {
let json = model.toJSON(), let molecules = model.molecules[topTabbar.selectedIndex]
let json2d = json.optionalArrayForKey("molecules") as? [[[AnyHashable: Any]]] { delegateObject?.moleculeDelegate?.removeMolecules(molecules, animation: index < tabs.selectedIndex ? .right : .left)
let molecules = json2d[topTabbar.selectedIndex]
delegateObject?.moleculeDelegate?.removeMolecules(molecules, sender: self, animation: index < tabs.selectedIndex ? .right : .left)
} }
previousTabIndex = tabs.selectedIndex previousTabIndex = tabs.selectedIndex
return true return true
} }
public func topTabbar(_ topTabbar: TopTabbar, didSelectItemAt index: Int) { public func topTabbar(_ topTabbar: TopTabbar, didSelectItemAt index: Int) {
if let model = tabsListItemModel, guard let model = tabsListItemModel,
let json = model.toJSON(), let indexPath = delegateObject?.moleculeDelegate?.getIndexPath(for: model) else { return }
let json2d = json.optionalArrayForKey("molecules") as? [[[AnyHashable: Any]]] { let molecules = model.molecules[index]
let molecules = json2d[index] delegateObject?.moleculeDelegate?.addMolecules(molecules, indexPath: indexPath, animation: index < previousTabIndex ? .left : .right)
delegateObject?.moleculeDelegate?.addMolecules(molecules, sender: self, animation: index < previousTabIndex ? .left : .right)
}
} }
} }

View File

@ -18,39 +18,16 @@ public protocol MoleculeDelegateProtocol {
func moleculeLayoutUpdated(_ molecule: UIView & MVMCoreUIMoleculeViewProtocol) //optional func moleculeLayoutUpdated(_ molecule: UIView & MVMCoreUIMoleculeViewProtocol) //optional
/// Asks the delegate to add or remove molecules. /// Asks the delegate to add or remove molecules.
//optional func getIndexPath(for molecule: ListItemModelProtocol & MoleculeModelProtocol) -> IndexPath?
func addMolecules(_ molecules: [[AnyHashable : Any]], sender: UITableViewCell, animation: UITableView.RowAnimation) func addMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], indexPath: IndexPath, animation: UITableView.RowAnimation)
func addMolecules(_ molecules: [[AnyHashable : Any]], indexPath: IndexPath, animation: UITableView.RowAnimation) func removeMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], animation: UITableView.RowAnimation)
func removeMolecules(_ molecules: [[AnyHashable : Any]], sender: UITableViewCell, animation: UITableView.RowAnimation)
//optional
func addMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], sender: UITableViewCell, animation: UITableView.RowAnimation)
func removeMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], sender: UITableViewCell, animation: UITableView.RowAnimation)
} }
extension MoleculeDelegateProtocol { extension MoleculeDelegateProtocol {
public func moleculeLayoutUpdated(_ molecule: UIView & MVMCoreUIMoleculeViewProtocol) {
// Do Nothing
}
public func addMolecules(_ molecules: [[AnyHashable : Any]], sender: UITableViewCell, animation: UITableView.RowAnimation) { public func moleculeLayoutUpdated(_ molecule: UIView & MVMCoreUIMoleculeViewProtocol) {}
// Do nothing
}
public func addMolecules(_ molecules: [[AnyHashable : Any]], indexPath: IndexPath, animation: UITableView.RowAnimation) { public func getIndexPath(for molecule: ListItemModelProtocol & MoleculeModelProtocol) -> IndexPath? { return nil }
// Do nothing public func addMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], indexPath: IndexPath, animation: UITableView.RowAnimation) {}
} public func removeMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], animation: UITableView.RowAnimation) {}
public func removeMolecules(_ molecules: [[AnyHashable : Any]], sender: UITableViewCell, animation: UITableView.RowAnimation) {
// Do nothing
}
public func addMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], sender: UITableViewCell, animation: UITableView.RowAnimation) {
// Do nothing
}
public func removeMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], sender: UITableViewCell, animation: UITableView.RowAnimation) {
// Do nothing
}
} }

View File

@ -158,77 +158,18 @@ open class MoleculeListTemplate: ThreeLayerTableViewController, TemplateProtocol
} }
} }
open override func addMolecules(_ molecules: [[AnyHashable: Any]], indexPath: IndexPath, animation: UITableView.RowAnimation) { open override func getIndexPath(for molecule: ListItemModelProtocol & MoleculeModelProtocol) -> IndexPath? {
guard let index = moleculesInfo?.firstIndex(where: { (moleculeInfo) -> Bool in
var tmpMolecules = [ListItemModelProtocol & MoleculeModelProtocol]() //TODO: cehck for molecule protocola eqality
let json = moleculeInfo.molecule.toJSON()
molecules.forEach { molecule in return json == molecule.toJSON()
if let data = try? JSONSerialization.data(withJSONObject: molecule), let listItemModel = try? JSONDecoder().decode(MoleculeListItemModel.self, from: data) { }) else { return nil }
tmpMolecules.append(listItemModel) return IndexPath(row: index, section: 0)
}
}
DispatchQueue.main.async {
var indexPaths: [IndexPath] = []
for molecule in tmpMolecules {
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()
}
} }
open override func addMolecules(_ molecules: [[AnyHashable: Any]], sender: UITableViewCell, animation: UITableView.RowAnimation) { open override func addMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], indexPath: IndexPath, animation: UITableView.RowAnimation) {
DispatchQueue.main.async { [weak self] in
guard let indexPath = self?.tableView?.indexPath(for: sender) else { return }
DispatchQueue.global().async {
self?.addMolecules(molecules, indexPath: indexPath, animation: animation)
}
}
}
open override func removeMolecules(_ molecules: [[AnyHashable: Any]], sender: UITableViewCell, animation: UITableView.RowAnimation) {
var tmpMolecules = [ListItemModelProtocol & MoleculeModelProtocol]()
molecules.forEach { molecule in
if let data = try? JSONSerialization.data(withJSONObject: molecule),
let listItemModel = try? JSONDecoder().decode(MoleculeListItemModel.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: { molecule.toJSON() == $0.molecule.toJSON() }) {
moleculesInfo?.remove(at: removeIndex)
indexPaths.append(IndexPath(row: removeIndex + indexPaths.count, section: 0))
}
}
tableView?.deleteRows(at: indexPaths, with: animation)
updateViewConstraints()
view.layoutIfNeeded()
}
open override func addMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], 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. // This dispatch is needed to fix a race condition that can occur if this function is called during the table setup.
DispatchQueue.main.async { DispatchQueue.main.async {
guard let indexPath = self.tableView?.indexPath(for: sender) else { return }
var indexPaths: [IndexPath] = [] var indexPaths: [IndexPath] = []
for molecule in molecules { for molecule in molecules {
@ -246,8 +187,7 @@ open class MoleculeListTemplate: ThreeLayerTableViewController, TemplateProtocol
} }
} }
open override func removeMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], sender: UITableViewCell, animation: UITableView.RowAnimation) { open override func removeMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], animation: UITableView.RowAnimation) {
var indexPaths: [IndexPath] = [] var indexPaths: [IndexPath] = []
//TODO: cehck for molecule protocola eqality //TODO: cehck for molecule protocola eqality