// // MoleculeStackView.swift // MVMCoreUI // // Created by Scott Pfeil on 2/11/19. // Copyright © 2019 Verizon Wireless. All rights reserved. // import UIKit public class MoleculeStackView: MFView { var spacingBlock: ((Any) -> UIEdgeInsets)? var moleculesArray: [UIView]? var useMargins: Bool = false public override init(frame: CGRect) { super.init(frame: frame) } public init(withJSON json: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable : Any]?) { super.init(frame: CGRect.zero) setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData) } public convenience init(withJSON json: [AnyHashable: Any]?, delegateObject: DelegateObject?, spacingBlock: ((Any) -> UIEdgeInsets)?) { self.init(withJSON: json, delegateObject: delegateObject, additionalData: nil) self.spacingBlock = spacingBlock } public required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } public override func setupView() { super.setupView() translatesAutoresizingMaskIntoConstraints = false backgroundColor = .clear self.setContentHuggingPriority(UILayoutPriority.required, for: NSLayoutConstraint.Axis.vertical) self.setContentCompressionResistancePriority(UILayoutPriority.required, for: NSLayoutConstraint.Axis.vertical) } public override func updateView(_ size: CGFloat) { super.updateView(size) for view in subviews { if let mvmView = view as? MVMCoreViewProtocol { mvmView.updateView(size) } } } open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) { super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData) guard let molecules = json?.arrayForKey("molecules") as? [[String: Any]] else { return } // Create the molecules and set the json. var moleculesArray = [] as [UIView] for moleculeJSON in molecules { if let name = moleculeJSON.optionalStringForKey("moleculeName"), let molecule = MVMCoreUIMoleculeMappingObject.shared()?.getMoleculeForName(name) { molecule.setWithJSON(moleculeJSON, delegateObject: delegateObject, additionalData: additionalData) moleculesArray.append(molecule) } } guard moleculesArray.count > 0 else { return } if let spacingBlock = spacingBlock { MVMCoreUIStackableViewController.populateView(self, withUIArray: moleculesArray, useMargins: useMargins, withSpacingBlock: spacingBlock) } else { MVMCoreUIStackableViewController.populateView(self, withUIArray: moleculesArray, useMargins: useMargins) { (object) -> UIEdgeInsets in if object as AnyObject? === moleculesArray.first { return UIEdgeInsets.zero } else { return UIEdgeInsets.init(top: PaddingTwo, left: 0, bottom: 0, right: 0) } } } } }