diff --git a/MVMCoreUI/Molecules/Switch.swift b/MVMCoreUI/Molecules/Switch.swift index f3fc775c..162ddebc 100644 --- a/MVMCoreUI/Molecules/Switch.swift +++ b/MVMCoreUI/Molecules/Switch.swift @@ -11,14 +11,15 @@ import UIKit public class Switch: ViewConstrainingView, FormValidationProtocol { var aSwitch = UISwitch() var label = Label() + var mfTextButton = MFTextButton() var isRequired: Bool? = false var state: Bool? = false var onTintColor: UIColor? = .clear var offTintColor: UIColor? = .clear var isOnState: Bool? = false var fieldKey: String? = "" + var delegate: DelegateObject? - // MARK: - Inits public init() { super.init(frame: .zero) } @@ -30,47 +31,50 @@ public class Switch: ViewConstrainingView, FormValidationProtocol { required public init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } - - // MARK: - MVMCoreUIMoleculeViewProtocol open override func setAsMolecule() { super.setAsMolecule() } - + @objc func switchChanged(mySwitch: UISwitch) { - let value = mySwitch.isOn - let str = value ? "Switch is On" : "Switch is Off" - print("\(str)") - // Do something + let formValidator = (self.delegate as? MVMCoreUIDelegateObject)?.formValidationProtocol?.formValidatorModel?() + formValidator?.enableByValidation() } - - + open override func setupView() { super.setupView() - aSwitch.onTintColor = self.onTintColor - aSwitch.addTarget(self, action: #selector(switchChanged), for: UIControl.Event.valueChanged) - self.addSubview(aSwitch) - self.addSubview(label) + aSwitch.onTintColor = onTintColor + + aSwitch.addTarget(self, action: #selector(Switch.switchChanged(mySwitch:)), for: .valueChanged) + mfTextButton = MFTextButton(nil, constrainHeight: true, forWidth: CGFloat.leastNormalMagnitude) + + addSubview(aSwitch) + addSubview(label) + addSubview(mfTextButton) + label.translatesAutoresizingMaskIntoConstraints = false aSwitch.translatesAutoresizingMaskIntoConstraints = false + mfTextButton.translatesAutoresizingMaskIntoConstraints = false; + setupConstraints(forView: self) } - public override func updateView(_ size: CGFloat) { super.updateView(size) - self.setupConstraints(forView: self) + label.updateView(size) + mfTextButton.updateView(size) } - - open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) { super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData) - self.isRequired = json?[KeyRequired] as? Bool - self.state = json?["state"] as? Bool + isRequired = json?[KeyRequired] as? Bool + state = json?["state"] as? Bool + + self.delegate = delegateObject if let dict = json?.optionalDictionaryForKey("label") { - label.text = dict["text"] as? String - if let textColor = json?.optionalStringForKey("textColor") { - label.textColor = .mfGet(forHex: textColor) - } + label.setWithJSON(dict, delegateObject: delegateObject, additionalData: additionalData) + } + + if let dict = json?.optionalDictionaryForKey("textButton") { + mfTextButton.setWithJSON(dict, delegateObject: delegateObject, additionalData: additionalData) } if let delegateObject = delegateObject as? MVMCoreUIDelegateObject { @@ -78,63 +82,36 @@ public class Switch: ViewConstrainingView, FormValidationProtocol { } if let onColorString = json?.optionalStringForKey("onTintColor") { - self.onTintColor = .mfGet(forHex: onColorString) + onTintColor = .mfGet(forHex: onColorString) } if let offColorString = json?.optionalStringForKey("offTintColor") { - self.offTintColor = .mfGet(forHex: offColorString) + offTintColor = .mfGet(forHex: offColorString) } - self.fieldKey = json?[KeyFieldKey] as? String - aSwitch.onTintColor = self.onTintColor + + aSwitch.onTintColor = onTintColor + aSwitch.isOn = state ?? false } func setupConstraints(forView view: UIView) { - label.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 0).isActive = true - label.trailingAnchor.constraint(equalTo: aSwitch.leadingAnchor, constant: 20).isActive = true - label.topAnchor.constraint(equalTo: view.topAnchor).isActive = true - label.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true + NSLayoutConstraint.constraintPinRightSubview(aSwitch, rightConstant: 0) + NSLayoutConstraint.constraintPinLeftSubview(label, leftConstant: 0) - aSwitch.leadingAnchor.constraint(equalTo: label.trailingAnchor, constant: PaddingOne).isActive = true - //aSwitch.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -50).isActive = true - + 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) - view.trailingAnchor.constraint(equalTo: aSwitch.trailingAnchor, constant: 0).isActive = true - - - aSwitch.topAnchor.constraint(equalTo: view.topAnchor).isActive = true - aSwitch.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true - // aSwitch.widthAnchor.constraint(equalToConstant: 60.0).isActive = true + 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.constraintPinLeftSubview(mfTextButton, leftConstant: 0) + NSLayoutConstraint.constraintPinBottomSubview(mfTextButton, bottomConstant: 0) } - - // MARK: - FormValidationProtocol - - -// { -// "moleculeName":"switch", -// "fieldName": "switchOption", -// "fieldKey": "switchImei1", -// "onTintColor": "#ffff00", -// "offTintColor": "#ffffff", -// "state": true, -// "label": { -// "moleculeName": "label", -// "text": "This is a switch", -// "textColor": "#000000", -// "backgroundColor": "#FFFFFF" -// }, -// "required": false, -// -// } public func isValidField() -> Bool { -// if self.isRequired ?? false { -// return aSwitch.isOn -// } - return aSwitch.isOn + return (isRequired == false) ? true : aSwitch.isOn } public func formFieldName() -> String? { - return self.fieldKey ?? "" + return json?.optionalStringForKey(KeyFieldKey) } public func formFieldValue() -> Any? {