From 8028c8a7854c352b895111315ecff4f4b5aaa539 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Thu, 19 Sep 2024 14:34:24 -0500 Subject: [PATCH] removed constraints and for this class to be setup correctly in its parent rather than doing it in the view. similar to how UIButton works. --- VDS/Components/Footnote/FootnoteGroup.swift | 39 +++++---------------- VDS/Components/Footnote/FootnoteItem.swift | 23 ++++-------- 2 files changed, 15 insertions(+), 47 deletions(-) diff --git a/VDS/Components/Footnote/FootnoteGroup.swift b/VDS/Components/Footnote/FootnoteGroup.swift index cb129f04..5d325d8b 100644 --- a/VDS/Components/Footnote/FootnoteGroup.swift +++ b/VDS/Components/Footnote/FootnoteGroup.swift @@ -63,7 +63,7 @@ open class FootnoteGroup: View { } else { _width = nil } - setNeedsUpdate() + updateContainerWidth() } } @@ -83,7 +83,7 @@ open class FootnoteGroup: View { //-------------------------------------------------- // MARK: - Configuration Properties //-------------------------------------------------- - internal var maxWidth: CGFloat { frame.size.width } + internal var maxWidth: CGFloat { horizontalPinnedWidth() ?? (superview?.frame.size.width ?? frame.size.width) } internal var minWidth: CGFloat { containerSize.width } internal var containerSize: CGSize { CGSize(width: 55, height: 44) } @@ -91,8 +91,6 @@ open class FootnoteGroup: View { // MARK: - Constraints //-------------------------------------------------- internal var widthConstraint: NSLayoutConstraint? - internal var trailingEqualsConstraint: NSLayoutConstraint? - internal var trailingLessThanEqualsConstraint: NSLayoutConstraint? //-------------------------------------------------- // MARK: - Overrides @@ -104,12 +102,8 @@ open class FootnoteGroup: View { // add footnote item stackview. addSubview(stackView) - stackView.pinTop().pinBottom().pinLeading() - trailingEqualsConstraint = stackView.pinTrailing(anchor: trailingAnchor) - - // width constraints - trailingLessThanEqualsConstraint = stackView.pinTrailingLessThanOrEqualTo(anchor: trailingAnchor)?.deactivate() - widthConstraint = stackView.widthAnchor.constraint(equalToConstant: 0).deactivate() + stackView.pinToSuperView() + widthConstraint = widthAnchor.constraint(equalToConstant: 0).deactivate() } open override func setDefaults() { @@ -117,18 +111,7 @@ open class FootnoteGroup: View { width = nil footnoteItems = [] } - - /// Resets to default settings. - open override func reset() { - super.reset() - } - - /// Used to make changes to the View based off a change events or from local properties. - open override func updateView() { - super.updateView() - updateContainerWidth() - } - + internal func updateFootnoteItems() { // symbol containers are as wide as the widest symbol container in the group. var symbolMaxWidth = 0.0 @@ -151,10 +134,11 @@ open class FootnoteGroup: View { stackView.addArrangedSubview(footnote) } } - + /// Update container width after updating content. internal func updateContainerWidth() { var newWidth = 0.0 + switch width { case .percentage(let percentage): newWidth = max(maxWidth * ((percentage) / 100), minWidth) @@ -162,20 +146,15 @@ open class FootnoteGroup: View { case .value(let value): newWidth = value > maxWidth ? maxWidth : value - case nil: - newWidth = maxWidth + case nil: break + } widthConstraint?.deactivate() - trailingLessThanEqualsConstraint?.deactivate() - trailingEqualsConstraint?.deactivate() if newWidth > minWidth && newWidth < maxWidth { widthConstraint?.constant = newWidth widthConstraint?.activate() - trailingLessThanEqualsConstraint?.activate() - } else { - trailingEqualsConstraint?.activate() } } } diff --git a/VDS/Components/Footnote/FootnoteItem.swift b/VDS/Components/Footnote/FootnoteItem.swift index 98233b3a..489c05be 100644 --- a/VDS/Components/Footnote/FootnoteItem.swift +++ b/VDS/Components/Footnote/FootnoteItem.swift @@ -117,7 +117,7 @@ open class FootnoteItem: View { } else { _width = nil } - setNeedsUpdate() + updateContainerWidth() } } @@ -152,7 +152,7 @@ open class FootnoteItem: View { //-------------------------------------------------- // MARK: - Configuration Properties //-------------------------------------------------- - internal var maxWidth: CGFloat { frame.size.width } + internal var maxWidth: CGFloat { horizontalPinnedWidth() ?? (superview?.frame.size.width ?? frame.size.width) } internal var minWidth: CGFloat { containerSize.width } internal var containerSize: CGSize { CGSize(width: 45, height: 44) } @@ -161,8 +161,6 @@ open class FootnoteItem: View { //-------------------------------------------------- internal var symbolWidthConstraint: NSLayoutConstraint? internal var itemWidthConstraint: NSLayoutConstraint? - internal var trailingEqualsConstraint: NSLayoutConstraint? - internal var trailingLessThanEqualsConstraint: NSLayoutConstraint? //-------------------------------------------------- // MARK: - Overrides @@ -174,12 +172,10 @@ open class FootnoteItem: View { // add footnote item stackview. addSubview(itemStackView) - itemStackView.pinTop().pinBottom().pinLeading() - trailingEqualsConstraint = itemStackView.pinTrailing(anchor: trailingAnchor) + itemStackView.pinToSuperView() // width constraints - itemWidthConstraint = itemStackView.widthAnchor.constraint(equalToConstant: 0).deactivate() - trailingLessThanEqualsConstraint = itemStackView.pinTrailingLessThanOrEqualTo(anchor: trailingAnchor)?.deactivate() + itemWidthConstraint = widthAnchor.constraint(equalToConstant: 0).deactivate() // add symbol label, text label to stack. itemStackView.addArrangedSubview(symbolLabel) @@ -236,8 +232,6 @@ open class FootnoteItem: View { attributes.append(TooltipLabelAttribute(surface: surface, model: tooltipModel, presenter: self)) textLabel.attributes = attributes } - - updateContainerWidth() } /// Update container width after updating content. @@ -251,18 +245,13 @@ open class FootnoteItem: View { newWidth = value > maxWidth ? maxWidth : value case nil: - newWidth = maxWidth - } + break + } itemWidthConstraint?.deactivate() - trailingLessThanEqualsConstraint?.deactivate() - trailingEqualsConstraint?.deactivate() if newWidth > minWidth && newWidth < maxWidth { itemWidthConstraint?.constant = newWidth itemWidthConstraint?.activate() - trailingLessThanEqualsConstraint?.activate() - } else { - trailingEqualsConstraint?.activate() } } }