further ehancement and reach
This commit is contained in:
parent
790a19b34f
commit
0c0fff22bb
@ -9,7 +9,7 @@
|
||||
import UIKit
|
||||
|
||||
|
||||
public class TwoButtonViewModel: MoleculeModelProtocol {
|
||||
public class TwoButtonViewModel: ParentMoleculeModelProtocol {
|
||||
//--------------------------------------------------
|
||||
// MARK: - Properties
|
||||
//--------------------------------------------------
|
||||
@ -19,6 +19,10 @@ public class TwoButtonViewModel: MoleculeModelProtocol {
|
||||
public var primaryButton: ButtonModel?
|
||||
public var secondaryButton: ButtonModel?
|
||||
|
||||
public var children: [MoleculeModelProtocol] {
|
||||
return [primaryButton, secondaryButton].compactMap { $0 }
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - Keys
|
||||
//--------------------------------------------------
|
||||
|
||||
@ -9,7 +9,7 @@
|
||||
import Foundation
|
||||
|
||||
|
||||
@objcMembers open class HeadlineBodyModel: MoleculeModelProtocol {
|
||||
@objcMembers open class HeadlineBodyModel: ParentMoleculeModelProtocol {
|
||||
//--------------------------------------------------
|
||||
// MARK: - Properties
|
||||
//--------------------------------------------------
|
||||
@ -21,6 +21,10 @@ import Foundation
|
||||
public var style: Style?
|
||||
public var backgroundColor: Color?
|
||||
|
||||
public var children: [MoleculeModelProtocol] {
|
||||
return [headline, body].compactMap { $0 }
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - Enum
|
||||
//--------------------------------------------------
|
||||
|
||||
@ -9,7 +9,7 @@
|
||||
import UIKit
|
||||
|
||||
|
||||
@objcMembers public class CarouselModel: MoleculeModelProtocol, FormFieldProtocol {
|
||||
@objcMembers public class CarouselModel: ParentMoleculeModelProtocol, FormFieldProtocol {
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - Properties
|
||||
@ -144,3 +144,11 @@ import UIKit
|
||||
try container.encode(selectedIndex, forKey: .selectedIndex)
|
||||
}
|
||||
}
|
||||
|
||||
extension CarouselModel {
|
||||
|
||||
public var children: [MoleculeModelProtocol] {
|
||||
return molecules
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -23,28 +23,29 @@ public extension MoleculeModelProtocol {
|
||||
|
||||
public extension MoleculeModelProtocol {
|
||||
|
||||
func reduceDepthFirstTraverse<Result>(initialResult:Result, nextPartialResult: (Result, MoleculeModelProtocol)->Result) -> Result {
|
||||
|
||||
// Base case. No additional children to traverse.
|
||||
func reduceDepthFirstTraverse<Result>(options: TreeTraversalOptions, initialResult: Result, nextPartialResult: (Result, MoleculeModelProtocol)->Result) -> Result {
|
||||
return nextPartialResult(initialResult, self)
|
||||
}
|
||||
|
||||
func depthFirstTraverse(callback: (MoleculeModelProtocol)->Void) {
|
||||
callback(self)
|
||||
// Base case. No additional children to traverse.
|
||||
func depthFirstTraverse(options: TreeTraversalOptions, onVisit: (MoleculeModelProtocol)->Void) {
|
||||
onVisit(self)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public extension Array where Element == MoleculeModelProtocol {
|
||||
|
||||
func reduceDepthFirstTraverse<Result>(initialResult:Result, nextPartialResult: (Result, MoleculeModelProtocol)->Result) -> Result {
|
||||
func reduceDepthFirstTraverse<Result>(options: TreeTraversalOptions, initialResult: Result, nextPartialResult: (Result, MoleculeModelProtocol)->Result) -> Result {
|
||||
return reduce(initialResult) { (result, molecule) -> Result in
|
||||
return molecule.reduceDepthFirstTraverse(initialResult: result, nextPartialResult: nextPartialResult)
|
||||
return molecule.reduceDepthFirstTraverse(options: options, initialResult: result, nextPartialResult: nextPartialResult)
|
||||
}
|
||||
}
|
||||
|
||||
func depthFirstTraverse(callback: (MoleculeModelProtocol)->Void) {
|
||||
func depthFirstTraverse(options: TreeTraversalOptions, callback: (MoleculeModelProtocol)->Void) {
|
||||
forEach { (molecule) in
|
||||
molecule.depthFirstTraverse(callback: callback)
|
||||
molecule.depthFirstTraverse(options: options, onVisit: callback)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -16,24 +16,39 @@ public protocol ParentMoleculeModelProtocol: MoleculeModelProtocol {
|
||||
|
||||
public extension ParentMoleculeModelProtocol {
|
||||
|
||||
func reduceDepthFirstTraverse<Result>(initialResult: Result, nextPartialResult: (Result, MoleculeModelProtocol) -> Result) -> Result {
|
||||
|
||||
let result = children.reduce(initialResult) { (result, molecule) -> Result in
|
||||
if let additionalParent = molecule as? ParentMoleculeModelProtocol {
|
||||
return additionalParent.reduceDepthFirstTraverse(initialResult: result, nextPartialResult: nextPartialResult)
|
||||
}
|
||||
return molecule.reduceDepthFirstTraverse(initialResult: result, nextPartialResult: nextPartialResult)
|
||||
func reduceDepthFirstTraverse<Result>(options: TreeTraversalOptions, initialResult: Result, nextPartialResult: (Result, MoleculeModelProtocol) -> Result) -> Result {
|
||||
var result = initialResult
|
||||
if (options == .parentFirst) {
|
||||
result = nextPartialResult(result, self)
|
||||
}
|
||||
return nextPartialResult(result, self)
|
||||
result = children.reduce(result) { (result, molecule) -> Result in
|
||||
if let additionalParent = molecule as? ParentMoleculeModelProtocol {
|
||||
// Safety net to make sure the ParentMoleculeModelProtocol's method extension is called over the base MoleculeModelProtocol.
|
||||
return additionalParent.reduceDepthFirstTraverse(options: options, initialResult: result, nextPartialResult: nextPartialResult)
|
||||
}
|
||||
return molecule.reduceDepthFirstTraverse(options: options, initialResult: result, nextPartialResult: nextPartialResult)
|
||||
}
|
||||
if (options == .childFirst) {
|
||||
result = nextPartialResult(result, self)
|
||||
}
|
||||
// if options == .leafOnly don't call on self.
|
||||
return result
|
||||
}
|
||||
|
||||
func depthFirstTraverse(callback: (MoleculeModelProtocol)->Void) {
|
||||
func depthFirstTraverse(options: TreeTraversalOptions, onVisit: (MoleculeModelProtocol)->Void) {
|
||||
if (options == .parentFirst) {
|
||||
onVisit(self)
|
||||
}
|
||||
children.forEach { (molecule) in
|
||||
if let additionalParent = molecule as? ParentMoleculeModelProtocol {
|
||||
additionalParent.depthFirstTraverse(callback: callback)
|
||||
// Safety net to make sure the ParentMoleculeModelProtocol's method extension is called over the base MoleculeModelProtocol.
|
||||
additionalParent.depthFirstTraverse(options: options, onVisit: onVisit)
|
||||
}
|
||||
molecule.depthFirstTraverse(callback: callback)
|
||||
molecule.depthFirstTraverse(options: options, onVisit: onVisit)
|
||||
}
|
||||
callback(self)
|
||||
if (options == .childFirst) {
|
||||
onVisit(self)
|
||||
}
|
||||
// if options == .leafOnly don't call on self.
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,13 +7,19 @@
|
||||
// Copyright © 2021 Verizon Wireless. All rights reserved.
|
||||
//
|
||||
|
||||
public enum TreeTraversalOptions {
|
||||
case parentFirst
|
||||
case childFirst
|
||||
case leafNodesOnly
|
||||
}
|
||||
|
||||
public protocol MoleculeTreeTraversalProtocol {
|
||||
|
||||
|
||||
// Future options -- Parent first depth first, leaves only.
|
||||
|
||||
func reduceDepthFirstTraverse<Result>(initialResult:Result, nextPartialResult: (Result, MoleculeModelProtocol)->Result) -> Result
|
||||
func reduceDepthFirstTraverse<Result>(options: TreeTraversalOptions, initialResult: Result, nextPartialResult: (Result, MoleculeModelProtocol)->Result) -> Result
|
||||
|
||||
func depthFirstTraverse(callback: (MoleculeModelProtocol)->Void)
|
||||
func depthFirstTraverse(options: TreeTraversalOptions, onVisit: (MoleculeModelProtocol)->Void)
|
||||
|
||||
//func breadthFirstTraverse()
|
||||
}
|
||||
@ -24,20 +30,20 @@ public protocol MoleculeTreeTraversalProtocol {
|
||||
|
||||
extension MoleculeTreeTraversalProtocol {
|
||||
|
||||
func countMolecules() -> Int {
|
||||
return reduceDepthFirstTraverse(initialResult: 0) { (accumulator, molecule) in
|
||||
func countMolecules(options: TreeTraversalOptions = .parentFirst) -> Int {
|
||||
return reduceDepthFirstTraverse(options: options, initialResult: 0) { (accumulator, molecule) in
|
||||
return accumulator + 1
|
||||
}
|
||||
}
|
||||
|
||||
func printMolecules() {
|
||||
depthFirstTraverse { (molecule) in
|
||||
print(molecule)
|
||||
func printMolecules(options: TreeTraversalOptions = .parentFirst) {
|
||||
depthFirstTraverse(options: options) { (molecule) in
|
||||
print("\"\(molecule.moleculeName)\" [\(molecule)]")
|
||||
}
|
||||
}
|
||||
|
||||
func allMoleculesOfType<T>() -> [T] {
|
||||
return reduceDepthFirstTraverse(initialResult: []) { (accumulator, molecule) in
|
||||
func allMoleculesOfType<T>(options: TreeTraversalOptions = .parentFirst) -> [T] {
|
||||
return reduceDepthFirstTraverse(options: options, initialResult: []) { (accumulator, molecule) in
|
||||
if let typedMolecule = molecule as? T {
|
||||
return accumulator + [typedMolecule]
|
||||
}
|
||||
|
||||
@ -16,14 +16,7 @@ import Foundation
|
||||
public var footer: MoleculeModelProtocol?
|
||||
|
||||
public override var rootMolecules: [MoleculeModelProtocol] {
|
||||
var rootMolecules:[MoleculeModelProtocol] = []
|
||||
if let header = header {
|
||||
rootMolecules.append(header)
|
||||
}
|
||||
if let footer = footer {
|
||||
rootMolecules.append(footer)
|
||||
}
|
||||
return rootMolecules
|
||||
return [header, footer].compactMap { $0 }
|
||||
}
|
||||
|
||||
public override init(pageType: String) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user