Upgrade to Pro — share decks privately, control downloads, hide ads and more …

5分で学ぶ差分更新とRxDataSources

 5分で学ぶ差分更新とRxDataSources

Learn in 5 minutes
Incremental updates and RxDataSources

Yuji Hato

April 10, 2018
Tweet

More Decks by Yuji Hato

Other Decks in Technology

Transcript

  1. RxDataSources ɾosteslag/Changeset … Wagner-Fischer ɾjflinter/Dwifft … Myers ɾwokalski/Diff.swift … Myers

    ɾonyarnold/Differ … Myers(Diff.swiftͷfork) ɾonmyway133/DeepDiff … HeckelΆ͍ ɾInstagram/IGListKit … Heckel ɾRxSwiftCommunity/RxDataSources … Heckelվ ɾkazuhiro4949/EditDistance … Wu
  2. public protocol SectionModelType { associatedtype Item public var items: [Self.Item]

    { get } public init(original: Self, items: [Self.Item]) } RxDataSources
  3. enum DownloadSeriesSectionModel: AnimatableSectionModelType { typealias Item = DownloadSeriesSectionItem case episodeList(season:

    DownloadSeason?, items: [Item]) case other(items: [Item]) // Mark: - IdentifiableType var identity: String { … } // Mark: - SectionModelType var items: [DownloadSeriesSectionItem] { switch self { case .episodeList(_, let items): return items case .other(let items): return items } } init(original: DownloadSeriesSectionModel, items: [Item]) { switch original { case .episodeList(let season, _): self = .episodeList(season: season, items: items) case .other: self = .other(items: items) } } } RxDataSources
  4. enum DownloadSeriesSectionItem: IdentifiableType, Equatable { case episode(downloadMedia: DownloadMedia) case seeOtherEpisode(series:

    DownloadSeries) // Mark: - IdentifiableType var identity: String { … } // Mark: - Equatable static func == (lhs: DownloadSeriesSectionItem, rhs: DownloadSeriesSectionItem) -> Bool { … } } RxDataSources
  5. public struct AnimatableSectionModel<Section: IdentifiableType, ItemType: IdentifiableType & Equatable> { public

    var model: Section public var items: [Item] public init(model: Section, items: [ItemType]) { self.model = model self.items = items } } extension AnimatableSectionModel : AnimatableSectionModelType { public typealias Item = ItemType public typealias Identity = Section.Identity public var identity: Section.Identity { return model.identity } public init(original: AnimatableSectionModel, items: [Item]) { self.model = original.model self.items = items } public var hashValue: Int { return self.model.identity.hashValue } } RxDataSources Α͘ݟͨΒ3Y%BUB4PVSDFT ʹ"OJNBUBCMF4FDUJPO.PEFM͕ ༻ҙ͞Ε͍ͯΔͷͰ͜ΕΛ࢖͏ ͷ͕CFUUFS
  6. open class RxTableViewSectionedAnimatedDataSource<S: AnimatableSectionModelType> : TableViewSectionedDataSource<S> , RxTableViewDataSourceType { …

    public init( animationConfiguration: AnimationConfiguration = AnimationConfiguration(), decideViewTransition: @escaping DecideViewTransition = { _, _, _ in .animated }, configureCell: @escaping ConfigureCell, titleForHeaderInSection: @escaping TitleForHeaderInSection = { _, _ in nil }, titleForFooterInSection: @escaping TitleForFooterInSection = { _, _ in nil }, canEditRowAtIndexPath: @escaping CanEditRowAtIndexPath = { _, _ in false }, canMoveRowAtIndexPath: @escaping CanMoveRowAtIndexPath = { _, _ in false }, sectionIndexTitles: @escaping SectionIndexTitles = { _ in nil }, sectionForSectionIndexTitle: @escaping SectionForSectionIndexTitle = { _, _, index in index } ) { self.animationConfiguration = animationConfiguration self.decideViewTransition = decideViewTransition super.init( configureCell: configureCell, titleForHeaderInSection: titleForHeaderInSection, titleForFooterInSection: titleForFooterInSection, canEditRowAtIndexPath: canEditRowAtIndexPath, canMoveRowAtIndexPath: canMoveRowAtIndexPath, sectionIndexTitles: sectionIndexTitles, sectionForSectionIndexTitle: sectionForSectionIndexTitle ) } … } RxDataSources
  7. final class DownloadSeriesDelegate: NSObject, UITableViewDelegate { … lazy var dataSource:

    RxTableViewSectionedAnimatedDataSource<DownloadSeriesSectionModel> = .init( animationConfiguration: AnimationConfiguration(insertAnimation: .fade, reloadAnimation: .none, deleteAnimation: .fade), configureCell: { [weak self] dataSource, table, indexPath, item in guard let me = self else { return UITableViewCell() // Should never reach here. } switch item { case .episode(let downloadMedia): let cell = table.dequeueReusableCell(DownloadListMediaCell.self, forIndexPath: indexPath) cell.configure(downloadMedia: downloadMedia) … return cell case .seeOtherEpisode(let series): let cell = table.dequeueReusableCell(DownloadSeeOtherEpisodeCell.self, forIndexPath: indexPath) cell.rx.tapGesture .subscribe(onNext: { [weak self] in … }) .disposed(by: cell.reusableDisposeBag) … return cell } }) … RxDataSources
  8. final class DownloadSeriesViewStream { … let sectionModels: Property<[DownloadSeriesSectionModel]> private let

    _sectionModels = Variable<[DownloadSeriesSectionModel]>([]) … } RxDataSources
  9. final class DownloadSeriesViewController: UIViewController { … viewStream.sectionModels.asObservable() // IMPORTANT: //

    crashճආɺཁௐࠪ. .throttle(1.0, latest: true, scheduler: ConcurrentMainScheduler.instance) .bind(to: tableView.rx.items(dataSource: delegate.dataSource)) .disposed(by: rx.disposeBag) … RxDataSources
  10. // MARK: - UITableViewDelegate func tableView(_ tableView: UITableView, heightForRowAt indexPath:

    IndexPath) -> CGFloat { … } func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { … } func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { … } func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { … } … RxDataSources