Skip to content

Commit

Permalink
Update concatenation logic and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jacksoncheek committed Sep 21, 2023
1 parent 7b08f6e commit 4afb1cf
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
18 changes: 12 additions & 6 deletions BlueprintUI/Sources/Layout/LayoutAttributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -54,7 +54,7 @@ public struct LayoutAttributes {
alpha = 1.0
isUserInteractionEnabled = true
isHidden = false
tintAdjustmentMode = nil
tintAdjustmentMode = .automatic

validateBounds()
validateCenter()
Expand Down Expand Up @@ -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
}


Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion BlueprintUI/Sources/Layout/LayoutSubelement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 20 additions & 7 deletions BlueprintUI/Tests/LayoutAttributesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -202,30 +202,43 @@ 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)

XCTAssertEqual(combined.tintAdjustmentMode, .normal)
}

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)
}
}
}

Expand Down

0 comments on commit 4afb1cf

Please sign in to comment.