Skip to content

Commit 25262ba

Browse files
committed
batched data source draft
1 parent 392e3a5 commit 25262ba

15 files changed

+612
-18
lines changed

Example/Example.xcodeproj/project.pbxproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
/* Begin PBXBuildFile section */
1010
9C326989230B2DDE00E93F9C /* CollectionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9C326988230B2DDE00E93F9C /* CollectionViewController.swift */; };
1111
9C371EE7230356CE00617B57 /* MenuTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9C371EE6230356CE00617B57 /* MenuTableViewController.swift */; };
12+
9CA01D192312EF7C00666EDE /* BatchesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9CA01D182312EF7C00666EDE /* BatchesViewController.swift */; };
13+
9CA01D1C2312F4CF00666EDE /* SampleData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9CA01D1B2312F4CF00666EDE /* SampleData.swift */; };
1214
9CA4B70D23048B470041CBA4 /* GitHubSearchViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9CA4B70C23048B470041CBA4 /* GitHubSearchViewController.swift */; };
1315
9CB630CA22FD510000368A0D /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9CB630C922FD510000368A0D /* AppDelegate.swift */; };
1416
9CB630CC22FD510000368A0D /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9CB630CB22FD510000368A0D /* SceneDelegate.swift */; };
@@ -22,6 +24,8 @@
2224
/* Begin PBXFileReference section */
2325
9C326988230B2DDE00E93F9C /* CollectionViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionViewController.swift; sourceTree = "<group>"; };
2426
9C371EE6230356CE00617B57 /* MenuTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuTableViewController.swift; sourceTree = "<group>"; };
27+
9CA01D182312EF7C00666EDE /* BatchesViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BatchesViewController.swift; sourceTree = "<group>"; };
28+
9CA01D1B2312F4CF00666EDE /* SampleData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SampleData.swift; sourceTree = "<group>"; };
2529
9CA4B70C23048B470041CBA4 /* GitHubSearchViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GitHubSearchViewController.swift; sourceTree = "<group>"; };
2630
9CB630C622FD510000368A0D /* Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Example.app; sourceTree = BUILT_PRODUCTS_DIR; };
2731
9CB630C922FD510000368A0D /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
@@ -46,6 +50,14 @@
4650
/* End PBXFrameworksBuildPhase section */
4751

4852
/* Begin PBXGroup section */
53+
9CA01D1A2312F4C700666EDE /* etc */ = {
54+
isa = PBXGroup;
55+
children = (
56+
9CA01D1B2312F4CF00666EDE /* SampleData.swift */,
57+
);
58+
path = etc;
59+
sourceTree = "<group>";
60+
};
4961
9CB630BD22FD510000368A0D = {
5062
isa = PBXGroup;
5163
children = (
@@ -67,12 +79,14 @@
6779
9CB630C822FD510000368A0D /* Example */ = {
6880
isa = PBXGroup;
6981
children = (
82+
9CA01D1A2312F4C700666EDE /* etc */,
7083
9CB630C922FD510000368A0D /* AppDelegate.swift */,
7184
9CB630CB22FD510000368A0D /* SceneDelegate.swift */,
7285
9C371EE6230356CE00617B57 /* MenuTableViewController.swift */,
7386
9CB630CD22FD510000368A0D /* ViewController.swift */,
7487
9C326988230B2DDE00E93F9C /* CollectionViewController.swift */,
7588
9CA4B70C23048B470041CBA4 /* GitHubSearchViewController.swift */,
89+
9CA01D182312EF7C00666EDE /* BatchesViewController.swift */,
7690
9CB630CF22FD510000368A0D /* Main.storyboard */,
7791
9CB630D222FD510100368A0D /* Assets.xcassets */,
7892
9CB630D422FD510100368A0D /* LaunchScreen.storyboard */,
@@ -165,10 +179,12 @@
165179
files = (
166180
9C326989230B2DDE00E93F9C /* CollectionViewController.swift in Sources */,
167181
9C371EE7230356CE00617B57 /* MenuTableViewController.swift in Sources */,
182+
9CA01D192312EF7C00666EDE /* BatchesViewController.swift in Sources */,
168183
9CB630CE22FD510000368A0D /* ViewController.swift in Sources */,
169184
9CA4B70D23048B470041CBA4 /* GitHubSearchViewController.swift in Sources */,
170185
9CB630CA22FD510000368A0D /* AppDelegate.swift in Sources */,
171186
9CB630CC22FD510000368A0D /* SceneDelegate.swift in Sources */,
187+
9CA01D1C2312F4CF00666EDE /* SampleData.swift in Sources */,
172188
);
173189
runOnlyForDeploymentPostprocessing = 0;
174190
};

Example/Example/Base.lproj/Main.storyboard

Lines changed: 108 additions & 12 deletions
Large diffs are not rendered by default.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//
2+
// For credits and licence check the LICENSE file included in this package.
3+
// (c) CombineOpenSource, Created by Marin Todorov.
4+
//
5+
6+
import UIKit
7+
import Combine
8+
import CombineDataSources
9+
10+
struct MockAPI {
11+
static func requestPage(pageNumber: Int) -> AnyPublisher<BatchesDataSource<String>.LoadResult, Error> {
12+
// Do your network request or otherwise fetch items here.
13+
return sampleData(.pages)
14+
}
15+
16+
static func requestBatch(token: Data?) -> AnyPublisher<BatchesDataSource<String>.LoadResult, Error> {
17+
// Do your network request or otherwise fetch items here.
18+
return sampleData(.batches)
19+
}
20+
}
21+
22+
class BatchesViewController: UIViewController {
23+
@IBOutlet var tableView: UITableView!
24+
25+
enum Demo: Int, RawRepresentable {
26+
case pages, batchesWithToken
27+
}
28+
29+
var demo: Demo!
30+
var controller: TableViewBatchesController<String>!
31+
32+
override func viewDidLoad() {
33+
super.viewDidLoad()
34+
35+
// Create a plain table data source.
36+
let itemsController = TableViewItemsController<[[String]]>(cellIdentifier: "Cell", cellType: UITableViewCell.self, cellConfig: { cell, indexPath, text in
37+
cell.textLabel!.text = "\(indexPath.row+1). \(text)"
38+
})
39+
40+
switch demo {
41+
case .batchesWithToken:
42+
43+
// Bind a batched data source to table view.
44+
controller = TableViewBatchesController<String>(
45+
tableView: tableView,
46+
itemsController: itemsController,
47+
initialToken: nil,
48+
loadItemsWithToken: { nextToken in
49+
MockAPI.requestBatch(token: nextToken)
50+
}
51+
)
52+
53+
case .pages:
54+
55+
// Bind a paged data source to table view.
56+
controller = TableViewBatchesController<String>(
57+
tableView: tableView,
58+
itemsController: itemsController,
59+
loadPage: { nextPage in
60+
return MockAPI.requestPage(pageNumber: nextPage)
61+
}
62+
)
63+
64+
default: break
65+
}
66+
}
67+
}

Example/Example/CollectionViewController.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ class PersonCollectionCell: UICollectionViewCell {
2525
}
2626

2727
class CollectionViewController: UIViewController {
28+
enum Demo: Int, RawRepresentable {
29+
case plain, multiple, sections, noAnimations
30+
}
31+
2832
@IBOutlet var collectionView: UICollectionView!
2933

3034
// The kind of demo to show

Example/Example/MenuTableViewController.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import UIKit
88
class MenuTableViewController: UITableViewController {
99
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
1010
let rowIndex = (sender as! UITableViewCell).tag
11-
(segue.destination as? ViewController)?.demo = Demo(rawValue: rowIndex)!
12-
(segue.destination as? CollectionViewController)?.demo = Demo(rawValue: rowIndex)!
11+
12+
(segue.destination as? ViewController)?.demo = ViewController.Demo(rawValue: rowIndex)!
13+
(segue.destination as? CollectionViewController)?.demo = CollectionViewController.Demo(rawValue: rowIndex)!
14+
(segue.destination as? BatchesViewController)?.demo = BatchesViewController.Demo(rawValue: rowIndex)!
1315
}
1416
}

Example/Example/ViewController.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ class PersonCell: UITableViewCell {
1515
@IBOutlet var nameLabel: UILabel!
1616
}
1717

18-
enum Demo: Int, RawRepresentable {
19-
case plain, multiple, sections, noAnimations
20-
}
21-
2218
class ViewController: UIViewController {
19+
enum Demo: Int, RawRepresentable {
20+
case plain, multiple, sections, noAnimations
21+
}
22+
2323
@IBOutlet var tableView: UITableView!
2424

2525
// The kind of demo to show
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
import Foundation
3+
import Combine
4+
import CombineDataSources
5+
6+
enum SampleDataType {
7+
case pages, batches
8+
}
9+
10+
func sampleData(_ type: SampleDataType) -> AnyPublisher<BatchesDataSource<String>.LoadResult, Error> {
11+
return Future<BatchesDataSource<String>.LoadResult, Error> { promise in
12+
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
13+
switch type {
14+
case .pages:
15+
promise(.success(BatchesDataSource.LoadResult.items((0..<20).map { _ in UUID().uuidString })))
16+
17+
case .batches:
18+
promise(.success(BatchesDataSource.LoadResult.itemsToken((0..<20).map { _ in UUID().uuidString },
19+
nextToken: "nextTokenFromServer".data(using: .utf8)!)
20+
))
21+
}
22+
}
23+
}.eraseToAnyPublisher()
24+
}

0 commit comments

Comments
 (0)