Digital PCT265 story MVAPCT-272: Crash prevention when the referenced molecule is not found.

This commit is contained in:
Hedden, Kyle Matthew 2024-10-17 23:58:43 -04:00
parent 8ec1a4495e
commit f2cdc02a7e

View File

@ -422,21 +422,22 @@ extension MoleculeListTemplate: MoleculeListProtocol {
return rowsToDelete.contains(index) ? itemIdentifier : nil return rowsToDelete.contains(index) ? itemIdentifier : nil
} }
snapshot.deleteItems(itemsToDelete) snapshot.deleteItems(itemsToDelete)
if let animation { dataSource.defaultRowAnimation = animation ?? .automatic
dataSource.defaultRowAnimation = animation
}
dataSource.apply(snapshot) { dataSource.apply(snapshot) {
self.dataSource.defaultRowAnimation = .automatic 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 /// Convenience function that removes the passed molecule
public func removeMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], animation: UITableView.RowAnimation?) { public func removeMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], animation: UITableView.RowAnimation?) {
var snapshot = dataSource.snapshot() var snapshot = dataSource.snapshot()
snapshot.deleteItems(molecules.asMoleculeInfoRef(delegateObject: delegateObjectIVar)) snapshot.deleteItems(molecules.asMoleculeInfoRef(delegateObject: delegateObjectIVar))
dataSource.defaultRowAnimation = animation ?? .automatic
dataSource.apply(snapshot, animatingDifferences: true) { dataSource.apply(snapshot, animatingDifferences: true) {
self.dataSource.defaultRowAnimation = .automatic
self.updateViewConstraints() self.updateViewConstraints()
self.view.setNeedsLayout() self.view.setNeedsLayout()
molecules.forEach { self.moleculeModelCache.removeValue(forKey: $0.id) } molecules.forEach { self.moleculeModelCache.removeValue(forKey: $0.id) }
@ -455,23 +456,31 @@ extension MoleculeListTemplate: MoleculeListProtocol {
snapshot.appendItems(additionalMoleculesRef) snapshot.appendItems(additionalMoleculesRef)
debugLog("[ADD] Applying: \(snapshot.itemIdentifiers)") debugLog("[ADD] Applying: \(snapshot.itemIdentifiers)")
} }
dataSource.defaultRowAnimation = animation ?? .automatic
dataSource.apply(snapshot, animatingDifferences: false) { dataSource.apply(snapshot, animatingDifferences: false) {
self.dataSource.defaultRowAnimation = .automatic
self.updateViewConstraints() self.updateViewConstraints()
self.view.setNeedsLayout() self.view.setNeedsLayout()
} }
} }
public func addMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], position: AddMoleculesPosition, molecule: (ListItemModelProtocol & MoleculeModelProtocol), animation: UITableView.RowAnimation?) { 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() var snapshot = dataSource.snapshot()
debugLog("[ADD] State before: \(snapshot.itemIdentifiers)") 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) let additionalMoleculesRef = register(listItemModels: molecules)
switch position { switch position {
case .below: case .below:
debugLog("[ADD] Inserting after: \(targetMoleculeRef)")
snapshot.insertItems(additionalMoleculesRef, afterItem: targetMoleculeRef) snapshot.insertItems(additionalMoleculesRef, afterItem: targetMoleculeRef)
case .above: case .above:
debugLog("[ADD] Inserting before: \(targetMoleculeRef)")
snapshot.insertItems(additionalMoleculesRef, beforeItem: targetMoleculeRef) snapshot.insertItems(additionalMoleculesRef, beforeItem: targetMoleculeRef)
} }
@ -511,7 +520,7 @@ extension MoleculeListTemplate: MoleculeListProtocol {
debugLog("[SWAP] Applying: \(snapshot.itemIdentifiers)") debugLog("[SWAP] Applying: \(snapshot.itemIdentifiers)")
dataSource.defaultRowAnimation = animation ?? .automatic dataSource.defaultRowAnimation = animation ?? .automatic
dataSource.apply(snapshot ) { dataSource.apply(snapshot) {
self.dataSource.defaultRowAnimation = .automatic self.dataSource.defaultRowAnimation = .automatic
self.updateViewConstraints() self.updateViewConstraints()
self.view.setNeedsLayout() self.view.setNeedsLayout()