Skip to content

Commit

Permalink
Issue 2: Rx Animated Data Source still animates when animation style …
Browse files Browse the repository at this point in the history
…is set to none

#2
  • Loading branch information
bigMOTOR committed Jul 23, 2020
1 parent 7183492 commit ea0c011
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 13 deletions.
2 changes: 1 addition & 1 deletion DataDrivenRxDatasources.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "DataDrivenRxDatasources"
s.version = "1.0.0"
s.version = "1.0.1"
s.summary = "MVVM abstraction boilerplate code over RxDataSources."
s.description = <<-DESC
DataDrivenRxDatasources - MVVM abstraction boilerplate code over RxDataSources.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,18 @@ private func _reloadDataSource<S>() -> RxCollectionViewSectionedReloadDataSource
}

private func _animatableDataSource<S>(animationConfiguration: AnimationConfiguration) -> RxCollectionViewSectionedAnimatedDataSource<AnimatableCollectionSectionModel<S>> {
return RxCollectionViewSectionedAnimatedDataSource<AnimatableCollectionSectionModel<S>>(
animationConfiguration: animationConfiguration,
configureCell: { (_, cv, indexPath, model) in _configureCell(cv: cv, indexPath: indexPath, model: model) }
)
switch (animationConfiguration.insertAnimation, animationConfiguration.reloadAnimation, animationConfiguration.deleteAnimation) {
case (.none, .none, .none):
return RxCollectionViewSectionedNonAnimatedDataSource<AnimatableCollectionSectionModel<S>>(
animationConfiguration: animationConfiguration,
configureCell: { (_, cv, indexPath, model) in _configureCell(cv: cv, indexPath: indexPath, model: model) }
)
default:
return RxCollectionViewSectionedAnimatedDataSource<AnimatableCollectionSectionModel<S>>(
animationConfiguration: animationConfiguration,
configureCell: { (_, cv, indexPath, model) in _configureCell(cv: cv, indexPath: indexPath, model: model) }
)
}
}

// MARK: - Configurations
Expand All @@ -92,7 +100,7 @@ private func _configureCell<C: CollectionCellViewModelWrapper>(cv: UICollectionV
return cell
}

// MARK: - TableViewControllerDelegateProxy
// MARK: - CollectionViewControllerDelegateProxy
private final class CollectionViewControllerDelegateProxy: NSObject, UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
if
Expand All @@ -104,3 +112,11 @@ private final class CollectionViewControllerDelegateProxy: NSObject, UICollectio
}
}

// MARK: - Data Source that disables animation on table view updates (history of this: https://github.com/RxSwiftCommunity/RxDataSources/issues/90)
final class RxCollectionViewSectionedNonAnimatedDataSource<Section: AnimatableSectionModelType>: RxCollectionViewSectionedAnimatedDataSource<Section> {
override func collectionView(_ collectionView: UICollectionView, observedEvent: Event<Element>) {
UIView.performWithoutAnimation {
super.collectionView(collectionView, observedEvent: observedEvent)
}
}
}
32 changes: 26 additions & 6 deletions Sources/DataDrivenRxDatasources/TableView/RxTableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,22 @@ private func _reloadDataSource<S>() -> RxTableViewSectionedReloadDataSource<Tabl
}

private func _animatableDataSource<S>(animationConfiguration: AnimationConfiguration) -> RxTableViewSectionedAnimatedDataSource<AnimatableTableSectionModel<S>> {
return RxTableViewSectionedAnimatedDataSource<AnimatableTableSectionModel<S>>(
animationConfiguration: animationConfiguration,
configureCell: { (_, tv, indexPath, model) in _configureCell(tv: tv, indexPath: indexPath, model: model) },
titleForHeaderInSection: _titleForHeaderInSection,
titleForFooterInSection: _titleForFooterInSection
)
switch (animationConfiguration.insertAnimation, animationConfiguration.reloadAnimation, animationConfiguration.deleteAnimation) {
case (.none, .none, .none):
return RxTableViewSectionedNonAnimatedDataSource<AnimatableTableSectionModel<S>>(
animationConfiguration: animationConfiguration,
configureCell: { (_, tv, indexPath, model) in _configureCell(tv: tv, indexPath: indexPath, model: model) },
titleForHeaderInSection: _titleForHeaderInSection,
titleForFooterInSection: _titleForFooterInSection
)
default:
return RxTableViewSectionedAnimatedDataSource<AnimatableTableSectionModel<S>>(
animationConfiguration: animationConfiguration,
configureCell: { (_, tv, indexPath, model) in _configureCell(tv: tv, indexPath: indexPath, model: model) },
titleForHeaderInSection: _titleForHeaderInSection,
titleForFooterInSection: _titleForFooterInSection
)
}
}

// MARK: - Configurations
Expand All @@ -107,6 +117,7 @@ private func _titleForFooterInSection<S: SectionModelType & ModelType>(dataSourc
guard let model = dataSource.sectionModels[index].model as? SectionFooterTitleType else { return nil }
return model.sectionFooterTitle
}

private func _configureCell<C: CellViewModelWrapper>(tv: UITableView, indexPath: IndexPath, model: C) -> UITableViewCell {
let cell = tv.dequeueReusableCell(withIdentifier: model.base.cellViewClass.identifier, for: indexPath)
guard var modeledCell = cell as? ModelledCell else { return cell }
Expand All @@ -125,3 +136,12 @@ private final class TableViewControllerDelegateProxy: NSObject, UITableViewDeleg
return false
}
}

// MARK: - Data Source that disables animation on table view updates (history of this: https://github.com/RxSwiftCommunity/RxDataSources/issues/90)
final class RxTableViewSectionedNonAnimatedDataSource<Section: AnimatableSectionModelType>: RxTableViewSectionedAnimatedDataSource<Section> {
override func tableView(_ tableView: UITableView, observedEvent: Event<Element>) {
UIView.performWithoutAnimation {
super.tableView(tableView, observedEvent: observedEvent)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,24 @@ final class RxCollectionViewTests: XCTestCase {
XCTAssertEqual(cellModel?.name, value)
}

func testSetAnimationToNoneUseNonAnimatedDataSource() {
let value = "Some Cell"
let sections: Driver<[AnimatedSectionViewModel]> = .just([AnimatedSectionViewModel(model: "Some Section", items: [SampleCollectionCellViewModel(name: value)])])

collectionView.rx
.bind(sections: sections, animationConfiguration: AnimationConfiguration(insertAnimation: .none, reloadAnimation: .none, deleteAnimation: .none))
.disposed(by: bag)

XCTAssertTrue(collectionView.rx.dataSource.forwardToDelegate()?.isKind(of: RxCollectionViewSectionedNonAnimatedDataSource<AnimatedSectionViewModel>.self) ?? false)
}

static var allTests = [
("testCellCountOnReloadDataSource", testCellCountOnReloadDataSource),
("testSectionCountOnReloadDataSource", testSectionCountOnReloadDataSource),
("testCellValueOnReloadDataSource", testCellValueOnReloadDataSource),
("testCellCountOnAnimatableDataSource", testCellCountOnAnimatableDataSource),
("testSectionCountOnAnimatableDataSource", testSectionCountOnAnimatableDataSource),
("testCellValueOnAnimatableDataSource", testCellValueOnAnimatableDataSource),
("testSetAnimationToNoneUseNonAnimatedDataSource", testSetAnimationToNoneUseNonAnimatedDataSource)
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,17 @@ final class RxTableViewTests: XCTestCase {
XCTAssertNil(sectionTitle)
}

func testSetAnimationToNoneUseNonAnimatedDataSource() {
let value = "Some Section"
let sections: Driver<[AnimatedSectionViewModel]> = .just([AnimatedSectionViewModel(model: value, items: [SampleCellViewModel(name: "Some Cell")])])

tableView.rx
.bind(sections: sections, animationConfiguration: AnimationConfiguration(insertAnimation: .none, reloadAnimation: .none, deleteAnimation: .none))
.disposed(by: bag)

XCTAssertTrue(tableView.rx.dataSource.forwardToDelegate()?.isKind(of: RxTableViewSectionedNonAnimatedDataSource<AnimatedSectionViewModel>.self) ?? false)
}

static var allTests = [
("testCellCountOnReloadDataSource", testCellCountOnReloadDataSource),
("testSectionCountOnReloadDataSource", testSectionCountOnReloadDataSource),
Expand All @@ -179,6 +190,7 @@ final class RxTableViewTests: XCTestCase {
("testSectionCountOnAnimatableDataSource", testSectionCountOnAnimatableDataSource),
("testCellValueOnAnimatableDataSource", testCellValueOnAnimatableDataSource),
("testStringSectionNameOnAnimatableDataSourceGenerateName", testStringSectionNameOnAnimatableDataSourceGenerateName),
("testSomeObjectAsSection", testSomeObjectAsSection)
("testSomeObjectAsSection", testSomeObjectAsSection),
("testSetAnimationToNoneUseNonAnimatedDataSource", testSetAnimationToNoneUseNonAnimatedDataSource)
]
}

0 comments on commit ea0c011

Please sign in to comment.