|
| 1 | +// |
| 2 | +// RxTableViewDiffableDataSource.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by mlch911 on 2023/5/30. |
| 6 | +// |
| 7 | + |
| 8 | +#if os(iOS) || os(tvOS) |
| 9 | +import Foundation |
| 10 | +import UIKit |
| 11 | +#if !RX_NO_MODULE |
| 12 | +import RxSwift |
| 13 | +import RxCocoa |
| 14 | +#endif |
| 15 | + |
| 16 | +@available(iOS 13.0, tvOS 13.0, *) |
| 17 | +open class RxTableViewDiffableDataSource<Section: DiffableSectionModelType> |
| 18 | + : TableViewDiffableDataSource<Section> |
| 19 | + , RxTableViewDataSourceType { |
| 20 | + public typealias Element = [Section] |
| 21 | + |
| 22 | + open func tableView(_ tableView: UITableView, observedEvent: RxSwift.Event<[Section]>) { |
| 23 | + Binder(self) { dataSource, sections in |
| 24 | + var snapshot = NSDiffableDataSourceSnapshot<Section, Section.Item>() |
| 25 | + snapshot.appendSections(sections) |
| 26 | + sections.forEach { section in |
| 27 | + snapshot.appendItems(section.items, toSection: section) |
| 28 | + } |
| 29 | + dataSource.apply(snapshot, animatingDifferences: false) |
| 30 | + }.on(observedEvent) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +@available(iOS 13.0, tvOS 13.0, *) |
| 35 | +open class RxTableViewAnimatedDiffableDataSource<Section: DiffableSectionModelType> |
| 36 | +: TableViewDiffableDataSource<Section> |
| 37 | +, RxTableViewDataSourceType { |
| 38 | + public typealias Element = [Section] |
| 39 | + public typealias DecideViewTransition = (TableViewDiffableDataSource<Section>, UITableView, NSDiffableDataSourceSnapshot<Section, Section.Item>) -> ViewTransition |
| 40 | + |
| 41 | + /// Calculates view transition depending on type of changes |
| 42 | + public var decideViewTransition: DecideViewTransition |
| 43 | + |
| 44 | +#if os(iOS) |
| 45 | + public init( |
| 46 | + tableView: UITableView, |
| 47 | + animation: UITableView.RowAnimation = .automatic, |
| 48 | + decideViewTransition: @escaping DecideViewTransition = { _, _, _ in .animated }, |
| 49 | + configureCell: @escaping ConfigureCell, |
| 50 | + titleForHeaderInSectionProvider: @escaping TitleForHeaderInSectionProvider = { _, _ in nil }, |
| 51 | + titleForFooterInSectionProvider: @escaping TitleForFooterInSectionProvider = { _, _ in nil }, |
| 52 | + canEditRowAtIndexPathProvider: @escaping CanEditRowAtIndexPathProvider = { _, _ in true }, |
| 53 | + canMoveRowAtIndexPathProvider: @escaping CanMoveRowAtIndexPathProvider = { _, _ in true }, |
| 54 | + sectionIndexTitles: @escaping SectionIndexTitlesProvider = { _ in nil }, |
| 55 | + sectionForSectionIndexTitle: @escaping SectionForSectionIndexTitleProvider = { _, _, index in index } |
| 56 | + ) { |
| 57 | + self.decideViewTransition = decideViewTransition |
| 58 | + super.init(tableView: tableView, |
| 59 | + configureCell: configureCell, |
| 60 | + titleForHeaderInSectionProvider: titleForHeaderInSectionProvider, |
| 61 | + titleForFooterInSectionProvider: titleForFooterInSectionProvider, |
| 62 | + canEditRowAtIndexPathProvider: canEditRowAtIndexPathProvider, |
| 63 | + canMoveRowAtIndexPathProvider: canMoveRowAtIndexPathProvider, |
| 64 | + sectionIndexTitles: sectionIndexTitles, |
| 65 | + sectionForSectionIndexTitle: sectionForSectionIndexTitle) |
| 66 | + defaultRowAnimation = animation |
| 67 | + } |
| 68 | +#else |
| 69 | + public init( |
| 70 | + tableView: UITableView, |
| 71 | + animation: UITableView.RowAnimation = .automatic, |
| 72 | + decideViewTransition: @escaping DecideViewTransition = { _, _, _ in .animated }, |
| 73 | + configureCell: @escaping ConfigureCell, |
| 74 | + titleForHeaderInSectionProvider: @escaping TitleForHeaderInSectionProvider = { _, _ in nil }, |
| 75 | + titleForFooterInSectionProvider: @escaping TitleForFooterInSectionProvider = { _, _ in nil }, |
| 76 | + canEditRowAtIndexPathProvider: @escaping CanEditRowAtIndexPathProvider = { _, _ in true }, |
| 77 | + canMoveRowAtIndexPathProvider: @escaping CanMoveRowAtIndexPathProvider = { _, _ in true } |
| 78 | + ) { |
| 79 | + self.decideViewTransition = decideViewTransition |
| 80 | + super.init(tableView: tableView, |
| 81 | + configureCell: configureCell, |
| 82 | + titleForHeaderInSectionProvider: titleForHeaderInSectionProvider, |
| 83 | + titleForFooterInSectionProvider: titleForFooterInSectionProvider, |
| 84 | + canEditRowAtIndexPathProvider: canEditRowAtIndexPathProvider, |
| 85 | + canMoveRowAtIndexPathProvider: canMoveRowAtIndexPathProvider) |
| 86 | + defaultRowAnimation = animation |
| 87 | + } |
| 88 | +#endif |
| 89 | + |
| 90 | + open func tableView(_ tableView: UITableView, observedEvent: RxSwift.Event<[Section]>) { |
| 91 | + Binder(self) { dataSource, sections in |
| 92 | + var snapshot = NSDiffableDataSourceSnapshot<Section, Section.Item>() |
| 93 | + snapshot.appendSections(sections) |
| 94 | + sections.forEach { section in |
| 95 | + snapshot.appendItems(section.items, toSection: section) |
| 96 | + } |
| 97 | + let animated = dataSource.decideViewTransition(dataSource, tableView, snapshot) == .animated |
| 98 | + dataSource.apply(snapshot, animatingDifferences: animated) |
| 99 | + }.on(observedEvent) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +#endif |
0 commit comments