// // Switch.swift // MVMCoreUI // // Created by Priya on 5/6/19. // Copyright © 2019 Verizon Wireless. All rights reserved. // import UIKit public class Switch: ViewConstrainingView, FormValidationProtocol { var aSwitch = 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() } @objc func switchChanged(mySwitch: UISwitch) { let formValidator = (self.delegate as? MVMCoreUIDelegateObject)?.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) 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) 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) isRequired = json?[KeyRequired] as? Bool self.delegate = delegateObject if let dict = json?.optionalDictionaryForKey("label") { 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 { FormValidator.setupValidation(molecule: self, delegate: delegateObject.formValidationProtocol) } if let onColorString = json?.optionalStringForKey("onTintColor") { onTintColor = .mfGet(forHex: onColorString) } if let offColorString = json?.optionalStringForKey("offTintColor") { offTintColor = .mfGet(forHex: offColorString) } aSwitch.onTintColor = onTintColor aSwitch.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.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) } public func isValidField() -> Bool { return (isRequired == false) ? true : aSwitch.isOn } public func formFieldName() -> String? { return json?.optionalStringForKey(KeyFieldKey) } public func formFieldValue() -> Any? { return aSwitch.isOn } public override func needsToBeConstrained() -> Bool { return true } public override func moleculeAlignment() -> UIStackView.Alignment { return UIStackView.Alignment.leading } }