Skip to content

Commit

Permalink
fix bug remove constraints wrongly (#406)
Browse files Browse the repository at this point in the history
* check if the constraints were modified and then restore the original

* identify constraints added by SkeletonView

* move removal of skeleton constraint to recoverable protocol implementation
  • Loading branch information
Juanpe committed Jun 11, 2021
1 parent 134463e commit c8fdd69
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
8 changes: 8 additions & 0 deletions Sources/Extensions/UIView+Autolayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@ extension UIView {
nonContentSizeLayoutConstraints.filter { $0.firstAttribute == NSLayoutConstraint.Attribute.height }
}

var skeletonHeightConstraints: [NSLayoutConstraint] {
nonContentSizeLayoutConstraints.filter {
$0.firstAttribute == NSLayoutConstraint.Attribute.height
&& $0.identifier?.contains("SkeletonView.Constraint.Height") ?? false
}
}

@discardableResult
func setHeight(equalToConstant constant: CGFloat) -> NSLayoutConstraint {
let heightConstraint = heightAnchor.constraint(equalToConstant: constant)
heightConstraint.identifier = "SkeletonView.Constraint.Height.\(constant)"
NSLayoutConstraint.activate([heightConstraint])
return heightConstraint
}
Expand Down
5 changes: 1 addition & 4 deletions Sources/Helpers/PrepareForSkeletonProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ extension UILabel {
}
}

func restoreBackupHeightConstraints() {
heightConstraints.forEach {
removeConstraint($0)
}
func restoreBackupHeightConstraintsIfNeeded() {
guard !backupHeightConstraints.isEmpty else { return }
NSLayoutConstraint.activate(backupHeightConstraints)
backupHeightConstraints.removeAll()
Expand Down
12 changes: 8 additions & 4 deletions Sources/Recoverable/Recoverable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,16 @@ extension UILabel {
override func recoverViewState(forced: Bool) {
super.recoverViewState(forced: forced)
startTransition { [weak self] in
guard let storedLabelState = self?.labelState else { return }
guard let self = self,
let storedLabelState = self.labelState else {
return
}

self?.restoreBackupHeightConstraints()
NSLayoutConstraint.deactivate(self.skeletonHeightConstraints)
self.restoreBackupHeightConstraintsIfNeeded()

if self?.textColor == .clear || forced {
self?.textColor = storedLabelState.textColor
if self.textColor == .clear || forced {
self.textColor = storedLabelState.textColor
}
}
}
Expand Down

0 comments on commit c8fdd69

Please sign in to comment.