From 3c9692d5b285ca3e94fbd66a93225a97ab70981a Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 5 Jul 2023 20:30:55 -0700 Subject: [PATCH] Remove ambiguating constraint priorities, avoid removing and recreating the same constraints Closes #407 --- ...nNotificationGroupCollectionViewCell.swift | 6 +-- ...wNotificationGroupCollectionViewCell.swift | 4 +- ...equestNotificationCollectionViewCell.swift | 4 +- ...nishedNotificationCollectionViewCell.swift | 5 +- Tusker/Views/Poll/PollOptionView.swift | 7 +-- .../Views/Status/StatusContentContainer.swift | 53 ++++++++++--------- .../TimelineStatusCollectionViewCell.swift | 41 ++++++++------ 7 files changed, 59 insertions(+), 61 deletions(-) diff --git a/Tusker/Screens/Notifications/ActionNotificationGroupCollectionViewCell.swift b/Tusker/Screens/Notifications/ActionNotificationGroupCollectionViewCell.swift index d0995027..5dd71732 100644 --- a/Tusker/Screens/Notifications/ActionNotificationGroupCollectionViewCell.swift +++ b/Tusker/Screens/Notifications/ActionNotificationGroupCollectionViewCell.swift @@ -48,11 +48,7 @@ class ActionNotificationGroupCollectionViewCell: UICollectionViewListCell { $0.axis = .horizontal $0.alignment = .fill $0.spacing = 8 - let heightConstraint = $0.heightAnchor.constraint(equalToConstant: 30) - // the collection view cell imposes a height constraint before it's calculated the actual height - // so let this constraint be broken temporarily to avoid unsatisfiable constraints log spam - heightConstraint.priority = .init(999) - heightConstraint.isActive = true + $0.heightAnchor.constraint(equalToConstant: 30).isActive = true } private lazy var actionLabel = MultiSourceEmojiLabel().configure { diff --git a/Tusker/Screens/Notifications/FollowNotificationGroupCollectionViewCell.swift b/Tusker/Screens/Notifications/FollowNotificationGroupCollectionViewCell.swift index 9f835311..513849c7 100644 --- a/Tusker/Screens/Notifications/FollowNotificationGroupCollectionViewCell.swift +++ b/Tusker/Screens/Notifications/FollowNotificationGroupCollectionViewCell.swift @@ -45,9 +45,7 @@ class FollowNotificationGroupCollectionViewCell: UICollectionViewListCell { ]).configure { $0.axis = .horizontal $0.alignment = .fill - let heightConstraint = $0.heightAnchor.constraint(equalToConstant: 30) - heightConstraint.priority = .init(999) - heightConstraint.isActive = true + $0.heightAnchor.constraint(equalToConstant: 30).isActive = true } private lazy var actionLabel = MultiSourceEmojiLabel().configure { diff --git a/Tusker/Screens/Notifications/FollowRequestNotificationCollectionViewCell.swift b/Tusker/Screens/Notifications/FollowRequestNotificationCollectionViewCell.swift index 871e69b0..1dc17197 100644 --- a/Tusker/Screens/Notifications/FollowRequestNotificationCollectionViewCell.swift +++ b/Tusker/Screens/Notifications/FollowRequestNotificationCollectionViewCell.swift @@ -48,9 +48,7 @@ class FollowRequestNotificationCollectionViewCell: UICollectionViewListCell { ]).configure { $0.axis = .horizontal $0.alignment = .fill - let heightConstraint = $0.heightAnchor.constraint(equalToConstant: 30) - heightConstraint.priority = .init(999) - heightConstraint.isActive = true + $0.heightAnchor.constraint(equalToConstant: 30).isActive = true } private lazy var actionLabel = EmojiLabel().configure { diff --git a/Tusker/Screens/Notifications/PollFinishedNotificationCollectionViewCell.swift b/Tusker/Screens/Notifications/PollFinishedNotificationCollectionViewCell.swift index d33f62d2..2c9758d1 100644 --- a/Tusker/Screens/Notifications/PollFinishedNotificationCollectionViewCell.swift +++ b/Tusker/Screens/Notifications/PollFinishedNotificationCollectionViewCell.swift @@ -91,9 +91,6 @@ class PollFinishedNotificationCollectionViewCell: UICollectionViewListCell { contentView.addSubview(iconView) vStack.translatesAutoresizingMaskIntoConstraints = false contentView.addSubview(vStack) - let vStackBottomConstraint = vStack.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8) - // need something to break during intermediate layouts when the cell imposes a 44pt height :S - vStackBottomConstraint.priority = .init(999) NSLayoutConstraint.activate([ iconView.topAnchor.constraint(equalTo: vStack.topAnchor), iconView.trailingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 16 + 50), @@ -101,7 +98,7 @@ class PollFinishedNotificationCollectionViewCell: UICollectionViewListCell { vStack.leadingAnchor.constraint(equalTo: iconView.trailingAnchor, constant: 8), vStack.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -16), vStack.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8), - vStackBottomConstraint, + vStack.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8), ]) NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPreferences), name: .preferencesChanged, object: nil) diff --git a/Tusker/Views/Poll/PollOptionView.swift b/Tusker/Views/Poll/PollOptionView.swift index 9db21fa2..38ec18c2 100644 --- a/Tusker/Views/Poll/PollOptionView.swift +++ b/Tusker/Views/Poll/PollOptionView.swift @@ -56,13 +56,8 @@ class PollOptionView: UIView { percentLabel.setContentHuggingPriority(.required, for: .horizontal) addSubview(percentLabel) - let minHeightConstraint = heightAnchor.constraint(greaterThanOrEqualToConstant: PollOptionView.minHeight) - // on the first layout, something is weird and this becomes ambiguous even though it's fine on subsequent layouts - // this keeps autolayout from complaining - minHeightConstraint.priority = .required - 1 - NSLayoutConstraint.activate([ - minHeightConstraint, + heightAnchor.constraint(greaterThanOrEqualToConstant: PollOptionView.minHeight), label.topAnchor.constraint(equalTo: topAnchor, constant: 4), label.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -4), diff --git a/Tusker/Views/Status/StatusContentContainer.swift b/Tusker/Views/Status/StatusContentContainer.swift index 842e2e0a..11f63fb4 100644 --- a/Tusker/Views/Status/StatusContentContainer.swift +++ b/Tusker/Views/Status/StatusContentContainer.swift @@ -50,8 +50,9 @@ class StatusContentContainer(useTopSpacer: false).configure { @@ -316,17 +323,12 @@ class TimelineStatusCollectionViewCell: UICollectionViewListCell, StatusCollecti } mainContainerTopToReblogLabelConstraint = mainContainer.topAnchor.constraint(equalTo: timelineReasonHStack.bottomAnchor, constant: 4) + mainContainerTopToReblogLabelConstraint.identifier = "MainContainerTopToReblog" mainContainerTopToSelfConstraint = mainContainer.topAnchor.constraint(equalTo: statusContainer.topAnchor, constant: 8) - // when flipping between topToReblog and topToSelf constraints, the framework sometimes thinks both of them should be active simultaneously - // even though the code never does that; so let this one get broken temporarily - mainContainerTopToSelfConstraint.priority = .init(999) + mainContainerTopToSelfConstraint.identifier = "MainContainerTopToSelf" mainContainerBottomToActionsConstraint = mainContainer.bottomAnchor.constraint(equalTo: actionsContainer.topAnchor, constant: -4) mainContainerBottomToSelfConstraint = mainContainer.bottomAnchor.constraint(equalTo: statusContainer.bottomAnchor, constant: -6) - let metaIndicatorsBottomConstraint = metaIndicatorsView.bottomAnchor.constraint(lessThanOrEqualTo: statusContainer.bottomAnchor, constant: -6) - // sometimes during intermediate layouts, there are conflicting constraints, so let this one get broken temporarily, to avoid a bunch of printing - metaIndicatorsBottomConstraint.priority = .init(999) - NSLayoutConstraint.activate([ // why is this 4 but the mainContainerTopSelfConstraint constant 8? because this looks more balanced timelineReasonHStack.topAnchor.constraint(equalTo: statusContainer.topAnchor, constant: 4), @@ -335,13 +337,14 @@ class TimelineStatusCollectionViewCell: UICollectionViewListCell, StatusCollecti mainContainer.leadingAnchor.constraint(equalTo: statusContainer.leadingAnchor, constant: 16), mainContainer.trailingAnchor.constraint(equalTo: statusContainer.trailingAnchor, constant: -16), + mainContainerBottomToActionsConstraint, actionsContainer.leadingAnchor.constraint(equalTo: statusContainer.leadingAnchor, constant: 16), actionsContainer.trailingAnchor.constraint(equalTo: statusContainer.trailingAnchor, constant: -16), // yes, this is deliberately 6. 4 looks to cramped, 8 looks uneven actionsContainer.bottomAnchor.constraint(equalTo: statusContainer.bottomAnchor, constant: -6), - metaIndicatorsBottomConstraint, + metaIndicatorsView.bottomAnchor.constraint(lessThanOrEqualTo: statusContainer.bottomAnchor, constant: -6), ]) updateActionsVisibility() @@ -590,8 +593,14 @@ class TimelineStatusCollectionViewCell: UICollectionViewListCell, StatusCollecti } timelineReasonHStack.isHidden = hideTimelineReason - mainContainerTopToReblogLabelConstraint.isActive = !hideTimelineReason - mainContainerTopToSelfConstraint.isActive = hideTimelineReason + // do this to make sure the currently active constraint is deactivated first + if hideTimelineReason { + mainContainerTopToReblogLabelConstraint.isActive = false + mainContainerTopToSelfConstraint.isActive = true + } else { + mainContainerTopToSelfConstraint.isActive = false + mainContainerTopToReblogLabelConstraint.isActive = true + } doUpdateUI(status: status, precomputedContent: precomputedContent) @@ -690,11 +699,11 @@ class TimelineStatusCollectionViewCell: UICollectionViewListCell, StatusCollecti } private func updateActionsVisibility() { - if Preferences.shared.hideActionsInTimeline { + if Preferences.shared.hideActionsInTimeline && !actionsContainer.isHidden { actionsContainer.isHidden = true - mainContainerBottomToSelfConstraint.isActive = true mainContainerBottomToActionsConstraint.isActive = false - } else { + mainContainerBottomToSelfConstraint.isActive = true + } else if !Preferences.shared.hideActionsInTimeline && actionsContainer.isHidden { actionsContainer.isHidden = false mainContainerBottomToSelfConstraint.isActive = false mainContainerBottomToActionsConstraint.isActive = true