refactored controls

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2022-08-10 14:43:00 -05:00
parent 26642e2d55
commit c368f275cb
9 changed files with 58 additions and 110 deletions

View File

@ -15,10 +15,6 @@ open class Control<ModelType: Modelable>: UIControl, ModelHandlerable, ViewProto
@Published public var model: ModelType @Published public var model: ModelType
private var cancellable: AnyCancellable? private var cancellable: AnyCancellable?
open func set(with model: ModelType) {
self.model = model
}
open func shouldUpdateView(viewModel: ModelType) -> Bool { open func shouldUpdateView(viewModel: ModelType) -> Bool {
fatalError("Implement shouldUpdateView") fatalError("Implement shouldUpdateView")
} }
@ -41,12 +37,22 @@ open class Control<ModelType: Modelable>: UIControl, ModelHandlerable, ViewProto
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Initializers // MARK: - Initializers
//-------------------------------------------------- //--------------------------------------------------
required public init() {
self.model = ModelType()
super.init(frame: .zero)
initialSetup()
}
required public init(with model: ModelType) { public required init(with model: ModelType) {
self.model = model self.model = model
super.init(frame: .zero) super.init(frame: .zero)
initialSetup() initialSetup()
set(with: model) }
public override init(frame: CGRect) {
self.model = ModelType()
super.init(frame: .zero)
initialSetup()
} }
public required init?(coder: NSCoder) { public required init?(coder: NSCoder) {

View File

@ -15,10 +15,6 @@ open class View<ModelType: Modelable>: UIView, ModelHandlerable, ViewProtocol, R
@Published public var model: ModelType @Published public var model: ModelType
private var cancellable: AnyCancellable? private var cancellable: AnyCancellable?
open func set(with model: ModelType) {
self.model = model
}
open func shouldUpdateView(viewModel: ModelType) -> Bool { open func shouldUpdateView(viewModel: ModelType) -> Bool {
fatalError("Implement shouldUpdateView") fatalError("Implement shouldUpdateView")
} }
@ -37,15 +33,26 @@ open class View<ModelType: Modelable>: UIView, ModelHandlerable, ViewProtocol, R
@Proxy(\.model.disabled) @Proxy(\.model.disabled)
open var disabled: Bool open var disabled: Bool
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Initializers // MARK: - Initializers
//-------------------------------------------------- //--------------------------------------------------
required public init() {
self.model = ModelType()
super.init(frame: .zero)
initialSetup()
}
required public init(with model: ModelType) { public required init(with model: ModelType) {
self.model = model self.model = model
super.init(frame: .zero) super.init(frame: .zero)
initialSetup() initialSetup()
set(with: model) }
public override init(frame: CGRect) {
self.model = ModelType()
super.init(frame: .zero)
initialSetup()
} }
public required init?(coder: NSCoder) { public required init?(coder: NSCoder) {

View File

@ -58,21 +58,6 @@ open class CheckboxBase<ModelType: CheckboxModel>: SelectorBase<ModelType> {
return config return config
}() }()
//--------------------------------------------------
// MARK: - Initializers
//--------------------------------------------------
public convenience init() {
self.init(with: ModelType())
}
required public init(with model: ModelType) {
super.init(with: model)
}
required public init?(coder: NSCoder) {
super.init(with: ModelType())
}
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Checkbox View // MARK: - Checkbox View
//-------------------------------------------------- //--------------------------------------------------

View File

@ -12,7 +12,7 @@ import Combine
public class Label:LabelBase<DefaultLabelModel>{} public class Label:LabelBase<DefaultLabelModel>{}
open class LabelBase<ModelType: LabelModel>: UILabel, ModelHandlerable, Initable, Resettable { open class LabelBase<ModelType: LabelModel>: UILabel, ModelHandlerable, Initable, Resettable, ViewProtocol {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Combine Properties // MARK: - Combine Properties
@ -62,32 +62,34 @@ open class LabelBase<ModelType: LabelModel>: UILabel, ModelHandlerable, Initable
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Initializers // MARK: - Initializers
//-------------------------------------------------- //--------------------------------------------------
required public convenience init() { required public init() {
self.init(frame: .zero)
self.model = ModelType() self.model = ModelType()
super.init(frame: .zero)
initialSetup()
} }
public required convenience init(with model: ModelType) { public required init(with model: ModelType) {
self.init()
self.model = model self.model = model
super.init(frame: .zero)
initialSetup()
} }
public override init(frame: CGRect) { public override init(frame: CGRect) {
self.model = ModelType() self.model = ModelType()
super.init(frame: frame) super.init(frame: .zero)
setup() initialSetup()
} }
required public init?(coder: NSCoder) { public required init?(coder: NSCoder) {
self.model = ModelType() self.model = ModelType.init()
super.init(coder: coder) super.init(coder: coder)
setup() initialSetup()
} }
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Public Functions // MARK: - Public Functions
//-------------------------------------------------- //--------------------------------------------------
open func setup() { open func initialSetup() {
backgroundColor = .clear backgroundColor = .clear
numberOfLines = 0 numberOfLines = 0
lineBreakMode = .byWordWrapping lineBreakMode = .byWordWrapping
@ -104,9 +106,13 @@ open class LabelBase<ModelType: LabelModel>: UILabel, ModelHandlerable, Initable
self.updateView(viewModel: viewModel) self.updateView(viewModel: viewModel)
} }
setup()
} }
public func reset() { open func setup() {}
open func reset() {
text = nil text = nil
attributedText = nil attributedText = nil
textColor = .black textColor = .black
@ -117,11 +123,6 @@ open class LabelBase<ModelType: LabelModel>: UILabel, ModelHandlerable, Initable
numberOfLines = 0 numberOfLines = 0
} }
//Modelable
open func set(with model: ModelType) {
self.model = model
}
//-------------------------------------------------- //--------------------------------------------------
// MARK: - State // MARK: - State
//-------------------------------------------------- //--------------------------------------------------

View File

@ -101,22 +101,6 @@ open class RadioButtonBase<ModelType: RadioButtonModel>: SelectorBase<ModelType>
return config return config
}() }()
//--------------------------------------------------
// MARK: - Initializers
//--------------------------------------------------
public convenience init() {
self.init(with: ModelType())
}
required public init(with model: ModelType) {
super.init(with: model)
}
required public init?(coder: NSCoder) {
super.init(with: ModelType())
}
//-------------------------------------------------- //--------------------------------------------------
// MARK: - RadioButton View // MARK: - RadioButton View
//-------------------------------------------------- //--------------------------------------------------

View File

@ -140,21 +140,6 @@ open class SelectorBase<ModelType: SelectorModel>: Control<ModelType>, Changable
} }
} }
//--------------------------------------------------
// MARK: - Initializers
//--------------------------------------------------
public convenience init() {
self.init(with: ModelType())
}
required public init(with model: ModelType) {
super.init(with: model)
}
required public init?(coder: NSCoder) {
super.init(with: ModelType())
}
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Constraints // MARK: - Constraints
//-------------------------------------------------- //--------------------------------------------------

View File

@ -115,19 +115,4 @@ open class SelectorGroup<SelectorType, SelectorGroupType: SelectorGroupModel<Sel
} }
open func didSelect(selector: SelectorHandlerType) { } open func didSelect(selector: SelectorHandlerType) { }
//--------------------------------------------------
// MARK: - Initializers
//--------------------------------------------------
public convenience init() {
self.init(with: ModelType())
}
required public init(with model: ModelType) {
super.init(with: model)
}
required public init?(coder: NSCoder) {
super.init(with: ModelType())
}
} }

View File

@ -171,21 +171,6 @@ open class ToggleBase<ModelType: ToggleModel>: Control<ModelType>, Changable {
} }
} }
//--------------------------------------------------
// MARK: - Initializers
//--------------------------------------------------
public convenience init() {
self.init(with: ModelType())
}
required public init(with model: ModelType) {
super.init(with: model)
}
required public init?(coder: NSCoder) {
super.init(with: ModelType())
}
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Constraints // MARK: - Constraints
//-------------------------------------------------- //--------------------------------------------------

View File

@ -7,7 +7,7 @@
import Foundation import Foundation
public protocol ModelHandlerable { public protocol ModelHandlerable: AnyObject {
associatedtype ModelType: Modelable associatedtype ModelType: Modelable
var model: ModelType { get set } var model: ModelType { get set }
@ -16,3 +16,13 @@ public protocol ModelHandlerable {
func shouldUpdateView(viewModel: ModelType) -> Bool func shouldUpdateView(viewModel: ModelType) -> Bool
func updateView(viewModel: ModelType) func updateView(viewModel: ModelType)
} }
extension ModelHandlerable {
public func set(with model: ModelType) {
if shouldUpdateView(viewModel: model){
updateView(viewModel: model)
self.model = model
}
}
}