mvm_core_ui/MVMCoreUI/Molecules/MoleculeStackView.swift
Pfeil, Scott Robert b00b43eab0 Helper functions
Molecule List.
Back to view instead of margins for stack due to background color issues.
Standard Header View to margin for internal use.
2019-04-22 13:14:22 -04:00

80 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]?
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 {
let separation = json?.optionalCGFloatForKey("separation") ?? PaddingDefault
MVMCoreUIStackableViewController.populateView(self, withUIArray: moleculesArray, useMargins: useMargins) { (object) -> UIEdgeInsets in
return UIEdgeInsets.init(top: separation, left: 0, bottom: 0, right: 0)
}
}
}
}