mvm_core_ui/MVMCoreUI/Molecules/StandardHeaderView.swift
Pfeil, Scott Robert eabb0c0384 file name fixes
2019-03-11 14:08:28 -04:00

139 lines
5.5 KiB
Swift

//
// StandardHeaderView.swift
// MVMCoreUI
//
// Created by Scott Pfeil on 2/12/19.
// Copyright © 2019 Verizon Wireless. All rights reserved.
//
import UIKit
public class StandardHeaderView: ViewConstrainingView {
let headlineLabel = MFLabel.commonLabelH2(true)
let messageLabel = MFLabel.commonLabelB2(true)
var separatorView: SeparatorView?
var spaceBetweenLabels: NSLayoutConstraint?
var leftConstraintTitle: NSLayoutConstraint?
var rightConstraintTitle: NSLayoutConstraint?
var leftConstraintMessage: NSLayoutConstraint?
var rightConstraintMessage: NSLayoutConstraint?
private var heightConstraint: NSLayoutConstraint?
public override func updateView(_ size: CGFloat) {
super.updateView(size)
headlineLabel.updateView(size)
messageLabel.updateView(size)
separatorView?.updateView(size)
setSpacing()
}
public override func setupView() {
super.setupView()
if separatorView == nil {
translatesAutoresizingMaskIntoConstraints = false
backgroundColor = .clear
clipsToBounds = true
addSubview(headlineLabel)
addSubview(messageLabel)
headlineLabel.setContentHuggingPriority(UILayoutPriority.required, for: NSLayoutConstraint.Axis.vertical)
messageLabel.setContentHuggingPriority(UILayoutPriority.required, for: NSLayoutConstraint.Axis.vertical)
setContentHuggingPriority(UILayoutPriority.required, for: NSLayoutConstraint.Axis.vertical)
topPin = headlineLabel.topAnchor.constraint(equalTo: topAnchor, constant: PaddingFive)
topPin?.isActive = true
spaceBetweenLabels = messageLabel.topAnchor.constraint(equalTo: headlineLabel.bottomAnchor, constant: PaddingTwo)
spaceBetweenLabels?.isActive = true
leftConstraintTitle = headlineLabel.leftAnchor.constraint(equalTo: leftAnchor)
leftConstraintTitle?.isActive = true
rightConstraintTitle = rightAnchor.constraint(equalTo: headlineLabel.rightAnchor)
rightConstraintTitle?.isActive = true
leftConstraintMessage = messageLabel.leftAnchor.constraint(equalTo: leftAnchor)
leftConstraintMessage?.isActive = true
rightConstraintMessage = rightAnchor.constraint(equalTo: messageLabel.rightAnchor)
rightConstraintMessage?.isActive = true
bottomPin = bottomAnchor.constraint(equalTo: messageLabel.bottomAnchor, constant: PaddingDefaultVerticalSpacing)
bottomPin?.isActive = true
heightConstraint = heightAnchor.constraint(equalToConstant: 0)
heightConstraint?.priority = UILayoutPriority(rawValue: 950)
if let separatorView = SeparatorView.separatorAdd(to: self, position: SeparatorPositionBot, withHorizontalPadding: 0) {
separatorView.setAsHeavy()
separatorView.isHidden = true
addSubview(separatorView)
self.separatorView = separatorView
}
}
}
public func setSpacing() {
if headlineLabel.hasText() && messageLabel.hasText() {
spaceBetweenLabels?.constant = PaddingTwo
show()
} else if headlineLabel.hasText() || messageLabel.hasText() {
spaceBetweenLabels?.constant = 0
show()
} else {
hide()
}
}
public override func show() {
super.show()
heightConstraint?.isActive = false
layoutIfNeeded()
}
public override func hide() {
super.hide()
heightConstraint?.isActive = true
layoutIfNeeded()
}
public override func setLeftPinConstant(_ constant: CGFloat) {
leftConstraintTitle?.constant = constant
leftConstraintMessage?.constant = constant
separatorView?.leftPin?.constant = constant
}
public override func setRightPinConstant(_ constant: CGFloat) {
rightConstraintTitle?.constant = constant
rightConstraintMessage?.constant = constant
separatorView?.rightPin?.constant = constant
}
public override func setWithJSON(_ json: [AnyHashable : Any]?, delegate: NSObject?, additionalData: [AnyHashable : Any]?) {
super.setWithJSON(json, delegate: delegate, additionalData: additionalData)
if let colorString = json?.optionalStringForKey(KeyBackgroundColor) {
backgroundColor = .mfGet(forHex: colorString)
}
if let colorString = json?.optionalStringForKey("contentColor") {
let color = UIColor.mfGet(forHex: colorString)
headlineLabel.textColor = color
messageLabel.textColor = color
separatorView?.backgroundColor = color
}
let headlineJSON = json?.optionalDictionaryForKey("headline")
headlineLabel.setWithJSON(headlineJSON, delegate: delegate, additionalData: additionalData)
let bodyJSON = json?.optionalDictionaryForKey("body")
messageLabel.setWithJSON(bodyJSON, delegate: delegate, additionalData: additionalData)
let separatorJSON = json?.optionalDictionaryForKey("separator")
separatorView?.setWithJSON(separatorJSON, delegate: delegate, additionalData: additionalData)
if separatorView?.isHidden ?? true {
bottomPin?.constant = 0
} else {
bottomPin?.constant = PaddingDefaultVerticalSpacing
}
}
}