vds_ios/VDS/Protocols/ModelHandlerable.swift
Matt Bruce 9e2d5fff47 added Equality to Modelable
added AnyEquality to classes that have an a property with 'any'
added default implementation on shouldUpdateView() to check equality, this can be re-implemented in classes
removed all shouldUpdateView methods everywhere since above was done

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2022-09-16 09:14:27 -05:00

54 lines
1.4 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 shouldUpdateView(viewModel: ModelType) -> Bool {
model != viewModel
}
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()
}
}