From 6758aed034bdced6fd0d18fadea278210eabd142 Mon Sep 17 00:00:00 2001 From: "Hedden, Kyle Matthew" Date: Wed, 13 Sep 2023 20:36:59 -0400 Subject: [PATCH] add comments --- .../ModelProtocols/ParentMoleculeModelProtocol.swift | 5 ++++- .../Protocols/ModelProtocols/TemplateModelProtocol.swift | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/MVMCoreUI/Atomic/Protocols/ModelProtocols/ParentMoleculeModelProtocol.swift b/MVMCoreUI/Atomic/Protocols/ModelProtocols/ParentMoleculeModelProtocol.swift index 3e16069e..c970bc39 100644 --- a/MVMCoreUI/Atomic/Protocols/ModelProtocols/ParentMoleculeModelProtocol.swift +++ b/MVMCoreUI/Atomic/Protocols/ModelProtocols/ParentMoleculeModelProtocol.swift @@ -12,14 +12,16 @@ public protocol ParentModelProtocol: MoleculeTreeTraversalProtocol, AnyObject { var children: [MoleculeModelProtocol] { get } + /// Method for replacing surface level children. (Does not recurse.) func replaceChildMolecule(with molecule: MoleculeModelProtocol) throws -> Bool } public extension ParentModelProtocol { + /// Top level test to replace child molecules. Each parent molecule should attempt to replace. func replaceChildMolecule(with molecule: MoleculeModelProtocol) throws -> Bool { return false } - /// Helper function for replacing molecules. + /// Helper function for replacing a single molecules with type and optionality checks. func replaceChildMolecule(at childMolecule: inout T, with replacementMolecule: MoleculeModelProtocol) throws -> Bool { guard let childIdMolecule = childMolecule as? MoleculeModelProtocol else { return false } if childIdMolecule.id == replacementMolecule.id { @@ -32,6 +34,7 @@ public extension ParentModelProtocol { return false } + /// Helper for replacing a molecule in place within an array. Note the "in". func replaceChildMolecule(in molecules: inout [T], with replacementMolecule: MoleculeModelProtocol) throws -> Bool { guard let moleculeIdModels = molecules as? [MoleculeModelProtocol], let matchingIndex = moleculeIdModels.firstIndex(where: { $0.id == replacementMolecule.id diff --git a/MVMCoreUI/Atomic/Protocols/ModelProtocols/TemplateModelProtocol.swift b/MVMCoreUI/Atomic/Protocols/ModelProtocols/TemplateModelProtocol.swift index 34a8e13a..f884e4aa 100644 --- a/MVMCoreUI/Atomic/Protocols/ModelProtocols/TemplateModelProtocol.swift +++ b/MVMCoreUI/Atomic/Protocols/ModelProtocols/TemplateModelProtocol.swift @@ -38,9 +38,10 @@ public extension TemplateModelProtocol { return rootMolecules.depthFirstTraverse(options: options, depth: depth, onVisit: onVisit) } + /// Recursively finds and replaces the first child matching the replacement molecule id property. func replaceMolecule(with replacementMolecule: MoleculeModelProtocol) throws -> Bool { // Attempt root level replacement on the template model first. - if try self.replaceChildMolecule(with: replacementMolecule) { + if try replaceChildMolecule(with: replacementMolecule) { return true }