Skip to content

Commit d41ca12

Browse files
committed
tableview custom animated datasource 추가.
1 parent b2bfcdd commit d41ca12

File tree

4 files changed

+104
-3
lines changed

4 files changed

+104
-3
lines changed

BaseExtension.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'BaseExtension'
11-
s.version = '1.2.5'
11+
s.version = '1.2.6'
1212
s.summary = 'base extension'
1313

1414
# This description is used to generate tags and improve search results.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
}

Example/BaseExtension.xcodeproj/project.pbxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@
429429
ONLY_ACTIVE_ARCH = YES;
430430
SDKROOT = iphoneos;
431431
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
432+
SWIFT_VERSION = 4.2;
432433
};
433434
name = Debug;
434435
};
@@ -474,6 +475,7 @@
474475
MTL_ENABLE_DEBUG_INFO = NO;
475476
SDKROOT = iphoneos;
476477
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
478+
SWIFT_VERSION = 4.2;
477479
VALIDATE_PRODUCT = YES;
478480
};
479481
name = Release;

Example/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PODS:
2-
- BaseExtension (1.2.4):
2+
- BaseExtension (1.2.5):
33
- RxCocoa
44
- RxDataSources
55
- RxSwift
@@ -42,7 +42,7 @@ EXTERNAL SOURCES:
4242
:path: "../"
4343

4444
SPEC CHECKSUMS:
45-
BaseExtension: 36d99aa5256aad56393b8b727b094d14aa0a876c
45+
BaseExtension: 0d00b8da5b47fa8d75deeb0f1824b5d9e66bc094
4646
Differentiator: be49ca3408f0ecfc761e4c7763d20c62be01b9ad
4747
ObjcExceptionBridging: c30e00eb3700467e695faeea30e26e18bd445001
4848
RxAtomic: eacf60db868c96bfd63320e28619fe29c179656f

0 commit comments

Comments
 (0)