Skip to content

Commit b564cf4

Browse files
committed
Add AnimatableSingleSectionTableView demo.
1 parent b2e2481 commit b564cf4

File tree

9 files changed

+143
-18
lines changed

9 files changed

+143
-18
lines changed

Example/RxDataSourcesSingleSection.xcodeproj/project.pbxproj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
77FF690A22A129ED0060BCE7 /* SingleSectionTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77FF690922A129ED0060BCE7 /* SingleSectionTableViewController.swift */; };
1818
77FF690C22A129F50060BCE7 /* SingleSectionTableViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77FF690B22A129F50060BCE7 /* SingleSectionTableViewModel.swift */; };
1919
77FF690F22A12D7F0060BCE7 /* PersonTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77FF690E22A12D7F0060BCE7 /* PersonTableViewCell.swift */; };
20+
77FF691122A1353C0060BCE7 /* AnimatableSingleSectionTableViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77FF691022A1353C0060BCE7 /* AnimatableSingleSectionTableViewModel.swift */; };
21+
77FF691322A135440060BCE7 /* AnimatableSingleSectionTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77FF691222A135440060BCE7 /* AnimatableSingleSectionTableViewController.swift */; };
2022
9364F1BE03950AEA27F51DAB /* Pods_RxDataSourcesSingleSection_Tests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4ADD3AD2C8F6C75FD73C950D /* Pods_RxDataSourcesSingleSection_Tests.framework */; };
2123
/* End PBXBuildFile section */
2224

@@ -48,6 +50,8 @@
4850
77FF690922A129ED0060BCE7 /* SingleSectionTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SingleSectionTableViewController.swift; sourceTree = "<group>"; };
4951
77FF690B22A129F50060BCE7 /* SingleSectionTableViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SingleSectionTableViewModel.swift; sourceTree = "<group>"; };
5052
77FF690E22A12D7F0060BCE7 /* PersonTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PersonTableViewCell.swift; sourceTree = "<group>"; };
53+
77FF691022A1353C0060BCE7 /* AnimatableSingleSectionTableViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnimatableSingleSectionTableViewModel.swift; sourceTree = "<group>"; };
54+
77FF691222A135440060BCE7 /* AnimatableSingleSectionTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnimatableSingleSectionTableViewController.swift; sourceTree = "<group>"; };
5155
9E79FD7D7142880C339D9223 /* LICENSE */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = "<group>"; };
5256
D7BF1E8604491F2A8C15748E /* Pods-RxDataSourcesSingleSection_Tests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RxDataSourcesSingleSection_Tests.release.xcconfig"; path = "Target Support Files/Pods-RxDataSourcesSingleSection_Tests/Pods-RxDataSourcesSingleSection_Tests.release.xcconfig"; sourceTree = "<group>"; };
5357
E7F238DE29547726EFF94CB2 /* Pods-RxDataSourcesSingleSection_Example.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RxDataSourcesSingleSection_Example.release.xcconfig"; path = "Target Support Files/Pods-RxDataSourcesSingleSection_Example/Pods-RxDataSourcesSingleSection_Example.release.xcconfig"; sourceTree = "<group>"; };
@@ -157,10 +161,12 @@
157161
77FF690D22A129FE0060BCE7 /* Controller */ = {
158162
isa = PBXGroup;
159163
children = (
164+
77FF691222A135440060BCE7 /* AnimatableSingleSectionTableViewController.swift */,
165+
77FF691022A1353C0060BCE7 /* AnimatableSingleSectionTableViewModel.swift */,
160166
607FACD71AFB9204008FA782 /* MenuTableViewController.swift */,
167+
77FF690E22A12D7F0060BCE7 /* PersonTableViewCell.swift */,
161168
77FF690922A129ED0060BCE7 /* SingleSectionTableViewController.swift */,
162169
77FF690B22A129F50060BCE7 /* SingleSectionTableViewModel.swift */,
163-
77FF690E22A12D7F0060BCE7 /* PersonTableViewCell.swift */,
164170
);
165171
path = Controller;
166172
sourceTree = "<group>";
@@ -384,6 +390,8 @@
384390
buildActionMask = 2147483647;
385391
files = (
386392
607FACD81AFB9204008FA782 /* MenuTableViewController.swift in Sources */,
393+
77FF691122A1353C0060BCE7 /* AnimatableSingleSectionTableViewModel.swift in Sources */,
394+
77FF691322A135440060BCE7 /* AnimatableSingleSectionTableViewController.swift in Sources */,
387395
77FF690C22A129F50060BCE7 /* SingleSectionTableViewModel.swift in Sources */,
388396
607FACD61AFB9204008FA782 /* AppDelegate.swift in Sources */,
389397
77FF690F22A12D7F0060BCE7 /* PersonTableViewCell.swift in Sources */,

Example/RxDataSourcesSingleSection/Base.lproj/Main.storyboard

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<sections>
2121
<tableViewSection id="oFx-af-l40">
2222
<cells>
23-
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" accessoryType="disclosureIndicator" indentationWidth="10" id="9Cr-DJ-ibd">
23+
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="none" accessoryType="disclosureIndicator" indentationWidth="10" id="9Cr-DJ-ibd">
2424
<rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
2525
<autoresizingMask key="autoresizingMask"/>
2626
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="9Cr-DJ-ibd" id="FY0-53-Rob">
@@ -37,7 +37,7 @@
3737
</subviews>
3838
</tableViewCellContentView>
3939
</tableViewCell>
40-
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" accessoryType="disclosureIndicator" indentationWidth="10" id="kkd-vU-0Ob">
40+
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="none" accessoryType="disclosureIndicator" indentationWidth="10" id="kkd-vU-0Ob">
4141
<rect key="frame" x="0.0" y="44" width="375" height="44"/>
4242
<autoresizingMask key="autoresizingMask"/>
4343
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="kkd-vU-0Ob" id="CS8-vM-wwI">
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//
2+
// AnimatableSingleSectionTableViewController.swift
3+
// RxDataSourcesSingleSection_Example
4+
//
5+
// Created by Meng Li on 2019/05/31.
6+
// Copyright © 2019 CocoaPods. All rights reserved.
7+
//
8+
9+
import RxSwift
10+
import UIKit
11+
import Reusable
12+
import RxDataSourcesSingleSection
13+
14+
class AnimatableSingleSectionTableViewController: UIViewController {
15+
16+
private lazy var addBarButtonItem: UIBarButtonItem = {
17+
let barButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: nil, action: nil)
18+
barButtonItem.rx.tap.bind { [unowned self] in
19+
self.viewModel.add()
20+
}.disposed(by: disposeBag)
21+
return barButtonItem
22+
}()
23+
24+
private lazy var tableView: UITableView = {
25+
let tableView = UITableView()
26+
tableView.rowHeight = UITableView.automaticDimension
27+
tableView.register(cellType: PersonTableViewCell.self)
28+
return tableView
29+
}()
30+
31+
private lazy var dataSource = TableViewAnimatedSingleSectionDataSource<Person>.configure(cellType: PersonTableViewCell.self)
32+
33+
34+
private let viewModel: AnimatableSingleSectionTableViewModel
35+
private let disposeBag = DisposeBag()
36+
37+
init(viewModel: AnimatableSingleSectionTableViewModel) {
38+
self.viewModel = viewModel
39+
super.init(nibName: nil, bundle: nil)
40+
}
41+
42+
required init?(coder aDecoder: NSCoder) {
43+
fatalError("init(coder:) has not been implemented")
44+
}
45+
46+
override func viewDidLoad() {
47+
super.viewDidLoad()
48+
49+
navigationItem.rightBarButtonItem = addBarButtonItem
50+
view.backgroundColor = .white
51+
view.addSubview(tableView)
52+
createConstraints()
53+
54+
viewModel.title.bind(to: rx.title).disposed(by: disposeBag)
55+
viewModel.personSection.bind(to: tableView.rx.items(dataSource: dataSource)).disposed(by: disposeBag)
56+
}
57+
58+
private func createConstraints() {
59+
tableView.snp.makeConstraints {
60+
$0.edges.equalToSuperview()
61+
}
62+
}
63+
64+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// AnimatableSingleSectionTableViewModel.swift
3+
// RxDataSourcesSingleSection_Example
4+
//
5+
// Created by Meng Li on 2019/05/31.
6+
// Copyright © 2019 CocoaPods. All rights reserved.
7+
//
8+
9+
import RxCocoa
10+
import RxSwift
11+
import RxDataSourcesSingleSection
12+
13+
class AnimatableSingleSectionTableViewModel {
14+
15+
private let persons = BehaviorRelay<[Person]>(value: (0...5).map { _ in Person.fake() })
16+
17+
let title = Observable.just("AnimatableSingleSectionTableView")
18+
19+
var personSection: Observable<AnimatableSingleSection<Person>> {
20+
return persons.map { AnimatableSingleSection.create($0) }
21+
}
22+
23+
func add() {
24+
var newPersons = persons.value
25+
newPersons.append(Person.fake())
26+
persons.accept(newPersons)
27+
}
28+
29+
}

Example/RxDataSourcesSingleSection/Controller/MenuTableViewController.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class MenuTableViewController: UITableViewController {
2020
case 0:
2121
let singleSectionTableViewController = SingleSectionTableViewController(viewModel: .init())
2222
navigationController?.pushViewController(singleSectionTableViewController, animated: true)
23+
case 1:
24+
let animatableSingleSectionTableViewController = AnimatableSingleSectionTableViewController(viewModel: .init())
25+
navigationController?.pushViewController(animatableSingleSectionTableViewController, animated: true)
2326
default:
2427
break
2528
}

Example/RxDataSourcesSingleSection/Controller/PersonTableViewCell.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,26 @@ import RxDataSourcesSingleSection
1212
import SnapKit
1313

1414
struct Person {
15+
var id: String
1516
var name: String
1617
var company: String
1718

1819
static let faker = Faker()
1920
static func fake() -> Person {
20-
return Person(name: faker.name.name(), company: faker.company.name())
21+
return Person(id: UUID().uuidString, name: faker.name.name(), company: faker.company.name())
2122
}
2223
}
2324

25+
extension Person: AnimatableModel {
26+
27+
typealias Identity = String
28+
29+
var identity: String {
30+
return id
31+
}
32+
33+
}
34+
2435
class PersonTableViewCell: UITableViewCell {
2536

2637
private lazy var nameLabel = UILabel()
@@ -35,6 +46,7 @@ class PersonTableViewCell: UITableViewCell {
3546
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
3647
super.init(style: style, reuseIdentifier: reuseIdentifier)
3748

49+
selectionStyle = .none
3850
addSubview(nameLabel)
3951
addSubview(companyLabel)
4052

Example/RxDataSourcesSingleSection/Controller/SingleSectionTableViewModel.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import RxCocoa
1010
import RxSwift
1111
import RxDataSourcesSingleSection
1212

13-
1413
class SingleSectionTableViewModel {
1514

1615
private let persons = BehaviorRelay<[Person]>(value: (0...5).map { _ in Person.fake() })
@@ -21,5 +20,4 @@ class SingleSectionTableViewModel {
2120
return persons.map { SingleSection.create($0) }
2221
}
2322

24-
2523
}
Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,38 @@
11

22
import RxDataSources
33

4-
protocol AnimatableModel: IdentifiableType, Equatable {}
4+
public protocol AnimatableModel: IdentifiableType, Equatable {}
55

6-
struct AnimatableSingleSectionModel<T: AnimatableModel>: AnimatableSectionModelType {
6+
public struct AnimatableSingleSectionModel<T: AnimatableModel>: AnimatableSectionModelType {
77

8-
typealias Item = T
9-
typealias Identity = String
8+
public typealias Item = T
9+
public typealias Identity = String
1010

11-
var items: [T]
12-
var identity: String
11+
public var items: [T]
12+
public var identity: String
1313

14-
init(items: [T]) {
14+
public init(items: [T]) {
1515
self.items = items
1616
self.identity = ""
1717
}
1818

19-
init(original: AnimatableSingleSectionModel<T>, items: [T]) {
19+
public init(original: AnimatableSingleSectionModel<T>, items: [T]) {
2020
self = original
2121
self.items = items
2222
}
2323

24-
static func create(_ items: [T]) -> AnimatableSingleSection<T> {
24+
public static func create(_ items: [T]) -> AnimatableSingleSection<T> {
2525
return [AnimatableSingleSectionModel(items: items)]
2626
}
2727

2828
}
2929

30-
typealias AnimatableSingleSection<T: AnimatableModel> = [AnimatableSingleSectionModel<T>]
30+
public typealias AnimatableSingleSection<T: AnimatableModel> = [AnimatableSingleSectionModel<T>]
3131

3232
extension AnimatableSingleSection where Element: AnimatableSectionModelType, Element.Item: AnimatableModel {
33-
static func create(_ items: [Element.Item]) -> AnimatableSingleSection<Element.Item> {
33+
34+
public static func create(_ items: [Element.Item]) -> AnimatableSingleSection<Element.Item> {
3435
return [AnimatableSingleSectionModel(items: items)]
3536
}
37+
3638
}
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11

22
import RxDataSources
33

4-
typealias TableViewAnimatedSingleSectionDataSource<T: AnimatableModel> = RxTableViewSectionedAnimatedDataSource<AnimatableSingleSectionModel<T>>
4+
public typealias TableViewAnimatedSingleSectionDataSource<T: AnimatableModel> = RxTableViewSectionedAnimatedDataSource<AnimatableSingleSectionModel<T>>
55

6+
extension TableViewAnimatedSingleSectionDataSource {
7+
8+
public static func configure<T: UITableViewCell>(cellType: T.Type) -> TableViewAnimatedSingleSectionDataSource<T.Model> where T: Configurable {
9+
return TableViewAnimatedSingleSectionDataSource<T.Model>(configureCell: { (_, tableView, indexPath, model) -> UITableViewCell in
10+
return tableView.dequeueReusableCell(for: indexPath, model: model, cellType: cellType)
11+
})
12+
}
13+
14+
}

0 commit comments

Comments
 (0)