diff --git a/MVMCoreUI/Atomic/Templates/MoleculeListTemplate.swift b/MVMCoreUI/Atomic/Templates/MoleculeListTemplate.swift index 7becea19..63358f76 100644 --- a/MVMCoreUI/Atomic/Templates/MoleculeListTemplate.swift +++ b/MVMCoreUI/Atomic/Templates/MoleculeListTemplate.swift @@ -422,21 +422,22 @@ extension MoleculeListTemplate: MoleculeListProtocol { return rowsToDelete.contains(index) ? itemIdentifier : nil } snapshot.deleteItems(itemsToDelete) - if let animation { - dataSource.defaultRowAnimation = animation - } + dataSource.defaultRowAnimation = animation ?? .automatic dataSource.apply(snapshot) { self.dataSource.defaultRowAnimation = .automatic + self.updateViewConstraints() + self.view.setNeedsLayout() + itemsToDelete.forEach { self.moleculeModelCache.removeValue(forKey: $0.moleculeId) } } - updateViewConstraints() - view.setNeedsLayout() } /// Convenience function that removes the passed molecule public func removeMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], animation: UITableView.RowAnimation?) { var snapshot = dataSource.snapshot() snapshot.deleteItems(molecules.asMoleculeInfoRef(delegateObject: delegateObjectIVar)) + dataSource.defaultRowAnimation = animation ?? .automatic dataSource.apply(snapshot, animatingDifferences: true) { + self.dataSource.defaultRowAnimation = .automatic self.updateViewConstraints() self.view.setNeedsLayout() molecules.forEach { self.moleculeModelCache.removeValue(forKey: $0.id) } @@ -455,23 +456,31 @@ extension MoleculeListTemplate: MoleculeListProtocol { snapshot.appendItems(additionalMoleculesRef) debugLog("[ADD] Applying: \(snapshot.itemIdentifiers)") } + dataSource.defaultRowAnimation = animation ?? .automatic dataSource.apply(snapshot, animatingDifferences: false) { + self.dataSource.defaultRowAnimation = .automatic self.updateViewConstraints() self.view.setNeedsLayout() } } public func addMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], position: AddMoleculesPosition, molecule: (ListItemModelProtocol & MoleculeModelProtocol), animation: UITableView.RowAnimation?) { - guard let targetMoleculeRef = MoleculeInfo(listItemModel: molecule) else { return } var snapshot = dataSource.snapshot() debugLog("[ADD] State before: \(snapshot.itemIdentifiers)") + guard let targetMoleculeRef = MoleculeInfo(listItemModel: molecule), snapshot.indexOfItem(targetMoleculeRef) != nil else { + debugLog("[ADD] Missing target: \(molecule.id). Skipping addition...") + return + } + let additionalMoleculesRef = register(listItemModels: molecules) switch position { case .below: + debugLog("[ADD] Inserting after: \(targetMoleculeRef)") snapshot.insertItems(additionalMoleculesRef, afterItem: targetMoleculeRef) case .above: + debugLog("[ADD] Inserting before: \(targetMoleculeRef)") snapshot.insertItems(additionalMoleculesRef, beforeItem: targetMoleculeRef) } @@ -511,7 +520,7 @@ extension MoleculeListTemplate: MoleculeListProtocol { debugLog("[SWAP] Applying: \(snapshot.itemIdentifiers)") dataSource.defaultRowAnimation = animation ?? .automatic - dataSource.apply(snapshot ) { + dataSource.apply(snapshot) { self.dataSource.defaultRowAnimation = .automatic self.updateViewConstraints() self.view.setNeedsLayout()