136 lines
6.2 KiB
Swift
136 lines
6.2 KiB
Swift
//
|
|
// Switch.swift
|
|
// MVMCoreUI
|
|
//
|
|
// Created by Priya on 5/6/19.
|
|
// Copyright © 2019 Verizon Wireless. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
@objcMembers public class Switch: ViewConstrainingView, FormValidationProtocol{
|
|
public var mvmSwitch = MVMCoreUISwitch()
|
|
public var label = Label()
|
|
public var leftContainerView = UIView()
|
|
public var mfTextButton = MFTextButton()
|
|
var topConstraint_Switch: NSLayoutConstraint?
|
|
var bottomConstraint_textBtn: NSLayoutConstraint?
|
|
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)
|
|
//leftContainerView.addSubview(mvmSwitch)
|
|
leftContainerView.addSubview(label)
|
|
leftContainerView.addSubview(mfTextButton)
|
|
addSubview(leftContainerView)
|
|
addSubview(mvmSwitch)
|
|
// label.translatesAutoresizingMaskIntoConstraints = false
|
|
mvmSwitch.translatesAutoresizingMaskIntoConstraints = false
|
|
mfTextButton.translatesAutoresizingMaskIntoConstraints = false;
|
|
setupConstraints()
|
|
}
|
|
|
|
public override func updateView(_ size: CGFloat) {
|
|
super.updateView(size)
|
|
label.updateView(size)
|
|
mvmSwitch.updateView(size)
|
|
mfTextButton.updateView(size)
|
|
}
|
|
|
|
open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) {
|
|
super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData)
|
|
isRequired = json?[KeyRequired] as? Bool ?? false
|
|
self.delegateObject = delegateObject
|
|
if let dict = json?.optionalDictionaryForKey("label") {
|
|
label.setWithJSON(dict, delegateObject: delegateObject, additionalData: additionalData)
|
|
}
|
|
if let dict = json?.optionalDictionaryForKey("textButton") {
|
|
mfTextButton.setWithJSON(dict, delegateObject: delegateObject, additionalData: additionalData)
|
|
}
|
|
if let delegateObject = delegateObject as? MVMCoreUIDelegateObject {
|
|
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.isOn = json?.optionalBoolForKey("state") ?? false
|
|
mvmSwitch.setState(json?.optionalBoolForKey("state") ?? false, animated: true)
|
|
// updateContraints(json?.optionalDictionaryForKey("textButton"))
|
|
leftContainerView.backgroundColor = .red
|
|
}
|
|
|
|
func setupConstraints() {
|
|
var constraint = label.topAnchor.constraint(equalTo: topAnchor)
|
|
constraint.priority = UILayoutPriority(rawValue: 249)
|
|
constraint.isActive = true
|
|
label.topAnchor.constraint(greaterThanOrEqualTo: topAnchor).isActive = true
|
|
label.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
|
|
NSLayoutConstraint.constraintPinSubview(mvmSwitch, pinCenterX: false, pinCenterY: true)
|
|
let _ = NSLayoutConstraint.constraintPinSubview(mvmSwitch, pinTop: true, topConstant: 0, topRelation: .greaterThanOrEqual, pinBottom: true, bottomConstant: 0, bottomRelation: .greaterThanOrEqual, pinLeft: false, leftConstant: 0, leftRelation: .equal, pinRight: true, rightConstant: 0, rightRelation: .equal)
|
|
// NSLayoutConstraint.constraintPinRightSubview(mvmSwitch, rightConstant: 0)
|
|
constraint = mvmSwitch.topAnchor.constraint(equalTo: topAnchor)
|
|
constraint.priority = UILayoutPriority(rawValue: 249)
|
|
constraint.isActive = true
|
|
constraint = bottomAnchor.constraint(equalTo: mvmSwitch.bottomAnchor)
|
|
constraint.priority = UILayoutPriority(rawValue: 249)
|
|
constraint.isActive = true
|
|
//topConstraint_Switch = dict?[ConstraintTop] as? NSLayoutConstraint
|
|
NSLayoutConstraint.constraintPinTopSubview(label, topConstant: 0)
|
|
|
|
// mvmSwitch.setContentHuggingPriority(UILayoutPriority(rawValue: 800), for: NSLayoutConstraint.Axis.horizontal)
|
|
|
|
mvmSwitch.leadingAnchor.constraint(greaterThanOrEqualTo: label.trailingAnchor).isActive = true
|
|
mvmSwitch.leadingAnchor.constraint(greaterThanOrEqualTo: mfTextButton.trailingAnchor).isActive = true
|
|
NSLayoutConstraint.constraintPinLeftSubview(mfTextButton, leftConstant: 0)
|
|
constraint = bottomAnchor.constraint(equalTo: mfTextButton.bottomAnchor)
|
|
constraint.priority = UILayoutPriority(249)
|
|
constraint.isActive = true
|
|
bottomAnchor.constraint(greaterThanOrEqualTo: mfTextButton.bottomAnchor).isActive = true
|
|
label.bottomAnchor.constraint(equalTo: mfTextButton.topAnchor, constant: 0).isActive = true
|
|
leftContainerView.setContentHuggingPriority(UILayoutPriority(rawValue: 900), for: NSLayoutConstraint.Axis.horizontal)
|
|
|
|
}
|
|
|
|
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 moleculeAlignment() -> UIStackView.Alignment {
|
|
return UIStackView.Alignment.leading
|
|
}
|
|
|
|
}
|
|
|
|
|