Skip to content

Commit c65795a

Browse files
author
danny
committed
added NewCommonCollectionViewHandler that uses diffable data source
1 parent 16dac5a commit c65795a

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

OnoKit.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
76FBC99427D7B864005FE78E /* NewCommonCollectionViewHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76FBC99327D7B864005FE78E /* NewCommonCollectionViewHandler.swift */; };
1011
770456EC251522F1006CC071 /* Common.h in Headers */ = {isa = PBXBuildFile; fileRef = 770456EA251522F1006CC071 /* Common.h */; settings = {ATTRIBUTES = (Public, ); }; };
1112
7704570525152373006CC071 /* UINavigationControllerExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 770456F625152372006CC071 /* UINavigationControllerExtension.swift */; };
1213
7704570625152373006CC071 /* UIButtonExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 770456F725152372006CC071 /* UIButtonExtension.swift */; };
@@ -83,6 +84,7 @@
8384
55A9E3F1689CD6C95BE0112C /* Pods_OnoKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_OnoKit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
8485
678736D52C2FCD745E0D3315 /* Pods-OnoKit.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-OnoKit.debug.xcconfig"; path = "Target Support Files/Pods-OnoKit/Pods-OnoKit.debug.xcconfig"; sourceTree = "<group>"; };
8586
7020874F68DD9D1C4D9477CE /* Pods-OnoKit.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-OnoKit.release.xcconfig"; path = "Target Support Files/Pods-OnoKit/Pods-OnoKit.release.xcconfig"; sourceTree = "<group>"; };
87+
76FBC99327D7B864005FE78E /* NewCommonCollectionViewHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewCommonCollectionViewHandler.swift; sourceTree = "<group>"; };
8688
770456E7251522F1006CC071 /* OnoKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = OnoKit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
8789
770456EA251522F1006CC071 /* Common.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Common.h; sourceTree = "<group>"; };
8890
770456EB251522F1006CC071 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -226,6 +228,7 @@
226228
77045716251523BD006CC071 /* CommonButton.swift */,
227229
77045718251523BD006CC071 /* CommonCollectionView.swift */,
228230
77045717251523BD006CC071 /* CommonCollectionViewHandler.swift */,
231+
76FBC99327D7B864005FE78E /* NewCommonCollectionViewHandler.swift */,
229232
7704571D251523BD006CC071 /* CommonDateFormatter.swift */,
230233
7704571C251523BD006CC071 /* CommonImageView.swift */,
231234
7704571F251523BE006CC071 /* CommonLabel.swift */,
@@ -503,6 +506,7 @@
503506
7704571125152373006CC071 /* UILabelExtension.swift in Sources */,
504507
774D019B251B0541005F35B2 /* UIFontExtension.swift in Sources */,
505508
77045726251523BE006CC071 /* CommonTextField.swift in Sources */,
509+
76FBC99427D7B864005FE78E /* NewCommonCollectionViewHandler.swift in Sources */,
506510
776F6E75251ACE6200C1A84B /* CommonFontManager.swift in Sources */,
507511
77E9AEEF25198A7E002A8538 /* ExampleViewModel.swift in Sources */,
508512
7704575625152523006CC071 /* CommonViewModel.swift in Sources */,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//
2+
// NewCommonCollectionViewHandler.swift
3+
// OnoKit
4+
//
5+
// Created by Danny on 3/7/22.
6+
//
7+
8+
import UIKit
9+
10+
open class NewCommonCollectionViewHandler: NSObject, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
11+
private lazy var dataSource = makeDataSource()
12+
13+
typealias DataSource = UICollectionViewDiffableDataSource<AnyHashable, AnyHashable>
14+
typealias Snapshot = NSDiffableDataSourceSnapshot<AnyHashable, AnyHashable>
15+
16+
let collectionView: UICollectionView
17+
18+
init(_ collectionView: UICollectionView) {
19+
self.collectionView = collectionView
20+
super.init()
21+
22+
collectionView.delegate = self
23+
}
24+
25+
open func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
26+
return .zero
27+
}
28+
29+
open func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
30+
return .zero
31+
}
32+
33+
// needs overridden by subclass
34+
open func collectionViewHandler(cellForItemAt indexPath: IndexPath, itemIdentifier: AnyHashable) -> UICollectionViewCell? {
35+
return nil
36+
}
37+
38+
open func collectionViewHandlerSections() -> [AnyHashable] {
39+
return []
40+
}
41+
42+
open func collectionViewHandler(itemIdentifiersFor section: AnyHashable) -> [AnyHashable] {
43+
return []
44+
}
45+
46+
private func makeDataSource() -> DataSource {
47+
let dataSource = DataSource(collectionView: collectionView) { _, indexPath, itemIdentifer in
48+
let cell = self.collectionViewHandler(cellForItemAt: indexPath, itemIdentifier: itemIdentifer)
49+
50+
return cell
51+
}
52+
53+
return dataSource
54+
}
55+
56+
public func applySnapshot(animatingDifferences: Bool) {
57+
var snapshot = Snapshot()
58+
59+
let sections = self.collectionViewHandlerSections()
60+
snapshot.appendSections(sections)
61+
62+
for section in sections {
63+
let identifiers = self.collectionViewHandler(itemIdentifiersFor: section)
64+
snapshot.appendItems(identifiers, toSection: section)
65+
}
66+
67+
dataSource.apply(snapshot, animatingDifferences: animatingDifferences)
68+
}
69+
}

OnoKit/Extensions/UICollectionViewExtension.swift

+27
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public extension UICollectionView {
1010

1111
// MARK: - Common Creators
1212

13+
@available(*, deprecated, message: "Use NewCommonCollectionViewHandler instead.")
1314
convenience init(_ superview: UIView, _ direction: UICollectionView.ScrollDirection, _ handler: CommonCollectionViewHandler, _ identifiers: [String]) {
1415

1516
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
@@ -41,4 +42,30 @@ public extension UICollectionView {
4142

4243
}
4344

45+
convenience init(_ superview: UIView, _ direction: UICollectionView.ScrollDirection, _ identifiers: [String]) {
46+
47+
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
48+
layout.scrollDirection = direction
49+
50+
self.init(frame: .zero, collectionViewLayout: layout)
51+
52+
self.backgroundColor = .clear
53+
54+
self.showsVerticalScrollIndicator = false
55+
self.showsHorizontalScrollIndicator = false
56+
57+
self.isPagingEnabled = true
58+
self.bounces = false
59+
60+
for identifier in identifiers {
61+
if let projectName = Bundle.main.infoDictionary!["CFBundleExecutable"] as? String {
62+
let namespace = projectName.replacingOccurrences(of: "-", with: "_")
63+
if let anyClass: AnyClass = NSClassFromString("\(namespace).\(identifier)") {
64+
self.register(anyClass, forCellWithReuseIdentifier: identifier)
65+
}
66+
}
67+
}
68+
69+
superview.addSubview(self)
70+
}
4471
}

0 commit comments

Comments
 (0)