swift button

This commit is contained in:
Pfeil, Scott Robert 2020-01-28 14:04:56 -05:00
parent c0ca81c568
commit 2ebce1b93b

View File

@ -8,12 +8,15 @@
import UIKit import UIKit
open class PillButton: Button, MVMCoreUIViewConstrainingProtocol { open class PillButton: Button, MVMCoreUIViewConstrainingProtocol, FormValidationEnableDisableProtocol {
// Used to size the button.
var size = MVMCoreUIUtility.getWidth() var size = MVMCoreUIUtility.getWidth()
var buttonModel: ButtonModel? { var buttonModel: ButtonModel? {
get { return model as? ButtonModel } get { return model as? ButtonModel }
} }
// Need to re-style on set.
open override var isEnabled: Bool { open override var isEnabled: Bool {
didSet { didSet {
style() style()
@ -25,6 +28,7 @@ open class PillButton: Button, MVMCoreUIViewConstrainingProtocol {
case standard = 42 case standard = 42
} }
/// The primary styling for a button. Should be used for main buttons
public func stylePrimary() { public func stylePrimary() {
setTitleColor(.white, for: .normal) setTitleColor(.white, for: .normal)
setTitleColor(.white, for: .disabled) setTitleColor(.white, for: .disabled)
@ -36,6 +40,7 @@ open class PillButton: Button, MVMCoreUIViewConstrainingProtocol {
} }
} }
/// The secondary styling for a button. Should be used for secondary buttons
public func styleSecondary() { public func styleSecondary() {
setTitleColor(.black, for: .normal) setTitleColor(.black, for: .normal)
setTitleColor(.mvmCoolGray6, for: .disabled) setTitleColor(.mvmCoolGray6, for: .disabled)
@ -48,6 +53,7 @@ open class PillButton: Button, MVMCoreUIViewConstrainingProtocol {
} }
} }
/// Styles the button based on the model style
private func style() { private func style() {
switch buttonModel?.style { switch buttonModel?.style {
case .secondary: case .secondary:
@ -97,13 +103,10 @@ open class PillButton: Button, MVMCoreUIViewConstrainingProtocol {
guard let model = model as? ButtonModel else { return } guard let model = model as? ButtonModel else { return }
setTitle(model.title, for: .normal) setTitle(model.title, for: .normal)
/*self.validationRequired = model.required ?? false if let required = model.required,
self.requiredGroupsList = model.requiredGroups required == true {
FormValidator.setupValidation(molecule: self, delegate: delegateObject?.formValidationProtocol)
if self.validationRequired, }
let selfForm = self as? FormValidationEnableDisableProtocol {
FormValidator.setupValidation(molecule: selfForm, delegate: delegateObject?.formValidationProtocol)
}*/
} }
open override class func estimatedHeight(forRow molecule: MoleculeModelProtocol?, delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? { open override class func estimatedHeight(forRow molecule: MoleculeModelProtocol?, delegateObject: MVMCoreUIDelegateObject?) -> CGFloat? {
@ -137,4 +140,17 @@ open class PillButton: Button, MVMCoreUIViewConstrainingProtocol {
open func horizontalAlignment() -> UIStackView.Alignment { open func horizontalAlignment() -> UIStackView.Alignment {
return .center return .center
} }
// MARK: - FormValidationEnableDisableProtocol
public func isValidationRequired() -> Bool {
return buttonModel?.required ?? false
}
public func requiredGroups() -> [String]? {
return buttonModel?.requiredGroups
}
public func enableField(_ enable: Bool) {
isEnabled = isValidationRequired() ? enable : true
}
} }