diff --git a/VDS/Classes/Control.swift b/VDS/Classes/Control.swift index ae0bba87..7154ec1c 100644 --- a/VDS/Classes/Control.swift +++ b/VDS/Classes/Control.swift @@ -15,10 +15,6 @@ open class Control: UIControl, ModelHandlerable, ViewProto @Published public var model: ModelType private var cancellable: AnyCancellable? - open func set(with model: ModelType) { - self.model = model - } - open func shouldUpdateView(viewModel: ModelType) -> Bool { fatalError("Implement shouldUpdateView") } @@ -41,12 +37,22 @@ open class Control: UIControl, ModelHandlerable, ViewProto //-------------------------------------------------- // 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 super.init(frame: .zero) initialSetup() - set(with: model) + } + + public override init(frame: CGRect) { + self.model = ModelType() + super.init(frame: .zero) + initialSetup() } public required init?(coder: NSCoder) { diff --git a/VDS/Classes/View.swift b/VDS/Classes/View.swift index c15f29b6..6d44f688 100644 --- a/VDS/Classes/View.swift +++ b/VDS/Classes/View.swift @@ -15,10 +15,6 @@ open class View: UIView, ModelHandlerable, ViewProtocol, R @Published public var model: ModelType private var cancellable: AnyCancellable? - open func set(with model: ModelType) { - self.model = model - } - open func shouldUpdateView(viewModel: ModelType) -> Bool { fatalError("Implement shouldUpdateView") } @@ -37,15 +33,26 @@ open class View: UIView, ModelHandlerable, ViewProtocol, R @Proxy(\.model.disabled) open var disabled: Bool + //-------------------------------------------------- // 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 super.init(frame: .zero) initialSetup() - set(with: model) + } + + public override init(frame: CGRect) { + self.model = ModelType() + super.init(frame: .zero) + initialSetup() } public required init?(coder: NSCoder) { diff --git a/VDS/Components/Checkbox/Checkbox.swift b/VDS/Components/Checkbox/Checkbox.swift index 91102971..b5df30ee 100644 --- a/VDS/Components/Checkbox/Checkbox.swift +++ b/VDS/Components/Checkbox/Checkbox.swift @@ -58,21 +58,6 @@ open class CheckboxBase: SelectorBase { 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 //-------------------------------------------------- diff --git a/VDS/Components/Label/Label.swift b/VDS/Components/Label/Label.swift index 5860473e..48dae20b 100644 --- a/VDS/Components/Label/Label.swift +++ b/VDS/Components/Label/Label.swift @@ -12,7 +12,7 @@ import Combine public class Label:LabelBase{} -open class LabelBase: UILabel, ModelHandlerable, Initable, Resettable { +open class LabelBase: UILabel, ModelHandlerable, Initable, Resettable, ViewProtocol { //-------------------------------------------------- // MARK: - Combine Properties @@ -62,32 +62,34 @@ open class LabelBase: UILabel, ModelHandlerable, Initable //-------------------------------------------------- // MARK: - Initializers //-------------------------------------------------- - required public convenience init() { - self.init(frame: .zero) + required public init() { self.model = ModelType() + super.init(frame: .zero) + initialSetup() } - public required convenience init(with model: ModelType) { - self.init() + public required init(with model: ModelType) { self.model = model + super.init(frame: .zero) + initialSetup() } - + public override init(frame: CGRect) { self.model = ModelType() - super.init(frame: frame) - setup() + super.init(frame: .zero) + initialSetup() } - required public init?(coder: NSCoder) { - self.model = ModelType() + public required init?(coder: NSCoder) { + self.model = ModelType.init() super.init(coder: coder) - setup() + initialSetup() } //-------------------------------------------------- // MARK: - Public Functions //-------------------------------------------------- - open func setup() { + open func initialSetup() { backgroundColor = .clear numberOfLines = 0 lineBreakMode = .byWordWrapping @@ -104,9 +106,13 @@ open class LabelBase: UILabel, ModelHandlerable, Initable self.updateView(viewModel: viewModel) } + + setup() } - public func reset() { + open func setup() {} + + open func reset() { text = nil attributedText = nil textColor = .black @@ -116,12 +122,7 @@ open class LabelBase: UILabel, ModelHandlerable, Initable accessibilityTraits = .staticText numberOfLines = 0 } - - //Modelable - open func set(with model: ModelType) { - self.model = model - } - + //-------------------------------------------------- // MARK: - State //-------------------------------------------------- diff --git a/VDS/Components/RadioButton/RadioButton.swift b/VDS/Components/RadioButton/RadioButton.swift index d49884a7..9fed7906 100644 --- a/VDS/Components/RadioButton/RadioButton.swift +++ b/VDS/Components/RadioButton/RadioButton.swift @@ -100,23 +100,7 @@ open class RadioButtonBase: SelectorBase config.forTrue.disabled.darkColor = VDSColor.interactiveDisabledOndark 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 //-------------------------------------------------- diff --git a/VDS/Components/SelectorBase/SelectorBase.swift b/VDS/Components/SelectorBase/SelectorBase.swift index 0625a613..87b39758 100644 --- a/VDS/Components/SelectorBase/SelectorBase.swift +++ b/VDS/Components/SelectorBase/SelectorBase.swift @@ -139,21 +139,6 @@ open class SelectorBase: Control, 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 diff --git a/VDS/Components/SelectorBase/SelectorGroup.swift b/VDS/Components/SelectorBase/SelectorGroup.swift index ddba6530..e379acf3 100644 --- a/VDS/Components/SelectorBase/SelectorGroup.swift +++ b/VDS/Components/SelectorBase/SelectorGroup.swift @@ -115,19 +115,4 @@ open class SelectorGroup: Control, 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 //-------------------------------------------------- diff --git a/VDS/Protocols/ModelHandlerable.swift b/VDS/Protocols/ModelHandlerable.swift index 49a79416..bf63f67c 100644 --- a/VDS/Protocols/ModelHandlerable.swift +++ b/VDS/Protocols/ModelHandlerable.swift @@ -7,7 +7,7 @@ import Foundation -public protocol ModelHandlerable { +public protocol ModelHandlerable: AnyObject { associatedtype ModelType: Modelable var model: ModelType { get set } @@ -16,3 +16,13 @@ public protocol ModelHandlerable { func shouldUpdateView(viewModel: ModelType) -> Bool func updateView(viewModel: ModelType) } + +extension ModelHandlerable { + + public func set(with model: ModelType) { + if shouldUpdateView(viewModel: model){ + updateView(viewModel: model) + self.model = model + } + } +}