enable parents, for testing.
Switch to KyleTraverse (tm)
This commit is contained in:
parent
7ccc5bcecf
commit
5a0399bad9
@ -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
|
||||
//--------------------------------------------------
|
||||
|
||||
@ -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
|
||||
//--------------------------------------------------
|
||||
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user