refactored to use buttongroup
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
fe580d12e4
commit
170da5f477
@ -9,144 +9,70 @@
|
||||
import UIKit
|
||||
import VDS
|
||||
|
||||
@objcMembers open class TwoButtonView: View, MVMCoreUIViewConstrainingProtocol {
|
||||
@objcMembers open class TwoButtonView: VDS.View, VDSMoleculeViewProtocol {
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - Properties
|
||||
//--------------------------------------------------
|
||||
|
||||
open var primaryButton: PillButton = PillButton()
|
||||
open var secondaryButton: PillButton = PillButton()
|
||||
private var stack = UIStackView()
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - Constraints
|
||||
//--------------------------------------------------
|
||||
|
||||
private var equalWidthConstraint: NSLayoutConstraint?
|
||||
|
||||
open var viewModel: TwoButtonViewModel!
|
||||
open var delegateObject: MVMCoreUIDelegateObject?
|
||||
open var additionalData: [AnyHashable : Any]?
|
||||
|
||||
open var primaryButton = PillButton()
|
||||
open var secondaryButton = PillButton()
|
||||
private var buttonGroup = VDS.ButtonGroup()
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - Lifecycle
|
||||
//--------------------------------------------------
|
||||
|
||||
public func setDefaultAppearance() {
|
||||
primaryButton.use = .primary
|
||||
secondaryButton.use = .secondary
|
||||
}
|
||||
|
||||
open override func updateView(_ size: CGFloat) {
|
||||
super.updateView(size)
|
||||
|
||||
primaryButton.updateView(size)
|
||||
secondaryButton.updateView(size)
|
||||
}
|
||||
|
||||
open override func setupView() {
|
||||
super.setupView()
|
||||
|
||||
stack.translatesAutoresizingMaskIntoConstraints = false
|
||||
open override func setup() {
|
||||
super.setup()
|
||||
isAccessibilityElement = false
|
||||
addSubview(stack)
|
||||
stack.addArrangedSubview(secondaryButton)
|
||||
stack.addArrangedSubview(primaryButton)
|
||||
NSLayoutConstraint.constraintPinSubview(toSuperview: stack)
|
||||
stack.axis = .horizontal
|
||||
stack.spacing = Padding.Component.gutterForApplicationWidth
|
||||
equalWidthConstraint = secondaryButton.widthAnchor.constraint(equalTo: primaryButton.widthAnchor, multiplier: 1)
|
||||
equalWidthConstraint?.isActive = true
|
||||
addSubview(buttonGroup)
|
||||
buttonGroup.pinToSuperView()
|
||||
buttonGroup.alignment = .center
|
||||
buttonGroup.rowQuantityPhone = 2
|
||||
buttonGroup.rowQuantityTablet = 2
|
||||
buttonGroup.childWidth = .percentage(50)
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - Stack Manipulation
|
||||
//--------------------------------------------------
|
||||
|
||||
public func showPrimaryButton() {
|
||||
|
||||
if !stack.arrangedSubviews.contains(primaryButton) {
|
||||
stack.addArrangedSubview(primaryButton)
|
||||
primaryButton.isHidden = false
|
||||
}
|
||||
|
||||
if secondaryButton.superview != nil {
|
||||
equalWidthConstraint?.isActive = true
|
||||
}
|
||||
|
||||
primaryButton.isAccessibilityElement = true
|
||||
}
|
||||
|
||||
public func showSecondaryButton() {
|
||||
|
||||
if !stack.arrangedSubviews.contains(secondaryButton) {
|
||||
stack.insertArrangedSubview(secondaryButton, at: 0)
|
||||
secondaryButton.isHidden = false
|
||||
}
|
||||
|
||||
if primaryButton.superview != nil {
|
||||
equalWidthConstraint?.isActive = true
|
||||
}
|
||||
|
||||
secondaryButton.isAccessibilityElement = true
|
||||
}
|
||||
|
||||
public func hidePrimaryButton() {
|
||||
|
||||
if primaryButton.superview != nil {
|
||||
stack.removeArrangedSubview(primaryButton)
|
||||
primaryButton.isHidden = true
|
||||
}
|
||||
|
||||
primaryButton.isAccessibilityElement = false
|
||||
equalWidthConstraint?.isActive = false
|
||||
}
|
||||
|
||||
public func hideSecondaryButton() {
|
||||
|
||||
if secondaryButton.superview != nil {
|
||||
stack.removeArrangedSubview(secondaryButton)
|
||||
secondaryButton.isHidden = true
|
||||
}
|
||||
|
||||
secondaryButton.isAccessibilityElement = false
|
||||
equalWidthConstraint?.isActive = false
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - MoleculeViewProtocol
|
||||
//--------------------------------------------------
|
||||
|
||||
open override func reset() {
|
||||
super.reset()
|
||||
|
||||
setDefaultAppearance()
|
||||
primaryButton.reset()
|
||||
secondaryButton.reset()
|
||||
primaryButton.use = .primary
|
||||
secondaryButton.use = .secondary
|
||||
buttonGroup.reset()
|
||||
}
|
||||
|
||||
public override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
|
||||
|
||||
public static func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
|
||||
guard let model = model as? TwoButtonViewModel,
|
||||
let buttonModel = model.primaryButton ?? model.secondaryButton
|
||||
else { return 0 }
|
||||
|
||||
return PillButton.estimatedHeight(with: buttonModel, delegateObject)
|
||||
}
|
||||
|
||||
public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
|
||||
super.set(with: model, delegateObject, additionalData)
|
||||
|
||||
guard let model = model as? TwoButtonViewModel else { return }
|
||||
|
||||
if let secondaryModel = model.secondaryButton {
|
||||
showSecondaryButton()
|
||||
|
||||
public func viewModelDidUpdate() {
|
||||
var buttons = [PillButton]()
|
||||
if let secondaryModel = viewModel.secondaryButton {
|
||||
secondaryButton.set(with: secondaryModel, delegateObject, additionalData)
|
||||
} else {
|
||||
hideSecondaryButton()
|
||||
buttons.append(secondaryButton)
|
||||
}
|
||||
|
||||
if let primaryModel = model.primaryButton {
|
||||
showPrimaryButton()
|
||||
if let primaryModel = viewModel.primaryButton {
|
||||
primaryButton.set(with: primaryModel, delegateObject, additionalData)
|
||||
} else {
|
||||
hidePrimaryButton()
|
||||
buttons.append(primaryButton)
|
||||
}
|
||||
|
||||
buttonGroup.buttons = buttons
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - MVMCoreUIViewConstrainingProtocol
|
||||
//--------------------------------------------------
|
||||
@ -154,4 +80,11 @@ import VDS
|
||||
open func horizontalAlignment() -> UIStackView.Alignment {
|
||||
return .center
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - MVMCoreViewProtocol
|
||||
//--------------------------------------------------
|
||||
public func updateView(_ size: CGFloat) {
|
||||
setNeedsUpdate()
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user