Skip to content

Commit 37a6413

Browse files
committed
Adds custom ViewTransition logic.
1 parent 83c6122 commit 37a6413

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

RxDataSources.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
C82C3C971F3B939100309AE8 /* SectionModelType.swift in Sources */ = {isa = PBXBuildFile; fileRef = C82C3C711F3B938C00309AE8 /* SectionModelType.swift */; };
5050
C82C3C991F3B939100309AE8 /* Utilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = C82C3C731F3B938C00309AE8 /* Utilities.swift */; };
5151
C833338D1F8A5FAC00D46EAE /* Deprecated.swift in Sources */ = {isa = PBXBuildFile; fileRef = C833338C1F8A5FAC00D46EAE /* Deprecated.swift */; };
52+
C84043C31F9D33CE0093C3E7 /* ViewTransition.swift in Sources */ = {isa = PBXBuildFile; fileRef = C84043C21F9D33CE0093C3E7 /* ViewTransition.swift */; };
5253
C87C34991F363B1400DB85FE /* RxSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C87C34321F36346A00DB85FE /* RxSwift.framework */; };
5354
C87C349A1F363B1400DB85FE /* RxSwift.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = C87C34321F36346A00DB85FE /* RxSwift.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
5455
C87C349D1F363B1400DB85FE /* RxCocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C87C343A1F36346A00DB85FE /* RxCocoa.framework */; };
@@ -367,6 +368,7 @@
367368
C82C3C711F3B938C00309AE8 /* SectionModelType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SectionModelType.swift; sourceTree = "<group>"; };
368369
C82C3C731F3B938C00309AE8 /* Utilities.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Utilities.swift; sourceTree = "<group>"; };
369370
C833338C1F8A5FAC00D46EAE /* Deprecated.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Deprecated.swift; sourceTree = "<group>"; };
371+
C84043C21F9D33CE0093C3E7 /* ViewTransition.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewTransition.swift; sourceTree = "<group>"; };
370372
C861C0F01E153FC400BEDC46 /* RxDataSources.podspec */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = RxDataSources.podspec; sourceTree = "<group>"; };
371373
C87C34191F36346A00DB85FE /* Rx.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = Rx.xcodeproj; path = RxSwift/Rx.xcodeproj; sourceTree = "<group>"; };
372374
C87DF3431D0219A7006308C5 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
@@ -666,6 +668,7 @@
666668
C8BBFBD51F3B8F8D00A225F7 /* TableViewSectionedDataSource.swift */,
667669
C8BBFBD61F3B8F8D00A225F7 /* UI+SectionedViewType.swift */,
668670
C833338C1F8A5FAC00D46EAE /* Deprecated.swift */,
671+
C84043C21F9D33CE0093C3E7 /* ViewTransition.swift */,
669672
);
670673
path = RxDataSources;
671674
sourceTree = "<group>";
@@ -1128,6 +1131,7 @@
11281131
C81FBF5E1F3B9D660094061E /* Array+Extensions.swift in Sources */,
11291132
C8BBFBDE1F3B8F8D00A225F7 /* RxTableViewSectionedAnimatedDataSource.swift in Sources */,
11301133
C81FBF601F3B9D8B0094061E /* FloatingPointType+IdentifiableType.swift in Sources */,
1134+
C84043C31F9D33CE0093C3E7 /* ViewTransition.swift in Sources */,
11311135
C81FBF661F3B9DF60094061E /* AnimationConfiguration.swift in Sources */,
11321136
C8BBFBDA1F3B8F8D00A225F7 /* RxCollectionViewSectionedAnimatedDataSource.swift in Sources */,
11331137
C8BBFBD81F3B8F8D00A225F7 /* DataSources.swift in Sources */,

Sources/RxDataSources/RxCollectionViewSectionedAnimatedDataSource.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,24 @@ open class RxCollectionViewSectionedAnimatedDataSource<S: AnimatableSectionModel
2323
: CollectionViewSectionedDataSource<S>
2424
, RxCollectionViewDataSourceType {
2525
public typealias Element = [S]
26+
public typealias DecideViewTransition = (CollectionViewSectionedDataSource<S>, UICollectionView, [Changeset<S>]) -> ViewTransition
2627

2728
// animation configuration
2829
public var animationConfiguration: AnimationConfiguration
2930

31+
/// Calculates view transition depending on type of changes
32+
public var decideViewTransition: DecideViewTransition
33+
3034
public init(
3135
animationConfiguration: AnimationConfiguration = AnimationConfiguration(),
36+
decideViewTransition: @escaping DecideViewTransition = { _, _, _ in .animated },
3237
configureCell: @escaping ConfigureCell,
3338
configureSupplementaryView: @escaping ConfigureSupplementaryView,
3439
moveItem: @escaping MoveItem = { _, _, _ in () },
3540
canMoveItemAtIndexPath: @escaping CanMoveItemAtIndexPath = { _, _ in false }
3641
) {
3742
self.animationConfiguration = animationConfiguration
43+
self.decideViewTransition = decideViewTransition
3844
super.init(
3945
configureCell: configureCell,
4046
configureSupplementaryView: configureSupplementaryView,
@@ -81,10 +87,16 @@ open class RxCollectionViewSectionedAnimatedDataSource<S: AnimatableSectionModel
8187
}
8288
let differences = try Diff.differencesForSectionedView(initialSections: oldSections, finalSections: newSections)
8389

84-
for difference in differences {
85-
dataSource.setSections(difference.finalSections)
90+
switch self.decideViewTransition(self, collectionView, differences) {
91+
case .animated:
92+
for difference in differences {
93+
dataSource.setSections(difference.finalSections)
8694

87-
collectionView.performBatchUpdates(difference, animationConfiguration: self.animationConfiguration)
95+
collectionView.performBatchUpdates(difference, animationConfiguration: self.animationConfiguration)
96+
}
97+
case .reload:
98+
self.setSections(newSections)
99+
collectionView.reloadData()
88100
}
89101
}
90102
catch let e {

Sources/RxDataSources/RxTableViewSectionedAnimatedDataSource.swift

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@ import Differentiator
1818
open class RxTableViewSectionedAnimatedDataSource<S: AnimatableSectionModelType>
1919
: TableViewSectionedDataSource<S>
2020
, RxTableViewDataSourceType {
21-
2221
public typealias Element = [S]
22+
public typealias DecideViewTransition = (TableViewSectionedDataSource<S>, UITableView, [Changeset<S>]) -> ViewTransition
2323

2424
/// Animation configuration for data source
2525
public var animationConfiguration: AnimationConfiguration
2626

27+
/// Calculates view transition depending on type of changes
28+
public var decideViewTransition: DecideViewTransition
29+
2730
#if os(iOS)
2831
public init(
2932
animationConfiguration: AnimationConfiguration = AnimationConfiguration(),
33+
decideViewTransition: @escaping DecideViewTransition = { _, _, _ in .animated },
3034
configureCell: @escaping ConfigureCell,
3135
titleForHeaderInSection: @escaping TitleForHeaderInSection = { _, _ in nil },
3236
titleForFooterInSection: @escaping TitleForFooterInSection = { _, _ in nil },
@@ -36,6 +40,7 @@ open class RxTableViewSectionedAnimatedDataSource<S: AnimatableSectionModelType>
3640
sectionForSectionIndexTitle: @escaping SectionForSectionIndexTitle = { _, _, index in index }
3741
) {
3842
self.animationConfiguration = animationConfiguration
43+
self.decideViewTransition = decideViewTransition
3944
super.init(
4045
configureCell: configureCell,
4146
titleForHeaderInSection: titleForHeaderInSection,
@@ -49,13 +54,15 @@ open class RxTableViewSectionedAnimatedDataSource<S: AnimatableSectionModelType>
4954
#else
5055
public init(
5156
animationConfiguration: AnimationConfiguration = AnimationConfiguration(),
57+
decideViewTransition: @escaping DecideViewTransition = { _, _, _ in .animated },
5258
configureCell: @escaping ConfigureCell,
5359
titleForHeaderInSection: @escaping TitleForHeaderInSection = { _, _ in nil },
5460
titleForFooterInSection: @escaping TitleForFooterInSection = { _, _ in nil },
5561
canEditRowAtIndexPath: @escaping CanEditRowAtIndexPath = { _, _ in false },
5662
canMoveRowAtIndexPath: @escaping CanMoveRowAtIndexPath = { _, _ in false }
5763
) {
5864
self.animationConfiguration = animationConfiguration
65+
self.decideViewTransition = decideViewTransition
5966
super.init(
6067
configureCell: configureCell,
6168
titleForHeaderInSection: titleForHeaderInSection,
@@ -90,10 +97,17 @@ open class RxTableViewSectionedAnimatedDataSource<S: AnimatableSectionModelType>
9097
do {
9198
let differences = try Diff.differencesForSectionedView(initialSections: oldSections, finalSections: newSections)
9299

93-
for difference in differences {
94-
dataSource.setSections(difference.finalSections)
100+
switch self.decideViewTransition(self, tableView, differences) {
101+
case .animated:
102+
for difference in differences {
103+
dataSource.setSections(difference.finalSections)
95104

96-
tableView.performBatchUpdates(difference, animationConfiguration: self.animationConfiguration)
105+
tableView.performBatchUpdates(difference, animationConfiguration: self.animationConfiguration)
106+
}
107+
case .reload:
108+
self.setSections(newSections)
109+
tableView.reloadData()
110+
return
97111
}
98112
}
99113
catch let e {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// ViewTransition.swift
3+
// RxDataSources
4+
//
5+
// Created by Krunoslav Zaher on 10/22/17.
6+
// Copyright © 2017 kzaher. All rights reserved.
7+
//
8+
9+
/// Transition between two view states
10+
public enum ViewTransition {
11+
/// animated transition
12+
case animated
13+
/// refresh view without animations
14+
case reload
15+
}
16+

0 commit comments

Comments
 (0)