diff --git a/MVMCoreUI/Molecules/TwoButtonView.swift b/MVMCoreUI/Molecules/TwoButtonView.swift index 4ae821d2..9e6cf4ee 100644 --- a/MVMCoreUI/Molecules/TwoButtonView.swift +++ b/MVMCoreUI/Molecules/TwoButtonView.swift @@ -10,7 +10,7 @@ import UIKit @objcMembers open class TwoButtonView: ButtonView { open var secondaryButton: PrimaryButton? - open var viewForButtons = MVMCoreUICommonViewsUtility.commonView() + open var viewForButtons: UIView? public var heightConstraint: NSLayoutConstraint? public override init() { @@ -41,7 +41,7 @@ import UIKit } let primaryButtonMap = json?.optionalDictionaryForKey("primaryButton") let secondaryButtonMap = json?.optionalDictionaryForKey("secondaryButton") - setupUI(withPrimaryButtonMap: primaryButtonMap, secondaryButtonMap: secondaryButtonMap) + setupUI(withPrimaryButtonMap: primaryButtonMap, secondaryButtonMap: secondaryButtonMap, legacy: false) primaryButton?.setAsStandardCustom() secondaryButton?.setAsSecondaryCustom() primaryButton?.setWithJSON(primaryButtonMap, delegate: delegate, additionalData: additionalData) @@ -50,30 +50,33 @@ import UIKit // MARK: - Constraining override func setupButton() { - guard viewForButtons.superview != self else { - return - } - addSubview(viewForButtons) - setupConstraints(forView: viewForButtons) setupWithTwoButtons() } - open func setupWithTwoButtons() { - guard viewForButtons != primaryButton?.superview && viewForButtons != secondaryButton?.superview else { - return - } - if let primaryButton = primaryButton { - primaryButton.removeFromSuperview() - } else { + func createPrimaryButton() { + if primaryButton == nil { primaryButton = PrimaryButton.button() } - if let secondaryButton = secondaryButton { - secondaryButton.removeFromSuperview() - } else { + } + + func createSecondaryButton() { + if secondaryButton == nil { secondaryButton = PrimaryButton.button() secondaryButton?.bordered = true } - guard let primaryButton = primaryButton, let secondaryButton = secondaryButton else { + } + + func removeButtons() { + viewForButtons?.removeFromSuperview() + primaryButton?.removeFromSuperview() + secondaryButton?.removeFromSuperview() + viewForButtons = nil + secondaryButton = nil + primaryButton = nil + } + + open func setupConstraintsForViewWithButtons() { + guard let viewForButtons = viewForButtons, let primaryButton = primaryButton, let secondaryButton = secondaryButton else { return } viewForButtons.addSubview(primaryButton) @@ -86,50 +89,54 @@ import UIKit NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[leftButton]-10-[rightButton]-0-|", options: NSLayoutConstraint.FormatOptions.alignAllCenterY, metrics: nil, views: ["leftButton": secondaryButton, "rightButton": primaryButton])) } + func setupWithTwoButtons() { + guard self.viewForButtons == nil else { + return + } + let viewForButtons = MVMCoreUICommonViewsUtility.commonView() + addSubview(viewForButtons) + self.viewForButtons = viewForButtons + + setupConstraints(forView: viewForButtons) + alignCenter() + + createPrimaryButton() + createSecondaryButton() + setupConstraintsForViewWithButtons() + } + open func setupWithPrimaryButton() { - primaryButton?.removeFromSuperview() - secondaryButton?.removeFromSuperview() - secondaryButton = nil - if primaryButton == nil { - primaryButton = PrimaryButton.button() + guard self.primaryButton == nil else { + return } - setupWithOneButton(primaryButton!) - } - - open func setupWithSecondaryButton() { - primaryButton?.removeFromSuperview() - secondaryButton?.removeFromSuperview() - primaryButton = nil - if secondaryButton == nil { - secondaryButton = PrimaryButton.button() - secondaryButton?.bordered = true + createPrimaryButton() + if let primaryButton = primaryButton { + addSubview(primaryButton) + setupConstraints(forView: primaryButton) + alignCenter() } - setupWithOneButton(secondaryButton!) } - func setupWithOneButton(_ button: PrimaryButton) { - viewForButtons.addSubview(button) - button.topAnchor.constraint(equalTo: viewForButtons.topAnchor).isActive = true - button.leftAnchor.constraint(equalTo: viewForButtons.leftAnchor).isActive = true - viewForButtons.rightAnchor.constraint(equalTo: button.rightAnchor).isActive = true - viewForButtons.bottomAnchor.constraint(equalTo: button.bottomAnchor).isActive = true - } - - open func setupUI(withPrimaryButtonMap primaryButtonMap: [AnyHashable: Any]?, secondaryButtonMap: [AnyHashable: Any]?) { + // Sets up the number of buttons based on the maps. Doesn't set the buttons with the maps because legacy code handles differently from modern code... + func setupUI(withPrimaryButtonMap primaryButtonMap: [AnyHashable: Any]?, secondaryButtonMap: [AnyHashable: Any]?, legacy: Bool) { if primaryButtonMap != nil, secondaryButtonMap != nil { heightConstraint?.isActive = false - setupWithTwoButtons() - } else if primaryButtonMap != nil || secondaryButtonMap != nil { + if primaryButton == nil || secondaryButton == nil { + removeButtons() + setupWithTwoButtons() + } + } else if primaryButtonMap != nil || (secondaryButtonMap != nil && legacy) { + // Only legacy sets up the primary button with a secondary map heightConstraint?.isActive = false - setupWithPrimaryButton() + if primaryButton == nil || secondaryButton != nil { + removeButtons() + setupWithPrimaryButton() + } } else { - primaryButton?.removeFromSuperview() - secondaryButton?.removeFromSuperview() - primaryButton = nil - secondaryButton = nil + removeButtons() if heightConstraint == nil { - self.heightConstraint = heightAnchor.constraint(equalToConstant: 0) - self.heightConstraint?.isActive = true + heightConstraint = heightAnchor.constraint(equalToConstant: 0) + heightConstraint?.isActive = true } } } @@ -142,19 +149,22 @@ import UIKit } open func setup(primaryButtonMap: [AnyHashable: Any]?, secondaryButtonMap: [AnyHashable: Any]?, actionDelegate: NSObjectProtocol?, additionalData: [AnyHashable: Any]?, buttonDelegate: Any?) { - setupUI(withPrimaryButtonMap: primaryButtonMap, secondaryButtonMap: secondaryButtonMap) + setupUI(withPrimaryButtonMap: primaryButtonMap, secondaryButtonMap: secondaryButtonMap, legacy: true) if primaryButtonMap != nil, secondaryButtonMap != nil { primaryButton?.setWithActionMap(primaryButtonMap, actionDelegate: actionDelegate as? MVMCoreActionDelegateProtocol & NSObjectProtocol, additionalData: additionalData, buttonDelegate: buttonDelegate as? ButtonDelegateProtocol) secondaryButton?.setWithActionMap(secondaryButtonMap, actionDelegate: actionDelegate as? MVMCoreActionDelegateProtocol & NSObjectProtocol, additionalData: additionalData, buttonDelegate: buttonDelegate as? ButtonDelegateProtocol) } else if primaryButtonMap != nil { primaryButton?.setWithActionMap(primaryButtonMap, actionDelegate: actionDelegate as? MVMCoreActionDelegateProtocol & NSObjectProtocol, additionalData: additionalData, buttonDelegate: buttonDelegate as? ButtonDelegateProtocol) - } else { + primaryButton?.bordered = false + } else if secondaryButtonMap != nil { primaryButton?.setWithActionMap(secondaryButtonMap, actionDelegate: actionDelegate as? MVMCoreActionDelegateProtocol & NSObjectProtocol, additionalData: additionalData, buttonDelegate: buttonDelegate as? ButtonDelegateProtocol) + primaryButton?.bordered = true } } public convenience init(buttonSmall small: Bool, enabled: Bool) { self.init() + removeButtons() setupWithPrimaryButton() primaryButton?.setAsSmall(small) primaryButton?.isEnabled = enabled @@ -173,23 +183,37 @@ import UIKit } public func hidePrimaryLeftButton() { - if let secondaryButton = secondaryButton, !secondaryButton.isHidden { - secondaryButton.isHidden = true - setupWithPrimaryButton() + guard let secondaryButton = secondaryButton, !secondaryButton.isHidden else { + return + } + secondaryButton.isHidden = true + if let primaryButton = primaryButton { + primaryButton.removeFromSuperview() + viewForButtons?.addSubview(primaryButton) + NSLayoutConstraint.constraintPinSubview(toSuperview: primaryButton) } } public func hidePrimaryRightButton() { - if let primaryButton = primaryButton, !primaryButton.isHidden { - primaryButton.isHidden = true - setupWithSecondaryButton() + guard let primaryButton = primaryButton, !primaryButton.isHidden else { + return + } + primaryButton.isHidden = true + if let secondaryButton = secondaryButton { + secondaryButton.removeFromSuperview() + viewForButtons?.addSubview(secondaryButton) + NSLayoutConstraint.constraintPinSubview(toSuperview: secondaryButton) } } public func showBothPrimaryButtons() { primaryButton?.isHidden = false secondaryButton?.isHidden = false - setupWithTwoButtons() + if let primaryButton = primaryButton, let secondaryButton = secondaryButton { + primaryButton.removeFromSuperview() + secondaryButton.removeFromSuperview() + setupConstraintsForViewWithButtons() + } } public func hideBothPrimaryButtons() {