From 4afb1cfc4d52a49e8e09ded64b08aea066f7247a Mon Sep 17 00:00:00 2001 From: Jackson Cheek Date: Thu, 21 Sep 2023 14:19:34 -0400 Subject: [PATCH] Update concatenation logic and unit tests --- .../Sources/Layout/LayoutAttributes.swift | 18 ++++++++----- .../Sources/Layout/LayoutSubelement.swift | 2 +- BlueprintUI/Tests/LayoutAttributesTests.swift | 27 ++++++++++++++----- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/BlueprintUI/Sources/Layout/LayoutAttributes.swift b/BlueprintUI/Sources/Layout/LayoutAttributes.swift index eac5d6496..18d7c8fff 100755 --- a/BlueprintUI/Sources/Layout/LayoutAttributes.swift +++ b/BlueprintUI/Sources/Layout/LayoutAttributes.swift @@ -30,7 +30,7 @@ public struct LayoutAttributes { public var isHidden: Bool /// Corresponds to `UIView.tintAdjustmentMode`. - public var tintAdjustmentMode: UIView.TintAdjustmentMode? + public var tintAdjustmentMode: UIView.TintAdjustmentMode public init() { self.init(center: .zero, bounds: .zero) @@ -54,7 +54,7 @@ public struct LayoutAttributes { alpha = 1.0 isUserInteractionEnabled = true isHidden = false - tintAdjustmentMode = nil + tintAdjustmentMode = .automatic validateBounds() validateCenter() @@ -93,9 +93,7 @@ public struct LayoutAttributes { view.alpha = alpha view.isUserInteractionEnabled = isUserInteractionEnabled view.isHidden = isHidden - if let tintAdjustmentMode { - view.tintAdjustmentMode = tintAdjustmentMode - } + view.tintAdjustmentMode = tintAdjustmentMode } @@ -163,7 +161,15 @@ public struct LayoutAttributes { result.alpha = alpha * layoutAttributes.alpha result.isUserInteractionEnabled = layoutAttributes.isUserInteractionEnabled && isUserInteractionEnabled result.isHidden = layoutAttributes.isHidden || isHidden - result.tintAdjustmentMode = layoutAttributes.tintAdjustmentMode ?? tintAdjustmentMode + + switch tintAdjustmentMode { + case .dimmed, .normal: + result.tintAdjustmentMode = tintAdjustmentMode + case .automatic: + result.tintAdjustmentMode = layoutAttributes.tintAdjustmentMode + @unknown default: + result.tintAdjustmentMode = layoutAttributes.tintAdjustmentMode + } return result } diff --git a/BlueprintUI/Sources/Layout/LayoutSubelement.swift b/BlueprintUI/Sources/Layout/LayoutSubelement.swift index 3e03721f5..fa8b0c9cd 100644 --- a/BlueprintUI/Sources/Layout/LayoutSubelement.swift +++ b/BlueprintUI/Sources/Layout/LayoutSubelement.swift @@ -182,7 +182,7 @@ extension LayoutSubelement { public var isHidden: Bool = false /// Corresponds to `UIView.tintAdjustmentMode`. - public var tintAdjustmentMode: UIView.TintAdjustmentMode? + public var tintAdjustmentMode: UIView.TintAdjustmentMode = .automatic } @propertyWrapper diff --git a/BlueprintUI/Tests/LayoutAttributesTests.swift b/BlueprintUI/Tests/LayoutAttributesTests.swift index f73d7078f..9388dbeb3 100644 --- a/BlueprintUI/Tests/LayoutAttributesTests.swift +++ b/BlueprintUI/Tests/LayoutAttributesTests.swift @@ -189,9 +189,9 @@ final class LayoutAttributesTests: XCTestCase { func test_concat_tintAdjustmentMode() { do { - /// parent adopts child attribute if parent not set + /// combined adopts child attribute if child is non-`.automatic` var a = LayoutAttributes() - a.tintAdjustmentMode = nil + a.tintAdjustmentMode = .automatic var b = LayoutAttributes() b.tintAdjustmentMode = .normal @@ -202,12 +202,12 @@ final class LayoutAttributesTests: XCTestCase { } do { - /// parent overrides child + /// combined adopts child attribute if both child and parent are non-`.automatic` var a = LayoutAttributes() - a.tintAdjustmentMode = .normal + a.tintAdjustmentMode = .dimmed var b = LayoutAttributes() - b.tintAdjustmentMode = .dimmed + b.tintAdjustmentMode = .normal let combined = b.within(a) @@ -215,17 +215,30 @@ final class LayoutAttributesTests: XCTestCase { } do { - /// child inherits from parent + /// combined inherits from parent if child is `.automatic` var a = LayoutAttributes() a.tintAdjustmentMode = .normal var b = LayoutAttributes() - b.tintAdjustmentMode = nil + b.tintAdjustmentMode = .automatic let combined = b.within(a) XCTAssertEqual(combined.tintAdjustmentMode, .normal) } + + do { + /// combined is `.automatic` if both parent and child attributes are `.automatic` + var a = LayoutAttributes() + a.tintAdjustmentMode = .automatic + + var b = LayoutAttributes() + b.tintAdjustmentMode = .automatic + + let combined = b.within(a) + + XCTAssertEqual(combined.tintAdjustmentMode, .automatic) + } } }