vds_ios/VDS/Protocols/ModelHandlerable.swift
Matt Bruce 2a53bae78c refactored handlerPublisher()
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2022-09-09 10:44:53 -05:00

50 lines
1.3 KiB
Swift

//
// Modelable.swift
// VDS
//
// Created by Matt Bruce on 7/22/22.
//
import Foundation
import Combine
import UIKit
public protocol ModelHandlerable: AnyObject, Initable {
associatedtype ModelType: Modelable
var model: ModelType { get set }
var modelPublisher: Published<ModelType>.Publisher { get }
var subscribers: Set<AnyCancellable> { get set }
init(with model: ModelType)
func set(with model: ModelType)
func shouldUpdateView(viewModel: ModelType) -> Bool
func updateView(viewModel: ModelType)
}
extension ModelHandlerable {
public init() {
self.init(with: ModelType())
}
public func set(with model: ModelType) {
if shouldUpdateView(viewModel: model){
updateView(viewModel: model)
self.model = model
}
}
public func setupUpdateView() {
handlerPublisher()
.subscribe(on: RunLoop.main)
.sink { [weak self] viewModel in
self?.updateView(viewModel: viewModel)
}
.store(in: &subscribers)
}
public func handlerPublisher() -> AnyPublisher<ModelType, Never> {
modelPublisher
.debounce(for: .seconds(Constants.ModelStateDebounce), scheduler: RunLoop.main)
.eraseToAnyPublisher()
}
}