From 5a0399bad95db7f4b152c05978af05a65d1a9e1f Mon Sep 17 00:00:00 2001 From: "Pfeil, Scott Robert" Date: Mon, 29 Mar 2021 16:59:26 -0400 Subject: [PATCH] enable parents, for testing. Switch to KyleTraverse (tm) --- ...istLeftVariableIconAllTextLinksModel.swift | 6 ++++- .../EyebrowHeadlineBodyLinkModel.swift | 7 +++++- .../Atomic/Protocols/TemplateProtocol.swift | 18 +++++++++++-- MVMCoreUI/Behaviors/GetContactBehavior.swift | 25 +++++-------------- .../PageBehaviorHandlerModelProtocol.swift | 11 -------- 5 files changed, 33 insertions(+), 34 deletions(-) diff --git a/MVMCoreUI/Atomic/Molecules/DesignedComponents/List/LeftVariable/ListLeftVariableIconAllTextLinksModel.swift b/MVMCoreUI/Atomic/Molecules/DesignedComponents/List/LeftVariable/ListLeftVariableIconAllTextLinksModel.swift index 8581deb1..c0700d5a 100644 --- a/MVMCoreUI/Atomic/Molecules/DesignedComponents/List/LeftVariable/ListLeftVariableIconAllTextLinksModel.swift +++ b/MVMCoreUI/Atomic/Molecules/DesignedComponents/List/LeftVariable/ListLeftVariableIconAllTextLinksModel.swift @@ -7,7 +7,7 @@ // -public class ListLeftVariableIconAllTextLinksModel: ListItemModel, MoleculeModelProtocol { +public class ListLeftVariableIconAllTextLinksModel: ListItemModel, MoleculeModelProtocol, ParentMoleculeModelProtocol { //-------------------------------------------------- // MARK: - Properties //-------------------------------------------------- @@ -16,6 +16,10 @@ public class ListLeftVariableIconAllTextLinksModel: ListItemModel, MoleculeModel public var image: ImageViewModel public var eyebrowHeadlineBodyLink: EyebrowHeadlineBodyLinkModel + public var children: [MoleculeModelProtocol] { + return [image] + eyebrowHeadlineBodyLink.children + } + //-------------------------------------------------- // MARK: - Method //-------------------------------------------------- diff --git a/MVMCoreUI/Atomic/Molecules/VerticalCombinationViews/EyebrowHeadlineBodyLinkModel.swift b/MVMCoreUI/Atomic/Molecules/VerticalCombinationViews/EyebrowHeadlineBodyLinkModel.swift index 2174514d..17aa4ea8 100644 --- a/MVMCoreUI/Atomic/Molecules/VerticalCombinationViews/EyebrowHeadlineBodyLinkModel.swift +++ b/MVMCoreUI/Atomic/Molecules/VerticalCombinationViews/EyebrowHeadlineBodyLinkModel.swift @@ -7,7 +7,7 @@ // -public class EyebrowHeadlineBodyLinkModel: MoleculeModelProtocol { +public class EyebrowHeadlineBodyLinkModel: MoleculeModelProtocol, ParentMoleculeModelProtocol { //-------------------------------------------------- // MARK: - Properties //-------------------------------------------------- @@ -20,6 +20,11 @@ public class EyebrowHeadlineBodyLinkModel: MoleculeModelProtocol { public var body: LabelModel? public var link: LinkModel? + public var children: [MoleculeModelProtocol] { + let molecules: [MoleculeModelProtocol?] = [eyebrow, headline, body, link] + return molecules.compactMap{ $0 } + } + //-------------------------------------------------- // MARK: - Initializer //-------------------------------------------------- diff --git a/MVMCoreUI/Atomic/Protocols/TemplateProtocol.swift b/MVMCoreUI/Atomic/Protocols/TemplateProtocol.swift index 6577b258..3aadd665 100644 --- a/MVMCoreUI/Atomic/Protocols/TemplateProtocol.swift +++ b/MVMCoreUI/Atomic/Protocols/TemplateProtocol.swift @@ -24,8 +24,8 @@ public extension TemplateProtocol where Self: ViewController { try decoder.add(delegateObject: delegateObjectIVar) templateModel = try decodeTemplate(using: decoder, from: data) model = templateModel as? MVMControllerModelProtocol - guard var model = model else { return } - model.traverseAndAdd(with: self) + guard let model = model else { return } + traverseAndAddRequiredBehaviors() var behaviorHandler = self behaviorHandler.createBehaviors(for: model, delegateObject: delegateObjectIVar) } @@ -33,4 +33,18 @@ public extension TemplateProtocol where Self: ViewController { func decodeTemplate(using decoder: JSONDecoder, from data: Data) throws -> TemplateModel { return try decoder.decode(TemplateModel.self, from: data) } + + /// Traverses all models and adds any required behavior models. + func traverseAndAddRequiredBehaviors() { + guard var model = model else { return } + let behaviorModels: [PageBehaviorModelProtocol] = model.rootMolecules.reduceDepthFirstTraverse(options: .childFirst, depth: 0, initialResult: []) { (accumulator, molecule, depth) in + if let behaviorRequirer = molecule as? PageBehaviorProtocolRequirer { + return accumulator + behaviorRequirer.getRequiredBehaviors() + } + return accumulator + } + for behavior in behaviorModels { + model.add(behavior: behavior) + } + } } diff --git a/MVMCoreUI/Behaviors/GetContactBehavior.swift b/MVMCoreUI/Behaviors/GetContactBehavior.swift index d92d2761..b83553db 100644 --- a/MVMCoreUI/Behaviors/GetContactBehavior.swift +++ b/MVMCoreUI/Behaviors/GetContactBehavior.swift @@ -46,34 +46,21 @@ public class PageGetContactBehavior: PageVisibilityBehavior { public required init(model: PageBehaviorModelProtocol, delegateObject: MVMCoreUIDelegateObject?) { self.delegate = delegateObject } - - public static func traverse(with page: PageProtocol?, closure: (MoleculeModelProtocol) -> Void) { - // Iterate models and provide contact - guard let template = page as? MoleculeListTemplate, - let models = template.templateModel?.molecules else { return } - for model in models { - if let model = model as? ListLeftVariableIconAllTextLinksModel, - let labelModel = model.eyebrowHeadlineBodyLink.headline { - closure(labelModel) - } else { - closure(model) - } - } - } public func onPageShown() { // Ask for permission CNContactStore().requestAccess(for: .contacts) { [weak self] (access, error) in guard access, - error == nil else { return } + error == nil, + let model = (self?.delegate?.moleculeDelegate as? PageProtocol)?.pageModel as? TemplateModelProtocol else { return } // Iterate models and provide contact let page = self?.delegate?.moleculeDelegate as? PageProtocol let store = CNContactStore() - PageGetContactBehavior.traverse(with: page) { (model) in - guard let model = model as? PageGetContactBehaviorConsumerProtocol, - let parameters = model.getMatchParameters(), + let consumers: [PageGetContactBehaviorConsumerProtocol] = model.rootMolecules.allMoleculesOfType() + for consumer in consumers { + guard let parameters = consumer.getMatchParameters(), let contacts = try? store.unifiedContacts(matching: parameters.0, keysToFetch: parameters.1) else { return } - model.consume(contacts: contacts) + consumer.consume(contacts: contacts) } // Tell template to update diff --git a/MVMCoreUI/Behaviors/PageBehaviorHandlerModelProtocol.swift b/MVMCoreUI/Behaviors/PageBehaviorHandlerModelProtocol.swift index d156d366..17f1b85e 100644 --- a/MVMCoreUI/Behaviors/PageBehaviorHandlerModelProtocol.swift +++ b/MVMCoreUI/Behaviors/PageBehaviorHandlerModelProtocol.swift @@ -21,16 +21,5 @@ public extension PageBehaviorHandlerModelProtocol { newBehaviors.append(behavior) self.behaviors = newBehaviors } - - /// Traverses all models and adds any required behavior models. - mutating func traverseAndAdd(with page: PageProtocol?) { - PageGetContactBehavior.traverse(with: page, closure: { (model) in - guard let behaviorRequirer = model as? PageBehaviorProtocolRequirer, - var pageModel = page?.pageModel as? PageBehaviorHandlerModelProtocol else { return } - for behavior in behaviorRequirer.getRequiredBehaviors() { - pageModel.add(behavior: behavior) - } - }) - } }