79 lines
2.3 KiB
Swift
79 lines
2.3 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 {
|
|
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()
|
|
}
|
|
|
|
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 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
|
|
|
|
}
|
|
|
|
func setupConstraints(forView view: UIView) {
|
|
leftPin = view.leftAnchor.constraint(equalTo: leftAnchor)
|
|
topPin = view.topAnchor.constraint(equalTo: topAnchor)
|
|
rightPin = rightAnchor.constraint(equalTo: view.rightAnchor)
|
|
bottomPin = bottomAnchor.constraint(equalTo: view.bottomAnchor)
|
|
leftPin?.isActive = true
|
|
topPin?.isActive = true
|
|
rightPin?.isActive = true
|
|
bottomPin?.isActive = true
|
|
}
|
|
|
|
// MARK: - FormValidationProtocol
|
|
|
|
func isValidField() -> Bool {
|
|
if self.isRequired ?? false {
|
|
return self.isOnState ?? false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func formFieldName() -> String {
|
|
return self.fieldKey ?? ""
|
|
}
|
|
|
|
func formFieldValue() -> Bool {
|
|
return self.isOnState ?? false
|
|
}
|
|
|
|
}
|