// // 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() public var label = Label() public var mfTextButton = MFTextButton() var topConstraint_Switch: NSLayoutConstraint? var bottomConstraint_textBtn: NSLayoutConstraint? var isRequired = 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() } 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 ?? false 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) updateContraints() } func updateContraints() { if let isEmptyText = label.text?.isEmpty,isEmptyText == false { topConstraint_Switch?.priority = UILayoutPriority(rawValue:249) NSLayoutConstraint.constraintPinSubview(mvmSwitch, pinCenterX: false, pinCenterY: true) _ = NSLayoutConstraint(pinFirstView: mvmSwitch, toSecondView: mfTextButton, withConstant: PaddingOne, directionVertical: true) } layoutIfNeeded() } func setupConstraints() { NSLayoutConstraint.constraintPinLeftSubview(label, leftConstant: 0) NSLayoutConstraint.constraintPinSubview(mvmSwitch, pinCenterX: false, pinCenterY: true) let dict = NSLayoutConstraint.constraintPinSubview(mvmSwitch, pinTop: true, topConstant: 0, topRelation: .greaterThanOrEqual, pinBottom: true, bottomConstant: 0, bottomRelation: .greaterThanOrEqual, pinLeft: false, leftConstant: 0, leftRelation: .equal, pinRight: true, rightConstant: 0, rightRelation: .greaterThanOrEqual) topConstraint_Switch = dict?[ConstraintTop] as? NSLayoutConstraint 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) } 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 } }