51 lines
2.1 KiB
Swift
51 lines
2.1 KiB
Swift
//
|
|
// BarButtonItem.swift
|
|
// MVMCoreUI
|
|
//
|
|
// Created by Scott Pfeil on 5/18/20.
|
|
// Copyright © 2020 Verizon Wireless. All rights reserved.
|
|
//
|
|
|
|
public typealias BarButtonAction = (BarButtonItem) -> ()
|
|
|
|
@objc class ActionDelegate: NSObject {
|
|
var buttonAction: BarButtonAction?
|
|
@objc func callActionBlock(_ sender: BarButtonItem) {
|
|
buttonAction?(sender)
|
|
}
|
|
}
|
|
|
|
@objcMembers open class BarButtonItem: UIBarButtonItem, MFButtonProtocol {
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Delegate
|
|
//--------------------------------------------------
|
|
|
|
open weak var buttonDelegate: ButtonDelegateProtocol?
|
|
var actionDelegate: ActionDelegate?
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Methods
|
|
//--------------------------------------------------
|
|
|
|
open func set(with actionModel: ActionModelProtocol, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
|
|
buttonDelegate = delegateObject?.buttonDelegate
|
|
actionDelegate?.buttonAction = { sender in
|
|
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?(sender, 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
|
|
actionDelegate?.buttonAction = { sender in
|
|
guard delegateObject?.buttonDelegate?.button?(sender, shouldPerformActionWithMap: actionMap, additionalData: additionalData) ?? true else { return }
|
|
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegateObject: delegateObject)
|
|
}
|
|
}
|
|
}
|
|
|