diff --git a/MVMCoreUI/Molecules/Switch.swift b/MVMCoreUI/Molecules/Switch.swift index 50f9aae0..b01ef73e 100644 --- a/MVMCoreUI/Molecules/Switch.swift +++ b/MVMCoreUI/Molecules/Switch.swift @@ -8,48 +8,31 @@ import UIKit -public class Switch: ViewConstrainingView, FormValidationProtocol { - var aSwitch = UISwitch() +@objcMembers public class Switch: ViewConstrainingView, FormValidationProtocol{ + public var mvmSwitch = UISwitch() var label = Label() var mfTextButton = MFTextButton() - var isRequired: Bool? = false - var onTintColor: UIColor? = .clear - var offTintColor: UIColor? = .clear - var delegate: DelegateObject? - - public init() { - super.init(frame: .zero) - } - - public override init(frame: CGRect) { - super.init(frame: frame) - } - - required public init?(coder aDecoder: NSCoder) { - super.init(coder: aDecoder) - } - open override func setAsMolecule() { - super.setAsMolecule() - } + var isRequired: Bool! = false - @objc func switchChanged(mySwitch: UISwitch) { - let formValidator = (self.delegate as? MVMCoreUIDelegateObject)?.formValidationProtocol?.formValidatorModel?() - formValidator?.enableByValidation() + var delegateObject: DelegateObject? + + + @objc func switchChanged() { + let delegate = delegateObject as? MVMCoreUIDelegateObject + if let delegate = delegate { + let formValidator = delegate.formValidationProtocol?.formValidatorModel?() + formValidator?.enableByValidation() + } } open override func setupView() { super.setupView() - aSwitch.onTintColor = onTintColor - - aSwitch.addTarget(self, action: #selector(Switch.switchChanged(mySwitch:)), for: .valueChanged) - mfTextButton = MFTextButton(nil, constrainHeight: true, forWidth: CGFloat.leastNormalMagnitude) - - addSubview(aSwitch) + mvmSwitch.addTarget(self, action: #selector(Switch.switchChanged), for: .valueChanged) + addSubview(mvmSwitch) addSubview(label) addSubview(mfTextButton) - - label.translatesAutoresizingMaskIntoConstraints = false - aSwitch.translatesAutoresizingMaskIntoConstraints = false + // label.translatesAutoresizingMaskIntoConstraints = false + mvmSwitch.translatesAutoresizingMaskIntoConstraints = false mfTextButton.translatesAutoresizingMaskIntoConstraints = false; setupConstraints(forView: self) } @@ -63,7 +46,7 @@ public class Switch: ViewConstrainingView, FormValidationProtocol { open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) { super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData) isRequired = json?[KeyRequired] as? Bool - self.delegate = delegateObject + self.delegateObject = delegateObject if let dict = json?.optionalDictionaryForKey("label") { label.setWithJSON(dict, delegateObject: delegateObject, additionalData: additionalData) } @@ -77,28 +60,28 @@ public class Switch: ViewConstrainingView, FormValidationProtocol { } if let onColorString = json?.optionalStringForKey("onTintColor") { - onTintColor = .mfGet(forHex: onColorString) + mvmSwitch.onTintColor = .mfGet(forHex: onColorString) } - if let offColorString = json?.optionalStringForKey("offTintColor") { - offTintColor = .mfGet(forHex: offColorString) - } - aSwitch.onTintColor = onTintColor - aSwitch.isOn = json?.optionalBoolForKey("state") ?? false + mvmSwitch.isOn = json?.optionalBoolForKey("state") ?? false } func setupConstraints(forView view: UIView) { - NSLayoutConstraint.constraintPinRightSubview(aSwitch, rightConstant: 0) NSLayoutConstraint.constraintPinLeftSubview(label, leftConstant: 0) - NSLayoutConstraint.constraintPinSubview(aSwitch, pinTop: true, topConstant: 0, topRelation: .equal, pinBottom: true, bottomConstant: 0, bottomRelation: .greaterThanOrEqual, pinLeft: false, leftConstant: 0, leftRelation: .equal, pinRight: false, rightConstant: 0, rightRelation: .equal) + NSLayoutConstraint.constraintPinSubview(mvmSwitch, pinTop: true, topConstant: 0, topRelation: .equal, pinBottom: true, bottomConstant: 0, bottomRelation: .greaterThanOrEqual, pinLeft: false, leftConstant: 0, leftRelation: .equal, pinRight: true, rightConstant: 0, rightRelation: .equal) NSLayoutConstraint.constraintPinTopSubview(label, topConstant: 0) - NSLayoutConstraint(pinFirstView: label, toSecondView: aSwitch, withConstant: PaddingOne, directionVertical: false)?.isActive = true - NSLayoutConstraint(pinFirstView: label, toSecondView: mfTextButton, withConstant: PaddingOne, directionVertical: true)?.isActive = true + _ = NSLayoutConstraint(pinFirstView: label, toSecondView: mvmSwitch, withConstant: PaddingOne, directionVertical: false) + _ = NSLayoutConstraint(pinFirstView: label, toSecondView: mfTextButton, withConstant: PaddingOne, directionVertical: true) NSLayoutConstraint.constraintPinLeftSubview(mfTextButton, leftConstant: 0) NSLayoutConstraint.constraintPinBottomSubview(mfTextButton, bottomConstant: 0) + NSLayoutConstraint.constraintPinRightSubview(mfTextButton, rightConstant: 0) + if label.text?.isEmpty ?? false == false { + _ = NSLayoutConstraint(pinFirstView: mvmSwitch, toSecondView: mfTextButton, withConstant: PaddingOne, directionVertical: true) + } + } public func isValidField() -> Bool { - return (isRequired == false) ? true : aSwitch.isOn + return (isRequired == false) ? true : mvmSwitch.isOn } public func formFieldName() -> String? { @@ -106,7 +89,7 @@ public class Switch: ViewConstrainingView, FormValidationProtocol { } public func formFieldValue() -> Any? { - return aSwitch.isOn + return mvmSwitch.isOn } public override func needsToBeConstrained() -> Bool {