From d79ec8dc6a81a1a2f5a821205b1242983546851c Mon Sep 17 00:00:00 2001 From: "Hedden, Kyle Matthew" Date: Tue, 21 May 2024 17:11:15 -0400 Subject: [PATCH] Digital PCT265 story PCT-135: Extract running the behavior transformations for method clarity. --- .../BaseControllers/ViewController.swift | 46 +++++++++++-------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/MVMCoreUI/BaseControllers/ViewController.swift b/MVMCoreUI/BaseControllers/ViewController.swift index 928a9390..7977ddc1 100644 --- a/MVMCoreUI/BaseControllers/ViewController.swift +++ b/MVMCoreUI/BaseControllers/ViewController.swift @@ -251,26 +251,7 @@ import MVMCore // Run through behavior tranformations. var behaviorUpdatedModels = [MoleculeModelProtocol]() if var newTemplateModel = newPageModel as? TemplateModelProtocol { - executeBehaviors { (behavior: PageMoleculeTransformationBehavior) in - var changes = [any MoleculeModelProtocol]() - if let updatedMolecules = behavior.onPageNew(rootMolecules: newTemplateModel.rootMolecules, delegateObjectIVar, changes: &changes) { - updatedMolecules.forEach { molecule in - // Replace again in case there is a template level child. - if let replaced = try? newTemplateModel.replaceChildMolecule(with: molecule) { - // Only recognize the molecules that actually changed. - if changes.count > 0 { - debugLog("\(behavior) updated \(changes) in template model.") - changes = changes.filter({ model in - !behaviorUpdatedModels.contains { $0.id == model.id } - }) - behaviorUpdatedModels.append(contentsOf: changes) - } - } else { - debugLog("Failed to replace \(molecule) in the template model.") - } - } - } - } + behaviorUpdatedModels = runBehaviorTransformations(on: &newTemplateModel) } // Apply the form validator to the controller. @@ -331,6 +312,31 @@ import MVMCore view.setNeedsLayout() } + func runBehaviorTransformations(on newTemplateModel: inout any TemplateModelProtocol) -> [any MoleculeModelProtocol] { + var behaviorUpdatedModels = [MoleculeModelProtocol]() + executeBehaviors { (behavior: PageMoleculeTransformationBehavior) in + var changes = [any MoleculeModelProtocol]() + if let updatedMolecules = behavior.onPageNew(rootMolecules: newTemplateModel.rootMolecules, delegateObjectIVar, changes: &changes) { + updatedMolecules.forEach { molecule in + // Replace again in case there is a template level child. + if let replaced = try? newTemplateModel.replaceChildMolecule(with: molecule) { + // Only recognize the molecules that actually changed. + if changes.count > 0 { + debugLog("\(behavior) updated \(changes) in template model.") + changes = changes.filter({ model in + !behaviorUpdatedModels.contains { $0.id == model.id } + }) + behaviorUpdatedModels.append(contentsOf: changes) + } + } else { + debugLog("Failed to replace \(molecule) in the template model.") + } + } + } + } + return behaviorUpdatedModels + } + public func generateMoleculeView(from model: MoleculeModelProtocol) -> MoleculeViewProtocol? { executeBehaviors { (behavior: PageMoleculeTransformationBehavior) in behavior.willSetupMolecule(with: model, updating: nil)