updated protocol

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2022-08-11 14:13:30 -05:00
parent 6deb2154a9
commit 0076a1ea21
4 changed files with 50 additions and 26 deletions

View File

@ -10,19 +10,17 @@ import UIKit
import Combine
open class Control<ModelType: Modelable>: UIControl, ModelHandlerable, ViewProtocol, Resettable {
open class Control<ModelType: Modelable>: UIControl, ModelHandlerPublishable, ViewProtocol, Resettable {
//--------------------------------------------------
// MARK: - Combine Properties
//--------------------------------------------------
@Published public var model: ModelType = ModelType()
public var modelPublished: Published<ModelType> { _model }
public var modelPublisher: Published<ModelType>.Publisher { $model }
public var cancellables = Set<AnyCancellable>()
open func shouldUpdateView(viewModel: ModelType) -> Bool {
fatalError("Implement shouldUpdateView")
}
open func updateView(viewModel: ModelType) {
fatalError("Implement updateView")
}
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
@ -66,7 +64,7 @@ open class Control<ModelType: Modelable>: UIControl, ModelHandlerable, ViewProto
if !initialSetupPerformed {
initialSetupPerformed = true
//setup viewUpdate
$model.filter { viewModel in
modelPublisher.filter { viewModel in
return self.shouldUpdateView(viewModel: viewModel)
}.debounce(for: .seconds(Constants.ModelStateDebounce), scheduler: RunLoop.main).sink { [weak self] viewModel in
@ -78,6 +76,17 @@ open class Control<ModelType: Modelable>: UIControl, ModelHandlerable, ViewProto
}
}
//--------------------------------------------------
// MARK: - Overrides
//--------------------------------------------------
open func shouldUpdateView(viewModel: ModelType) -> Bool {
fatalError("Implement shouldUpdateView")
}
open func updateView(viewModel: ModelType) {
fatalError("Implement updateView")
}
open func reset() {
backgroundColor = .clear
}

View File

@ -10,19 +10,16 @@ import UIKit
import Combine
open class View<ModelType: Modelable>: UIView, ModelHandlerable, ViewProtocol, Resettable {
open class View<ModelType: Modelable>: UIView, ModelHandlerPublishable, ViewProtocol, Resettable {
//--------------------------------------------------
// MARK: - Combine Properties
//--------------------------------------------------
@Published public var model: ModelType = ModelType()
public var modelPublished: Published<ModelType> { _model }
public var modelPublisher: Published<ModelType>.Publisher { $model }
public var cancellables = Set<AnyCancellable>()
open func shouldUpdateView(viewModel: ModelType) -> Bool {
fatalError("Implement shouldUpdateView")
}
open func updateView(viewModel: ModelType) {
fatalError("Implement updateView")
}
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
@ -66,7 +63,7 @@ open class View<ModelType: Modelable>: UIView, ModelHandlerable, ViewProtocol, R
if !initialSetupPerformed {
initialSetupPerformed = true
//setup viewUpdate
$model.filter { viewModel in
modelPublisher.filter { viewModel in
return self.shouldUpdateView(viewModel: viewModel)
}.debounce(for: .seconds(Constants.ModelStateDebounce), scheduler: RunLoop.main).sink { [weak self] viewModel in
@ -78,6 +75,17 @@ open class View<ModelType: Modelable>: UIView, ModelHandlerable, ViewProtocol, R
}
}
//--------------------------------------------------
// MARK: - Overrides
//--------------------------------------------------
open func shouldUpdateView(viewModel: ModelType) -> Bool {
fatalError("Implement shouldUpdateView")
}
open func updateView(viewModel: ModelType) {
fatalError("Implement updateView")
}
open func reset() {
backgroundColor = .clear
}

View File

@ -12,12 +12,14 @@ import Combine
public class Label:LabelBase<DefaultLabelModel>{}
open class LabelBase<ModelType: LabelModel>: UILabel, ModelHandlerable, Initable, Resettable, ViewProtocol {
open class LabelBase<ModelType: LabelModel>: UILabel, ModelHandlerPublishable, ViewProtocol, Resettable {
//--------------------------------------------------
// MARK: - Combine Properties
//--------------------------------------------------
@Published public var model: ModelType = ModelType()
public var modelPublished: Published<ModelType> { _model }
public var modelPublisher: Published<ModelType>.Publisher { $model }
public var cancellables = Set<AnyCancellable>()
//--------------------------------------------------
@ -95,7 +97,7 @@ open class LabelBase<ModelType: LabelModel>: UILabel, ModelHandlerable, Initable
accessibilityTraits = .staticText
//setup viewUpdate
$model.filter { viewModel in
modelPublisher.filter { viewModel in
return self.shouldUpdateView(viewModel: viewModel)
}.debounce(for: .seconds(Constants.ModelStateDebounce), scheduler: RunLoop.main).sink { [weak self] viewModel in
@ -121,7 +123,7 @@ open class LabelBase<ModelType: LabelModel>: UILabel, ModelHandlerable, Initable
}
//--------------------------------------------------
// MARK: - State
// MARK: - Overrides
//--------------------------------------------------
/// Follow the SwiftUI View paradigm
/// - Parameter viewModel: state
@ -177,7 +179,7 @@ open class LabelBase<ModelType: LabelModel>: UILabel, ModelHandlerable, Initable
text = viewModel.text
}
}
//--------------------------------------------------
// MARK: - Actionable
//--------------------------------------------------

View File

@ -26,3 +26,8 @@ extension ModelHandlerable {
}
}
}
public protocol ModelHandlerPublishable: ModelHandlerable {
var modelPublished: Published<ModelType> { get }
var modelPublisher: Published<ModelType>.Publisher { get }
}