Skip to content

Commit ebbbfd9

Browse files
committed
Added Identifier for item & section
1 parent 2fcab4d commit ebbbfd9

File tree

6 files changed

+25
-13
lines changed

6 files changed

+25
-13
lines changed

Sources/SPDiffable/CellProvider/Models/SPDiffableItem.swift

+10-4
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ open class SPDiffableItem: NSObject, NSCopying {
3434
Always shoud be uniq. But if it changed, diffable system remove old and insert new (not reload).
3535
Identifier uses in `Hashable` and `Equatable` protocols.
3636
*/
37-
public var identifier: String
37+
public var identifier: Identifier
3838

39-
public init(identifier: String) {
39+
// MARK: - Init
40+
41+
public init(identifier: Identifier) {
4042
self.identifier = identifier
4143
}
4244

43-
// MARK: Hashable and Equatable
45+
// MARK: - Hashable and Equatable
4446

4547
public override var hash: Int {
4648
var hasher = Hasher()
@@ -53,11 +55,15 @@ open class SPDiffableItem: NSObject, NSCopying {
5355
return identifier == object.identifier
5456
}
5557

56-
// MARK: NSCopying
58+
// MARK: - NSCopying
5759

5860
// Implemented becouse when using with collection,
5961
// sometimes catch error about unregognized selector.
6062
public func copy(with zone: NSZone? = nil) -> Any {
6163
return SPDiffableItem(identifier: self.identifier)
6264
}
65+
66+
// MARK: - Identifier
67+
68+
public typealias Identifier = String
6369
}

Sources/SPDiffable/CellProvider/Models/SPDiffableSection.swift

+10-4
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,22 @@ open class SPDiffableSection: NSObject, NSCopying {
3535
Always shoud be uniq. But if it changed, diffable system remove old and insert new (not reload).
3636
Identifier uses in `Hashable` and `Equatable` protocols.
3737
*/
38-
public var identifier: String
38+
public var identifier: SectionIdentifier
3939
public var header: SPDiffableItem?
4040
public var footer: SPDiffableItem?
4141

4242
public var items: [SPDiffableItem]
4343

44-
public init(identifier: String, header: SPDiffableItem? = nil, footer: SPDiffableItem? = nil, items: [SPDiffableItem] = []) {
44+
// MARK: - Init
45+
46+
public init(identifier: SectionIdentifier, header: SPDiffableItem? = nil, footer: SPDiffableItem? = nil, items: [SPDiffableItem] = []) {
4547
self.identifier = identifier
4648
self.header = header
4749
self.footer = footer
4850
self.items = items
4951
}
5052

51-
// MARK: Hashable and Equatable
53+
// MARK: - Hashable and Equatable
5254

5355
public override var hash: Int {
5456
var hasher = Hasher()
@@ -61,7 +63,7 @@ open class SPDiffableSection: NSObject, NSCopying {
6163
return identifier == object.identifier
6264
}
6365

64-
// MARK: NSCopying
66+
// MARK: - NSCopying
6567

6668
// Implemented becouse when using with collection,
6769
// sometimes catch error about unregognized selector.
@@ -73,4 +75,8 @@ open class SPDiffableSection: NSObject, NSCopying {
7375
items: self.items
7476
)
7577
}
78+
79+
// MARK: - Item Identifier
80+
81+
public typealias SectionIdentifier = String
7682
}

Sources/SPDiffable/Collection/Models/SPDiffableSideBarButton.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ open class SPDiffableSideBarButton: SPDiffableItem {
3434
public var accessories: [UICellAccessory]
3535
public var action: Action
3636

37-
public init(identifier: String? = nil, title: String, image: UIImage?, accessories: [UICellAccessory] = [], action: @escaping Action) {
37+
public init(identifier: SPDiffableItem.Identifier? = nil, title: String, image: UIImage?, accessories: [UICellAccessory] = [], action: @escaping Action) {
3838
self.title = title
3939
self.image = image
4040
self.accessories = accessories

Sources/SPDiffable/Collection/Models/SPDiffableSideBarHeader.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ open class SPDiffableSideBarHeader: SPDiffableItem {
3030
public var text: String
3131
public var accessories: [UICellAccessory]
3232

33-
public init(identifier: String? = nil, text: String, accessories: [UICellAccessory] = []) {
33+
public init(identifier: SPDiffableItem.Identifier? = nil, text: String, accessories: [UICellAccessory] = []) {
3434
self.text = text
3535
self.accessories = accessories
3636
super.init(identifier: identifier ?? text)

Sources/SPDiffable/Collection/Models/SPDiffableSideBarItem.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ open class SPDiffableSideBarItem: SPDiffableItem {
3434
public var accessories: [UICellAccessory]
3535
public var action: Action
3636

37-
public init(identifier: String? = nil, title: String, image: UIImage?, accessories: [UICellAccessory] = [], action: @escaping Action) {
37+
public init(identifier: SPDiffableItem.Identifier? = nil, title: String, image: UIImage?, accessories: [UICellAccessory] = [], action: @escaping Action) {
3838
self.title = title
3939
self.image = image
4040
self.accessories = accessories

Sources/SPDiffable/DataSource/SPDiffableTableDataSource.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ open class SPDiffableTableDataSource: UITableViewDiffableDataSource<SPDiffableSe
9494
/**
9595
SPDiffable: Get index path for item by identifier.
9696
*/
97-
public func indexPath(for itemIdentifier: String) -> IndexPath? {
97+
public func indexPath(for itemIdentifier: SPDiffableItem.Identifier) -> IndexPath? {
9898
return indexPath(for: SPDiffableItem(identifier: itemIdentifier))
9999
}
100100

101101
/**
102102
SPDiffable: Get cell specific type `T` by indetifier.
103103
*/
104-
public func cell<T: UITableViewCell>(_ type: T.Type, for itemIdentifier: String) -> T? {
104+
public func cell<T: UITableViewCell>(_ type: T.Type, for itemIdentifier: SPDiffableItem.Identifier) -> T? {
105105
guard let indexPath = indexPath(for: itemIdentifier) else { return nil }
106106
guard let cell = self.tableView?.cellForRow(at: indexPath) as? T else { return nil }
107107
return cell

0 commit comments

Comments
 (0)