Update button to model

This commit is contained in:
Pfeil, Scott Robert 2020-01-22 09:22:57 -05:00
parent 00d1bf7be6
commit db0cb77756

View File

@ -8,14 +8,11 @@
public typealias ButtonBlock = (Button) -> () public typealias ButtonBlock = (Button) -> ()
@objcMembers open class Button: UIButton, MFButtonProtocol { @objcMembers open class Button: UIButton, MFButtonProtocol {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Properties // MARK: - Properties
//-------------------------------------------------- //--------------------------------------------------
public var actionModel: ActionModelProtocol?
public var json: [AnyHashable: Any]?
public var actionMap: [AnyHashable: Any]?
private var initialSetupPerformed = false private var initialSetupPerformed = false
@ -71,44 +68,20 @@ public typealias ButtonBlock = (Button) -> ()
buttonBlock?(self) buttonBlock?(self)
} }
public func setWithActionMap(_ actionMap: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) { public func setWithAction(_ actionModel: ActionModelProtocol, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
self.actionMap = actionMap self.actionModel = actionModel
buttonDelegate = delegateObject?.buttonDelegate buttonDelegate = delegateObject?.buttonDelegate
addBlock(event: .touchUpInside) { [weak self] sender in addBlock(event: .touchUpInside) { [weak self] sender in
guard let self = self else { return } guard let self = self else { return }
if let data = try? actionModel.encode(using: JSONEncoder()),
if self.buttonDelegate?.button?(self, shouldPerformActionWithMap: actionMap, additionalData: additionalData) ?? true { 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) 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 // MARK: - MVMCoreViewProtocol
extension Button: MVMCoreViewProtocol { extension Button: MVMCoreViewProtocol {
@ -116,7 +89,6 @@ extension Button: MVMCoreViewProtocol {
/// Will be called only once. /// Will be called only once.
public func setupView() { public func setupView() {
translatesAutoresizingMaskIntoConstraints = false translatesAutoresizingMaskIntoConstraints = false
insetsLayoutMarginsFromSafeArea = false insetsLayoutMarginsFromSafeArea = false
titleLabel?.numberOfLines = 0 titleLabel?.numberOfLines = 0