switch modification

This commit is contained in:
Priya 2019-05-16 10:20:13 -04:00
parent 8ef420e3a4
commit 97b7fbebad

View File

@ -8,48 +8,31 @@
import UIKit import UIKit
public class Switch: ViewConstrainingView, FormValidationProtocol { @objcMembers public class Switch: ViewConstrainingView, FormValidationProtocol{
var aSwitch = UISwitch() public var mvmSwitch = UISwitch()
var label = Label() var label = Label()
var mfTextButton = MFTextButton() var mfTextButton = MFTextButton()
var isRequired: Bool? = false 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) { var delegateObject: DelegateObject?
let formValidator = (self.delegate as? MVMCoreUIDelegateObject)?.formValidationProtocol?.formValidatorModel?()
formValidator?.enableByValidation()
@objc func switchChanged() {
let delegate = delegateObject as? MVMCoreUIDelegateObject
if let delegate = delegate {
let formValidator = delegate.formValidationProtocol?.formValidatorModel?()
formValidator?.enableByValidation()
}
} }
open override func setupView() { open override func setupView() {
super.setupView() super.setupView()
aSwitch.onTintColor = onTintColor mvmSwitch.addTarget(self, action: #selector(Switch.switchChanged), for: .valueChanged)
addSubview(mvmSwitch)
aSwitch.addTarget(self, action: #selector(Switch.switchChanged(mySwitch:)), for: .valueChanged)
mfTextButton = MFTextButton(nil, constrainHeight: true, forWidth: CGFloat.leastNormalMagnitude)
addSubview(aSwitch)
addSubview(label) addSubview(label)
addSubview(mfTextButton) addSubview(mfTextButton)
// label.translatesAutoresizingMaskIntoConstraints = false
label.translatesAutoresizingMaskIntoConstraints = false mvmSwitch.translatesAutoresizingMaskIntoConstraints = false
aSwitch.translatesAutoresizingMaskIntoConstraints = false
mfTextButton.translatesAutoresizingMaskIntoConstraints = false; mfTextButton.translatesAutoresizingMaskIntoConstraints = false;
setupConstraints(forView: self) setupConstraints(forView: self)
} }
@ -63,7 +46,7 @@ public class Switch: ViewConstrainingView, FormValidationProtocol {
open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) { open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) {
super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData) super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData)
isRequired = json?[KeyRequired] as? Bool isRequired = json?[KeyRequired] as? Bool
self.delegate = delegateObject self.delegateObject = delegateObject
if let dict = json?.optionalDictionaryForKey("label") { if let dict = json?.optionalDictionaryForKey("label") {
label.setWithJSON(dict, delegateObject: delegateObject, additionalData: additionalData) label.setWithJSON(dict, delegateObject: delegateObject, additionalData: additionalData)
} }
@ -77,28 +60,28 @@ public class Switch: ViewConstrainingView, FormValidationProtocol {
} }
if let onColorString = json?.optionalStringForKey("onTintColor") { if let onColorString = json?.optionalStringForKey("onTintColor") {
onTintColor = .mfGet(forHex: onColorString) mvmSwitch.onTintColor = .mfGet(forHex: onColorString)
} }
if let offColorString = json?.optionalStringForKey("offTintColor") { mvmSwitch.isOn = json?.optionalBoolForKey("state") ?? false
offTintColor = .mfGet(forHex: offColorString)
}
aSwitch.onTintColor = onTintColor
aSwitch.isOn = json?.optionalBoolForKey("state") ?? false
} }
func setupConstraints(forView view: UIView) { func setupConstraints(forView view: UIView) {
NSLayoutConstraint.constraintPinRightSubview(aSwitch, rightConstant: 0)
NSLayoutConstraint.constraintPinLeftSubview(label, leftConstant: 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.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.constraintPinTopSubview(label, topConstant: 0)
NSLayoutConstraint(pinFirstView: label, toSecondView: aSwitch, withConstant: PaddingOne, directionVertical: false)?.isActive = true _ = NSLayoutConstraint(pinFirstView: label, toSecondView: mvmSwitch, withConstant: PaddingOne, directionVertical: false)
NSLayoutConstraint(pinFirstView: label, toSecondView: mfTextButton, withConstant: PaddingOne, directionVertical: true)?.isActive = true _ = NSLayoutConstraint(pinFirstView: label, toSecondView: mfTextButton, withConstant: PaddingOne, directionVertical: true)
NSLayoutConstraint.constraintPinLeftSubview(mfTextButton, leftConstant: 0) NSLayoutConstraint.constraintPinLeftSubview(mfTextButton, leftConstant: 0)
NSLayoutConstraint.constraintPinBottomSubview(mfTextButton, bottomConstant: 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 { public func isValidField() -> Bool {
return (isRequired == false) ? true : aSwitch.isOn return (isRequired == false) ? true : mvmSwitch.isOn
} }
public func formFieldName() -> String? { public func formFieldName() -> String? {
@ -106,7 +89,7 @@ public class Switch: ViewConstrainingView, FormValidationProtocol {
} }
public func formFieldValue() -> Any? { public func formFieldValue() -> Any? {
return aSwitch.isOn return mvmSwitch.isOn
} }
public override func needsToBeConstrained() -> Bool { public override func needsToBeConstrained() -> Bool {