Digital PCT265 defect MVAPCT-272: Fix remaining swap bug. Add positional logic.

This commit is contained in:
Hedden, Kyle Matthew 2024-09-13 18:24:54 -04:00
parent 84e1405757
commit fe0a83df0b
3 changed files with 33 additions and 33 deletions

View File

@ -15,7 +15,7 @@ public protocol MoleculeListProtocol {
func addMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], indexPath: IndexPath, animation: UITableView.RowAnimation?) 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. /// Asks the delegate to remove molecules. Never ask to remove a molecule at an index.
//func removeMolecules(at indexPaths: [IndexPath], animation: UITableView.RowAnimation?) //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?) func swapMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], with replacements: [ListItemModelProtocol & MoleculeModelProtocol], at indexPath: IndexPath, animation: UITableView.RowAnimation?)
/// Asks the delegate to swap batches of molecules. /// 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 { public extension MVMCoreUIDelegateObject {

View File

@ -112,7 +112,7 @@ open class MoleculeListTemplate: ThreeLayerTableViewController, TemplateProtocol
guard let self = self, guard let self = self,
let cell = tableView.dequeueReusableCell(withIdentifier: moleculeInfo.cellReuseId) let cell = tableView.dequeueReusableCell(withIdentifier: moleculeInfo.cellReuseId)
else { return UITableViewCell() } else { return UITableViewCell() }
cell.isHidden = moleculeInfo.model.gone == true cell.isHidden = moleculeInfo.model.gone
(cell as? MoleculeViewProtocol)?.reset() (cell as? MoleculeViewProtocol)?.reset()
(cell as? MoleculeListCellProtocol)?.setLines(with: templateModel?.line, delegateObject: delegateObjectIVar, additionalData: nil, indexPath: indexPath) (cell as? MoleculeListCellProtocol)?.setLines(with: templateModel?.line, delegateObject: delegateObjectIVar, additionalData: nil, indexPath: indexPath)
if let moleculeView = cell as? MoleculeViewProtocol { 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. // If the view is in a cell, refresh the table ui.
let point = molecule.convert(molecule.bounds.origin, to: tableView) let point = molecule.convert(molecule.bounds.origin, to: tableView)
if let indexPath = tableView.indexPathForRow(at: point), tableView.indexPathsForVisibleRows?.contains(indexPath) ?? false { if let indexPath = tableView.indexPathForRow(at: point), tableView.indexPathsForVisibleRows?.contains(indexPath) ?? false {
//TOOD: Find alternate. tableView.setNeedsLayout()
//refreshTable()
} }
} }
@ -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 } guard let targetMoleculeRef = MoleculeInfo(listItemModel: molecule) else { return }
let additionalMoleculesRef = molecules.asMoleculeInfoRef(delegateObject: delegateObjectIVar).registerTypes(with: tableView)
var snapshot = dataSource.snapshot() var snapshot = dataSource.snapshot()
debugLog("[ADD] State before: \(snapshot.itemIdentifiers)") 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)") 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.updateViewConstraints()
self.view.setNeedsLayout() self.view.setNeedsLayout()
} }
} }
public func swapMolecules(_ molecules: [ListItemModelProtocol & MoleculeModelProtocol], with replacements: [ListItemModelProtocol & MoleculeModelProtocol], at indexPath: IndexPath, animation: UITableView.RowAnimation?) { 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 } guard let tableView else { return }
@ -453,9 +462,14 @@ extension MoleculeListTemplate: MoleculeListProtocol {
debugLog("[SWAP] State after delete: \(snapshot.itemIdentifiers)") debugLog("[SWAP] State after delete: \(snapshot.itemIdentifiers)")
if let targetMolecule, let targetRef = MoleculeInfo(listItemModel: targetMolecule) { if let molecule, let targetRef = MoleculeInfo(listItemModel: molecule) {
let replacementRefs = molecules.asMoleculeInfoRef(delegateObject: delegateObjectIVar).registerTypes(with: tableView) let replacementRefs = replacements.asMoleculeInfoRef(delegateObject: delegateObjectIVar).registerTypes(with: tableView)
snapshot.insertItems(replacementRefs, afterItem: targetRef) switch position {
case .below:
snapshot.insertItems(replacementRefs, afterItem: targetRef)
case .above:
snapshot.insertItems(replacementRefs, beforeItem: targetRef)
}
} }
debugLog("[SWAP] Applying: \(snapshot.itemIdentifiers)") debugLog("[SWAP] Applying: \(snapshot.itemIdentifiers)")

View File

@ -80,7 +80,7 @@ public class AddRemoveMoleculesBehavior: PageCustomActionHandlerBehavior, PageMo
guard let list = delegate?.moleculeListDelegate else { return } guard let list = delegate?.moleculeListDelegate else { return }
for case let model as (MoleculeModelProtocol & ListItemModelProtocol & AddMolecules) in rootMolecules { for case let model as (MoleculeModelProtocol & ListItemModelProtocol & AddMolecules) in rootMolecules {
if let moleculesToAdd = model.getRecursiveMoleculesToAdd() { 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: case let model as AddMoleculesActionModel:
guard let list = delegate?.moleculeListDelegate, guard let list = delegate?.moleculeListDelegate,
let sourceModel = MVMCoreUIActionHandler.getSourceModel(from: additionalData) as? (ListItemModelProtocol & MoleculeModelProtocol & AddMolecules), let sourceModel = MVMCoreUIActionHandler.getSourceModel(from: additionalData) as? (ListItemModelProtocol & MoleculeModelProtocol & AddMolecules),
let moleculesToAdd = sourceModel.getRecursiveMoleculesToAdd(), let moleculesToAdd = sourceModel.getRecursiveMoleculesToAdd()
let indexPath = list.getAdjustedIndexPath(for: sourceModel, position: moleculesToAdd.1) else { break } else { break }
await MainActor.run { await MainActor.run {
// TODO: Pipe the position. list.addMolecules(moleculesToAdd.0, position: moleculesToAdd.1, molecule: sourceModel, animation: model.animation)
list.addMolecules(moleculesToAdd.0, after: sourceModel, animation: model.animation)
} }
case let model as RemoveMoleculesActionModel: case let model as RemoveMoleculesActionModel:
guard let list = delegate?.moleculeListDelegate, 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 sourceModel = MVMCoreUIActionHandler.getSourceModel(from: additionalData) as? (ListItemModelProtocol & MoleculeModelProtocol & RemoveMolecules & AddMolecules),
let moleculesToRemove = sourceModel.getRecursiveMoleculesToRemove(), let moleculesToRemove = sourceModel.getRecursiveMoleculesToRemove(),
let moleculesToAdd = sourceModel.getRecursiveMoleculesToAdd() let moleculesToAdd = sourceModel.getRecursiveMoleculesToAdd()
//let indexPath = list.getAdjustedIndexPath(for: sourceModel, position: moleculesToAdd.1)
else { break } else { break }
await MainActor.run { await MainActor.run {
// TODO: Take into account position. list.swapMolecules(moleculesToRemove, with: moleculesToAdd.0, position: moleculesToAdd.1, molecule: sourceModel, animation: model.animation)
list.swapMolecules(moleculesToRemove, with: moleculesToAdd.0, after: sourceModel, animation: model.animation)
} }
default: default:
break 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 class AddMoleculesActionModel: ActionModelProtocol {
public static var identifier: String = "addMoleculesAction" public static var identifier: String = "addMoleculesAction"
public var actionType: String = AddMoleculesActionModel.identifier public var actionType: String = AddMoleculesActionModel.identifier