mvm_core_ui/MVMCoreUI/Molecules/StandardHeaderView.swift
Pfeil, Scott Robert 161f3e9f08 reset function.
standard spacing for cell molecule
MFTextField to view contstraining view
constraining protocol
2019-06-04 12:42:52 -04:00

151 lines
5.9 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 = Label.commonLabelH2(true)
let messageLabel = Label.commonLabelB2(true)
var separatorView: SeparatorView?
var spaceBetweenLabels: NSLayoutConstraint?
var leftConstraintTitle: NSLayoutConstraint?
var rightConstraintTitle: NSLayoutConstraint?
var leftConstraintMessage: NSLayoutConstraint?
var rightConstraintMessage: NSLayoutConstraint?
private var heightConstraint: NSLayoutConstraint?
// MARK: - MVMCoreViewProtocol
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: PaddingDefaultVerticalSpacing)
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
}
}
}
// MARK: - Constraining
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
}
open override func reset() {
backgroundColor = .clear
headlineLabel.styleH2(true)
messageLabel.styleB2(true)
separatorView?.setAsHeavy()
separatorView?.show()
}
// MARK: - MVMCoreUIMoleculeViewProtocol
open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
super.setWithJSON(json, delegateObject: delegateObject, 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, delegateObject: delegateObject, additionalData: additionalData)
let bodyJSON = json?.optionalDictionaryForKey("body")
messageLabel.setWithJSON(bodyJSON, delegateObject: delegateObject, additionalData: additionalData)
if let separatorJSON = json?.optionalDictionaryForKey("separator") {
separatorView?.setWithJSON(separatorJSON, delegateObject: delegateObject, additionalData: additionalData)
}
if separatorView?.isHidden ?? true {
bottomPin?.constant = 0
} else {
bottomPin?.constant = PaddingDefaultVerticalSpacing
}
}
}