refactored two link view

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2023-12-15 16:17:36 -06:00
parent e1b6d82007
commit 83b2cb80ad

View File

@ -7,79 +7,81 @@
//
import Foundation
import VDS
@objcMembers open class TwoLinkView: View, MVMCoreUIViewConstrainingProtocol {
@objcMembers open class TwoLinkView: VDS.View, VDSMoleculeViewProtocol {
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
open var viewModel: TwoLinkViewModel!
open var delegateObject: MVMCoreUIDelegateObject?
open var additionalData: [AnyHashable : Any]?
open var leftLink = Link()
open var rightLink = Link()
private var stack = UIStackView()
private var buttonGroup = VDS.ButtonGroup()
private var buttons: [Link] = []
//--------------------------------------------------
// MARK: - Lifecycle
//--------------------------------------------------
open override func setup() {
super.setup()
isAccessibilityElement = false
addSubview(buttonGroup)
buttonGroup.pinToSuperView()
buttonGroup.alignment = .center
buttonGroup.rowQuantityPhone = 2
buttonGroup.rowQuantityTablet = 2
buttonGroup.childWidth = .percentage(50)
}
//--------------------------------------------------
// MARK: - MVMCoreViewProtocol
//--------------------------------------------------
open override func updateView(_ size: CGFloat) {
super.updateView(size)
stack.updateView(size)
open override func reset() {
super.reset()
leftLink.reset()
rightLink.reset()
buttonGroup.reset()
}
open override func setupView() {
super.setupView()
stack.translatesAutoresizingMaskIntoConstraints = false
addSubview(stack)
stack.addArrangedSubview(leftLink)
stack.addArrangedSubview(rightLink)
NSLayoutConstraint.constraintPinSubview(toSuperview: stack)
stack.axis = .horizontal
stack.spacing = 8
}
open func updateView(_ size: CGFloat) { }
//--------------------------------------------------
// MARK: - Stack Manipulation
//--------------------------------------------------
public func showRightLink() {
if !stack.arrangedSubviews.contains(rightLink) {
stack.addArrangedSubview(rightLink)
rightLink.isHidden = false
if !buttons.contains(rightLink) {
buttons.insert(rightLink, at: buttons.count)
}
buttonGroup.buttons = buttons
}
public func showLeftLink() {
if !stack.arrangedSubviews.contains(leftLink) {
stack.insertArrangedSubview(leftLink, at: 0)
leftLink.isHidden = false
if !buttons.contains(leftLink) {
buttons.insert(leftLink, at: 0)
}
buttonGroup.buttons = buttons
}
public func hideRightLink() {
if rightLink.superview != nil {
stack.removeArrangedSubview(rightLink)
rightLink.isHidden = true
if let index = buttons.firstIndex(of: rightLink) {
buttons.remove(at: index)
}
buttonGroup.buttons = buttons
}
public func hideLeftLink() {
if leftLink.superview != nil {
stack.removeArrangedSubview(leftLink)
leftLink.isHidden = true
if let index = buttons.firstIndex(of: leftLink) {
buttons.remove(at: index)
}
buttonGroup.buttons = buttons
}
//--------------------------------------------------
// MARK: - MoleculeViewProtocol
//--------------------------------------------------
open override func reset() {
super.reset()
stack.reset()
}
//--------------------------------------------------
// MARK: - MVMCoreUIViewConstrainingProtocol
//--------------------------------------------------
@ -92,27 +94,26 @@ import Foundation
// MARK: - MoleculeViewProtocol
//--------------------------------------------------
public override class func estimatedHeight(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
return 16
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? TwoLinkViewModel else { return }
if let model = model.leftLink {
showLeftLink()
public func viewModelDidUpdate() {
if let model = viewModel.leftLink {
leftLink.set(with: model, delegateObject, additionalData)
} else {
hideLeftLink()
buttons.append(leftLink)
}
if let model = model.rightLink {
showRightLink()
if let model = viewModel.rightLink {
rightLink.set(with: model, delegateObject, additionalData)
} else {
hideRightLink()
buttons.append(rightLink)
}
buttonGroup.buttons = buttons
}
}