99 lines
3.0 KiB
Swift
99 lines
3.0 KiB
Swift
//
|
|
// Control.swift
|
|
// MVMCoreUI
|
|
//
|
|
// Created by Scott Pfeil on 10/23/19.
|
|
// Copyright © 2019 Verizon Wireless. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
@objcMembers open class Control: UIControl, ModelMoleculeViewProtocol {
|
|
//--------------------------------------------------
|
|
// MARK: - Properties
|
|
//--------------------------------------------------
|
|
|
|
public var json: [AnyHashable: Any]?
|
|
public var model: MoleculeProtocol?
|
|
|
|
private var initialSetupPerformed = false
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Initializers
|
|
//--------------------------------------------------
|
|
|
|
public override init(frame: CGRect) {
|
|
super.init(frame: .zero)
|
|
initialSetup()
|
|
}
|
|
|
|
public init() {
|
|
super.init(frame: .zero)
|
|
initialSetup()
|
|
}
|
|
|
|
public required init?(coder: NSCoder) {
|
|
super.init(coder: coder)
|
|
fatalError("Control does not support xib.")
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Setup
|
|
//--------------------------------------------------
|
|
|
|
public func initialSetup() {
|
|
|
|
if !initialSetupPerformed {
|
|
initialSetupPerformed = true
|
|
setupView()
|
|
}
|
|
}
|
|
|
|
public func setWithModel(_ model: MoleculeProtocol?, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [String : AnyHashable]?) {
|
|
self.model = model
|
|
if let backgroundColor = model?.backgroundColor {
|
|
self.backgroundColor = backgroundColor.uiColor
|
|
}
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - UITouch
|
|
//--------------------------------------------------
|
|
|
|
override open func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
|
|
|
|
// If the control is smaller than 44pt by width or height, this will compensate.
|
|
let faultToleranceX: CGFloat = max((MinimumTappableArea - bounds.size.width) / 2.0, 0)
|
|
let faultToleranceY: CGFloat = max((MinimumTappableArea - bounds.size.height) / 2.0, 0)
|
|
let area = bounds.insetBy(dx: -faultToleranceX, dy: -faultToleranceY)
|
|
return area.contains(point)
|
|
}
|
|
}
|
|
|
|
// MARK: - MVMCoreViewProtocol
|
|
extension Control: MVMCoreViewProtocol {
|
|
|
|
public func updateView(_ size: CGFloat) {}
|
|
|
|
/// Will be called only once.
|
|
public func setupView() {
|
|
translatesAutoresizingMaskIntoConstraints = false
|
|
insetsLayoutMarginsFromSafeArea = false
|
|
}
|
|
}
|
|
|
|
// MARK: - MVMCoreUIMoleculeViewProtocol
|
|
extension Control: MVMCoreUIMoleculeViewProtocol {
|
|
public func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
|
|
self.json = json
|
|
|
|
if let backgroundColorString = json?.optionalStringForKey(KeyBackgroundColor) {
|
|
backgroundColor = UIColor.mfGet(forHex: backgroundColorString)
|
|
}
|
|
}
|
|
|
|
public func reset() {
|
|
backgroundColor = .clear
|
|
}
|
|
}
|