// // Switch.swift // MVMCoreUI // // Created by Priya on 5/23/19. // Copyright © 2019 Verizon Wireless. All rights reserved. // import UIKit @objcMembers public class Switch: ViewConstrainingView, FormValidationProtocol{ public var mvmSwitch = MVMCoreUISwitch() 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) self.clipsToBounds = true addSubview(mvmSwitch) mvmSwitch.translatesAutoresizingMaskIntoConstraints = false setupContainerConstraints() } public override func updateView(_ size: CGFloat) { super.updateView(size) mvmSwitch.updateView(size) } open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) { super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData) isRequired = json?[KeyRequired] as? Bool ?? false self.delegateObject = delegateObject if let delegateObject = delegateObject { 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.setState(json?.optionalBoolForKey("state") ?? false, animated: true) } func setupContainerConstraints() { mvmSwitch.topAnchor.constraint(equalTo: topAnchor).isActive = true mvmSwitch.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true mvmSwitch.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true mvmSwitch.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = 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 alignment() -> UIStackView.Alignment { return UIStackView.Alignment.leading } public override static func estimatedHeight(forRow json: [AnyHashable : Any]?, delegateObject: MVMCoreUIDelegateObject?) -> CGFloat { return MVMCoreUISwitch.getHeight() } }