Skip to content

Commit 9288d58

Browse files
committed
Open data sources
1 parent 793f587 commit 9288d58

File tree

14 files changed

+60
-59
lines changed

14 files changed

+60
-59
lines changed

ReusableDataSource.xcodeproj/project.pbxproj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
/* Begin PBXBuildFile section */
1010
F68F117D209CE0F600BADC8D /* ReusableDataSource.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F68F1173209CE0F600BADC8D /* ReusableDataSource.framework */; };
1111
F68F1184209CE0F600BADC8D /* ReusableDataSource.h in Headers */ = {isa = PBXBuildFile; fileRef = F68F1176209CE0F600BADC8D /* ReusableDataSource.h */; settings = {ATTRIBUTES = (Public, ); }; };
12-
F68F119C209CE1CB00BADC8D /* ReusableViewModelPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = F68F118F209CE1CA00BADC8D /* ReusableViewModelPresenter.swift */; };
13-
F68F119D209CE1CB00BADC8D /* ReusableViewSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = F68F1190209CE1CA00BADC8D /* ReusableViewSource.swift */; };
12+
F68F119C209CE1CB00BADC8D /* ReusablePresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = F68F118F209CE1CA00BADC8D /* ReusablePresenter.swift */; };
13+
F68F119D209CE1CB00BADC8D /* ReusablePresenterSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = F68F1190209CE1CA00BADC8D /* ReusablePresenterSource.swift */; };
1414
F68F119E209CE1CB00BADC8D /* ReusableViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = F68F1191209CE1CA00BADC8D /* ReusableViewModel.swift */; };
1515
F68F119F209CE1CB00BADC8D /* UICollectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F68F1193209CE1CB00BADC8D /* UICollectionView.swift */; };
1616
F68F11A0209CE1CB00BADC8D /* UITableView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F68F1194209CE1CB00BADC8D /* UITableView.swift */; };
@@ -73,8 +73,8 @@
7373
F68F1177209CE0F600BADC8D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
7474
F68F117C209CE0F600BADC8D /* ReusableDataSourceTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = ReusableDataSourceTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
7575
F68F1183209CE0F600BADC8D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
76-
F68F118F209CE1CA00BADC8D /* ReusableViewModelPresenter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReusableViewModelPresenter.swift; sourceTree = "<group>"; };
77-
F68F1190209CE1CA00BADC8D /* ReusableViewSource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReusableViewSource.swift; sourceTree = "<group>"; };
76+
F68F118F209CE1CA00BADC8D /* ReusablePresenter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReusablePresenter.swift; sourceTree = "<group>"; };
77+
F68F1190209CE1CA00BADC8D /* ReusablePresenterSource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReusablePresenterSource.swift; sourceTree = "<group>"; };
7878
F68F1191209CE1CA00BADC8D /* ReusableViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReusableViewModel.swift; sourceTree = "<group>"; };
7979
F68F1193209CE1CB00BADC8D /* UICollectionView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UICollectionView.swift; sourceTree = "<group>"; };
8080
F68F1194209CE1CB00BADC8D /* UITableView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UITableView.swift; sourceTree = "<group>"; };
@@ -175,8 +175,8 @@
175175
isa = PBXGroup;
176176
children = (
177177
F68F1191209CE1CA00BADC8D /* ReusableViewModel.swift */,
178-
F68F118F209CE1CA00BADC8D /* ReusableViewModelPresenter.swift */,
179-
F68F1190209CE1CA00BADC8D /* ReusableViewSource.swift */,
178+
F68F118F209CE1CA00BADC8D /* ReusablePresenter.swift */,
179+
F68F1190209CE1CA00BADC8D /* ReusablePresenterSource.swift */,
180180
);
181181
path = ReusableViewModel;
182182
sourceTree = "<group>";
@@ -438,15 +438,15 @@
438438
isa = PBXSourcesBuildPhase;
439439
buildActionMask = 2147483647;
440440
files = (
441-
F68F119D209CE1CB00BADC8D /* ReusableViewSource.swift in Sources */,
441+
F68F119D209CE1CB00BADC8D /* ReusablePresenterSource.swift in Sources */,
442442
F68F119E209CE1CB00BADC8D /* ReusableViewModel.swift in Sources */,
443443
F68F11A0209CE1CB00BADC8D /* UITableView.swift in Sources */,
444444
F68F11A1209CE1CB00BADC8D /* ReusableTableViewDataSource.swift in Sources */,
445445
F68F119F209CE1CB00BADC8D /* UICollectionView.swift in Sources */,
446446
F68F11A2209CE1CB00BADC8D /* AnyTableViewPresentableViewModel.swift in Sources */,
447447
F68F11A4209CE1CB00BADC8D /* AnyCollectionViewPresentableViewModel.swift in Sources */,
448448
F68F11A3209CE1CB00BADC8D /* ReusableCollectionViewDataSource.swift in Sources */,
449-
F68F119C209CE1CB00BADC8D /* ReusableViewModelPresenter.swift in Sources */,
449+
F68F119C209CE1CB00BADC8D /* ReusablePresenter.swift in Sources */,
450450
);
451451
runOnlyForDeploymentPostprocessing = 0;
452452
};

ReusableDataSource/Extensions/UICollectionView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ extension UICollectionView {
66

77
- Important: Call before `dequeueReusableCell(for:)` to avoid `NSInternalInconsistencyException`.
88
*/
9-
public func register<T: UICollectionViewCell>(cell: T.Type, reusableCellSource: ReusableViewSource) {
9+
public func register<T: UICollectionViewCell>(cell: T.Type, reusableCellSource: ReusablePresenterSource) {
1010
switch reusableCellSource {
1111
case .nib:
1212
register(UINib(nibName: String(describing: cell), bundle: nil), forCellWithReuseIdentifier: String(describing: cell.self))
@@ -38,7 +38,7 @@ extension UICollectionView {
3838

3939
- Important: Causes the app to crashes with `NSInternalInconsistencyException` if the `PresentingCell` type isn't previously registered.
4040
*/
41-
func dequeueAndPresent<Presenter: ReusableViewModelPresenter>(presentableViewModel: ReusableViewModel<Presenter>, for indexPath: IndexPath) -> Presenter
41+
func dequeueAndPresent<Presenter: ReusablePresenter>(presentableViewModel: ReusableViewModel<Presenter>, for indexPath: IndexPath) -> Presenter
4242
where Presenter: UICollectionViewCell {
4343
let cell = dequeueReusableCell(for: indexPath) as Presenter
4444

ReusableDataSource/Extensions/UITableView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ extension UITableView {
66

77
- Important: Call before `dequeueReusableCell(for:)` to avoid `NSInternalInconsistencyException`.
88
*/
9-
public func register<T: UITableViewCell>(cell: T.Type, reusableCellSource: ReusableViewSource) {
9+
public func register<T: UITableViewCell>(cell: T.Type, reusableCellSource: ReusablePresenterSource) {
1010
switch reusableCellSource {
1111
case .nib:
1212
register(UINib(nibName: String(describing: cell), bundle: nil), forCellReuseIdentifier: String(describing: cell))
@@ -38,7 +38,7 @@ extension UITableView {
3838

3939
- Important: Causes the app to crashes with `NSInternalInconsistencyException` if the `PresentingCell` type isn't previously registered.
4040
*/
41-
func dequeueAndPresent<Presenter: ReusableViewModelPresenter>(presentableViewModel: ReusableViewModel<Presenter>, for indexPath: IndexPath) -> Presenter
41+
func dequeueAndPresent<Presenter: ReusablePresenter>(presentableViewModel: ReusableViewModel<Presenter>, for indexPath: IndexPath) -> Presenter
4242
where Presenter: UITableViewCell {
4343
let cell = dequeueReusableCell(for: indexPath) as Presenter
4444

ReusableDataSource/ReusableDataSource/CollectionView/AnyCollectionViewPresentableViewModel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class AnyCollectionViewPresentableViewModel {
44
let dequeueAndPresentCellCallback: (UICollectionView) -> UICollectionViewCell
55
let registerCellCallback: (UICollectionView) -> Void
66

7-
public init<Presenter: ReusableViewModelPresenter>(base: ReusableViewModel<Presenter>) where Presenter: UICollectionViewCell {
7+
public init<Presenter: ReusablePresenter>(base: ReusableViewModel<Presenter>) where Presenter: UICollectionViewCell {
88
self.dequeueAndPresentCellCallback = { (collectionView: UICollectionView) -> UICollectionViewCell in
99
collectionView.dequeueAndPresent(presentableViewModel: base, for: IndexPath(item: 0, section: 0))
1010
}
@@ -16,7 +16,7 @@ public class AnyCollectionViewPresentableViewModel {
1616
}
1717

1818
extension ReusableViewModel where Presenter: UICollectionViewCell {
19-
public var any: AnyCollectionViewPresentableViewModel {
19+
public var anyPresentable: AnyCollectionViewPresentableViewModel {
2020
return AnyCollectionViewPresentableViewModel(base: self)
2121
}
2222
}
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import UIKit
22

3-
public class ReusableCollectionViewDataSource: NSObject, UICollectionViewDataSource {
3+
open class ReusableCollectionViewDataSource: NSObject {
44
var presentableViewModels = [[AnyCollectionViewPresentableViewModel]]()
55

6-
public var automaticallyRegisterCells: Bool
6+
public var automaticallyRegisterReuseIdentifiers: Bool
77

8-
public init(automaticallyRegisterCells: Bool = true) {
9-
self.automaticallyRegisterCells = automaticallyRegisterCells
8+
public init(automaticallyRegisterReuseIdentifiers: Bool = true) {
9+
self.automaticallyRegisterReuseIdentifiers = automaticallyRegisterReuseIdentifiers
1010
}
1111

1212
public func present(presentableViewModels: [AnyCollectionViewPresentableViewModel], on collectionView: UICollectionView) {
@@ -16,24 +16,26 @@ public class ReusableCollectionViewDataSource: NSObject, UICollectionViewDataSou
1616
public func present(presentableViewModels: [[AnyCollectionViewPresentableViewModel]], on collectionView: UICollectionView) {
1717
self.presentableViewModels = presentableViewModels
1818

19-
if automaticallyRegisterCells {
19+
if automaticallyRegisterReuseIdentifiers {
2020
presentableViewModels
2121
.flatMap { $0 }
2222
.forEach { $0.registerCellCallback(collectionView) }
2323
}
2424

2525
collectionView.reloadData()
2626
}
27+
}
2728

28-
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
29+
extension ReusableCollectionViewDataSource: UICollectionViewDataSource {
30+
open func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
2931
return presentableViewModels[section].count
3032
}
3133

32-
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
34+
open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
3335
return presentableViewModels[indexPath.section][indexPath.row].dequeueAndPresentCellCallback(collectionView)
3436
}
3537

36-
public func numberOfSections(in collectionView: UICollectionView) -> Int {
38+
open func numberOfSections(in collectionView: UICollectionView) -> Int {
3739
return presentableViewModels.count
3840
}
3941
}

ReusableDataSource/ReusableDataSource/TableView/AnyTableViewPresentableViewModel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class AnyTableViewPresentableViewModel {
44
public let dequeueAndPresentCellCallback: (UITableView) -> UITableViewCell
55
public let registerCellCallback: (UITableView) -> Void
66

7-
public init<Presenter: ReusableViewModelPresenter>(base: ReusableViewModel<Presenter>) where Presenter: UITableViewCell {
7+
public init<Presenter: ReusablePresenter>(base: ReusableViewModel<Presenter>) where Presenter: UITableViewCell {
88
self.dequeueAndPresentCellCallback = { (tableView: UITableView) -> UITableViewCell in
99
tableView.dequeueAndPresent(presentableViewModel: base, for: IndexPath(item: 0, section: 0))
1010
}
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import UIKit
22

3-
public class ReusableTableViewDataSource: NSObject, UITableViewDataSource {
3+
open class ReusableTableViewDataSource: NSObject {
44
var presentableViewModels = [[AnyTableViewPresentableViewModel]]()
55

6-
public var automaticallyRegisterCells: Bool
6+
public var automaticallyRegisterReuseIdentifiers: Bool
77

8-
public init(automaticallyRegisterCells: Bool = true) {
9-
self.automaticallyRegisterCells = automaticallyRegisterCells
8+
public init(automaticallyRegisterReuseIdentifiers: Bool = true) {
9+
self.automaticallyRegisterReuseIdentifiers = automaticallyRegisterReuseIdentifiers
1010
}
1111

1212
public func present(presentableViewModels: [AnyTableViewPresentableViewModel], on tableView: UITableView) {
@@ -16,24 +16,26 @@ public class ReusableTableViewDataSource: NSObject, UITableViewDataSource {
1616
public func present(presentableViewModels: [[AnyTableViewPresentableViewModel]], on tableView: UITableView) {
1717
self.presentableViewModels = presentableViewModels
1818

19-
if automaticallyRegisterCells {
19+
if automaticallyRegisterReuseIdentifiers {
2020
presentableViewModels
2121
.flatMap { $0 }
2222
.forEach { $0.registerCellCallback(tableView) }
2323
}
2424

2525
tableView.reloadData()
2626
}
27+
}
2728

28-
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
29+
extension ReusableTableViewDataSource: UITableViewDataSource {
30+
open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
2931
return presentableViewModels[indexPath.section][indexPath.row].dequeueAndPresentCellCallback(tableView)
3032
}
3133

32-
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
34+
open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
3335
return presentableViewModels[section].count
3436
}
3537

36-
public func numberOfSections(in tableView: UITableView) -> Int {
38+
open func numberOfSections(in tableView: UITableView) -> Int {
3739
return presentableViewModels.count
3840
}
3941
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public protocol ReusablePresenter {
2+
associatedtype ViewModel
3+
4+
static var source: ReusablePresenterSource { get }
5+
6+
func present(viewModel: ViewModel)
7+
}
8+
9+
extension ReusablePresenter {
10+
public static var source: ReusablePresenterSource {
11+
return .class
12+
}
13+
}

ReusableDataSource/ReusableViewModel/ReusableViewSource.swift renamed to ReusableDataSource/ReusableViewModel/ReusablePresenterSource.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/// Reusable cell source
2-
public enum ReusableViewSource {
2+
public enum ReusablePresenterSource {
33
case nib
44
case `class`
55
case storyboard

ReusableDataSource/ReusableViewModel/ReusableViewModel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
public struct ReusableViewModel<Presenter: ReusableViewModelPresenter> {
1+
public struct ReusableViewModel<Presenter: ReusablePresenter> {
22
public let viewModel: Presenter.ViewModel
33

44
public init(viewModel: Presenter.ViewModel) {

0 commit comments

Comments
 (0)