154 lines
4.8 KiB
Swift
154 lines
4.8 KiB
Swift
//
|
|
// Switch.swift
|
|
// MVMCoreUI
|
|
//
|
|
// Created by Priya on 5/6/19.
|
|
// Copyright © 2019 Verizon Wireless. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
public class Switch: ViewConstrainingView, FormValidationProtocol {
|
|
var aSwitch = UISwitch()
|
|
var label = Label()
|
|
var isRequired: Bool? = false
|
|
var state: Bool? = false
|
|
var onTintColor: UIColor? = .clear
|
|
var offTintColor: UIColor? = .clear
|
|
var isOnState: Bool? = false
|
|
var fieldKey: String? = ""
|
|
|
|
// MARK: - Inits
|
|
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)
|
|
}
|
|
|
|
// MARK: - MVMCoreUIMoleculeViewProtocol
|
|
open override func setAsMolecule() {
|
|
super.setAsMolecule()
|
|
}
|
|
|
|
@objc func switchChanged(mySwitch: UISwitch) {
|
|
let value = mySwitch.isOn
|
|
let str = value ? "Switch is On" : "Switch is Off"
|
|
print("\(str)")
|
|
// Do something
|
|
}
|
|
|
|
|
|
open override func setupView() {
|
|
super.setupView()
|
|
aSwitch.onTintColor = self.onTintColor
|
|
aSwitch.addTarget(self, action: #selector(switchChanged), for: UIControl.Event.valueChanged)
|
|
self.addSubview(aSwitch)
|
|
self.addSubview(label)
|
|
label.translatesAutoresizingMaskIntoConstraints = false
|
|
aSwitch.translatesAutoresizingMaskIntoConstraints = false
|
|
}
|
|
|
|
|
|
public override func updateView(_ size: CGFloat) {
|
|
super.updateView(size)
|
|
self.setupConstraints(forView: self)
|
|
}
|
|
|
|
|
|
|
|
open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) {
|
|
super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData)
|
|
self.isRequired = json?[KeyRequired] as? Bool
|
|
self.state = json?["state"] as? Bool
|
|
if let dict = json?.optionalDictionaryForKey("label") {
|
|
label.text = dict["text"] as? String
|
|
if let textColor = json?.optionalStringForKey("textColor") {
|
|
label.textColor = .mfGet(forHex: textColor)
|
|
}
|
|
}
|
|
|
|
if let delegateObject = delegateObject as? MVMCoreUIDelegateObject {
|
|
FormValidator.setupValidation(molecule: self, delegate: delegateObject.formValidationProtocol)
|
|
}
|
|
|
|
if let onColorString = json?.optionalStringForKey("onTintColor") {
|
|
self.onTintColor = .mfGet(forHex: onColorString)
|
|
}
|
|
if let offColorString = json?.optionalStringForKey("offTintColor") {
|
|
self.offTintColor = .mfGet(forHex: offColorString)
|
|
}
|
|
self.fieldKey = json?[KeyFieldKey] as? String
|
|
aSwitch.onTintColor = self.onTintColor
|
|
}
|
|
|
|
func setupConstraints(forView view: UIView) {
|
|
label.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 0).isActive = true
|
|
label.trailingAnchor.constraint(equalTo: aSwitch.leadingAnchor, constant: 20).isActive = true
|
|
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
|
|
//aSwitch.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -50).isActive = true
|
|
|
|
|
|
view.trailingAnchor.constraint(equalTo: aSwitch.trailingAnchor, constant: 0).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
|
|
|
|
}
|
|
|
|
|
|
// 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 {
|
|
// if self.isRequired ?? false {
|
|
// return aSwitch.isOn
|
|
// }
|
|
return aSwitch.isOn
|
|
}
|
|
|
|
public func formFieldName() -> String? {
|
|
return self.fieldKey ?? ""
|
|
}
|
|
|
|
public func formFieldValue() -> Any? {
|
|
return aSwitch.isOn
|
|
}
|
|
|
|
public override func needsToBeConstrained() -> Bool {
|
|
return true
|
|
}
|
|
|
|
public override func moleculeAlignment() -> UIStackView.Alignment {
|
|
return UIStackView.Alignment.leading
|
|
}
|
|
|
|
}
|
|
|