mvm_core_ui/MVMCoreUI/Molecules/MoleculeStackView.swift
2019-03-26 13:51:33 -04:00

82 lines
3.1 KiB
Swift

//
// 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]?
public override init(frame: CGRect) {
super.init(frame: frame)
}
public init(withJSON json: [AnyHashable : Any]?, delegate: NSObject?, additionalData: [AnyHashable : Any]?) {
super.init(frame: CGRect.zero)
setWithJSON(json, delegate: delegate, additionalData: additionalData)
}
public convenience init(withJSON json: [AnyHashable : Any]?, delegate: NSObject?, spacingBlock: ((Any) -> UIEdgeInsets)?) {
self.init(withJSON: json, delegate: delegate, 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)
}
}
}
public override func setWithJSON(_ json: [AnyHashable : Any]?, delegate: NSObject?, additionalData: [AnyHashable : Any]?) {
super.setWithJSON(json, delegate: delegate, 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, delegate: delegate, additionalData: additionalData)
moleculesArray.append(molecule)
}
}
guard moleculesArray.count > 0 else {
return
}
if let spacingBlock = spacingBlock {
MVMCoreUIStackableViewController.populateView(self, withUIArray: moleculesArray, withSpacingBlock: spacingBlock)
} else {
MVMCoreUIStackableViewController.populateView(self, withUIArray: moleculesArray) { (object) -> UIEdgeInsets in
if object as AnyObject? === moleculesArray.first {
return UIEdgeInsets.zero
} else {
return UIEdgeInsets.init(top: PaddingTwo, left: 0, bottom: 0, right: 0)
}
}
}
}
}