128 lines
4.1 KiB
Swift
128 lines
4.1 KiB
Swift
//
|
|
// ButtonView.swift
|
|
// MVMCoreUI
|
|
//
|
|
// Created by Scott Pfeil on 3/21/19.
|
|
// Copyright © 2019 Verizon Wireless. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
@objcMembers open class ButtonView: ViewConstrainingView {
|
|
open var primaryButton: PrimaryButton? = PrimaryButton.button()
|
|
open var alignCenterPin: NSLayoutConstraint?
|
|
open var alignCenterLeftPin: NSLayoutConstraint?
|
|
open var alignCenterRightPin: NSLayoutConstraint?
|
|
|
|
// MARK: - Inits
|
|
public init() {
|
|
super.init(frame: .zero)
|
|
}
|
|
|
|
public override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
}
|
|
|
|
required public init?(coder aDecoder: NSCoder) {
|
|
super.init(coder: aDecoder)
|
|
}
|
|
|
|
public convenience init(buttonSmall small: Bool, enabled: Bool) {
|
|
self.init()
|
|
primaryButton?.setAsSmall(small)
|
|
primaryButton?.isEnabled = enabled
|
|
}
|
|
|
|
public init(withJSON json: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, actionDelegate: NSObject?, buttonDelegate: Any?) {
|
|
super.init(frame: .zero)
|
|
setWithJSON(json, delegate: actionDelegate, additionalData: additionalData)
|
|
}
|
|
|
|
// MARK: - MVMCoreViewProtocol
|
|
open override func updateView(_ size: CGFloat) {
|
|
super.updateView(size)
|
|
MVMCoreDispatchUtility.performBlock(onMainThread: {
|
|
self.primaryButton?.updateView(size)
|
|
})
|
|
}
|
|
|
|
open override func setupView() {
|
|
super.setupView()
|
|
setupButton()
|
|
alignCenter()
|
|
}
|
|
|
|
// MARK: - MVMCoreUIMoleculeViewProtocol
|
|
open override func setAsMolecule() {
|
|
super.setAsMolecule()
|
|
primaryButton?.setAsMolecule()
|
|
}
|
|
|
|
open override func setWithJSON(_ json: [AnyHashable : Any]?, delegate: NSObject?, additionalData: [AnyHashable : Any]?) {
|
|
super.setWithJSON(json, delegate: delegate, additionalData: additionalData)
|
|
primaryButton?.setWithJSON(json, delegate: delegate, additionalData: additionalData)
|
|
}
|
|
|
|
// MARK: - Constraining
|
|
func setupButton() {
|
|
if let primaryButton = primaryButton, !subviews.contains(primaryButton) {
|
|
addSubview(primaryButton)
|
|
setupConstraints(forView: primaryButton)
|
|
}
|
|
}
|
|
|
|
func setupConstraints(forView view: UIView) {
|
|
leftPin = view.leftAnchor.constraint(equalTo: leftAnchor)
|
|
topPin = view.topAnchor.constraint(equalTo: topAnchor)
|
|
rightPin = rightAnchor.constraint(equalTo: view.rightAnchor)
|
|
bottomPin = bottomAnchor.constraint(equalTo: view.bottomAnchor)
|
|
leftPin?.isActive = true
|
|
topPin?.isActive = true
|
|
rightPin?.isActive = true
|
|
bottomPin?.isActive = true
|
|
|
|
alignCenterPin = view.centerXAnchor.constraint(equalTo: centerXAnchor)
|
|
alignCenterLeftPin = view.leftAnchor.constraint(greaterThanOrEqualTo: leftAnchor)
|
|
alignCenterRightPin = rightAnchor.constraint(greaterThanOrEqualTo: view.rightAnchor)
|
|
}
|
|
|
|
open func alignLeft() {
|
|
alignCenterPin?.isActive = false
|
|
alignCenterLeftPin?.isActive = false
|
|
alignCenterRightPin?.isActive = true
|
|
leftPin?.isActive = true
|
|
rightPin?.isActive = false
|
|
}
|
|
|
|
open func alignCenter() {
|
|
alignCenterPin?.isActive = true
|
|
alignCenterLeftPin?.isActive = true
|
|
alignCenterRightPin?.isActive = true
|
|
leftPin?.isActive = false
|
|
rightPin?.isActive = false
|
|
}
|
|
|
|
open func alignRight() {
|
|
alignCenterPin?.isActive = false
|
|
alignCenterLeftPin?.isActive = true
|
|
alignCenterRightPin?.isActive = false
|
|
leftPin?.isActive = false
|
|
rightPin?.isActive = true
|
|
}
|
|
|
|
open override func setLeftPinConstant(_ constant: CGFloat) {
|
|
super.setLeftPinConstant(constant)
|
|
alignCenterLeftPin?.constant = constant
|
|
}
|
|
|
|
open override func setRightPinConstant(_ constant: CGFloat) {
|
|
super.setRightPinConstant(constant)
|
|
alignCenterRightPin?.constant = constant
|
|
}
|
|
|
|
open override func resetConstraints() {
|
|
super.resetConstraints()
|
|
primaryButton?.isEnabled = false
|
|
}
|
|
}
|