updated to have 2 subjects for the strategies
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
1934da2543
commit
abc7f409e7
@ -16,7 +16,9 @@ open class Control: UIControl, Handlerable, ViewProtocol, Resettable, UserInfoab
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Combine Properties
|
// MARK: - Combine Properties
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
public var subject = PassthroughSubject<Void, Never>()
|
public var delayedSubject = PassthroughSubject<Void, Never>()
|
||||||
|
public var immediateSubject = PassthroughSubject<Void, Never>()
|
||||||
|
public var updateStrategy: HandlerableUpdateStrategy = .immediate
|
||||||
|
|
||||||
/// Set of Subscribers for any Publishers for this Control
|
/// Set of Subscribers for any Publishers for this Control
|
||||||
public var subscribers = Set<AnyCancellable>()
|
public var subscribers = Set<AnyCancellable>()
|
||||||
@ -35,9 +37,6 @@ open class Control: UIControl, Handlerable, ViewProtocol, Resettable, UserInfoab
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
private var initialSetupPerformed = false
|
private var initialSetupPerformed = false
|
||||||
|
|
||||||
/// Key of whether or not updateView() is called in setNeedsUpdate()
|
|
||||||
open var shouldUpdateView: Bool = true
|
|
||||||
|
|
||||||
/// Dictionary for keeping information for this Control use only Primitives
|
/// Dictionary for keeping information for this Control use only Primitives
|
||||||
open var userInfo = [String: Primitive]()
|
open var userInfo = [String: Primitive]()
|
||||||
|
|
||||||
@ -141,6 +140,13 @@ open class Control: UIControl, Handlerable, ViewProtocol, Resettable, UserInfoab
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Overrides
|
// MARK: - Overrides
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
|
open override func didMoveToWindow() {
|
||||||
|
super.didMoveToWindow()
|
||||||
|
if window != nil {
|
||||||
|
print("sss - \(Self.self) changed to delayed update strategy")
|
||||||
|
updateStrategy = .delayed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Update this view based off of property changes
|
/// Update this view based off of property changes
|
||||||
open func updateView() {
|
open func updateView() {
|
||||||
|
|||||||
@ -228,7 +228,6 @@ open class SelectorItemBase<Selector: SelectorControlable>: Control, Errorable,
|
|||||||
|
|
||||||
open override func reset() {
|
open override func reset() {
|
||||||
super.reset()
|
super.reset()
|
||||||
shouldUpdateView = false
|
|
||||||
label.reset()
|
label.reset()
|
||||||
childLabel.reset()
|
childLabel.reset()
|
||||||
errorLabel.reset()
|
errorLabel.reset()
|
||||||
@ -249,7 +248,6 @@ open class SelectorItemBase<Selector: SelectorControlable>: Control, Errorable,
|
|||||||
value = nil
|
value = nil
|
||||||
isSelected = false
|
isSelected = false
|
||||||
|
|
||||||
shouldUpdateView = true
|
|
||||||
setNeedsUpdate()
|
setNeedsUpdate()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -17,7 +17,10 @@ open class View: UIView, Handlerable, ViewProtocol, Resettable, UserInfoable {
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Combine Properties
|
// MARK: - Combine Properties
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
public var subject = PassthroughSubject<Void, Never>()
|
public var delayedSubject = PassthroughSubject<Void, Never>()
|
||||||
|
public var immediateSubject = PassthroughSubject<Void, Never>()
|
||||||
|
public var updateStrategy: HandlerableUpdateStrategy = .immediate
|
||||||
|
|
||||||
public var subscribers = Set<AnyCancellable>()
|
public var subscribers = Set<AnyCancellable>()
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
@ -25,9 +28,6 @@ open class View: UIView, Handlerable, ViewProtocol, Resettable, UserInfoable {
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
private var initialSetupPerformed = false
|
private var initialSetupPerformed = false
|
||||||
|
|
||||||
/// Key of whether or not updateView() is called in setNeedsUpdate()
|
|
||||||
open var shouldUpdateView: Bool = true
|
|
||||||
|
|
||||||
/// Dictionary for keeping information for this Control use only Primitives
|
/// Dictionary for keeping information for this Control use only Primitives
|
||||||
open var userInfo = [String: Primitive]()
|
open var userInfo = [String: Primitive]()
|
||||||
|
|
||||||
@ -72,6 +72,12 @@ open class View: UIView, Handlerable, ViewProtocol, Resettable, UserInfoable {
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Overrides
|
// MARK: - Overrides
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
|
open override func didMoveToWindow() {
|
||||||
|
super.didMoveToWindow()
|
||||||
|
if window != nil {
|
||||||
|
updateStrategy = .delayed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Update this view based off of property changes
|
/// Update this view based off of property changes
|
||||||
open func updateView() {
|
open func updateView() {
|
||||||
|
|||||||
@ -9,32 +9,39 @@ import Foundation
|
|||||||
import Combine
|
import Combine
|
||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
|
public enum HandlerableUpdateStrategy {
|
||||||
|
case immediate
|
||||||
|
case delayed
|
||||||
|
}
|
||||||
|
|
||||||
public protocol Handlerable: AnyObject, Initable, Disabling, Surfaceable {
|
public protocol Handlerable: AnyObject, Initable, Disabling, Surfaceable {
|
||||||
var subject: PassthroughSubject<Void, Never> { get set }
|
var delayedSubject: PassthroughSubject<Void, Never> { get set }
|
||||||
|
var immediateSubject: PassthroughSubject<Void, Never> { get set }
|
||||||
var subscribers: Set<AnyCancellable> { get set }
|
var subscribers: Set<AnyCancellable> { get set }
|
||||||
var shouldUpdateView: Bool { get set }
|
var updateStrategy: HandlerableUpdateStrategy { get set }
|
||||||
func updateView()
|
func updateView()
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Handlerable {
|
extension Handlerable {
|
||||||
public func setupNeedsUpdateEvent() {
|
public func setupNeedsUpdateEvent() {
|
||||||
handlerPublisher().sink { [weak self] _ in
|
delayedSubject
|
||||||
|
.debounce(for: .seconds(0), scheduler: RunLoop.main)
|
||||||
|
.sink { [weak self] _ in
|
||||||
|
self?.updateView()
|
||||||
|
}.store(in: &subscribers)
|
||||||
|
|
||||||
|
immediateSubject
|
||||||
|
.sink { [weak self] _ in
|
||||||
self?.updateView()
|
self?.updateView()
|
||||||
}.store(in: &subscribers)
|
}.store(in: &subscribers)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func handlerPublisher() -> AnyPublisher<Void, Never> {
|
|
||||||
subject
|
|
||||||
.eraseToAnyPublisher()
|
|
||||||
.debounce(for: .milliseconds(50), scheduler: RunLoop.main)
|
|
||||||
.eraseToAnyPublisher()
|
|
||||||
}
|
|
||||||
|
|
||||||
public func setNeedsUpdate() {
|
public func setNeedsUpdate() {
|
||||||
if shouldUpdateView {
|
switch updateStrategy {
|
||||||
shouldUpdateView = false
|
case .delayed:
|
||||||
updateView()
|
delayedSubject.send()
|
||||||
shouldUpdateView = true
|
case .immediate:
|
||||||
|
immediateSubject.send()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user