From 34d7b60cf2d55be216fe86093ffda0501603f049 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erin=C3=A7=20Olkan=20Dokumac=C4=B1o=C4=9Flu?= <32938402+erincolkan@users.noreply.github.com> Date: Fri, 22 Apr 2022 15:25:22 +0300 Subject: [PATCH] Add automatically computed reuse identifier feature for CollectionViewCell and TableViewCell (#15) * [AddReuseIdentifiersForTableAndCollectionViewCells] - Finish implementation * [AddReuseIdentifiersForTableAndCollectionViewCells] - Add unit tests and improve implementation --- FCExtensionKit.xcodeproj/project.pbxproj | 28 ++++++++++++++ .../UICollectionViewCell+Additions.swift | 20 ++++++++++ Sources/UIKit/UITableViewCell+Additions.swift | 17 +++++++++ .../UICollectionViewCell+AdditionsTests.swift | 37 +++++++++++++++++++ .../UITableViewCell+AdditionsTests.swift | 28 ++++++++++++++ Tests/UIKit/Utilities/FakeCellClasses.swift | 23 ++++++++++++ 6 files changed, 153 insertions(+) create mode 100644 Sources/UIKit/UICollectionViewCell+Additions.swift create mode 100644 Sources/UIKit/UITableViewCell+Additions.swift create mode 100644 Tests/UIKit/UICollectionViewCell+AdditionsTests.swift create mode 100644 Tests/UIKit/UITableViewCell+AdditionsTests.swift create mode 100644 Tests/UIKit/Utilities/FakeCellClasses.swift diff --git a/FCExtensionKit.xcodeproj/project.pbxproj b/FCExtensionKit.xcodeproj/project.pbxproj index b1e41ac..5f6b875 100644 --- a/FCExtensionKit.xcodeproj/project.pbxproj +++ b/FCExtensionKit.xcodeproj/project.pbxproj @@ -7,6 +7,11 @@ objects = { /* Begin PBXBuildFile section */ + A01455772812962200E89557 /* UICollectionViewCell+Additions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A01455762812962200E89557 /* UICollectionViewCell+Additions.swift */; }; + A01455792812968600E89557 /* UITableViewCell+Additions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A01455782812968600E89557 /* UITableViewCell+Additions.swift */; }; + A03050582812B88B0087489C /* FakeCellClasses.swift in Sources */ = {isa = PBXBuildFile; fileRef = A03050572812B88B0087489C /* FakeCellClasses.swift */; }; + A030505A2812B8D90087489C /* UITableViewCell+AdditionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A03050592812B8D90087489C /* UITableViewCell+AdditionsTests.swift */; }; + A030507E2812C0520087489C /* UICollectionViewCell+AdditionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A030507D2812C0520087489C /* UICollectionViewCell+AdditionsTests.swift */; }; D06791AE2704736A00E369CF /* Data+Additions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D06791AD2704736A00E369CF /* Data+Additions.swift */; }; FE7B494926C482C8009AAB82 /* CodableEnum.swift in Sources */ = {isa = PBXBuildFile; fileRef = FE7B494826C482C8009AAB82 /* CodableEnum.swift */; }; FE7B494D26C485D7009AAB82 /* CodableEnumTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FE7B494C26C485D7009AAB82 /* CodableEnumTests.swift */; }; @@ -91,6 +96,11 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + A01455762812962200E89557 /* UICollectionViewCell+Additions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UICollectionViewCell+Additions.swift"; sourceTree = ""; }; + A01455782812968600E89557 /* UITableViewCell+Additions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UITableViewCell+Additions.swift"; sourceTree = ""; }; + A03050572812B88B0087489C /* FakeCellClasses.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FakeCellClasses.swift; sourceTree = ""; }; + A03050592812B8D90087489C /* UITableViewCell+AdditionsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UITableViewCell+AdditionsTests.swift"; sourceTree = ""; }; + A030507D2812C0520087489C /* UICollectionViewCell+AdditionsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UICollectionViewCell+AdditionsTests.swift"; sourceTree = ""; }; D06791AD2704736A00E369CF /* Data+Additions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Data+Additions.swift"; sourceTree = ""; }; FE7B494826C482C8009AAB82 /* CodableEnum.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CodableEnum.swift; sourceTree = ""; }; FE7B494C26C485D7009AAB82 /* CodableEnumTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CodableEnumTests.swift; sourceTree = ""; }; @@ -186,6 +196,14 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + A03050562812B8670087489C /* Utilities */ = { + isa = PBXGroup; + children = ( + A03050572812B88B0087489C /* FakeCellClasses.swift */, + ); + path = Utilities; + sourceTree = ""; + }; FEF297AA264DC78300E50ACB = { isa = PBXGroup; children = ( @@ -290,6 +308,8 @@ FEF297FB264DC81900E50ACB /* UIScrollView+Additions.swift */, FEF297FC264DC81900E50ACB /* UITableView+Additions.swift */, FEF297FD264DC81900E50ACB /* UICollectionView+Additions.swift */, + A01455762812962200E89557 /* UICollectionViewCell+Additions.swift */, + A01455782812968600E89557 /* UITableViewCell+Additions.swift */, ); path = UIKit; sourceTree = ""; @@ -342,6 +362,7 @@ FEF29832264DC85200E50ACB /* UIKit */ = { isa = PBXGroup; children = ( + A03050562812B8670087489C /* Utilities */, FEF29833264DC85200E50ACB /* UIColor+AdditionsTests.swift */, FEF29834264DC85200E50ACB /* Helpers.swift */, FEF29835264DC85200E50ACB /* UIViewController+AdditionsTests.swift */, @@ -350,6 +371,8 @@ FEF29838264DC85200E50ACB /* UIView+AdditionsTests.swift */, FEF29839264DC85200E50ACB /* UIApplication+AdditionsTests.swift */, FEF2983A264DC85200E50ACB /* UIEdgeInsets+AdditionsTests.swift */, + A03050592812B8D90087489C /* UITableViewCell+AdditionsTests.swift */, + A030507D2812C0520087489C /* UICollectionViewCell+AdditionsTests.swift */, ); path = UIKit; sourceTree = ""; @@ -507,6 +530,7 @@ FEF29821264DC81900E50ACB /* UIScrollView+Additions.swift in Sources */, FEF2980E264DC81900E50ACB /* Collection+Additions.swift in Sources */, FEF2981F264DC81900E50ACB /* UILabel+Additions.swift in Sources */, + A01455772812962200E89557 /* UICollectionViewCell+Additions.swift in Sources */, FEF2980B264DC81900E50ACB /* NSMutableAttributedString+Additions.swift in Sources */, FEF29813264DC81900E50ACB /* String+Additions.swift in Sources */, FEF29825264DC81900E50ACB /* WebPProcessor.swift in Sources */, @@ -529,6 +553,7 @@ FEF29807264DC81900E50ACB /* NSNumber+Additions.swift in Sources */, FEF29823264DC81900E50ACB /* UICollectionView+Additions.swift in Sources */, FEF29811264DC81900E50ACB /* CountableClosedRage+Additions.swift in Sources */, + A01455792812968600E89557 /* UITableViewCell+Additions.swift in Sources */, FEF29818264DC81900E50ACB /* UIImage+Additions.swift in Sources */, FEF29808264DC81900E50ACB /* Date+Additions.swift in Sources */, FEF29812264DC81900E50ACB /* Bool+Additions.swift in Sources */, @@ -555,11 +580,14 @@ FEF2986C264DC90900E50ACB /* Int+AdditionsTests.swift in Sources */, FEF29873264DCA2000E50ACB /* CGRect+AdditionsTests.swift in Sources */, FEF29890264DCA5400E50ACB /* UIApplication+AdditionsTests.swift in Sources */, + A030507E2812C0520087489C /* UICollectionViewCell+AdditionsTests.swift in Sources */, FEF29874264DCA2400E50ACB /* URL+AdditionsTests.swift in Sources */, FEF29875264DCA2700E50ACB /* NSString+AdditionsTests.swift in Sources */, FEF29877264DCA2E00E50ACB /* IndexPath+AdditionsTests.swift in Sources */, FEF2988D264DCA5400E50ACB /* UIEdgeInsets+AdditionsTests.swift in Sources */, + A030505A2812B8D90087489C /* UITableViewCell+AdditionsTests.swift in Sources */, FEF29871264DCA1A00E50ACB /* CGSize+AdditionsTests.swift in Sources */, + A03050582812B88B0087489C /* FakeCellClasses.swift in Sources */, FEF2988E264DCA5400E50ACB /* Collection+AdditionsTests.swift in Sources */, FEF2986E264DCA0300E50ACB /* UIImage+AdditionsTests.swift in Sources */, FEF29879264DCA3400E50ACB /* NSMutableAttributedString+AdditionsTests.swift in Sources */, diff --git a/Sources/UIKit/UICollectionViewCell+Additions.swift b/Sources/UIKit/UICollectionViewCell+Additions.swift new file mode 100644 index 0000000..48852b9 --- /dev/null +++ b/Sources/UIKit/UICollectionViewCell+Additions.swift @@ -0,0 +1,20 @@ +// +// UICollectionViewCell+Additions.swift +// FCExtensionKit +// +// Created by Erinç Olkan Dokumacıoğlu on 22.04.2022. +// + +import UIKit + +extension UICollectionViewCell { + /// Automatically changes reuse identifier property of an xib designed cell. + open override var reuseIdentifier: String { + return String(describing: Self.self) + } + + /// Creates a static reuse identifier for collection view cell using its class name. + public static var reuseIdentifier: String { + return String(describing: Self.self) + } +} diff --git a/Sources/UIKit/UITableViewCell+Additions.swift b/Sources/UIKit/UITableViewCell+Additions.swift new file mode 100644 index 0000000..447db24 --- /dev/null +++ b/Sources/UIKit/UITableViewCell+Additions.swift @@ -0,0 +1,17 @@ +// +// UITableViewCell+Additions.swift +// FCExtensionKit +// +// Created by Erinç Olkan Dokumacıoğlu on 22.04.2022. +// + +import UIKit + +public extension UITableViewCell { + /// Creates a static reuse identifier for table view cell using its class name. + /// + /// - Warning: If you are using xib files for your cell design, you are obliged to manually assign a reuse identifier for your cell in the Interface Builder. + static var reuseIdentifier: String { + return String(describing: self) + } +} diff --git a/Tests/UIKit/UICollectionViewCell+AdditionsTests.swift b/Tests/UIKit/UICollectionViewCell+AdditionsTests.swift new file mode 100644 index 0000000..37c4ed6 --- /dev/null +++ b/Tests/UIKit/UICollectionViewCell+AdditionsTests.swift @@ -0,0 +1,37 @@ +// +// UICollectionViewCell+AdditionsTests.swift +// FCExtensionKitTests +// +// Created by Erinç Olkan Dokumacıoğlu on 22.04.2022. +// + +import XCTest +import UIKit +@testable import FCExtensionKit + +class UICollectionViewCell_AdditionsTests: XCTestCase { + var collectionView: UICollectionView! + + override func setUp() { + super.setUp() + + let layout = UICollectionViewLayout() + collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) + collectionView.register(FakeCollectionViewCell.self, forCellWithReuseIdentifier: FakeCollectionViewCell.reuseIdentifier) + } + + func testCanDequeueCellUsingInstanceVariableReuseIdentifier() { + let cellInstance = FakeCollectionViewCell() + let dequeuedCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellInstance.reuseIdentifier, for: IndexPath(row: 0, section: 0)) as? FakeCollectionViewCell + + XCTAssertNotNil(dequeuedCell) + XCTAssertEqual(dequeuedCell?.reuseIdentifier, FakeCollectionViewCell.reuseIdentifier) + } + + func testCanDequeueCellUsingStaticReuseIdentifier() { + let dequeuedCell = collectionView.dequeueReusableCell(withReuseIdentifier: FakeCollectionViewCell.reuseIdentifier, for: IndexPath(row: 0, section: 0)) as? FakeCollectionViewCell + + XCTAssertNotNil(dequeuedCell) + XCTAssertEqual(dequeuedCell?.reuseIdentifier, FakeCollectionViewCell.reuseIdentifier) + } +} diff --git a/Tests/UIKit/UITableViewCell+AdditionsTests.swift b/Tests/UIKit/UITableViewCell+AdditionsTests.swift new file mode 100644 index 0000000..288fb83 --- /dev/null +++ b/Tests/UIKit/UITableViewCell+AdditionsTests.swift @@ -0,0 +1,28 @@ +// +// UITableViewCell+AdditionsTests.swift +// FCExtensionKitTests +// +// Created by Erinç Olkan Dokumacıoğlu on 22.04.2022. +// + +import XCTest +import UIKit +@testable import FCExtensionKit + +class UITableViewCell_AdditionsTests: XCTestCase { + var tableView: UITableView! + + override func setUp() { + super.setUp() + + tableView = UITableView() + tableView.register(FakeTableViewCell.self, forCellReuseIdentifier: FakeTableViewCell.reuseIdentifier) + } + + func testCanDequeueCellUsingReuseIdentifier() { + let cell = tableView.dequeueReusableCell(withIdentifier: FakeTableViewCell.reuseIdentifier) as? FakeTableViewCell + + XCTAssertNotNil(cell) + XCTAssertEqual(cell?.reuseIdentifier, FakeTableViewCell.reuseIdentifier) + } +} diff --git a/Tests/UIKit/Utilities/FakeCellClasses.swift b/Tests/UIKit/Utilities/FakeCellClasses.swift new file mode 100644 index 0000000..f95d6fe --- /dev/null +++ b/Tests/UIKit/Utilities/FakeCellClasses.swift @@ -0,0 +1,23 @@ +// +// FakeCellClasses.swift +// FCExtensionKitTests +// +// Created by Erinç Olkan Dokumacıoğlu on 22.04.2022. +// + +import UIKit +import FCExtensionKit + +class FakeCollectionViewCell: UICollectionViewCell { + +} + +class FakeTableViewCell: UITableViewCell { + override init(style: UITableViewCell.CellStyle = .default, reuseIdentifier: String?) { + super.init(style: style, reuseIdentifier: reuseIdentifier) + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } +}