Fixes to make two button more compatible with legacy

This commit is contained in:
Pfeil, Scott Robert 2019-03-25 16:51:51 -04:00
parent b25b869c4d
commit f63b157c16

View File

@ -10,7 +10,7 @@ import UIKit
@objcMembers open class TwoButtonView: ButtonView { @objcMembers open class TwoButtonView: ButtonView {
open var secondaryButton: PrimaryButton? open var secondaryButton: PrimaryButton?
open var viewForButtons = MVMCoreUICommonViewsUtility.commonView() open var viewForButtons: UIView?
public var heightConstraint: NSLayoutConstraint? public var heightConstraint: NSLayoutConstraint?
public override init() { public override init() {
@ -41,7 +41,7 @@ import UIKit
} }
let primaryButtonMap = json?.optionalDictionaryForKey("primaryButton") let primaryButtonMap = json?.optionalDictionaryForKey("primaryButton")
let secondaryButtonMap = json?.optionalDictionaryForKey("secondaryButton") let secondaryButtonMap = json?.optionalDictionaryForKey("secondaryButton")
setupUI(withPrimaryButtonMap: primaryButtonMap, secondaryButtonMap: secondaryButtonMap) setupUI(withPrimaryButtonMap: primaryButtonMap, secondaryButtonMap: secondaryButtonMap, legacy: false)
primaryButton?.setAsStandardCustom() primaryButton?.setAsStandardCustom()
secondaryButton?.setAsSecondaryCustom() secondaryButton?.setAsSecondaryCustom()
primaryButton?.setWithJSON(primaryButtonMap, delegate: delegate, additionalData: additionalData) primaryButton?.setWithJSON(primaryButtonMap, delegate: delegate, additionalData: additionalData)
@ -50,30 +50,33 @@ import UIKit
// MARK: - Constraining // MARK: - Constraining
override func setupButton() { override func setupButton() {
guard viewForButtons.superview != self else {
return
}
addSubview(viewForButtons)
setupConstraints(forView: viewForButtons)
setupWithTwoButtons() setupWithTwoButtons()
} }
open func setupWithTwoButtons() { func createPrimaryButton() {
guard viewForButtons != primaryButton?.superview && viewForButtons != secondaryButton?.superview else { if primaryButton == nil {
return
}
if let primaryButton = primaryButton {
primaryButton.removeFromSuperview()
} else {
primaryButton = PrimaryButton.button() primaryButton = PrimaryButton.button()
} }
if let secondaryButton = secondaryButton { }
secondaryButton.removeFromSuperview()
} else { func createSecondaryButton() {
if secondaryButton == nil {
secondaryButton = PrimaryButton.button() secondaryButton = PrimaryButton.button()
secondaryButton?.bordered = true 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 return
} }
viewForButtons.addSubview(primaryButton) 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])) 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() { open func setupWithPrimaryButton() {
primaryButton?.removeFromSuperview() guard self.primaryButton == nil else {
secondaryButton?.removeFromSuperview() return
secondaryButton = nil
if primaryButton == nil {
primaryButton = PrimaryButton.button()
} }
setupWithOneButton(primaryButton!) createPrimaryButton()
} if let primaryButton = primaryButton {
addSubview(primaryButton)
open func setupWithSecondaryButton() { setupConstraints(forView: primaryButton)
primaryButton?.removeFromSuperview() alignCenter()
secondaryButton?.removeFromSuperview()
primaryButton = nil
if secondaryButton == nil {
secondaryButton = PrimaryButton.button()
secondaryButton?.bordered = true
} }
setupWithOneButton(secondaryButton!)
} }
func setupWithOneButton(_ button: PrimaryButton) { // 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...
viewForButtons.addSubview(button) func setupUI(withPrimaryButtonMap primaryButtonMap: [AnyHashable: Any]?, secondaryButtonMap: [AnyHashable: Any]?, legacy: Bool) {
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]?) {
if primaryButtonMap != nil, secondaryButtonMap != nil { if primaryButtonMap != nil, secondaryButtonMap != nil {
heightConstraint?.isActive = false heightConstraint?.isActive = false
setupWithTwoButtons() if primaryButton == nil || secondaryButton == nil {
} else if primaryButtonMap != nil || secondaryButtonMap != nil { removeButtons()
setupWithTwoButtons()
}
} else if primaryButtonMap != nil || (secondaryButtonMap != nil && legacy) {
// Only legacy sets up the primary button with a secondary map
heightConstraint?.isActive = false heightConstraint?.isActive = false
setupWithPrimaryButton() if primaryButton == nil || secondaryButton != nil {
removeButtons()
setupWithPrimaryButton()
}
} else { } else {
primaryButton?.removeFromSuperview() removeButtons()
secondaryButton?.removeFromSuperview()
primaryButton = nil
secondaryButton = nil
if heightConstraint == nil { if heightConstraint == nil {
self.heightConstraint = heightAnchor.constraint(equalToConstant: 0) heightConstraint = heightAnchor.constraint(equalToConstant: 0)
self.heightConstraint?.isActive = true 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?) { 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 { if primaryButtonMap != nil, secondaryButtonMap != nil {
primaryButton?.setWithActionMap(primaryButtonMap, actionDelegate: actionDelegate as? MVMCoreActionDelegateProtocol & NSObjectProtocol, additionalData: additionalData, buttonDelegate: buttonDelegate as? ButtonDelegateProtocol) 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) secondaryButton?.setWithActionMap(secondaryButtonMap, actionDelegate: actionDelegate as? MVMCoreActionDelegateProtocol & NSObjectProtocol, additionalData: additionalData, buttonDelegate: buttonDelegate as? ButtonDelegateProtocol)
} else if primaryButtonMap != nil { } else if primaryButtonMap != nil {
primaryButton?.setWithActionMap(primaryButtonMap, actionDelegate: actionDelegate as? MVMCoreActionDelegateProtocol & NSObjectProtocol, additionalData: additionalData, buttonDelegate: buttonDelegate as? ButtonDelegateProtocol) 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?.setWithActionMap(secondaryButtonMap, actionDelegate: actionDelegate as? MVMCoreActionDelegateProtocol & NSObjectProtocol, additionalData: additionalData, buttonDelegate: buttonDelegate as? ButtonDelegateProtocol)
primaryButton?.bordered = true
} }
} }
public convenience init(buttonSmall small: Bool, enabled: Bool) { public convenience init(buttonSmall small: Bool, enabled: Bool) {
self.init() self.init()
removeButtons()
setupWithPrimaryButton() setupWithPrimaryButton()
primaryButton?.setAsSmall(small) primaryButton?.setAsSmall(small)
primaryButton?.isEnabled = enabled primaryButton?.isEnabled = enabled
@ -173,23 +183,37 @@ import UIKit
} }
public func hidePrimaryLeftButton() { public func hidePrimaryLeftButton() {
if let secondaryButton = secondaryButton, !secondaryButton.isHidden { guard let secondaryButton = secondaryButton, !secondaryButton.isHidden else {
secondaryButton.isHidden = true return
setupWithPrimaryButton() }
secondaryButton.isHidden = true
if let primaryButton = primaryButton {
primaryButton.removeFromSuperview()
viewForButtons?.addSubview(primaryButton)
NSLayoutConstraint.constraintPinSubview(toSuperview: primaryButton)
} }
} }
public func hidePrimaryRightButton() { public func hidePrimaryRightButton() {
if let primaryButton = primaryButton, !primaryButton.isHidden { guard let primaryButton = primaryButton, !primaryButton.isHidden else {
primaryButton.isHidden = true return
setupWithSecondaryButton() }
primaryButton.isHidden = true
if let secondaryButton = secondaryButton {
secondaryButton.removeFromSuperview()
viewForButtons?.addSubview(secondaryButton)
NSLayoutConstraint.constraintPinSubview(toSuperview: secondaryButton)
} }
} }
public func showBothPrimaryButtons() { public func showBothPrimaryButtons() {
primaryButton?.isHidden = false primaryButton?.isHidden = false
secondaryButton?.isHidden = false secondaryButton?.isHidden = false
setupWithTwoButtons() if let primaryButton = primaryButton, let secondaryButton = secondaryButton {
primaryButton.removeFromSuperview()
secondaryButton.removeFromSuperview()
setupConstraintsForViewWithButtons()
}
} }
public func hideBothPrimaryButtons() { public func hideBothPrimaryButtons() {