|
| 1 | +// |
| 2 | +// RxTableViewCustomAnimatedDataSource.swift |
| 3 | +// BaseExtension |
| 4 | +// |
| 5 | +// Created by wade.hawk on 09/12/2018. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | +import UIKit |
| 10 | +import RxSwift |
| 11 | +import RxCocoa |
| 12 | +import RxDataSources |
| 13 | +import Differentiator |
| 14 | + |
| 15 | +open class RxTableViewCustomAnimatedDataSource<S: AnimatableSectionModelType> |
| 16 | + : TableViewSectionedDataSource<S> |
| 17 | +, RxTableViewDataSourceType { |
| 18 | + public typealias Element = [S] |
| 19 | + public typealias DecideViewTransition = (TableViewSectionedDataSource<S>, UITableView, [Changeset<S>]) -> ViewTransition |
| 20 | + |
| 21 | + /// Animation configuration for data source |
| 22 | + public var animationConfiguration: AnimationConfiguration |
| 23 | + |
| 24 | + /// Calculates view transition depending on type of changes |
| 25 | + public var decideViewTransition: DecideViewTransition |
| 26 | + |
| 27 | + public var reloadedEvent: (() -> Void)? = nil |
| 28 | + |
| 29 | + public init( |
| 30 | + animationConfiguration: AnimationConfiguration = AnimationConfiguration(), |
| 31 | + decideViewTransition: @escaping DecideViewTransition = { _, _, _ in .animated }, |
| 32 | + configureCell: @escaping ConfigureCell, |
| 33 | + titleForHeaderInSection: @escaping TitleForHeaderInSection = { _, _ in nil }, |
| 34 | + titleForFooterInSection: @escaping TitleForFooterInSection = { _, _ in nil }, |
| 35 | + canEditRowAtIndexPath: @escaping CanEditRowAtIndexPath = { _, _ in false }, |
| 36 | + canMoveRowAtIndexPath: @escaping CanMoveRowAtIndexPath = { _, _ in false }, |
| 37 | + sectionIndexTitles: @escaping SectionIndexTitles = { _ in nil }, |
| 38 | + sectionForSectionIndexTitle: @escaping SectionForSectionIndexTitle = { _, _, index in index } |
| 39 | + ) { |
| 40 | + self.animationConfiguration = animationConfiguration |
| 41 | + self.decideViewTransition = decideViewTransition |
| 42 | + super.init( |
| 43 | + configureCell: configureCell, |
| 44 | + titleForHeaderInSection: titleForHeaderInSection, |
| 45 | + titleForFooterInSection: titleForFooterInSection, |
| 46 | + canEditRowAtIndexPath: canEditRowAtIndexPath, |
| 47 | + canMoveRowAtIndexPath: canMoveRowAtIndexPath, |
| 48 | + sectionIndexTitles: sectionIndexTitles, |
| 49 | + sectionForSectionIndexTitle: sectionForSectionIndexTitle |
| 50 | + ) |
| 51 | + } |
| 52 | + |
| 53 | + var dataSet = false |
| 54 | + |
| 55 | + open func tableView(_ tableView: UITableView, observedEvent: Event<Element>) { |
| 56 | + Binder(self) { dataSource, newSections in |
| 57 | + if !self.dataSet { |
| 58 | + self.dataSet = true |
| 59 | + dataSource.setSections(newSections) |
| 60 | + tableView.reloadData() |
| 61 | + self.reloadedEvent?() |
| 62 | + } |
| 63 | + else { |
| 64 | + DispatchQueue.main.async { |
| 65 | + // if view is not in view hierarchy, performing batch updates will crash the app |
| 66 | + if tableView.window == nil { |
| 67 | + dataSource.setSections(newSections) |
| 68 | + tableView.reloadData() |
| 69 | + self.reloadedEvent?() |
| 70 | + return |
| 71 | + } |
| 72 | + let oldSections = dataSource.sectionModels |
| 73 | + do { |
| 74 | + let differences = try Diff.differencesForSectionedView(initialSections: oldSections, finalSections: newSections) |
| 75 | + |
| 76 | + switch self.decideViewTransition(self, tableView, differences) { |
| 77 | + case .animated: |
| 78 | + for difference in differences { |
| 79 | + dataSource.setSections(difference.finalSections) |
| 80 | + |
| 81 | + tableView.performBatchUpdates(difference, animationConfiguration: self.animationConfiguration) |
| 82 | + } |
| 83 | + case .reload: |
| 84 | + self.setSections(newSections) |
| 85 | + tableView.reloadData() |
| 86 | + self.reloadedEvent?() |
| 87 | + return |
| 88 | + } |
| 89 | + } |
| 90 | + catch { |
| 91 | + self.setSections(newSections) |
| 92 | + tableView.reloadData() |
| 93 | + self.reloadedEvent?() |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + }.on(observedEvent) |
| 98 | + } |
| 99 | +} |
0 commit comments