From 0c11ded57778570e316f981f16a4ac4f4361d567 Mon Sep 17 00:00:00 2001 From: Kevin G Christiano Date: Fri, 21 Feb 2020 14:41:53 -0500 Subject: [PATCH] rolled back change. --- MVMCoreUI/Organisms/Stack.swift | 35 ++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/MVMCoreUI/Organisms/Stack.swift b/MVMCoreUI/Organisms/Stack.swift index 1f2fa288..67ff0110 100644 --- a/MVMCoreUI/Organisms/Stack.swift +++ b/MVMCoreUI/Organisms/Stack.swift @@ -9,13 +9,20 @@ import Foundation open class Stack: Container where T: StackModelProtocol { + //-------------------------------------------------- + // MARK: - Properties + //-------------------------------------------------- + var contentView: UIView = MVMCoreUICommonViewsUtility.commonView() var stackModel: T? { get { return model as? T } } var stackItems: [UIView] = [] + //-------------------------------------------------- // MARK: - Helpers + //-------------------------------------------------- + public func pinView(_ view: UIView, toView: UIView, attribute: NSLayoutConstraint.Attribute, relation: NSLayoutConstraint.Relation, priority: UILayoutPriority, constant: CGFloat) { let constraint = NSLayoutConstraint(item: view, attribute: attribute, relatedBy: relation, toItem: toView, attribute: attribute, multiplier: 1.0, constant: constant) constraint.priority = priority @@ -45,8 +52,11 @@ open class Stack: Container where T: StackModelProtocol { item.removeFromSuperview() } } + + //-------------------------------------------------- + // MARK: - Initializers + //-------------------------------------------------- - // MARK: - Inits public override init(frame: CGRect) { super.init(frame: frame) } @@ -60,7 +70,10 @@ open class Stack: Container where T: StackModelProtocol { fatalError("init(coder:) has not been implemented") } + //-------------------------------------------------- // MARK: - MFViewProtocol + //-------------------------------------------------- + public override func setupView() { super.setupView() guard contentView.superview == nil else { return } @@ -80,7 +93,10 @@ open class Stack: Container where T: StackModelProtocol { } } + //-------------------------------------------------- // MARK: - MVMCoreUIMoleculeViewProtocol + //-------------------------------------------------- + public override func reset() { super.reset() backgroundColor = .clear @@ -157,8 +173,10 @@ open class Stack: Container where T: StackModelProtocol { return modules.count > 0 ? modules : nil } + //-------------------------------------------------- // MARK: - Subclassables - + //-------------------------------------------------- + /// Can be subclassed to create views when we get stack item models and have no views yet func createStackItemsFromModel(_ model: MoleculeModelProtocol?, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) { } @@ -170,7 +188,10 @@ open class Stack: Container where T: StackModelProtocol { } } + //-------------------------------------------------- // MARK: - Adding to stack + //-------------------------------------------------- + /// Gets the percent modifier. This value is used to help properly calculate percent for stack items when spacing is involved. private func getTotalSpace() -> CGFloat { guard let stackModel = stackModel else { return 0.0 } @@ -213,13 +234,15 @@ open class Stack: Container where T: StackModelProtocol { if stackModel.axis == .vertical { if first { pinView(view, toView: contentView, attribute: .top, relation: .equal, priority: .required, constant: stackModel.useStackSpacingBeforeFirstItem ? spacing : model.spacing ?? 0) - } else if let previousView = stackItems.last(where: { item in !model.gone }) { + } else if let previousView = stackItems.last(where: { item in + return !model.gone + }) { view.topAnchor.constraint(equalTo: previousView.bottomAnchor, constant: spacing).isActive = true } pinView(view, toView: contentView, attribute: .leading, relation: .equal, priority: .required, constant: 0) pinView(contentView, toView: view, attribute: .trailing, relation: .equal, priority: .required, constant: 0) if let percent = model.percent { - let multiplier = CGFloat(percent)/100.0 + let multiplier = CGFloat(percent) / 100.0 let constant = multiplier * totalSpacing view.heightAnchor.constraint(equalTo: contentView.heightAnchor, multiplier: multiplier, constant: -constant).isActive = true } @@ -230,7 +253,9 @@ open class Stack: Container where T: StackModelProtocol { if first { // First horizontal item has no spacing by default unless told otherwise. pinView(view, toView: contentView, attribute: .leading, relation: .equal, priority: .required, constant: stackModel.useStackSpacingBeforeFirstItem ? spacing : model.spacing ?? 0) - } else if let previousView = stackItems.last(where: { item in !model.gone }) { + } else if let previousView = stackItems.last(where: { item in + return !model.gone + }) { view.leftAnchor.constraint(equalTo: previousView.rightAnchor, constant: spacing).isActive = true } pinView(view, toView: contentView, attribute: .top, relation: .equal, priority: .required, constant: 0)