Skip to content

Commit 866eae8

Browse files
committed
refactor
1 parent 9b47f7d commit 866eae8

5 files changed

Lines changed: 75 additions & 81 deletions

File tree

Demo.xcodeproj/project.pbxproj

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88

99
/* Begin PBXBuildFile section */
1010
642FDD4E819B3694FD83391B /* Pods_Demo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 54ABCD53545EDBB85F3C822B /* Pods_Demo.framework */; };
11-
8D27972A1D77DDEA00BBAC45 /* OriginalRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D2797261D77DDEA00BBAC45 /* OriginalRow.swift */; };
12-
8D27972B1D77DDEA00BBAC45 /* OriginalSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D2797271D77DDEA00BBAC45 /* OriginalSection.swift */; };
13-
8D27972C1D77DDEA00BBAC45 /* OriginalTable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D2797281D77DDEA00BBAC45 /* OriginalTable.swift */; };
11+
8D27972A1D77DDEA00BBAC45 /* TableRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D2797261D77DDEA00BBAC45 /* TableRow.swift */; };
12+
8D27972B1D77DDEA00BBAC45 /* TableSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D2797271D77DDEA00BBAC45 /* TableSection.swift */; };
13+
8D27972C1D77DDEA00BBAC45 /* TableViewWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D2797281D77DDEA00BBAC45 /* TableViewWrapper.swift */; };
1414
8D27972D1D77DDEA00BBAC45 /* StaticTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D2797291D77DDEA00BBAC45 /* StaticTableViewController.swift */; };
1515
8D27972F1D77DE5F00BBAC45 /* Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 8D27972E1D77DE5F00BBAC45 /* Info.plist */; };
1616
8D2797471D77E74C00BBAC45 /* StaticTableViewController.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8D2797401D77E74B00BBAC45 /* StaticTableViewController.framework */; };
1717
8D2797481D77E74C00BBAC45 /* StaticTableViewController.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 8D2797401D77E74B00BBAC45 /* StaticTableViewController.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
18-
8D27974C1D77E75D00BBAC45 /* OriginalRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D2797261D77DDEA00BBAC45 /* OriginalRow.swift */; };
19-
8D27974D1D77E75D00BBAC45 /* OriginalSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D2797271D77DDEA00BBAC45 /* OriginalSection.swift */; };
20-
8D27974E1D77E75D00BBAC45 /* OriginalTable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D2797281D77DDEA00BBAC45 /* OriginalTable.swift */; };
18+
8D27974C1D77E75D00BBAC45 /* TableRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D2797261D77DDEA00BBAC45 /* TableRow.swift */; };
19+
8D27974D1D77E75D00BBAC45 /* TableSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D2797271D77DDEA00BBAC45 /* TableSection.swift */; };
20+
8D27974E1D77E75D00BBAC45 /* TableViewWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D2797281D77DDEA00BBAC45 /* TableViewWrapper.swift */; };
2121
8D27974F1D77E75D00BBAC45 /* StaticTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D2797291D77DDEA00BBAC45 /* StaticTableViewController.swift */; };
2222
8D654C311F2C2D56005019EF /* DemoTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D654C301F2C2D56005019EF /* DemoTests.swift */; };
2323
8DB921701D76BF0F006961FE /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8DB921681D76BF0F006961FE /* AppDelegate.swift */; };
@@ -61,9 +61,9 @@
6161
/* Begin PBXFileReference section */
6262
50106CF00BC81E4459C9B134 /* Pods-Demo.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Demo.release.xcconfig"; path = "Pods/Target Support Files/Pods-Demo/Pods-Demo.release.xcconfig"; sourceTree = "<group>"; };
6363
54ABCD53545EDBB85F3C822B /* Pods_Demo.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Demo.framework; sourceTree = BUILT_PRODUCTS_DIR; };
64-
8D2797261D77DDEA00BBAC45 /* OriginalRow.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OriginalRow.swift; sourceTree = "<group>"; };
65-
8D2797271D77DDEA00BBAC45 /* OriginalSection.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OriginalSection.swift; sourceTree = "<group>"; };
66-
8D2797281D77DDEA00BBAC45 /* OriginalTable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OriginalTable.swift; sourceTree = "<group>"; };
64+
8D2797261D77DDEA00BBAC45 /* TableRow.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableRow.swift; sourceTree = "<group>"; };
65+
8D2797271D77DDEA00BBAC45 /* TableSection.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableSection.swift; sourceTree = "<group>"; };
66+
8D2797281D77DDEA00BBAC45 /* TableViewWrapper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableViewWrapper.swift; sourceTree = "<group>"; };
6767
8D2797291D77DDEA00BBAC45 /* StaticTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StaticTableViewController.swift; sourceTree = "<group>"; };
6868
8D27972E1D77DE5F00BBAC45 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
6969
8D2797401D77E74B00BBAC45 /* StaticTableViewController.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = StaticTableViewController.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -128,9 +128,9 @@
128128
isa = PBXGroup;
129129
children = (
130130
8D27972E1D77DE5F00BBAC45 /* Info.plist */,
131-
8D2797261D77DDEA00BBAC45 /* OriginalRow.swift */,
132-
8D2797271D77DDEA00BBAC45 /* OriginalSection.swift */,
133-
8D2797281D77DDEA00BBAC45 /* OriginalTable.swift */,
131+
8D2797261D77DDEA00BBAC45 /* TableRow.swift */,
132+
8D2797271D77DDEA00BBAC45 /* TableSection.swift */,
133+
8D2797281D77DDEA00BBAC45 /* TableViewWrapper.swift */,
134134
8D2797291D77DDEA00BBAC45 /* StaticTableViewController.swift */,
135135
);
136136
path = Source;
@@ -377,9 +377,9 @@
377377
isa = PBXSourcesBuildPhase;
378378
buildActionMask = 2147483647;
379379
files = (
380-
8D27974C1D77E75D00BBAC45 /* OriginalRow.swift in Sources */,
381-
8D27974D1D77E75D00BBAC45 /* OriginalSection.swift in Sources */,
382-
8D27974E1D77E75D00BBAC45 /* OriginalTable.swift in Sources */,
380+
8D27974C1D77E75D00BBAC45 /* TableRow.swift in Sources */,
381+
8D27974D1D77E75D00BBAC45 /* TableSection.swift in Sources */,
382+
8D27974E1D77E75D00BBAC45 /* TableViewWrapper.swift in Sources */,
383383
8D27974F1D77E75D00BBAC45 /* StaticTableViewController.swift in Sources */,
384384
);
385385
runOnlyForDeploymentPostprocessing = 0;
@@ -399,9 +399,9 @@
399399
8DB921751D76BF0F006961FE /* ViewController.swift in Sources */,
400400
8D27972D1D77DDEA00BBAC45 /* StaticTableViewController.swift in Sources */,
401401
8DB921701D76BF0F006961FE /* AppDelegate.swift in Sources */,
402-
8D27972C1D77DDEA00BBAC45 /* OriginalTable.swift in Sources */,
403-
8D27972B1D77DDEA00BBAC45 /* OriginalSection.swift in Sources */,
404-
8D27972A1D77DDEA00BBAC45 /* OriginalRow.swift in Sources */,
402+
8D27972C1D77DDEA00BBAC45 /* TableViewWrapper.swift in Sources */,
403+
8D27972B1D77DDEA00BBAC45 /* TableSection.swift in Sources */,
404+
8D27972A1D77DDEA00BBAC45 /* TableRow.swift in Sources */,
405405
);
406406
runOnlyForDeploymentPostprocessing = 0;
407407
};
@@ -654,6 +654,7 @@
654654
8D654C361F2C2D56005019EF /* Release */,
655655
);
656656
defaultConfigurationIsVisible = 0;
657+
defaultConfigurationName = Release;
657658
};
658659
8DB921311D76A0C2006961FE /* Build configuration list for PBXProject "Demo" */ = {
659660
isa = XCConfigurationList;

Source/StaticTableViewController.swift

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,86 @@
11
import UIKit
22

3-
open class StaticTableViewController: UITableViewController, OriginalTableConfig {
3+
open class StaticTableViewController: UITableViewController, TableViewConfig {
44

55
open var animateSectionHeaders = false
66

7-
open var insertTableViewRowAnimation: UITableViewRowAnimation = .fade
8-
open var deleteTableViewRowAnimation: UITableViewRowAnimation = .fade
9-
open var reloadTableViewRowAnimation: UITableViewRowAnimation = .fade
7+
open var insertAnimation: UITableViewRowAnimation = .fade
8+
open var deleteAnimation: UITableViewRowAnimation = .fade
9+
open var reloadAnimation: UITableViewRowAnimation = .fade
1010

11-
var originalTable: OriginalTable?
11+
var tableViewWrapper: TableViewWrapper?
1212

1313
override open func viewDidLoad() {
1414
super.viewDidLoad()
1515

16-
originalTable = OriginalTable(tableView: tableView, config: self)
16+
tableViewWrapper = TableViewWrapper(tableView: tableView, config: self)
1717
}
1818

1919
open func update(cells: UITableViewCell...) {
2020
cells.forEach { cell in
21-
let row = originalTable!.originalRowWithTableViewCell(cell)
21+
let row = tableViewWrapper!.row(with: cell)
2222
row.update()
2323
}
2424
}
2525

2626
open func set(cells: UITableViewCell..., hidden: Bool) {
2727
cells.forEach { cell in
28-
let row = originalTable!.originalRowWithTableViewCell(cell)
28+
let row = tableViewWrapper!.row(with: cell)
2929
row.hiding = hidden
3030
}
3131
}
3232

3333
open func set(cells: UITableViewCell..., height: CGFloat) {
3434
cells.forEach { cell in
35-
let row = originalTable!.originalRowWithTableViewCell(cell)
35+
let row = tableViewWrapper!.row(with: cell)
3636
row.height = height
3737
}
3838
}
3939

4040
open func isHidden(cell: UITableViewCell) -> Bool {
41-
return originalTable!.originalRowWithTableViewCell(cell).hidden
41+
return tableViewWrapper!.row(with: cell).hidden
4242
}
4343

4444
open func reloadData(animated: Bool) {
45-
originalTable!.reloadRows(animated: animated)
45+
tableViewWrapper!.reloadRows(animated: animated)
4646
}
4747

4848
// MARK: UITableViewDataSource
4949
override open func numberOfSections(in tableView: UITableView) -> Int {
50-
if originalTable == nil {
50+
if tableViewWrapper == nil {
5151
return super.numberOfSections(in: tableView)
5252
} else {
53-
return originalTable!.sections.filter { $0.rows.count != 0 }.count
53+
return tableViewWrapper!.sections.filter { $0.rows.count != 0 }.count
5454
}
5555
}
5656

5757
override open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
58-
if originalTable == nil {
58+
if tableViewWrapper == nil {
5959
return super.tableView(tableView, numberOfRowsInSection: section)
6060
} else {
61-
return originalTable!.sections[section].numberOfVissibleRows()
61+
return tableViewWrapper!.sections[section].numberOfVissibleRows()
6262
}
6363
}
6464

6565
override open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
66-
if originalTable == nil {
66+
if tableViewWrapper == nil {
6767
return super.tableView(tableView, cellForRowAt: indexPath)
6868
} else {
69-
let row = originalTable?.vissibleOriginalRowWithIndexPath(indexPath)
69+
let row = tableViewWrapper?.vissibleRow(with: indexPath)
7070
return row!.cell
7171
}
7272
}
7373

7474
override open func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
75-
if originalTable == nil {
75+
if tableViewWrapper == nil {
7676
return super.tableView(tableView, heightForRowAt: indexPath)
7777
} else {
78-
let row = originalTable!.vissibleOriginalRowWithIndexPath(indexPath)
78+
let row = tableViewWrapper!.vissibleRow(with: indexPath)
7979

8080
if row.height != CGFloat.greatestFiniteMagnitude {
8181
return row.height
8282
} else {
83-
return super.tableView(tableView, heightForRowAt: row.originalIndexPath)
83+
return super.tableView(tableView, heightForRowAt: row.indexPath)
8484
}
8585
}
8686
}
@@ -114,7 +114,7 @@ open class StaticTableViewController: UITableViewController, OriginalTableConfig
114114
}
115115

116116
func headerFooterHeightForSection(_ section: Int, height: CGFloat) -> CGFloat {
117-
let section = originalTable?.sections[section]
117+
let section = tableViewWrapper?.sections[section]
118118
if section?.numberOfVissibleRows() == 0 {
119119
return 0
120120
} else {
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ enum BatchOperation {
44
case none, insert, delete, update
55
}
66

7-
class OriginalRow {
7+
class TableRow {
88

9-
init(cell: UITableViewCell, originalIndexPath: IndexPath){
9+
init(cell: UITableViewCell, indexPath: IndexPath){
1010
self.cell = cell
11-
self.originalIndexPath = originalIndexPath
11+
self.indexPath = indexPath
1212
}
1313

1414
var hidden: Bool = false
@@ -27,7 +27,7 @@ class OriginalRow {
2727

2828
var cell: UITableViewCell
2929

30-
var originalIndexPath: IndexPath
30+
var indexPath: IndexPath
3131

3232
var height: CGFloat = CGFloat.greatestFiniteMagnitude
3333

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import UIKit
22

3-
class OriginalSection {
3+
class TableSection {
44

5-
var rows: [OriginalRow] = []
5+
var rows: [TableRow] = []
66

77
func numberOfVissibleRows() -> Int {
88
return rows.filter { !$0.hiding }.count
Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
import UIKit
22

3-
protocol OriginalTableConfig {
4-
var insertTableViewRowAnimation: UITableViewRowAnimation { get set }
5-
var deleteTableViewRowAnimation: UITableViewRowAnimation { get set }
6-
var reloadTableViewRowAnimation: UITableViewRowAnimation { get set }
3+
protocol TableViewConfig {
4+
var insertAnimation: UITableViewRowAnimation { get set }
5+
var deleteAnimation: UITableViewRowAnimation { get set }
6+
var reloadAnimation: UITableViewRowAnimation { get set }
77

88
var animateSectionHeaders: Bool { get set }
99
}
1010

11-
class OriginalTable {
11+
class TableViewWrapper {
1212

13-
var sections: [OriginalSection]
13+
var sections: [TableSection]
1414

1515
var tableView: UITableView
1616

1717
var insertIndexPaths: [IndexPath]
1818

1919
var deleteIndexPaths: [IndexPath]
2020

21-
var updateIndexPaths: [IndexPath]
21+
var reloadIndexPaths: [IndexPath]
2222

23-
var config: OriginalTableConfig
23+
var config: TableViewConfig
2424

25-
init(tableView: UITableView, config: OriginalTableConfig) {
26-
sections = Array(repeating: OriginalSection(), count: tableView.numberOfSections)
25+
init(tableView: UITableView, config: TableViewConfig) {
26+
sections = Array(repeating: TableSection(), count: tableView.numberOfSections)
2727

2828
var totalNumberOfRows: Int = 0
2929
for i in 0..<tableView.numberOfSections {
30-
let section = OriginalSection()
30+
let section = TableSection()
3131
let numberOfRows = tableView.numberOfRows(inSection: i)
3232

3333
totalNumberOfRows += numberOfRows
@@ -36,7 +36,7 @@ class OriginalTable {
3636
let indexPath = IndexPath(row: ii, section: i)
3737
let cell = tableView.dataSource!.tableView(tableView, cellForRowAt: indexPath)
3838

39-
let row = OriginalRow(cell: cell, originalIndexPath: indexPath)
39+
let row = TableRow(cell: cell, indexPath: indexPath)
4040
section.rows.append(row)
4141
}
4242

@@ -45,45 +45,38 @@ class OriginalTable {
4545

4646
insertIndexPaths = Array(repeating: IndexPath(), count: totalNumberOfRows)
4747
deleteIndexPaths = Array(repeating: IndexPath(), count: totalNumberOfRows)
48-
updateIndexPaths = Array(repeating: IndexPath(), count: totalNumberOfRows)
48+
reloadIndexPaths = Array(repeating: IndexPath(), count: totalNumberOfRows)
4949

5050
self.tableView = tableView
5151
self.config = config
5252
}
5353

54-
func originalRowWithIndexPath(_ indexPath: IndexPath) -> OriginalRow {
55-
let section = sections[indexPath.section]
56-
let row = section.rows[indexPath.row]
57-
58-
return row
59-
}
60-
61-
func vissibleOriginalRowWithIndexPath(_ indexPath: IndexPath) -> OriginalRow {
54+
func vissibleRow(with indexPath: IndexPath) -> TableRow {
6255
let section = sections[indexPath.section]
6356
let visibleRows = section.rows.filter { !$0.hiding }
6457

6558
return visibleRows[indexPath.row]
6659
}
6760

68-
func originalRowWithTableViewCell(_ cell: UITableViewCell) -> OriginalRow {
61+
func row(with cell: UITableViewCell) -> TableRow {
6962
let allRows = sections.flatMap { $0.rows }
7063

7164
return allRows.filter { $0.cell === cell }.first!
7265
}
7366

74-
func indexPathForInsertingOriginalRow(_ originalRow: OriginalRow) -> IndexPath {
75-
let indexSection = originalRow.originalIndexPath.section
76-
var indexRow = originalRow.originalIndexPath.row
67+
func insert(row: TableRow) -> IndexPath {
68+
let indexSection = row.indexPath.section
69+
var indexRow = row.indexPath.row
7770

7871
let section = sections[indexSection]
7972
indexRow = section.rows[0..<indexRow].filter { !$0.hiding }.count
8073

8174
return IndexPath(row: indexRow, section: indexSection)
8275
}
8376

84-
func indexPathForDeletingOriginalRow(_ originalRow: OriginalRow) -> IndexPath {
85-
let indexSection = originalRow.originalIndexPath.section
86-
var indexRow = originalRow.originalIndexPath.row
77+
func delete(row: TableRow) -> IndexPath {
78+
let indexSection = row.indexPath.section
79+
var indexRow = row.indexPath.row
8780

8881
let section = sections[indexSection]
8982
indexRow = section.rows[0..<indexRow].filter { !$0.hidden }.count
@@ -94,20 +87,20 @@ class OriginalTable {
9487
func prepareUpdates() {
9588
insertIndexPaths.removeAll()
9689
deleteIndexPaths.removeAll()
97-
updateIndexPaths.removeAll()
90+
reloadIndexPaths.removeAll()
9891

9992
let allRows = sections.flatMap { $0.rows }
10093

10194
allRows.forEach { (row) in
10295
if row.batchOperation == .delete {
103-
let indexPath = indexPathForDeletingOriginalRow(row)
96+
let indexPath = delete(row: row)
10497
deleteIndexPaths.append(indexPath)
10598
} else if row.batchOperation == .insert {
106-
let indexPath = indexPathForInsertingOriginalRow(row)
99+
let indexPath = insert(row: row)
107100
insertIndexPaths.append(indexPath)
108101
} else if row.batchOperation == .update {
109-
let indexPath = indexPathForInsertingOriginalRow(row)
110-
updateIndexPaths.append(indexPath)
102+
let indexPath = insert(row: row)
103+
reloadIndexPaths.append(indexPath)
111104
}
112105
}
113106

@@ -132,9 +125,9 @@ class OriginalTable {
132125

133126
tableView.beginUpdates()
134127

135-
tableView.reloadRows(at: updateIndexPaths, with: config.reloadTableViewRowAnimation)
136-
tableView.insertRows(at: insertIndexPaths, with: config.insertTableViewRowAnimation)
137-
tableView.deleteRows(at: deleteIndexPaths, with: config.deleteTableViewRowAnimation)
128+
tableView.reloadRows(at: reloadIndexPaths, with: config.reloadAnimation)
129+
tableView.insertRows(at: insertIndexPaths, with: config.insertAnimation)
130+
tableView.deleteRows(at: deleteIndexPaths, with: config.deleteAnimation)
138131

139132
tableView.endUpdates()
140133

0 commit comments

Comments
 (0)