From 207bfaa4f7e578ad85a2a35626f9093c8b7d7062 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanpe=20Catala=CC=81n?= Date: Fri, 11 Jun 2021 10:10:18 +0200 Subject: [PATCH 1/3] check if the constraints were modified and then restore the original --- Sources/Helpers/PrepareForSkeletonProtocol.swift | 4 ++-- Sources/Recoverable/Recoverable.swift | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Sources/Helpers/PrepareForSkeletonProtocol.swift b/Sources/Helpers/PrepareForSkeletonProtocol.swift index dd274f34..daa43a2b 100755 --- a/Sources/Helpers/PrepareForSkeletonProtocol.swift +++ b/Sources/Helpers/PrepareForSkeletonProtocol.swift @@ -47,11 +47,11 @@ extension UILabel { } } - func restoreBackupHeightConstraints() { + func restoreBackupHeightConstraintsIfNeeded() { + guard !backupHeightConstraints.isEmpty else { return } heightConstraints.forEach { removeConstraint($0) } - guard !backupHeightConstraints.isEmpty else { return } NSLayoutConstraint.activate(backupHeightConstraints) backupHeightConstraints.removeAll() } diff --git a/Sources/Recoverable/Recoverable.swift b/Sources/Recoverable/Recoverable.swift index 34fc48e7..a4f1e8a7 100644 --- a/Sources/Recoverable/Recoverable.swift +++ b/Sources/Recoverable/Recoverable.swift @@ -59,7 +59,7 @@ extension UILabel { startTransition { [weak self] in guard let storedLabelState = self?.labelState else { return } - self?.restoreBackupHeightConstraints() + self?.restoreBackupHeightConstraintsIfNeeded() if self?.textColor == .clear || forced { self?.textColor = storedLabelState.textColor From 3a0b2722ed752fd1f8f08a1b6a64db8e877930b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanpe=20Catala=CC=81n?= Date: Fri, 11 Jun 2021 11:00:17 +0200 Subject: [PATCH 2/3] identify constraints added by SkeletonView --- Sources/Extensions/UIView+Autolayout.swift | 8 ++++++++ Sources/Helpers/PrepareForSkeletonProtocol.swift | 5 ++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Sources/Extensions/UIView+Autolayout.swift b/Sources/Extensions/UIView+Autolayout.swift index dc937a58..9769bc55 100644 --- a/Sources/Extensions/UIView+Autolayout.swift +++ b/Sources/Extensions/UIView+Autolayout.swift @@ -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 } diff --git a/Sources/Helpers/PrepareForSkeletonProtocol.swift b/Sources/Helpers/PrepareForSkeletonProtocol.swift index daa43a2b..a9d12d2c 100755 --- a/Sources/Helpers/PrepareForSkeletonProtocol.swift +++ b/Sources/Helpers/PrepareForSkeletonProtocol.swift @@ -48,10 +48,9 @@ extension UILabel { } func restoreBackupHeightConstraintsIfNeeded() { + NSLayoutConstraint.deactivate(skeletonHeightConstraints) + guard !backupHeightConstraints.isEmpty else { return } - heightConstraints.forEach { - removeConstraint($0) - } NSLayoutConstraint.activate(backupHeightConstraints) backupHeightConstraints.removeAll() } From c17b0b597fd5203e59259476c98316af2105132a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanpe=20Catala=CC=81n?= Date: Fri, 11 Jun 2021 11:11:22 +0200 Subject: [PATCH 3/3] move removal of skeleton constraint to recoverable protocol implementation --- Sources/Helpers/PrepareForSkeletonProtocol.swift | 2 -- Sources/Recoverable/Recoverable.swift | 12 ++++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Sources/Helpers/PrepareForSkeletonProtocol.swift b/Sources/Helpers/PrepareForSkeletonProtocol.swift index a9d12d2c..b0831c82 100755 --- a/Sources/Helpers/PrepareForSkeletonProtocol.swift +++ b/Sources/Helpers/PrepareForSkeletonProtocol.swift @@ -48,8 +48,6 @@ extension UILabel { } func restoreBackupHeightConstraintsIfNeeded() { - NSLayoutConstraint.deactivate(skeletonHeightConstraints) - guard !backupHeightConstraints.isEmpty else { return } NSLayoutConstraint.activate(backupHeightConstraints) backupHeightConstraints.removeAll() diff --git a/Sources/Recoverable/Recoverable.swift b/Sources/Recoverable/Recoverable.swift index a4f1e8a7..a603f9c2 100644 --- a/Sources/Recoverable/Recoverable.swift +++ b/Sources/Recoverable/Recoverable.swift @@ -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?.restoreBackupHeightConstraintsIfNeeded() + NSLayoutConstraint.deactivate(self.skeletonHeightConstraints) + self.restoreBackupHeightConstraintsIfNeeded() - if self?.textColor == .clear || forced { - self?.textColor = storedLabelState.textColor + if self.textColor == .clear || forced { + self.textColor = storedLabelState.textColor } } }