mvm_core_ui/MVMCoreUI/BaseClasses/BarButtonItem.swift
Scott Pfeil d8bf3c9a08 Button -> Delegate -> ActionHandler.
Components will go through delegate. Delegate will choose how to handle it.
2022-08-08 17:45:28 -04:00

42 lines
1.5 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 var model: (MoleculeModelProtocol & NavigationButtonModelProtocol)?
open weak var buttonDelegate: ButtonDelegateProtocol?
var actionDelegate: ActionDelegate?
//--------------------------------------------------
// MARK: - Methods
//--------------------------------------------------
open func set(with model: MoleculeModelProtocol & NavigationButtonModelProtocol, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
self.model = model
buttonDelegate = delegateObject?.buttonDelegate
actionDelegate?.buttonAction = { sender in
let additionalDataWithSource = additionalData.dictionaryAdding(key: KeySourceModel, value: model)
Task(priority: .userInitiated) {
try await Button.performButtonAction(with: model.action, button: sender, delegateObject: delegateObject, additionalData: additionalDataWithSource)
}
}
}
}