mvm_core_ui/MVMCoreUI/BaseClasses/Button.swift
2020-01-09 16:01:12 -05:00

134 lines
4.0 KiB
Swift

//
// Button.swift
// MVMCoreUI
//
// Created by Robinson, Blake on 12/18/19.
// Copyright © 2019 Verizon Wireless. All rights reserved.
//
public typealias ButtonBlock = (Button) -> ()
@objcMembers open class Button: UIButton, MFButtonProtocol {
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
public var json: [AnyHashable: Any]?
public var actionMap: [AnyHashable: Any]?
private var initialSetupPerformed = false
private var buttonBlock: ButtonBlock?
//--------------------------------------------------
// MARK: - Delegate
//--------------------------------------------------
public weak var buttonDelegate: ButtonDelegateProtocol?
//--------------------------------------------------
// MARK: - Initializers
//--------------------------------------------------
public override init(frame: CGRect) {
super.init(frame: .zero)
initialSetup()
}
public convenience init() {
self.init(frame: .zero)
initialSetup()
}
public required init?(coder: NSCoder) {
super.init(coder: coder)
fatalError("Button does not support xib.")
}
//--------------------------------------------------
// MARK: - Setup
//--------------------------------------------------
public func initialSetup() {
if !initialSetupPerformed {
initialSetupPerformed = true
setupView()
}
}
//--------------------------------------------------
// MARK: - Methods
//--------------------------------------------------
public func addBlock( event: Event, _ buttonBlock: @escaping ButtonBlock) {
self.buttonBlock = buttonBlock
addTarget(self, action: #selector(callBlock(_:)), for: event)
}
func callBlock(_ sender: Button) {
buttonBlock?(self)
}
public func setWithActionMap(_ actionMap: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
self.actionMap = actionMap
buttonDelegate = delegateObject?.buttonDelegate
addBlock(event: .touchUpInside) { [weak self] sender in
guard let self = self else { return }
if self.buttonDelegate?.button?(self, shouldPerformActionWithMap: actionMap, additionalData: additionalData) ?? true {
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegateObject: delegateObject)
}
}
}
}
// MARK: - MVMCoreUIMoleculeViewProtocol
extension Button: MVMCoreUIMoleculeViewProtocol {
public func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
self.json = json
guard let dictionary = json else { return }
if let backgroundColorString = dictionary[KeyBackgroundColor] as? String {
backgroundColor = UIColor.mfGet(forHex: backgroundColorString)
}
if let title = dictionary[KeyTitle] as? String {
setTitle(title, for: .normal)
}
}
public func reset() {
backgroundColor = .clear
}
}
// MARK: - MVMCoreViewProtocol
extension Button: MVMCoreViewProtocol {
public func updateView(_ size: CGFloat) {}
/// Will be called only once.
public func setupView() {
translatesAutoresizingMaskIntoConstraints = false
insetsLayoutMarginsFromSafeArea = false
titleLabel?.numberOfLines = 0
titleLabel?.lineBreakMode = .byWordWrapping
}
}
// MARK: AppleGuidelinesProtocol
extension Button: AppleGuidelinesProtocol {
override open func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
return Self.acceptablyOutsideBounds(point: point, bounds: bounds)
}
}