79 lines
3.7 KiB
Swift
79 lines
3.7 KiB
Swift
//
|
|
// BarButtonItem.swift
|
|
// MVMCoreUI
|
|
//
|
|
// Created by Scott Pfeil on 5/18/20.
|
|
// Copyright © 2020 Verizon Wireless. All rights reserved.
|
|
//
|
|
|
|
public typealias BarButtonAction = (BarButtonItem) -> ()
|
|
|
|
@objcMembers open class BarButtonItem: UIBarButtonItem, MFButtonProtocol {
|
|
//--------------------------------------------------
|
|
// MARK: - Properties
|
|
//--------------------------------------------------
|
|
|
|
private var buttonAction: BarButtonAction?
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Delegate
|
|
//--------------------------------------------------
|
|
|
|
open weak var buttonDelegate: ButtonDelegateProtocol?
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Initializers
|
|
//--------------------------------------------------
|
|
|
|
/// Creates the item with the passed in action.
|
|
public static func create(with image: UIImage, actionModel: ActionModelProtocol, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) -> Self {
|
|
let button = self.init(image: image, style: .plain, target: self, action: #selector(callActionBlock(_:)))
|
|
button.set(with: actionModel, delegateObject: delegateObject, additionalData: additionalData)
|
|
return button
|
|
}
|
|
|
|
/// Creates the item with the passed in action map.
|
|
public static func create(with image: UIImage, actionMap: [AnyHashable : Any], delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) -> Self {
|
|
let button = self.init(image: image, style: .plain, target: self, action: #selector(callActionBlock(_:)))
|
|
button.set(with: actionMap, delegateObject: delegateObject, additionalData: additionalData)
|
|
return button
|
|
}
|
|
|
|
/// Creates the item with the passed in action.
|
|
public static func create(with image: UIImage, action: @escaping BarButtonAction) -> Self {
|
|
let button = self.init(image: image, style: .plain, target: self, action: #selector(callActionBlock(_:)))
|
|
button.buttonAction = action
|
|
return button
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Methods
|
|
//--------------------------------------------------
|
|
|
|
@objc func callActionBlock(_ sender: BarButtonItem) {
|
|
buttonAction?(self)
|
|
}
|
|
|
|
open func set(with actionModel: ActionModelProtocol, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
|
|
buttonDelegate = delegateObject?.buttonDelegate
|
|
buttonAction = { [weak self] sender in
|
|
guard let self = self else { return }
|
|
if let data = try? actionModel.encode(using: JSONEncoder()),
|
|
let actionMap = try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.init()) as? [AnyHashable: Any],
|
|
delegateObject?.buttonDelegate?.button?(self, shouldPerformActionWithMap: actionMap, additionalData: additionalData) ?? true {
|
|
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegateObject: delegateObject)
|
|
}
|
|
}
|
|
}
|
|
|
|
open func set(with actionMap: [AnyHashable : Any], delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
|
|
buttonDelegate = delegateObject?.buttonDelegate
|
|
buttonAction = { [weak self] sender in
|
|
guard let self = self,
|
|
delegateObject?.buttonDelegate?.button?(self, shouldPerformActionWithMap: actionMap, additionalData: additionalData) ?? true else { return }
|
|
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegateObject: delegateObject)
|
|
}
|
|
}
|
|
}
|
|
|