Skip to content

Commit 98d1605

Browse files
committed
initial readme
1 parent 7a1b5a5 commit 98d1605

File tree

5 files changed

+126
-31
lines changed

5 files changed

+126
-31
lines changed

README.md

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,57 @@
11
# CombineDataSources
22

3-
A description of this package.
3+
<p align="center">
4+
<a href="https://github.com/apple/swift-package-manager" target="_blank"><img src="https://img.shields.io/badge/Swift%20Package%20Manager-compatible-brightgreen.svg" alt="CombineDataSources supports Swift Package Manager (SPM)"></a>
5+
<img src="https://img.shields.io/badge/platforms-iOS%2013.0-333333.svg" />
6+
</p>
7+
8+
**CombineDataSources** provides custom Combine subscribers that act as table and collection view controllers and bind a stream of element collections to table or collection sections with cells.
9+
10+
**Note**: The package is currently work in progress.
11+
12+
## Usage
13+
14+
The repo contains a demo app in the *Example* sub-folder that demonstrates visually different ways to use CombineDataSources.
15+
16+
#### Bind a plain list of elements
17+
18+
```swift
19+
var data = PassthroughSubject<[Person], Never>()
20+
21+
data
22+
.receive(subscriber: tableView.rowsSubscriber(cellIdentifier: "Cell", cellType: PersonCell.self, cellConfig: { cell, indexPath, model in
23+
cell.nameLabel.text = model.name
24+
}))
25+
```
26+
27+
![Plain list updates with CombineDataSources](https://github.com/combineopensource/CombineDataSources/raw/master/Assets/plain-list.gif)
28+
29+
#### Bind a list of Section models
30+
31+
```swift
32+
var data = PassthroughSubject<[Section<Person>], Never>()
33+
34+
data
35+
.receive(subscriber: tableView.sectionsSubscriber(cellIdentifier: "Cell", cellType: PersonCell.self, cellConfig: { cell, indexPath, model in
36+
cell.nameLabel.text = model.name
37+
}))
38+
```
39+
40+
![Sectioned list updates with CombineDataSources](https://github.com/combineopensource/CombineDataSources/raw/master/Assets/sections-list.gif)
41+
42+
#### Customize the table controller
43+
44+
```swift
45+
var data = PassthroughSubject<[[Person]], Never>()
46+
47+
let controller = TableViewItemsController<[[Person]]>(cellIdentifier: "Cell", cellType: PersonCell.self) { cell, indexPath, person in
48+
cell.nameLabel.text = person.name
49+
}
50+
controller.animated = false
51+
52+
// More custom controller configuration ...
53+
54+
data
55+
.receive(subscriber: tableView.sectionsSubscriber(controller))
56+
```
57+

Sources/CombineDataSources/TableViewItemsController.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ public class TableViewItemsController<CollectionType>: NSObject, UITableViewData
5454
self.cellFactory = cellFactory
5555
}
5656

57+
deinit {
58+
print("Controller is released")
59+
}
60+
5761
// MARK: - Update collection
5862
private let fromRow = {(section: Int) in return {(row: Int) in return IndexPath(row: row, section: section)}}
5963

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import XCTest
2+
import UIKit
3+
@testable import CombineDataSources
4+
5+
final class MemoryManagementTests: XCTestCase {
6+
func testControllerOwnsTableViewAndDataSource() {
7+
let ctr: TableViewItemsController<[[Model]]>? = TableViewItemsController<[[Model]]>(cellIdentifier: "Cell", cellType: UITableViewCell.self) { (cell, indexPath, model) in
8+
//
9+
}
10+
11+
// Configure the controller
12+
var tableView: UITableView? = UITableView()
13+
tableView!.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
14+
var dataSource: TestDataSource? = TestDataSource()
15+
16+
ctr!.tableView = tableView
17+
ctr!.dataSource = dataSource
18+
ctr!.updateCollection([dataSet1, dataSet1])
19+
20+
tableView = nil
21+
XCTAssertNotNil(ctr!.tableView)
22+
23+
dataSource = nil
24+
XCTAssertNotNil(ctr!.dataSource)
25+
}
26+
}

Tests/CombineDataSourcesTests/TableViewItemsControllerTests.swift

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,6 @@ import XCTest
22
import UIKit
33
@testable import CombineDataSources
44

5-
struct Model: Equatable {
6-
var text: String
7-
}
8-
9-
let dataSet1 = [
10-
Model(text: "test1"), Model(text: "test2"), Model(text: "test3")
11-
]
12-
let dataSet2 = [
13-
Section(header: "section header", items: [Model(text: "test model")], footer: "section footer")
14-
]
15-
165
final class TableViewItemsControllerTests: XCTestCase {
176

187
func testDataSource() {
@@ -53,25 +42,7 @@ final class TableViewItemsControllerTests: XCTestCase {
5342
let tableView = UITableView()
5443
ctr.tableView = tableView
5544
ctr.updateCollection([dataSet1])
56-
57-
// Provide fallback data source
58-
class TestDataSource: NSObject, UITableViewDataSource {
59-
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
60-
fatalError()
61-
}
62-
63-
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
64-
fatalError()
65-
}
66-
67-
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
68-
return "test header"
69-
}
70-
func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
71-
return "test footer"
72-
}
73-
}
74-
45+
7546
let fallbackDataSource = TestDataSource()
7647
ctr.dataSource = fallbackDataSource
7748

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by Marin Todorov on 8/14/19.
6+
//
7+
8+
import XCTest
9+
import CombineDataSources
10+
import UIKit
11+
12+
struct Model: Equatable {
13+
var text: String
14+
}
15+
16+
let dataSet1 = [
17+
Model(text: "test1"), Model(text: "test2"), Model(text: "test3")
18+
]
19+
let dataSet2 = [
20+
Section(header: "section header", items: [Model(text: "test model")], footer: "section footer")
21+
]
22+
23+
// Provide fallback data source
24+
class TestDataSource: NSObject, UITableViewDataSource {
25+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
26+
fatalError()
27+
}
28+
29+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
30+
fatalError()
31+
}
32+
33+
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
34+
return "test header"
35+
}
36+
func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
37+
return "test footer"
38+
}
39+
}
40+

0 commit comments

Comments
 (0)