Simpler stack logic

This commit is contained in:
Pfeil, Scott Robert 2020-04-18 18:08:13 -04:00
parent c89929648f
commit 4d7740854d

View File

@ -14,22 +14,24 @@ import Foundation
// MARK: - Outlets
//-----------------------------------------------------
public let eyebrowHeadlineBodyLink = EyebrowHeadlineBodyLink(frame: .zero)
public let rightLabel = Label.commonLabelB2(true)
public let arrow = Arrow(frame: .zero)
public let arrowAndLabel2Stack: Stack<StackModel>
let view = MVMCoreUICommonViewsUtility.commonView()
public var alignmentConstraint: NSLayoutConstraint?
public let rightLabel = Label.commonLabelB2(true)
private let stack: Stack<StackModel>
private let arrowStackItem: StackItem
private let rightLabelStackItem: StackItem
//-----------------------------------------------------
// MARK: - Initializers
//-----------------------------------------------------
public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
arrowAndLabel2Stack = Stack<StackModel>.createStack(with: [(view: arrow, model: StackItemModel(horizontalAlignment: .fill, verticalAlignment: .center)),
(view: rightLabel, model: StackItemModel(horizontalAlignment: .fill, verticalAlignment: .center))],
axis: .horizontal, spacing: 8)
rightLabel.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 900), for: .horizontal)
rightLabel.setContentHuggingPriority(UILayoutPriority(rawValue: 900), for: .horizontal)
rightLabel.numberOfLines = 1
let stackModel = StackModel(molecules: [StackItemModel(horizontalAlignment: .leading),
StackItemModel(horizontalAlignment: .fill),
StackItemModel(spacing: 8, horizontalAlignment: .fill)],
axis: .horizontal)
arrowStackItem = StackItem(andContain: arrow)
rightLabelStackItem = StackItem(andContain: rightLabel)
let stackItems = [StackItem(andContain: eyebrowHeadlineBodyLink), arrowStackItem, rightLabelStackItem]
stack = Stack<StackModel>(with: stackModel, stackItems: stackItems)
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
@ -44,32 +46,24 @@ import Foundation
public override func alignAccessoryToHero() {
super.alignAccessoryToHero()
// Aligns the center of the right side to the headline.
// Aligns the center of the right side items to the headline.
if let heroCenter = heroAccessoryCenter {
let convertedPoint = view.convert(heroCenter, from: self)
alignmentConstraint?.constant = convertedPoint.y - view.bounds.midY
let convertedPoint = stack.convert(heroCenter, from: self)
arrowStackItem.containerHelper.alignCenterVerticalConstraint?.constant = convertedPoint.y - stack.bounds.midY
rightLabelStackItem.containerHelper.alignCenterVerticalConstraint?.constant = convertedPoint.y - stack.bounds.midY
}
}
override open func setupView() {
super.setupView()
arrow.pinHeightAndWidth()
arrowAndLabel2Stack.restack()
view.addSubview(eyebrowHeadlineBodyLink)
view.addSubview(arrowAndLabel2Stack)
contentView.addSubview(view)
containerHelper.constrainView(view)
rightLabel.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 900), for: .horizontal)
rightLabel.setContentHuggingPriority(UILayoutPriority(rawValue: 900), for: .horizontal)
rightLabel.numberOfLines = 1
NSLayoutConstraint.pinViews(leftView: eyebrowHeadlineBodyLink, rightView: arrowAndLabel2Stack, alignTop: false)
alignmentConstraint = view.constraintsAffectingLayout(for: .vertical).first { (layout) -> Bool in
return layout.firstAnchor == arrowAndLabel2Stack.centerYAnchor && layout.firstAttribute == .centerY
}
}
public override func updateView(_ size: CGFloat) {
super.updateView(size)
eyebrowHeadlineBodyLink.updateView(size)
arrowAndLabel2Stack.updateView(size)
arrow.pinHeightAndWidth()
addMolecule(stack)
stack.restack()
}
open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable : Any]?) {
@ -83,12 +77,5 @@ import Foundation
open override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
return 121
}
open override func reset() {
super.reset()
eyebrowHeadlineBodyLink.reset()
arrowAndLabel2Stack.reset()
rightLabel.styleB2(true)
}
}