diff --git a/MVMCoreUI/Molecules/Switch.swift b/MVMCoreUI/Molecules/Switch.swift index dec7f46b..f3fc775c 100644 --- a/MVMCoreUI/Molecules/Switch.swift +++ b/MVMCoreUI/Molecules/Switch.swift @@ -8,13 +8,16 @@ import UIKit -public class Switch: ViewConstrainingView { +public class Switch: ViewConstrainingView, FormValidationProtocol { + var aSwitch = UISwitch() + var label = Label() var isRequired: Bool? = false var state: Bool? = false var onTintColor: UIColor? = .clear var offTintColor: UIColor? = .clear var isOnState: Bool? = false var fieldKey: String? = "" + // MARK: - Inits public init() { super.init(frame: .zero) @@ -33,10 +36,47 @@ public class Switch: ViewConstrainingView { super.setAsMolecule() } + @objc func switchChanged(mySwitch: UISwitch) { + let value = mySwitch.isOn + let str = value ? "Switch is On" : "Switch is Off" + print("\(str)") + // Do something + } + + + 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) + label.translatesAutoresizingMaskIntoConstraints = false + aSwitch.translatesAutoresizingMaskIntoConstraints = false + } + + + public override func updateView(_ size: CGFloat) { + super.updateView(size) + self.setupConstraints(forView: self) + } + + + 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 + if let dict = json?.optionalDictionaryForKey("label") { + label.text = dict["text"] as? String + if let textColor = json?.optionalStringForKey("textColor") { + label.textColor = .mfGet(forHex: textColor) + } + } + + if let delegateObject = delegateObject as? MVMCoreUIDelegateObject { + FormValidator.setupValidation(molecule: self, delegate: delegateObject.formValidationProtocol) + } + if let onColorString = json?.optionalStringForKey("onTintColor") { self.onTintColor = .mfGet(forHex: onColorString) } @@ -44,35 +84,70 @@ public class Switch: ViewConstrainingView { self.offTintColor = .mfGet(forHex: offColorString) } self.fieldKey = json?[KeyFieldKey] as? String - + aSwitch.onTintColor = self.onTintColor } func setupConstraints(forView view: UIView) { - leftPin = view.leftAnchor.constraint(equalTo: leftAnchor) - topPin = view.topAnchor.constraint(equalTo: topAnchor) - rightPin = rightAnchor.constraint(equalTo: view.rightAnchor) - bottomPin = bottomAnchor.constraint(equalTo: view.bottomAnchor) - leftPin?.isActive = true - topPin?.isActive = true - rightPin?.isActive = true - bottomPin?.isActive = true + 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 + + aSwitch.leadingAnchor.constraint(equalTo: label.trailingAnchor, constant: PaddingOne).isActive = true + //aSwitch.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -50).isActive = true + + + 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 + } + // MARK: - FormValidationProtocol - func isValidField() -> Bool { - if self.isRequired ?? false { - return self.isOnState ?? false - } - return true + +// { +// "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 } - func formFieldName() -> String { + public func formFieldName() -> String? { return self.fieldKey ?? "" } - func formFieldValue() -> Bool { - return self.isOnState ?? false + public func formFieldValue() -> Any? { + return aSwitch.isOn + } + + public override func needsToBeConstrained() -> Bool { + return true + } + + public override func moleculeAlignment() -> UIStackView.Alignment { + return UIStackView.Alignment.leading } } +