Skip to content

Commit

Permalink
tableview custom animated datasource 추가.
Browse files Browse the repository at this point in the history
  • Loading branch information
tilltue committed Dec 9, 2018
1 parent b2bfcdd commit d41ca12
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 3 deletions.
2 changes: 1 addition & 1 deletion BaseExtension.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'BaseExtension'
s.version = '1.2.5'
s.version = '1.2.6'
s.summary = 'base extension'

# This description is used to generate tags and improve search results.
Expand Down
99 changes: 99 additions & 0 deletions BaseExtension/Classes/RxTableViewCustomAnimatedDataSource.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//
// RxTableViewCustomAnimatedDataSource.swift
// BaseExtension
//
// Created by wade.hawk on 09/12/2018.
//

import Foundation
import UIKit
import RxSwift
import RxCocoa
import RxDataSources
import Differentiator

open class RxTableViewCustomAnimatedDataSource<S: AnimatableSectionModelType>
: TableViewSectionedDataSource<S>
, RxTableViewDataSourceType {
public typealias Element = [S]
public typealias DecideViewTransition = (TableViewSectionedDataSource<S>, UITableView, [Changeset<S>]) -> ViewTransition

/// Animation configuration for data source
public var animationConfiguration: AnimationConfiguration

/// Calculates view transition depending on type of changes
public var decideViewTransition: DecideViewTransition

public var reloadedEvent: (() -> Void)? = nil

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
)
}

var dataSet = false

open func tableView(_ tableView: UITableView, observedEvent: Event<Element>) {
Binder(self) { dataSource, newSections in
if !self.dataSet {
self.dataSet = true
dataSource.setSections(newSections)
tableView.reloadData()
self.reloadedEvent?()
}
else {
DispatchQueue.main.async {
// if view is not in view hierarchy, performing batch updates will crash the app
if tableView.window == nil {
dataSource.setSections(newSections)
tableView.reloadData()
self.reloadedEvent?()
return
}
let oldSections = dataSource.sectionModels
do {
let differences = try Diff.differencesForSectionedView(initialSections: oldSections, finalSections: newSections)

switch self.decideViewTransition(self, tableView, differences) {
case .animated:
for difference in differences {
dataSource.setSections(difference.finalSections)

tableView.performBatchUpdates(difference, animationConfiguration: self.animationConfiguration)
}
case .reload:
self.setSections(newSections)
tableView.reloadData()
self.reloadedEvent?()
return
}
}
catch {
self.setSections(newSections)
tableView.reloadData()
self.reloadedEvent?()
}
}
}
}.on(observedEvent)
}
}
2 changes: 2 additions & 0 deletions Example/BaseExtension.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.2;
};
name = Debug;
};
Expand Down Expand Up @@ -474,6 +475,7 @@
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 4.2;
VALIDATE_PRODUCT = YES;
};
name = Release;
Expand Down
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- BaseExtension (1.2.4):
- BaseExtension (1.2.5):
- RxCocoa
- RxDataSources
- RxSwift
Expand Down Expand Up @@ -42,7 +42,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
BaseExtension: 36d99aa5256aad56393b8b727b094d14aa0a876c
BaseExtension: 0d00b8da5b47fa8d75deeb0f1824b5d9e66bc094
Differentiator: be49ca3408f0ecfc761e4c7763d20c62be01b9ad
ObjcExceptionBridging: c30e00eb3700467e695faeea30e26e18bd445001
RxAtomic: eacf60db868c96bfd63320e28619fe29c179656f
Expand Down

0 comments on commit d41ca12

Please sign in to comment.