Skip to content

Commit

Permalink
Make reloading sections an array. Fixed section deletion range logic.…
Browse files Browse the repository at this point in the history
… Hooked up tests.
  • Loading branch information
colinhumber committed Apr 9, 2019
1 parent c7d9132 commit 3e705a3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
13 changes: 13 additions & 0 deletions Static.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@
remoteGlobalIDString = 21826AA91B3F51A100AA9641;
remoteInfo = "Static-iOS";
};
3B1129252226E5D800B6E06A /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 21826AA11B3F51A100AA9641 /* Project object */;
proxyType = 1;
remoteGlobalIDString = 36748D4E1B5034EC0046F207;
remoteInfo = Example;
};
/* End PBXContainerItemProxy section */

/* Begin PBXCopyFilesBuildPhase section */
Expand Down Expand Up @@ -242,6 +249,7 @@
);
dependencies = (
21826AB71B3F51A100AA9641 /* PBXTargetDependency */,
3B1129262226E5D800B6E06A /* PBXTargetDependency */,
);
name = StaticTests;
productName = StaticTests;
Expand Down Expand Up @@ -391,6 +399,11 @@
target = 21826AA91B3F51A100AA9641 /* Static-iOS */;
targetProxy = 363129C51B50354E0024E339 /* PBXContainerItemProxy */;
};
3B1129262226E5D800B6E06A /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = 36748D4E1B5034EC0046F207 /* Example */;
targetProxy = 3B1129252226E5D800B6E06A /* PBXContainerItemProxy */;
};
/* End PBXTargetDependency section */

/* Begin PBXVariantGroup section */
Expand Down
10 changes: 10 additions & 0 deletions Static.xcodeproj/xcshareddata/xcschemes/Example.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "21826AB31B3F51A100AA9641"
BuildableName = "StaticTests.xctest"
BlueprintName = "StaticTests"
ReferencedContainer = "container:Static.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
Expand Down
9 changes: 4 additions & 5 deletions Static/DataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public class DataSource: NSObject {
refresh()
}

private func refresh(oldSections: [Section]? = nil) {
private func refresh(oldSections: [Section] = []) {
refreshTableSections(oldSections: oldSections)
refreshRegisteredCells()
}
Expand All @@ -127,9 +127,9 @@ public class DataSource: NSObject {
return nil
}

private func refreshTableSections(oldSections: [Section]? = nil) {
private func refreshTableSections(oldSections: [Section] = []) {
guard let tableView = tableView else { return }
guard let oldSections = oldSections else {
guard !oldSections.isEmpty else {
tableView.reloadData()
return
}
Expand All @@ -151,8 +151,7 @@ public class DataSource: NSObject {
tableView.insertSections(IndexSet(integersIn: range), with: animation)
} else {
// Remove sections
let start = oldCount - 1
let range: Range<IndexSet.Element> = start..<(start - delta)
let range: Range<IndexSet.Element> = newCount..<(newCount - delta)
tableView.deleteSections(IndexSet(integersIn: range), with: animation)
}

Expand Down
8 changes: 8 additions & 0 deletions Static/Tests/DataSourceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ class DataSourceTests: XCTestCase {
XCTAssertEqual(4, tableView.numberOfRows(inSection: 1))
XCTAssertEqual(2, tableView.numberOfRows(inSection: 2))

dataSource.sections = [
Section(rows: [Row(text: "Merrily"), Row(text: "Merrily")]),
Section(rows: [Row(text: "Life"), Row(text: "Is"), Row(text: "But"), Row(text: "A"), Row(text: "Dream")]),
]
XCTAssertEqual(2, tableView.numberOfSections)
XCTAssertEqual(2, tableView.numberOfRows(inSection: 0))
XCTAssertEqual(5, tableView.numberOfRows(inSection: 1))

dataSource.sections = []
XCTAssertEqual(0, tableView.numberOfSections)
}
Expand Down

0 comments on commit 3e705a3

Please sign in to comment.