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 UIKit
|
||||||
import VDS
|
import VDS
|
||||||
|
|
||||||
@objcMembers open class TwoButtonView: View, MVMCoreUIViewConstrainingProtocol {
|
@objcMembers open class TwoButtonView: VDS.View, VDSMoleculeViewProtocol {
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Properties
|
// MARK: - Properties
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
|
open var viewModel: TwoButtonViewModel!
|
||||||
open var primaryButton: PillButton = PillButton()
|
open var delegateObject: MVMCoreUIDelegateObject?
|
||||||
open var secondaryButton: PillButton = PillButton()
|
open var additionalData: [AnyHashable : Any]?
|
||||||
private var stack = UIStackView()
|
|
||||||
|
open var primaryButton = PillButton()
|
||||||
//--------------------------------------------------
|
open var secondaryButton = PillButton()
|
||||||
// MARK: - Constraints
|
private var buttonGroup = VDS.ButtonGroup()
|
||||||
//--------------------------------------------------
|
|
||||||
|
|
||||||
private var equalWidthConstraint: NSLayoutConstraint?
|
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Lifecycle
|
// MARK: - Lifecycle
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
|
|
||||||
public func setDefaultAppearance() {
|
open override func setup() {
|
||||||
primaryButton.use = .primary
|
super.setup()
|
||||||
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
|
|
||||||
isAccessibilityElement = false
|
isAccessibilityElement = false
|
||||||
addSubview(stack)
|
addSubview(buttonGroup)
|
||||||
stack.addArrangedSubview(secondaryButton)
|
buttonGroup.pinToSuperView()
|
||||||
stack.addArrangedSubview(primaryButton)
|
buttonGroup.alignment = .center
|
||||||
NSLayoutConstraint.constraintPinSubview(toSuperview: stack)
|
buttonGroup.rowQuantityPhone = 2
|
||||||
stack.axis = .horizontal
|
buttonGroup.rowQuantityTablet = 2
|
||||||
stack.spacing = Padding.Component.gutterForApplicationWidth
|
buttonGroup.childWidth = .percentage(50)
|
||||||
equalWidthConstraint = secondaryButton.widthAnchor.constraint(equalTo: primaryButton.widthAnchor, multiplier: 1)
|
|
||||||
equalWidthConstraint?.isActive = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
|
||||||
// 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
|
// MARK: - MoleculeViewProtocol
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
|
|
||||||
open override func reset() {
|
open override func reset() {
|
||||||
super.reset()
|
super.reset()
|
||||||
|
primaryButton.reset()
|
||||||
setDefaultAppearance()
|
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,
|
guard let model = model as? TwoButtonViewModel,
|
||||||
let buttonModel = model.primaryButton ?? model.secondaryButton
|
let buttonModel = model.primaryButton ?? model.secondaryButton
|
||||||
else { return 0 }
|
else { return 0 }
|
||||||
|
|
||||||
return PillButton.estimatedHeight(with: buttonModel, delegateObject)
|
return PillButton.estimatedHeight(with: buttonModel, delegateObject)
|
||||||
}
|
}
|
||||||
|
|
||||||
public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
|
public func viewModelDidUpdate() {
|
||||||
super.set(with: model, delegateObject, additionalData)
|
var buttons = [PillButton]()
|
||||||
|
if let secondaryModel = viewModel.secondaryButton {
|
||||||
guard let model = model as? TwoButtonViewModel else { return }
|
|
||||||
|
|
||||||
if let secondaryModel = model.secondaryButton {
|
|
||||||
showSecondaryButton()
|
|
||||||
secondaryButton.set(with: secondaryModel, delegateObject, additionalData)
|
secondaryButton.set(with: secondaryModel, delegateObject, additionalData)
|
||||||
} else {
|
buttons.append(secondaryButton)
|
||||||
hideSecondaryButton()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if let primaryModel = model.primaryButton {
|
if let primaryModel = viewModel.primaryButton {
|
||||||
showPrimaryButton()
|
|
||||||
primaryButton.set(with: primaryModel, delegateObject, additionalData)
|
primaryButton.set(with: primaryModel, delegateObject, additionalData)
|
||||||
} else {
|
buttons.append(primaryButton)
|
||||||
hidePrimaryButton()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buttonGroup.buttons = buttons
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - MVMCoreUIViewConstrainingProtocol
|
// MARK: - MVMCoreUIViewConstrainingProtocol
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
@ -154,4 +80,11 @@ import VDS
|
|||||||
open func horizontalAlignment() -> UIStackView.Alignment {
|
open func horizontalAlignment() -> UIStackView.Alignment {
|
||||||
return .center
|
return .center
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------
|
||||||
|
// MARK: - MVMCoreViewProtocol
|
||||||
|
//--------------------------------------------------
|
||||||
|
public func updateView(_ size: CGFloat) {
|
||||||
|
setNeedsUpdate()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user