Two button remove legacy

This commit is contained in:
Pfeil, Scott Robert 2020-01-31 16:27:20 -05:00
parent 365bbad168
commit 37228669e0
3 changed files with 49 additions and 2 deletions

View File

@ -41,6 +41,12 @@ public class ButtonModel: ButtonModelProtocol, MoleculeModelProtocol {
self.action = action
}
init(secondaryButtonWith title: String, action: ActionModelProtocol) {
self.title = title
self.action = action
style = .secondary
}
private enum CodingKeys: String, CodingKey {
case moleculeName
case backgroundColor

View File

@ -12,7 +12,8 @@ import UIKit
open var primaryButton: PillButton = PillButton()
open var secondaryButton: PillButton = PillButton()
private var stack = UIStackView()
private var equalWidthConstraint: NSLayoutConstraint?
public init() {
super.init(frame: .zero)
}
@ -47,31 +48,45 @@ import UIKit
NSLayoutConstraint.constraintPinSubview(toSuperview: stack)
stack.axis = .horizontal
stack.spacing = 10
equalWidthConstraint = secondaryButton.widthAnchor.constraint(equalTo: primaryButton.widthAnchor, multiplier: 1)
equalWidthConstraint?.isActive = true
}
// MARK: - Stack Manipulation
public func showPrimaryButton() {
if primaryButton.superview == nil {
stack.addArrangedSubview(primaryButton)
primaryButton.isHidden = false
}
if secondaryButton.superview != nil {
equalWidthConstraint?.isActive = true
}
}
public func showSecondaryButton() {
if secondaryButton.superview == nil {
stack.addArrangedSubview(secondaryButton)
secondaryButton.isHidden = false
}
if primaryButton.superview != nil {
equalWidthConstraint?.isActive = true
}
}
public func hidePrimaryButton() {
if primaryButton.superview != nil {
stack.removeArrangedSubview(primaryButton)
primaryButton.isHidden = true
}
equalWidthConstraint?.isActive = false
}
public func hideSecondaryButton() {
if secondaryButton.superview != nil {
stack.removeArrangedSubview(secondaryButton)
secondaryButton.isHidden = true
}
equalWidthConstraint?.isActive = false
}
// MARK: - MVMCoreUIMoleculeViewProtocol

View File

@ -8,9 +8,35 @@
import UIKit
public struct TwoButtonViewModel: MoleculeModelProtocol {
public class TwoButtonViewModel: MoleculeModelProtocol {
public static var identifier: String = "twoButtonView"
public var backgroundColor: Color?
public var primaryButton: ButtonModel?
public var secondaryButton: ButtonModel?
private enum CodingKeys: String, CodingKey {
case moleculeName
case backgroundColor
case primaryButton
case secondaryButton
}
required public init(from decoder: Decoder) throws {
let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
primaryButton = try typeContainer.decodeIfPresent(ButtonModel.self, forKey: .primaryButton)
secondaryButton = try typeContainer.decodeIfPresent(ButtonModel.self, forKey: .secondaryButton)
// Default value
if secondaryButton?.style == nil {
secondaryButton?.style = .secondary
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(moleculeName, forKey: .moleculeName)
try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
try container.encodeIfPresent(primaryButton, forKey: .primaryButton)
try container.encodeIfPresent(secondaryButton, forKey: .secondaryButton)
}
}