Switch molecule
This commit is contained in:
parent
af81b62f93
commit
b77ec675e0
@ -11,14 +11,15 @@ import UIKit
|
|||||||
public class Switch: ViewConstrainingView, FormValidationProtocol {
|
public class Switch: ViewConstrainingView, FormValidationProtocol {
|
||||||
var aSwitch = UISwitch()
|
var aSwitch = UISwitch()
|
||||||
var label = Label()
|
var label = Label()
|
||||||
|
var mfTextButton = MFTextButton()
|
||||||
var isRequired: Bool? = false
|
var isRequired: Bool? = false
|
||||||
var state: Bool? = false
|
var state: Bool? = false
|
||||||
var onTintColor: UIColor? = .clear
|
var onTintColor: UIColor? = .clear
|
||||||
var offTintColor: UIColor? = .clear
|
var offTintColor: UIColor? = .clear
|
||||||
var isOnState: Bool? = false
|
var isOnState: Bool? = false
|
||||||
var fieldKey: String? = ""
|
var fieldKey: String? = ""
|
||||||
|
var delegate: DelegateObject?
|
||||||
|
|
||||||
// MARK: - Inits
|
|
||||||
public init() {
|
public init() {
|
||||||
super.init(frame: .zero)
|
super.init(frame: .zero)
|
||||||
}
|
}
|
||||||
@ -30,47 +31,50 @@ public class Switch: ViewConstrainingView, FormValidationProtocol {
|
|||||||
required public init?(coder aDecoder: NSCoder) {
|
required public init?(coder aDecoder: NSCoder) {
|
||||||
super.init(coder: aDecoder)
|
super.init(coder: aDecoder)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - MVMCoreUIMoleculeViewProtocol
|
|
||||||
open override func setAsMolecule() {
|
open override func setAsMolecule() {
|
||||||
super.setAsMolecule()
|
super.setAsMolecule()
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc func switchChanged(mySwitch: UISwitch) {
|
@objc func switchChanged(mySwitch: UISwitch) {
|
||||||
let value = mySwitch.isOn
|
let formValidator = (self.delegate as? MVMCoreUIDelegateObject)?.formValidationProtocol?.formValidatorModel?()
|
||||||
let str = value ? "Switch is On" : "Switch is Off"
|
formValidator?.enableByValidation()
|
||||||
print("\(str)")
|
|
||||||
// Do something
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
open override func setupView() {
|
open override func setupView() {
|
||||||
super.setupView()
|
super.setupView()
|
||||||
aSwitch.onTintColor = self.onTintColor
|
aSwitch.onTintColor = onTintColor
|
||||||
aSwitch.addTarget(self, action: #selector(switchChanged), for: UIControl.Event.valueChanged)
|
|
||||||
self.addSubview(aSwitch)
|
aSwitch.addTarget(self, action: #selector(Switch.switchChanged(mySwitch:)), for: .valueChanged)
|
||||||
self.addSubview(label)
|
mfTextButton = MFTextButton(nil, constrainHeight: true, forWidth: CGFloat.leastNormalMagnitude)
|
||||||
|
|
||||||
|
addSubview(aSwitch)
|
||||||
|
addSubview(label)
|
||||||
|
addSubview(mfTextButton)
|
||||||
|
|
||||||
label.translatesAutoresizingMaskIntoConstraints = false
|
label.translatesAutoresizingMaskIntoConstraints = false
|
||||||
aSwitch.translatesAutoresizingMaskIntoConstraints = false
|
aSwitch.translatesAutoresizingMaskIntoConstraints = false
|
||||||
|
mfTextButton.translatesAutoresizingMaskIntoConstraints = false;
|
||||||
|
setupConstraints(forView: self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public override func updateView(_ size: CGFloat) {
|
public override func updateView(_ size: CGFloat) {
|
||||||
super.updateView(size)
|
super.updateView(size)
|
||||||
self.setupConstraints(forView: self)
|
label.updateView(size)
|
||||||
|
mfTextButton.updateView(size)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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)
|
||||||
self.isRequired = json?[KeyRequired] as? Bool
|
isRequired = json?[KeyRequired] as? Bool
|
||||||
self.state = json?["state"] as? Bool
|
state = json?["state"] as? Bool
|
||||||
|
|
||||||
|
self.delegate = delegateObject
|
||||||
if let dict = json?.optionalDictionaryForKey("label") {
|
if let dict = json?.optionalDictionaryForKey("label") {
|
||||||
label.text = dict["text"] as? String
|
label.setWithJSON(dict, delegateObject: delegateObject, additionalData: additionalData)
|
||||||
if let textColor = json?.optionalStringForKey("textColor") {
|
}
|
||||||
label.textColor = .mfGet(forHex: textColor)
|
|
||||||
}
|
if let dict = json?.optionalDictionaryForKey("textButton") {
|
||||||
|
mfTextButton.setWithJSON(dict, delegateObject: delegateObject, additionalData: additionalData)
|
||||||
}
|
}
|
||||||
|
|
||||||
if let delegateObject = delegateObject as? MVMCoreUIDelegateObject {
|
if let delegateObject = delegateObject as? MVMCoreUIDelegateObject {
|
||||||
@ -78,63 +82,36 @@ public class Switch: ViewConstrainingView, FormValidationProtocol {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let onColorString = json?.optionalStringForKey("onTintColor") {
|
if let onColorString = json?.optionalStringForKey("onTintColor") {
|
||||||
self.onTintColor = .mfGet(forHex: onColorString)
|
onTintColor = .mfGet(forHex: onColorString)
|
||||||
}
|
}
|
||||||
if let offColorString = json?.optionalStringForKey("offTintColor") {
|
if let offColorString = json?.optionalStringForKey("offTintColor") {
|
||||||
self.offTintColor = .mfGet(forHex: offColorString)
|
offTintColor = .mfGet(forHex: offColorString)
|
||||||
}
|
}
|
||||||
self.fieldKey = json?[KeyFieldKey] as? String
|
|
||||||
aSwitch.onTintColor = self.onTintColor
|
aSwitch.onTintColor = onTintColor
|
||||||
|
aSwitch.isOn = state ?? false
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupConstraints(forView view: UIView) {
|
func setupConstraints(forView view: UIView) {
|
||||||
label.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 0).isActive = true
|
NSLayoutConstraint.constraintPinRightSubview(aSwitch, rightConstant: 0)
|
||||||
label.trailingAnchor.constraint(equalTo: aSwitch.leadingAnchor, constant: 20).isActive = true
|
NSLayoutConstraint.constraintPinLeftSubview(label, leftConstant: 0)
|
||||||
label.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
|
|
||||||
label.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
|
|
||||||
|
|
||||||
aSwitch.leadingAnchor.constraint(equalTo: label.trailingAnchor, constant: PaddingOne).isActive = true
|
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)
|
||||||
//aSwitch.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -50).isActive = true
|
|
||||||
|
|
||||||
|
|
||||||
view.trailingAnchor.constraint(equalTo: aSwitch.trailingAnchor, constant: 0).isActive = true
|
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
|
||||||
aSwitch.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
|
|
||||||
aSwitch.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
|
|
||||||
// aSwitch.widthAnchor.constraint(equalToConstant: 60.0).isActive = true
|
|
||||||
|
|
||||||
|
NSLayoutConstraint.constraintPinLeftSubview(mfTextButton, leftConstant: 0)
|
||||||
|
NSLayoutConstraint.constraintPinBottomSubview(mfTextButton, bottomConstant: 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// MARK: - FormValidationProtocol
|
|
||||||
|
|
||||||
|
|
||||||
// {
|
|
||||||
// "moleculeName":"switch",
|
|
||||||
// "fieldName": "switchOption",
|
|
||||||
// "fieldKey": "switchImei1",
|
|
||||||
// "onTintColor": "#ffff00",
|
|
||||||
// "offTintColor": "#ffffff",
|
|
||||||
// "state": true,
|
|
||||||
// "label": {
|
|
||||||
// "moleculeName": "label",
|
|
||||||
// "text": "This is a switch",
|
|
||||||
// "textColor": "#000000",
|
|
||||||
// "backgroundColor": "#FFFFFF"
|
|
||||||
// },
|
|
||||||
// "required": false,
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
public func isValidField() -> Bool {
|
public func isValidField() -> Bool {
|
||||||
// if self.isRequired ?? false {
|
return (isRequired == false) ? true : aSwitch.isOn
|
||||||
// return aSwitch.isOn
|
|
||||||
// }
|
|
||||||
return aSwitch.isOn
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func formFieldName() -> String? {
|
public func formFieldName() -> String? {
|
||||||
return self.fieldKey ?? ""
|
return json?.optionalStringForKey(KeyFieldKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func formFieldValue() -> Any? {
|
public func formFieldValue() -> Any? {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user