From 85337833d455e1b5ecabebe345431793cb3f5109 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Wed, 2 Nov 2022 11:04:34 -0500 Subject: [PATCH] refactored out layout subviews, causing infinite loop Signed-off-by: Matt Bruce --- VDS/Classes/Control.swift | 6 +----- VDS/Classes/View.swift | 6 +----- VDS/Components/Button/Button.swift | 21 ++++++++++----------- VDS/Components/Label/Label.swift | 28 ++++++++++++++-------------- VDS/Protocols/Handlerable.swift | 7 ++++++- 5 files changed, 32 insertions(+), 36 deletions(-) diff --git a/VDS/Classes/Control.swift b/VDS/Classes/Control.swift index 79a26591..e068d5db 100644 --- a/VDS/Classes/Control.swift +++ b/VDS/Classes/Control.swift @@ -66,6 +66,7 @@ open class Control: UIControl, Handlerable, ViewProtocol, Resettable { if !initialSetupPerformed { initialSetupPerformed = true setup() + setupDidChangeEvent() } } @@ -79,11 +80,6 @@ open class Control: UIControl, Handlerable, ViewProtocol, Resettable { //-------------------------------------------------- // MARK: - Overrides //-------------------------------------------------- - open override func layoutSubviews() { - super.layoutSubviews() - updateView() - } - open func updateView() {} open func reset() { diff --git a/VDS/Classes/View.swift b/VDS/Classes/View.swift index 1a08301a..7ead2431 100644 --- a/VDS/Classes/View.swift +++ b/VDS/Classes/View.swift @@ -65,14 +65,10 @@ open class View: UIView, Handlerable, ViewProtocol, Resettable { if !initialSetupPerformed { initialSetupPerformed = true setup() + setupDidChangeEvent() } } - open override func layoutSubviews() { - super.layoutSubviews() - updateView() - } - //-------------------------------------------------- // MARK: - Overrides //-------------------------------------------------- diff --git a/VDS/Components/Button/Button.swift b/VDS/Components/Button/Button.swift index 0d5ed7d5..c21802fd 100644 --- a/VDS/Components/Button/Button.swift +++ b/VDS/Components/Button/Button.swift @@ -31,7 +31,8 @@ open class Button: UIButton, Handlerable, ViewProtocol, Resettable, Useable { private var minWidthConstraint: NSLayoutConstraint? private var widthConstraint: NSLayoutConstraint? private var heightConstraint: NSLayoutConstraint? - + private var initialSetupPerformed = false + //-------------------------------------------------- // MARK: - Properties //-------------------------------------------------- @@ -120,11 +121,14 @@ open class Button: UIButton, Handlerable, ViewProtocol, Resettable, Useable { // MARK: - Public Functions //-------------------------------------------------- open func initialSetup() { - backgroundColor = .clear - translatesAutoresizingMaskIntoConstraints = false - accessibilityCustomActions = [] - accessibilityTraits = .staticText - setup() + if !initialSetupPerformed { + backgroundColor = .clear + translatesAutoresizingMaskIntoConstraints = false + accessibilityCustomActions = [] + accessibilityTraits = .staticText + setup() + setupDidChangeEvent() + } } open func setup() { @@ -150,11 +154,6 @@ open class Button: UIButton, Handlerable, ViewProtocol, Resettable, Useable { accessibilityCustomActions = [] accessibilityTraits = .staticText } - - open override func layoutSubviews() { - super.layoutSubviews() - updateView() - } //-------------------------------------------------- // MARK: - Overrides diff --git a/VDS/Components/Label/Label.swift b/VDS/Components/Label/Label.swift index 06f610f8..45b5997a 100644 --- a/VDS/Components/Label/Label.swift +++ b/VDS/Components/Label/Label.swift @@ -26,11 +26,13 @@ open class LabelBase: UILabel, Handlerable, ViewProtocol, Resettable { //-------------------------------------------------- // MARK: - Properties //-------------------------------------------------- + private var initialSetupPerformed = false + open var surface: Surface = .light { didSet { didChange() }} open var disabled: Bool = false { didSet { isEnabled = !disabled } } - open var attributes: [any LabelAttributeModel]? { didSet { didChange() }} + open var attributes: [any LabelAttributeModel]? { didSet { didChange() }} open var typograpicalStyle: TypographicalStyle = .defaultStyle { didSet { didChange() }} @@ -85,13 +87,16 @@ open class LabelBase: UILabel, Handlerable, ViewProtocol, Resettable { // MARK: - Public Functions //-------------------------------------------------- open func initialSetup() { - backgroundColor = .clear - numberOfLines = 0 - lineBreakMode = .byWordWrapping - translatesAutoresizingMaskIntoConstraints = false - accessibilityCustomActions = [] - accessibilityTraits = .staticText - setup() + if !initialSetupPerformed { + backgroundColor = .clear + numberOfLines = 0 + lineBreakMode = .byWordWrapping + translatesAutoresizingMaskIntoConstraints = false + accessibilityCustomActions = [] + accessibilityTraits = .staticText + setup() + setupDidChangeEvent() + } } open func setup() {} @@ -111,12 +116,7 @@ open class LabelBase: UILabel, Handlerable, ViewProtocol, Resettable { //-------------------------------------------------- // MARK: - Overrides - //-------------------------------------------------- - open override func layoutSubviews() { - super.layoutSubviews() - updateView() - } - + //-------------------------------------------------- open func updateView() { textAlignment = textPosition.textAlignment textColor = textColorConfiguration.getColor(self) diff --git a/VDS/Protocols/Handlerable.swift b/VDS/Protocols/Handlerable.swift index 70c2508f..db3ad4ab 100644 --- a/VDS/Protocols/Handlerable.swift +++ b/VDS/Protocols/Handlerable.swift @@ -17,6 +17,12 @@ public protocol Handlerable: AnyObject, Initable, Disabling, Surfaceable { extension Handlerable { + public func setupDidChangeEvent() { + handlerPublisher().sink { [weak self] _ in + self?.updateView() + }.store(in: &subscribers) + } + public func handlerPublisher() -> AnyPublisher { subject .eraseToAnyPublisher() @@ -28,6 +34,5 @@ extension Handlerable { extension Handlerable where Self: UIView { public func didChange() { subject.send() - setNeedsLayout() } }