Skip to content

Commit d7545fe

Browse files
committed
Improves collection view behavior.
1 parent 9fbf3aa commit d7545fe

10 files changed

+86
-49
lines changed

Cartfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
github "ReactiveX/RxSwift" "3.0.0.alpha.1"
1+
github "ReactiveX/RxSwift" "master"

Cartfile.resolved

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
github "ReactiveX/RxSwift" "3.0.0.alpha.1"
1+
github "ReactiveX/RxSwift" "979f3723e43cf5e340818142baca15b3b37ec351"

Example/Example1_CustomizationUsingTableViewDelegate.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ class CustomizationUsingTableViewDelegate : UIViewController {
7878
}
7979

8080
extension CustomizationUsingTableViewDelegate : UITableViewDelegate {
81-
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: IndexPath) -> CGFloat {
81+
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
8282

8383
// you can also fetch item
84-
guard let item = dataSource?.itemAtIndexPath(indexPath),
84+
guard let item = dataSource?[indexPath],
8585
// .. or section and customize what you like
8686
let _ = dataSource?.sectionAtIndex(indexPath.section)
8787
else {

Example/Example2_RandomizedSectionsAnimation.swift

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@ class NumberSectionView : UICollectionReusableView {
2020
@IBOutlet weak var value: UILabel?
2121
}
2222

23+
extension ObservableType {
24+
func implicitTransaction() -> Observable<E> {
25+
return Observable.create { observer in
26+
return self.subscribe { event in
27+
UIView.setAnimationDuration(5.0)
28+
//CATransaction.begin()
29+
//CATransaction.setAnimationDuration(5.0)
30+
//UIView.animate(withDuration: 3.0) {
31+
observer.on(event)
32+
//}
33+
//CATransaction.commit()
34+
}
35+
}
36+
}
37+
}
38+
2339
class ViewController: UIViewController {
2440

2541
@IBOutlet weak var animatedTableView: UITableView!
@@ -74,6 +90,7 @@ class ViewController: UIViewController {
7490
skinCollectionViewDataSource(cvAnimatedDataSource)
7591

7692
randomSections
93+
.implicitTransaction()
7794
.bindTo(animatedCollectionView.rx.items(dataSource: cvAnimatedDataSource))
7895
.addDisposableTo(disposeBag)
7996
}
@@ -129,7 +146,7 @@ extension ViewController {
129146
dataSource.supplementaryViewFactory = { (ds ,cv, kind, ip) in
130147
let section = cv.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Section", for: ip) as! NumberSectionView
131148

132-
section.value!.text = "\(ds.sectionAtIndex(ip.section).header)"
149+
section.value!.text = "\(ds[ip.section].header)"
133150

134151
return section
135152
}
@@ -141,8 +158,8 @@ extension ViewController {
141158
let generate = true
142159
if generate {
143160

144-
let nSections = 4
145-
let nItems = 2
161+
let nSections = 10
162+
let nItems = 100
146163

147164

148165
/*

Example/Example4_DifferentSectionAndItemTypes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class MultipleSectionModelViewController: UIViewController {
4040

4141
func skinTableViewDataSource(_ dataSource: RxTableViewSectionedReloadDataSource<MultipleSectionModel>) {
4242
dataSource.configureCell = { (dataSource, table, idxPath, _) in
43-
switch dataSource.itemAtIndexPath(idxPath) {
43+
switch dataSource[idxPath] {
4444
case let .ImageSectionItem(image, title):
4545
let cell: ImageTitleTableViewCell = table.dequeueReusableCell(forIndexPath: idxPath)
4646
cell.titleLabel.text = title

Podfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ platform :ios, '8.0'
22
use_frameworks!
33

44
def common
5-
pod 'RxSwift', :git => '[email protected]:ReactiveX/RxSwift.git', :branch => 'swift-3.0'
6-
pod 'RxCocoa', :git => '[email protected]:ReactiveX/RxSwift.git', :branch => 'swift-3.0'
5+
pod 'RxSwift', :git => '[email protected]:ReactiveX/RxSwift.git', :branch => 'master'
6+
pod 'RxCocoa', :git => '[email protected]:ReactiveX/RxSwift.git', :branch => 'master'
77
end
88

99
target 'RxDataSources' do

Podfile.lock

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
PODS:
2-
- RxCocoa (2.6.0):
3-
- RxSwift (~> 2.5)
4-
- RxSwift (2.6.0)
2+
- RxCocoa (3.0.0.alpha.1):
3+
- RxSwift (~> 3.0.0.alpha.1)
4+
- RxSwift (3.0.0.alpha.1)
55

66
DEPENDENCIES:
7-
- RxCocoa (from `[email protected]:ReactiveX/RxSwift.git`, branch `swift-3.0`)
8-
- RxSwift (from `[email protected]:ReactiveX/RxSwift.git`, branch `swift-3.0`)
7+
- RxCocoa (from `[email protected]:ReactiveX/RxSwift.git`, branch `master`)
8+
- RxSwift (from `[email protected]:ReactiveX/RxSwift.git`, branch `master`)
99

1010
EXTERNAL SOURCES:
1111
RxCocoa:
12-
:branch: swift-3.0
12+
:branch: master
1313
:git: [email protected]:ReactiveX/RxSwift.git
1414
RxSwift:
15-
:branch: swift-3.0
15+
:branch: master
1616
:git: [email protected]:ReactiveX/RxSwift.git
1717

1818
CHECKOUT OPTIONS:
1919
RxCocoa:
20-
:commit: 9b1f22e6a02bdc2ff511069ccb80781c06b1ec6e
20+
:commit: 979f3723e43cf5e340818142baca15b3b37ec351
2121
:git: [email protected]:ReactiveX/RxSwift.git
2222
RxSwift:
23-
:commit: 9b1f22e6a02bdc2ff511069ccb80781c06b1ec6e
23+
:commit: 979f3723e43cf5e340818142baca15b3b37ec351
2424
:git: [email protected]:ReactiveX/RxSwift.git
2525

2626
SPEC CHECKSUMS:
27-
RxCocoa: c611f45c19ef2e159fe0ad4279684190b385e061
28-
RxSwift: 799f527c4aa01e235fbccfb22f46d1c381aa4151
27+
RxCocoa: 37b3b167eaa14446a315493ef6a188b277942b15
28+
RxSwift: 0bccfee1e979c1b72a5d1dffdf96119303b5d111
2929

30-
PODFILE CHECKSUM: 810d5edbe5528082fd2e6c64ec9959cbc8e24ec4
30+
PODFILE CHECKSUM: 43bd333b10d7dfc5029b1ff3167bbf181e4f3f65
3131

32-
COCOAPODS: 1.0.1
32+
COCOAPODS: 1.1.0.beta.2

RxDataSources.podspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "RxDataSources"
3-
s.version = "1.0.0.beta.1"
3+
s.version = "1.0.0-beta.2"
44
s.summary = "This is a collection of reactive data sources for UITableView and UICollectionView."
55
s.description = <<-DESC
66
This is a collection of reactive data sources for UITableView and UICollectionView.
@@ -40,6 +40,6 @@ data
4040

4141
s.source_files = 'Sources/**/*.swift'
4242

43-
s.dependency 'RxSwift', '~> 3.0.0.alpha.1'
44-
s.dependency 'RxCocoa', '~> 3.0.0.alpha.1'
43+
s.dependency 'RxSwift', '~> 3.0.0-beta.1'
44+
s.dependency 'RxCocoa', '~> 3.0.0-beta.1'
4545
end

RxDataSources.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@
737737
);
738738
runOnlyForDeploymentPostprocessing = 0;
739739
shellPath = /bin/sh;
740-
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n";
740+
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
741741
showEnvVarsInLog = 0;
742742
};
743743
5D41B451D84BA67C35C4AC67 /* [CP] Embed Pods Frameworks */ = {
@@ -782,7 +782,7 @@
782782
);
783783
runOnlyForDeploymentPostprocessing = 0;
784784
shellPath = /bin/sh;
785-
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n";
785+
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
786786
showEnvVarsInLog = 0;
787787
};
788788
FF90FC9979157345588A89F5 /* [CP] Copy Pods Resources */ = {

Sources/DataSources+Rx/RxCollectionViewSectionedAnimatedDataSource.swift

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import RxSwift
1313
import RxCocoa
1414
#endif
1515

16+
/*
17+
This is commented becuse collection view has bugs when doing animated updates.
18+
Take a look at randomized sections.
19+
*/
1620
public class RxCollectionViewSectionedAnimatedDataSource<S: AnimatableSectionModelType>
1721
: CollectionViewSectionedDataSource<S>
1822
, RxCollectionViewDataSourceType {
@@ -23,10 +27,44 @@ public class RxCollectionViewSectionedAnimatedDataSource<S: AnimatableSectionMod
2327
// it crashes. Still need to figure out that one.
2428
var dataSet = false
2529

30+
private let disposeBag = DisposeBag()
31+
32+
private let partialUpdateEvent = PublishSubject<(UICollectionView, Event<Element>)>()
33+
2634
public override init() {
2735
super.init()
36+
37+
self.partialUpdateEvent
38+
.throttle(0.5, scheduler: MainScheduler.instance)
39+
.subscribe(onNext: { [weak self] event in
40+
self?.collectionView(event.0, observedPartialUpdateEvent: event.1)
41+
})
42+
.addDisposableTo(disposeBag)
2843
}
29-
44+
45+
public func collectionView(_ collectionView: UICollectionView, observedPartialUpdateEvent: Event<Element>) {
46+
UIBindingObserver(UIElement: self) { dataSource, newSections in
47+
let oldSections = dataSource.sectionModels
48+
do {
49+
let differences = try differencesForSectionedView(oldSections, finalSections: newSections)
50+
51+
for difference in differences {
52+
dataSource.setSections(difference.finalSections)
53+
54+
collectionView.performBatchUpdates(difference, animationConfiguration: self.animationConfiguration)
55+
}
56+
}
57+
catch let e {
58+
#if DEBUG
59+
print("Error while binding data animated: \(e)\nFallback to normal `reloadData` behavior.")
60+
rxDebugFatalError(e)
61+
#endif
62+
self.setSections(newSections)
63+
collectionView.reloadData()
64+
}
65+
}.on(observedPartialUpdateEvent)
66+
}
67+
3068
public func collectionView(_ collectionView: UICollectionView, observedEvent: Event<Element>) {
3169
UIBindingObserver(UIElement: self) { dataSource, newSections in
3270
#if DEBUG
@@ -38,26 +76,8 @@ public class RxCollectionViewSectionedAnimatedDataSource<S: AnimatableSectionMod
3876
collectionView.reloadData()
3977
}
4078
else {
41-
DispatchQueue.main.async {
42-
let oldSections = dataSource.sectionModels
43-
do {
44-
let differences = try differencesForSectionedView(oldSections, finalSections: newSections)
45-
46-
for difference in differences {
47-
dataSource.setSections(difference.finalSections)
48-
49-
collectionView.performBatchUpdates(difference, animationConfiguration: self.animationConfiguration)
50-
}
51-
}
52-
catch let e {
53-
#if DEBUG
54-
print("Error while binding data animated: \(e)\nFallback to normal `reloadData` behavior.")
55-
rxDebugFatalError(e)
56-
#endif
57-
self.setSections(newSections)
58-
collectionView.reloadData()
59-
}
60-
}
79+
let element = (collectionView, observedEvent)
80+
dataSource.partialUpdateEvent.on(.next(element))
6181
}
6282
}.on(observedEvent)
6383
}

0 commit comments

Comments
 (0)