From 36d459d6bfad077dbe94e58592069a368cdc8fcd Mon Sep 17 00:00:00 2001 From: Filipe Lemos Date: Thu, 20 May 2021 10:58:33 +0100 Subject: [PATCH] =?UTF-8?q?Remove=20old=20`UIView`=20extensions=20for=20au?= =?UTF-8?q?to-layout=20constraints=20=F0=9F=93=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Alicerce has two types of auto-layout constraint helpers, these old ones defined as extensions on `UIView` and also a newer, more powerful, approach on the 'Auto-Layout' module. Removing the old approach should avoid confusions and simplify the code provided through Alicerce. --- Alicerce.xcodeproj/project.pbxproj | 8 -- Sources/Extensions/UIKit/UIView.swift | 65 ------------ .../UIKit/UIViewConstraintTestCase.swift | 98 ------------------- 3 files changed, 171 deletions(-) delete mode 100644 Sources/Extensions/UIKit/UIView.swift delete mode 100644 Tests/AlicerceTests/Extensions/UIKit/UIViewConstraintTestCase.swift diff --git a/Alicerce.xcodeproj/project.pbxproj b/Alicerce.xcodeproj/project.pbxproj index 51406015..b4d64a27 100644 --- a/Alicerce.xcodeproj/project.pbxproj +++ b/Alicerce.xcodeproj/project.pbxproj @@ -325,12 +325,10 @@ 65D46CB5203C90E5008E847B /* PersistencePerformanceMetricsTracker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65D46CB4203C90E5008E847B /* PersistencePerformanceMetricsTracker.swift */; }; 73C6051B20F3BBA300D0B643 /* Token.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73C6051A20F3BBA300D0B643 /* Token.swift */; }; 73C6051D20F3BD0E00D0B643 /* TokenTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73C6051C20F3BD0E00D0B643 /* TokenTests.swift */; }; - 97FACBA2203612F6008D0AF0 /* UIView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97FACBA1203612F6008D0AF0 /* UIView.swift */; }; 9D4E3AA1239A6557007F3050 /* CollectionReusableViewSizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9D4E3AA0239A6557007F3050 /* CollectionReusableViewSizer.swift */; }; 9D4E3AA3239A6841007F3050 /* CollectionReusableViewSizerTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9D4E3AA2239A6841007F3050 /* CollectionReusableViewSizerTestCase.swift */; }; 9DEC00AB209A043A00F94353 /* BuilderCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9DEC00AA209A043A00F94353 /* BuilderCache.swift */; }; 9DEC00AE209A052300F94353 /* BuilderCacheTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9DEC00AC209A04D000F94353 /* BuilderCacheTestCase.swift */; }; - F19A442D20384DD400AD6448 /* UIViewConstraintTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = F19A442C20384DD400AD6448 /* UIViewConstraintTestCase.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -661,7 +659,6 @@ 65D46CB4203C90E5008E847B /* PersistencePerformanceMetricsTracker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PersistencePerformanceMetricsTracker.swift; sourceTree = ""; }; 73C6051A20F3BBA300D0B643 /* Token.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Token.swift; sourceTree = ""; }; 73C6051C20F3BD0E00D0B643 /* TokenTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TokenTests.swift; sourceTree = ""; }; - 97FACBA1203612F6008D0AF0 /* UIView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIView.swift; sourceTree = ""; }; 9D4E3AA0239A6557007F3050 /* CollectionReusableViewSizer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollectionReusableViewSizer.swift; sourceTree = ""; }; 9D4E3AA2239A6841007F3050 /* CollectionReusableViewSizerTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollectionReusableViewSizerTestCase.swift; sourceTree = ""; }; 9DEC00AA209A043A00F94353 /* BuilderCache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BuilderCache.swift; sourceTree = ""; }; @@ -670,7 +667,6 @@ F19A028922664D2A00650D93 /* TestNIBCollectionViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = TestNIBCollectionViewCell.xib; sourceTree = ""; }; F19A028C22664EC300650D93 /* TestNIBCollectionReusableView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestNIBCollectionReusableView.swift; sourceTree = ""; }; F19A028D22664EC300650D93 /* TestNIBCollectionReusableView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = TestNIBCollectionReusableView.xib; sourceTree = ""; }; - F19A442C20384DD400AD6448 /* UIViewConstraintTestCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIViewConstraintTestCase.swift; sourceTree = ""; }; F1D1F60022661D0300CC46B3 /* TestNIBTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestNIBTableViewCell.swift; sourceTree = ""; }; F1D1F60122661D0300CC46B3 /* TestNIBTableViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = TestNIBTableViewCell.xib; sourceTree = ""; }; F1D1F60622661EBA00CC46B3 /* TestNIBTableHeaderFooterView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = TestNIBTableHeaderFooterView.xib; sourceTree = ""; }; @@ -903,7 +899,6 @@ 0A266F0C1ED33B34009CD0D7 /* CALayer.swift */, 0A3C2CBE1EA7E18500EFB7D4 /* UIColor.swift */, 1BBEB6081F333E5600D06526 /* UIImage.swift */, - 97FACBA1203612F6008D0AF0 /* UIView.swift */, 0A3C2CBF1EA7E18500EFB7D4 /* UIViewController.swift */, ); path = UIKit; @@ -999,7 +994,6 @@ 0A3C2D391EA7E1EE00EFB7D4 /* UIColorTestCase.swift */, 1BBEB60A1F333E6600D06526 /* UIImageTestCase.swift */, 0A3C2D3A1EA7E1EE00EFB7D4 /* UIViewControllerTestCase.swift */, - F19A442C20384DD400AD6448 /* UIViewConstraintTestCase.swift */, ); path = UIKit; sourceTree = ""; @@ -1844,7 +1838,6 @@ 0A266F9D1ED59FB6009CD0D7 /* JSONLogItemFormatterTestCase.swift in Sources */, 0A3907F91FC366BC0050714A /* ViewModelCollectionReusableViewTestCase.swift in Sources */, 0AECE68721777E7F003A2509 /* URLSessionTask+CancelableTestCase.swift in Sources */, - F19A442D20384DD400AD6448 /* UIViewConstraintTestCase.swift in Sources */, 4838FE5123A951D2007311F0 /* BottomConstrainableProxyTestCase.swift in Sources */, 0A266F9E1ED59FB6009CD0D7 /* MultiLoggerTestCase.swift in Sources */, 0A55DCFC24CB20E400BCEAEB /* Network+ErrorDecodingTestCase.swift in Sources */, @@ -1967,7 +1960,6 @@ 0A6BD638228876A10063CEAB /* Network+URLSessionError.swift in Sources */, 0A708F6620E97B6E001784DA /* AnyAnalyticsTracker.swift in Sources */, 1B667A0A20127C1600A8CD5A /* StackOrchestrator+Store.swift in Sources */, - 97FACBA2203612F6008D0AF0 /* UIView.swift in Sources */, 0A708F6220E96CD1001784DA /* Analytics.swift in Sources */, 0AD7F24820A9D1D000CC927E /* ServerTrustEvaluator.swift in Sources */, 0A132EB724842EFE00FC108A /* StackOrchestratorStore.swift in Sources */, diff --git a/Sources/Extensions/UIKit/UIView.swift b/Sources/Extensions/UIKit/UIView.swift deleted file mode 100644 index fc14f11c..00000000 --- a/Sources/Extensions/UIKit/UIView.swift +++ /dev/null @@ -1,65 +0,0 @@ -import Foundation -import UIKit - -extension UIView { - - /// Similar to NSLayoutConstraint.activate(), but this method ensures that the views - /// that are adding the constraints disable translatesAutoresizingMaskIntoConstraints - /// - /// - Parameter constraints: an array of constrains to activate - public static func activateConstraints(_ constraints: [NSLayoutConstraint]) { - - constraints.forEach { - - ($0.firstItem as? UIView)?.translatesAutoresizingMaskIntoConstraints = false - } - - NSLayoutConstraint.activate(constraints) - } - - public func top(of view: UIView, offset: CGFloat = 0.0) { - - UIView.activateConstraints([ - bottomAnchor.constraint(equalTo: view.topAnchor, constant: -offset) - ]) - } - - public func left(of view: UIView, offset: CGFloat = 0.0) { - - UIView.activateConstraints([ - rightAnchor.constraint(equalTo: view.leftAnchor, constant: -offset) - ]) - } - - public func bottom(of view: UIView, offset: CGFloat = 0.0) { - - UIView.activateConstraints([ - topAnchor.constraint(equalTo: view.bottomAnchor, constant: offset) - ]) - } - - public func right(of view: UIView, offset: CGFloat = 0.0) { - - UIView.activateConstraints([ - leftAnchor.constraint(equalTo: view.rightAnchor, constant: offset) - ]) - } - - public func edges(to view: UIView, insets: UIEdgeInsets = .zero) { - - UIView.activateConstraints([ - topAnchor.constraint(equalTo: view.topAnchor, constant: insets.top), - leftAnchor.constraint(equalTo: view.leftAnchor, constant: insets.left), - bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -insets.bottom), - rightAnchor.constraint(equalTo: view.rightAnchor, constant: -insets.right) - ]) - } - - public func center(in view: UIView, offset: UIOffset = .zero) { - - UIView.activateConstraints([ - centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: offset.horizontal), - centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: offset.vertical) - ]) - } -} diff --git a/Tests/AlicerceTests/Extensions/UIKit/UIViewConstraintTestCase.swift b/Tests/AlicerceTests/Extensions/UIKit/UIViewConstraintTestCase.swift deleted file mode 100644 index 465053e7..00000000 --- a/Tests/AlicerceTests/Extensions/UIKit/UIViewConstraintTestCase.swift +++ /dev/null @@ -1,98 +0,0 @@ -import XCTest - -@testable import Alicerce - -class UIViewConstraintTestCase: XCTestCase { - - var superview: UIView! - - var view1: UIView! - var view2: UIView! - - override func setUp() { - super.setUp() - - superview = UIView() - - view1 = UIView() - view2 = UIView() - - superview.addSubview(view1) - superview.addSubview(view2) - } - - override func tearDown() { - superview = nil - view1 = nil - view2 = nil - - super.tearDown() - } - - func test_translatesAutoresizingMaskIntoConstraints_is_false() { - - UIView.activateConstraints([ - view1.topAnchor.constraint(equalTo: superview.topAnchor), - view2.topAnchor.constraint(equalTo: view1.topAnchor), - view2.bottomAnchor.constraint(equalTo: superview.bottomAnchor) - ]) - - XCTAssert(view1.translatesAutoresizingMaskIntoConstraints == false) - XCTAssert(view2.translatesAutoresizingMaskIntoConstraints == false) - } - - func test_left_adds_constraine() { - - XCTAssert(superview.constraints.count == 0) - - view1.left(of: view2) - - XCTAssert(superview.constraints.count == 1) - } - - func test_right_adds_constraine() { - - XCTAssert(superview.constraints.count == 0) - - view1.right(of: view2) - - XCTAssert(superview.constraints.count == 1) - } - - func test_top_adds_constraine() { - - XCTAssert(superview.constraints.count == 0) - - view1.top(of: view2) - - XCTAssert(superview.constraints.count == 1) - } - - func test_bottom_adds_constraine() { - - XCTAssert(superview.constraints.count == 0) - - view1.bottom(of: view2) - - XCTAssert(superview.constraints.count == 1) - } - - func test_center_adds_constraine() { - - XCTAssert(superview.constraints.count == 0) - - view1.center(in: view2) - - XCTAssert(superview.constraints.count == 2) - } - - func test_edges_adds_constraine() { - - XCTAssert(superview.constraints.count == 0) - - view1.edges(to: view2) - - XCTAssert(superview.constraints.count == 4) - } - -}