diff --git a/MVMCoreUI/Atomic/Templates/MoleculeListTemplate.swift b/MVMCoreUI/Atomic/Templates/MoleculeListTemplate.swift index ac862337..fa404172 100644 --- a/MVMCoreUI/Atomic/Templates/MoleculeListTemplate.swift +++ b/MVMCoreUI/Atomic/Templates/MoleculeListTemplate.swift @@ -188,10 +188,8 @@ open class MoleculeListTemplate: ThreeLayerTableViewController, TemplateProtocol open func newData(for molecule: MoleculeModelProtocol) { //TODO: expand for header, navigation, etc - let json = molecule.toJSON() guard let index = moleculesInfo?.firstIndex(where: { (moleculeInfo) -> Bool in - //TODO: check for molecule protocol eqaulity - if json == moleculeInfo.molecule.toJSON() { + if equal(moleculeA: molecule, moleculeB: moleculeInfo.molecule) { return true } else if let parent = moleculeInfo.molecule as? ParentMoleculeModelProtocol { // Get all molecules of the same type for faster check. @@ -201,8 +199,8 @@ open class MoleculeListTemplate: ThreeLayerTableViewController, TemplateProtocol } return accumulator } - for molecule in molecules { - if json == molecule.toJSON() { + for moleculeB in molecules { + if equal(moleculeA: molecule, moleculeB: moleculeB) { return true } } @@ -283,6 +281,18 @@ open class MoleculeListTemplate: ThreeLayerTableViewController, TemplateProtocol return modules } + + /// Checks if the two molecules are equal + private func equal(moleculeA: MoleculeModelProtocol, moleculeB: MoleculeModelProtocol) -> Bool { + // TODO: move this to a better approach, maybe a UUID for each model. + // Do instance check + if let classMoleculeA = moleculeA as? NSObjectProtocol, + let classMoleculeB = moleculeB as? NSObjectProtocol { + return classMoleculeA === classMoleculeB + } + // Do json check + return moleculeA.toJSON() == moleculeB.toJSON() + } } extension MoleculeListTemplate: MoleculeListProtocol { @@ -320,9 +330,7 @@ extension MoleculeListTemplate: MoleculeListProtocol { open func getIndexPath(for molecule: ListItemModelProtocol & MoleculeModelProtocol) -> IndexPath? { guard let index = moleculesInfo?.firstIndex(where: { (moleculeInfo) -> Bool in - //TODO: check for molecule protocol eqaulity - let json = moleculeInfo.molecule.toJSON() - return json == molecule.toJSON() + return equal(moleculeA: molecule, moleculeB: moleculeInfo.molecule) }) else { return nil } return IndexPath(row: index, section: 0)