From 5c3c452e9a08a37806110684773e10f56b4c9868 Mon Sep 17 00:00:00 2001 From: "Hedden, Kyle Matthew" Date: Fri, 13 Sep 2024 18:24:54 -0400 Subject: [PATCH] Digital PCT265 defect MVAPCT-272: Fix remaining swap bug. Add positional logic. --- .../Protocols/MoleculeListProtocol.swift | 4 +- .../Templates/MoleculeListTemplate.swift | 38 +++++++++++++------ .../Behaviors/AddRemoveMoleculeBehavior.swift | 24 +++--------- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/MVMCoreUI/Atomic/Protocols/MoleculeListProtocol.swift b/MVMCoreUI/Atomic/Protocols/MoleculeListProtocol.swift index d00ae005..226c076f 100644 --- a/MVMCoreUI/Atomic/Protocols/MoleculeListProtocol.swift +++ b/MVMCoreUI/Atomic/Protocols/MoleculeListProtocol.swift @@ -15,7 +15,7 @@ public protocol MoleculeListProtocol { func addMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], indexPath: IndexPath, animation: UITableView.RowAnimation?) /// - func addMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], after molecule: (ListItemModelProtocol & MoleculeModelProtocol), animation: UITableView.RowAnimation?) + func addMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], position: AddMoleculesPosition, molecule: (ListItemModelProtocol & MoleculeModelProtocol), animation: UITableView.RowAnimation?) /// Asks the delegate to remove molecules. Never ask to remove a molecule at an index. //func removeMolecules(at indexPaths: [IndexPath], animation: UITableView.RowAnimation?) @@ -27,7 +27,7 @@ public protocol MoleculeListProtocol { func swapMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], with replacements: [ListItemModelProtocol & MoleculeModelProtocol], at indexPath: IndexPath, animation: UITableView.RowAnimation?) /// Asks the delegate to swap batches of molecules. - func swapMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], with replacements: [ListItemModelProtocol & MoleculeModelProtocol], after molecule: (ListItemModelProtocol & MoleculeModelProtocol)?, animation: UITableView.RowAnimation?) + func swapMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], with replacements: [ListItemModelProtocol & MoleculeModelProtocol], position: AddMoleculesPosition, molecule: (ListItemModelProtocol & MoleculeModelProtocol)?, animation: UITableView.RowAnimation?) } public extension MVMCoreUIDelegateObject { diff --git a/MVMCoreUI/Atomic/Templates/MoleculeListTemplate.swift b/MVMCoreUI/Atomic/Templates/MoleculeListTemplate.swift index c6ba5b1e..1f919dd3 100644 --- a/MVMCoreUI/Atomic/Templates/MoleculeListTemplate.swift +++ b/MVMCoreUI/Atomic/Templates/MoleculeListTemplate.swift @@ -112,7 +112,7 @@ open class MoleculeListTemplate: ThreeLayerTableViewController, TemplateProtocol guard let self = self, let cell = tableView.dequeueReusableCell(withIdentifier: moleculeInfo.cellReuseId) else { return UITableViewCell() } - cell.isHidden = moleculeInfo.model.gone == true + cell.isHidden = moleculeInfo.model.gone (cell as? MoleculeViewProtocol)?.reset() (cell as? MoleculeListCellProtocol)?.setLines(with: templateModel?.line, delegateObject: delegateObjectIVar, additionalData: nil, indexPath: indexPath) if let moleculeView = cell as? MoleculeViewProtocol { @@ -280,8 +280,7 @@ open class MoleculeListTemplate: ThreeLayerTableViewController, TemplateProtocol // If the view is in a cell, refresh the table ui. let point = molecule.convert(molecule.bounds.origin, to: tableView) if let indexPath = tableView.indexPathForRow(at: point), tableView.indexPathsForVisibleRows?.contains(indexPath) ?? false { - //TOOD: Find alternate. - //refreshTable() + tableView.setNeedsLayout() } } @@ -423,24 +422,34 @@ extension MoleculeListTemplate: MoleculeListProtocol { } } - public func addMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], after 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 } - let additionalMoleculesRef = molecules.asMoleculeInfoRef(delegateObject: delegateObjectIVar).registerTypes(with: tableView) + var snapshot = dataSource.snapshot() debugLog("[ADD] State before: \(snapshot.itemIdentifiers)") - snapshot.insertItems(additionalMoleculesRef, afterItem: targetMoleculeRef) + + let additionalMoleculesRef = molecules.asMoleculeInfoRef(delegateObject: delegateObjectIVar).registerTypes(with: tableView) + switch position { + case .below: + snapshot.insertItems(additionalMoleculesRef, afterItem: targetMoleculeRef) + case .above: + snapshot.insertItems(additionalMoleculesRef, beforeItem: targetMoleculeRef) + } + debugLog("[ADD] Applying: \(snapshot.itemIdentifiers)") - dataSource.apply(snapshot, animatingDifferences: false) { + dataSource.defaultRowAnimation = animation ?? .automatic + dataSource.apply(snapshot, animatingDifferences: animation != nil) { + self.dataSource.defaultRowAnimation = .automatic self.updateViewConstraints() self.view.setNeedsLayout() } } public func swapMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], with replacements: [ListItemModelProtocol & MoleculeModelProtocol], at indexPath: IndexPath, animation: UITableView.RowAnimation?) { - swapMolecules(molecules, with: replacements, after: dataSource.snapshot().itemIdentifiers[safe: indexPath.row] as? (ListItemModelProtocol & MoleculeModelProtocol), animation: animation) + swapMolecules(molecules, with: replacements, position: .below, molecule: dataSource.snapshot().itemIdentifiers[safe: indexPath.row] as? (ListItemModelProtocol & MoleculeModelProtocol), animation: animation) } - public func swapMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], with replacements: [ListItemModelProtocol & MoleculeModelProtocol], after targetMolecule: (ListItemModelProtocol & MoleculeModelProtocol)?, animation: UITableView.RowAnimation?) { + public func swapMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], with replacements: [ListItemModelProtocol & MoleculeModelProtocol], position: AddMoleculesPosition, molecule: (ListItemModelProtocol & MoleculeModelProtocol)?, animation: UITableView.RowAnimation?) { guard let tableView else { return } @@ -453,9 +462,14 @@ extension MoleculeListTemplate: MoleculeListProtocol { debugLog("[SWAP] State after delete: \(snapshot.itemIdentifiers)") - if let targetMolecule, let targetRef = MoleculeInfo(listItemModel: targetMolecule) { - let replacementRefs = molecules.asMoleculeInfoRef(delegateObject: delegateObjectIVar).registerTypes(with: tableView) - snapshot.insertItems(replacementRefs, afterItem: targetRef) + if let molecule, let targetRef = MoleculeInfo(listItemModel: molecule) { + let replacementRefs = replacements.asMoleculeInfoRef(delegateObject: delegateObjectIVar).registerTypes(with: tableView) + switch position { + case .below: + snapshot.insertItems(replacementRefs, afterItem: targetRef) + case .above: + snapshot.insertItems(replacementRefs, beforeItem: targetRef) + } } debugLog("[SWAP] Applying: \(snapshot.itemIdentifiers)") diff --git a/MVMCoreUI/Behaviors/AddRemoveMoleculeBehavior.swift b/MVMCoreUI/Behaviors/AddRemoveMoleculeBehavior.swift index 219ec8ff..2d321017 100644 --- a/MVMCoreUI/Behaviors/AddRemoveMoleculeBehavior.swift +++ b/MVMCoreUI/Behaviors/AddRemoveMoleculeBehavior.swift @@ -80,7 +80,7 @@ public class AddRemoveMoleculesBehavior: PageCustomActionHandlerBehavior, PageMo guard let list = delegate?.moleculeListDelegate else { return } for case let model as (MoleculeModelProtocol & ListItemModelProtocol & AddMolecules) in rootMolecules { if let moleculesToAdd = model.getRecursiveMoleculesToAdd() { - list.addMolecules(moleculesToAdd.0, after: model, animation: nil) + list.addMolecules(moleculesToAdd.0, position: moleculesToAdd.1, molecule: model, animation: nil) } } } @@ -94,11 +94,10 @@ public class AddRemoveMoleculesBehavior: PageCustomActionHandlerBehavior, PageMo case let model as AddMoleculesActionModel: guard let list = delegate?.moleculeListDelegate, let sourceModel = MVMCoreUIActionHandler.getSourceModel(from: additionalData) as? (ListItemModelProtocol & MoleculeModelProtocol & AddMolecules), - let moleculesToAdd = sourceModel.getRecursiveMoleculesToAdd(), - let indexPath = list.getAdjustedIndexPath(for: sourceModel, position: moleculesToAdd.1) else { break } + let moleculesToAdd = sourceModel.getRecursiveMoleculesToAdd() + else { break } await MainActor.run { - // TODO: Pipe the position. - list.addMolecules(moleculesToAdd.0, after: sourceModel, animation: model.animation) + list.addMolecules(moleculesToAdd.0, position: moleculesToAdd.1, molecule: sourceModel, animation: model.animation) } case let model as RemoveMoleculesActionModel: guard let list = delegate?.moleculeListDelegate, @@ -112,11 +111,9 @@ public class AddRemoveMoleculesBehavior: PageCustomActionHandlerBehavior, PageMo let sourceModel = MVMCoreUIActionHandler.getSourceModel(from: additionalData) as? (ListItemModelProtocol & MoleculeModelProtocol & RemoveMolecules & AddMolecules), let moleculesToRemove = sourceModel.getRecursiveMoleculesToRemove(), let moleculesToAdd = sourceModel.getRecursiveMoleculesToAdd() - //let indexPath = list.getAdjustedIndexPath(for: sourceModel, position: moleculesToAdd.1) else { break } await MainActor.run { - // TODO: Take into account position. - list.swapMolecules(moleculesToRemove, with: moleculesToAdd.0, after: sourceModel, animation: model.animation) + list.swapMolecules(moleculesToRemove, with: moleculesToAdd.0, position: moleculesToAdd.1, molecule: sourceModel, animation: model.animation) } default: break @@ -124,17 +121,6 @@ public class AddRemoveMoleculesBehavior: PageCustomActionHandlerBehavior, PageMo } } -private extension MoleculeListProtocol { - /// Convenience function to get the index path adjusted for position. - func getAdjustedIndexPath(for molecule: ListItemModelProtocol & MoleculeModelProtocol, position: AddMoleculesPosition) -> IndexPath? { - guard let indexPath = getIndexPath(for: molecule) else { return nil } - if position == .below { - return IndexPath(row: indexPath.row + 1, section: indexPath.section) - } - return indexPath - } -} - public class AddMoleculesActionModel: ActionModelProtocol { public static var identifier: String = "addMoleculesAction" public var actionType: String = AddMoleculesActionModel.identifier