rolled back change.

This commit is contained in:
Kevin G Christiano 2020-02-21 14:41:53 -05:00
parent f7058e0a3f
commit 0c11ded577

View File

@ -9,13 +9,20 @@
import Foundation
open class Stack<T>: 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<T>: 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<T>: 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<T>: Container where T: StackModelProtocol {
}
}
//--------------------------------------------------
// MARK: - MVMCoreUIMoleculeViewProtocol
//--------------------------------------------------
public override func reset() {
super.reset()
backgroundColor = .clear
@ -157,8 +173,10 @@ open class Stack<T>: 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<T>: 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<T>: 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<T>: 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)