// // Switch.swift // MVMCoreUI // // Created by Priya on 5/6/19. // Copyright © 2019 Verizon Wireless. All rights reserved. // import UIKit @objcMembers public class Switch: ViewConstrainingView, FormValidationProtocol{ public var mvmSwitch = MVMCoreUISwitch() var label = Label() var mfTextButton = MFTextButton() var isRequired: Bool! = false 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() mvmSwitch.addTarget(self, action: #selector(Switch.switchChanged), for: .valueChanged) addSubview(mvmSwitch) addSubview(label) addSubview(mfTextButton) // label.translatesAutoresizingMaskIntoConstraints = false mvmSwitch.translatesAutoresizingMaskIntoConstraints = false mfTextButton.translatesAutoresizingMaskIntoConstraints = false; setupConstraints(forView: self) } public override func updateView(_ size: CGFloat) { super.updateView(size) label.updateView(size) mvmSwitch.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.delegateObject = 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") { mvmSwitch.onTintColor = .mfGet(forHex: onColorString) } if let offColorString = json?.optionalStringForKey("offTintColor") { mvmSwitch.offTintColor = .mfGet(forHex: offColorString) } if let onKnobColorString = json?.optionalStringForKey("onKnobTintColor") { mvmSwitch.onKnobTintColor = .mfGet(forHex: onKnobColorString) } if let offKnobColorString = json?.optionalStringForKey("offKnobTintColor") { mvmSwitch.offKnobTintColor = .mfGet(forHex: offKnobColorString) } // mvmSwitch.isOn = json?.optionalBoolForKey("state") ?? false mvmSwitch.setState(json?.optionalBoolForKey("state") ?? false, animated: true) } func setupConstraints(forView view: UIView) { NSLayoutConstraint.constraintPinLeftSubview(label, leftConstant: 0) 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: 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 : mvmSwitch.isOn } public func formFieldName() -> String? { return json?.optionalStringForKey(KeyFieldKey) } public func formFieldValue() -> Any? { return mvmSwitch.isOn } public override func needsToBeConstrained() -> Bool { return true } public override func moleculeAlignment() -> UIStackView.Alignment { return UIStackView.Alignment.leading } }