diff --git a/MVMCoreUI/Atomic/Protocols/MoleculeViewProtocol.swift b/MVMCoreUI/Atomic/Protocols/MoleculeViewProtocol.swift index 3869b398..3ddbdb6f 100644 --- a/MVMCoreUI/Atomic/Protocols/MoleculeViewProtocol.swift +++ b/MVMCoreUI/Atomic/Protocols/MoleculeViewProtocol.swift @@ -84,6 +84,7 @@ public extension ModelRegistry { let type = try ModelRegistry.getHandler(model) as! MoleculeViewProtocol.Type return type } catch { + // TODO: Wrong level to log. Barf so we can get more context on who is asking for a molecule. if let errorObject = MVMCoreErrorObject.createErrorObject(for: error, location: #function) { MVMCoreLoggingHandler.shared()?.addError(toLog: errorObject) } diff --git a/MVMCoreUI/Atomic/Templates/MoleculeListTemplate.swift b/MVMCoreUI/Atomic/Templates/MoleculeListTemplate.swift index 3e78dc86..9e1a42f1 100644 --- a/MVMCoreUI/Atomic/Templates/MoleculeListTemplate.swift +++ b/MVMCoreUI/Atomic/Templates/MoleculeListTemplate.swift @@ -102,7 +102,7 @@ open class MoleculeListTemplate: ThreeLayerTableViewController, TemplateProtocol guard let molecules else { return } - // For updating individual specfied molecules. (Not a full table reload done in the base class.) These molecule types should remain the same type by replacement standards. + // For updating individual specfied molecules. (Not a full table reload done in the base class.) molecules.forEach({ molecule in // Replace any top level cell data if required. if let index = moleculesInfo?.firstIndex(where: { $0.molecule.id == molecule.id }) { @@ -256,7 +256,8 @@ open class MoleculeListTemplate: ThreeLayerTableViewController, TemplateProtocol guard let moleculesInfo = moleculesInfo else { return } - let indicies = moleculesInfo.indices.filter({ index -> Bool in + // Deep search to find any updated molecules under the top level list models. + var indicies = moleculesInfo.indices.filter({ index -> Bool in return moleculesInfo[index].molecule.findFirstMolecule(by: { existingMolecule in molecules.contains { newMolecule in existingMolecule.moleculeName == newMolecule.moleculeName && equal(moleculeA: existingMolecule, moleculeB: newMolecule) @@ -264,21 +265,47 @@ open class MoleculeListTemplate: ThreeLayerTableViewController, TemplateProtocol }) != nil }) + // Refresh cell identifers. 1st partition are those that don't need to be reloaded. 2nd partition are those that do need a complete reload. + let partionedIndex = indicies.partition { index in + guard let refreshedMoleculeInfo = self.moleculesInfo?[safe: index], + let latestMoleculeInfo = createMoleculeInfo(with: refreshedMoleculeInfo.molecule) + else { return true } + + if refreshedMoleculeInfo.identifier == latestMoleculeInfo.identifier { + return false + } else { + tableView.register(latestMoleculeInfo.class, forCellReuseIdentifier: latestMoleculeInfo.identifier) + self.moleculesInfo?[index].identifier = latestMoleculeInfo.identifier + self.moleculesInfo?[index].class = latestMoleculeInfo.class + return true + } + } + // Refresh the cell. (reload loses cell selection) let selectedIndex = tableView.indexPathForSelectedRow let indexPaths = indicies.map { return IndexPath(row: $0, section: 0) } - debugLog("Refreshing rows \(indexPaths.map { $0.row })") + let refreshRows = Array(indexPaths[..