vds_ios/VDS/Protocols/Handlerable.swift
Matt Bruce ce9e64da0f updated comments
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2023-08-04 10:22:31 -05:00

41 lines
1.2 KiB
Swift

//
// Handlerable.swift
// VDS
//
// Created by Matt Bruce on 7/22/22.
//
import Foundation
import Combine
import UIKit
public protocol Handlerable: AnyObject, Initable, Disabling, Surfaceable {
/// Set of Subscribers for any Publishers for this Control.
var subscribers: Set<AnyCancellable> { get set }
/// Key of whether or not updateView() is called in setNeedsUpdate()
var shouldUpdateView: Bool { get set }
/// Function used to make changes to the View based off a change events or from local properties.
func updateView()
}
extension Handlerable {
/// Function called when there are changes in a View based off a change events or from local properties.
public func setNeedsUpdate() {
if shouldUpdateView {
shouldUpdateView = false
updateView()
shouldUpdateView = true
}
}
}
extension Handlerable where Self: UIControl {
/// Helper function to assign a completion block to a specific UIControl Event using Combine and stored in the subscribers.
public func addEvent(event: UIControl.Event, block: @escaping (Self)->()) {
publisher(for: event)
.sink(receiveValue: { c in
block(c)
}).store(in: &subscribers)
}
}