Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix removeAll method in sections #2141

Merged
merged 4 commits into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Source/Core/Form.swift
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,20 @@ extension Form : RangeReplaceableCollection {
for section in sections {
section.willBeRemovedFromForm()
}
}

public func removeAll(where shouldBeRemoved: (Section) throws -> Bool) rethrows {
let indices = try kvoWrapper._allSections.enumerated()
.filter { try shouldBeRemoved($0.element)}
.map { $0.offset }

var removedSections = [Section]()
for index in indices.reversed() {
removedSections.append(kvoWrapper._allSections.remove(at: index))
}
kvoWrapper.sections.removeObjects(in: removedSections)

removedSections.forEach { $0.willBeRemovedFromForm() }
}

private func indexForInsertion(at index: Int) -> Int {
Expand Down
14 changes: 14 additions & 0 deletions Source/Core/Section.swift
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,20 @@ extension Section: RangeReplaceableCollection {
}
}

public func removeAll(where shouldBeRemoved: (BaseRow) throws -> Bool) rethrows {
let indices = try kvoWrapper._allRows.enumerated()
.filter { try shouldBeRemoved($0.element)}
.map { $0.offset }

var removedRows = [BaseRow]()
for index in indices.reversed() {
removedRows.append(kvoWrapper._allRows.remove(at: index))
}
kvoWrapper.rows.removeObjects(in: removedRows)

removedRows.forEach { $0.willBeRemovedFromSection() }
}

@discardableResult
public func remove(at position: Int) -> BaseRow {
let row = kvoWrapper.rows.object(at: position) as! BaseRow
Expand Down
43 changes: 43 additions & 0 deletions Tests/SectionsInsertionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,49 @@ class SectionInsertionTests: XCTestCase {
XCTAssertEqual(form.allRows[2], bRow)
}

func testDeletingRows() {
let form = Form()
let section = Section("section_01")
form.append(section)

section.append(NameRow(tag: "row_01"))
section.append(NameRow(tag: "row_2"))
section.append(NameRow("row_03") { $0.hidden = true })
section.append(NameRow("row_04") { $0.hidden = true })

section.removeAll(where: { row in row.tag?.hasPrefix("row_0") ?? false })
XCTAssertNotNil(form.rowBy(tag: "row_2"))
XCTAssertEqual(form.allRows.count, 1)
}

func testDeletingSections() {
let form = Form()
form +++ Section("section_0")
+++ Section("section_1") { $0.hidden = true }
+++ Section("section_22")
+++ Section("section_32")

form.removeAll(where: { section in section.header?.title?.hasSuffix("2") ?? false })
XCTAssertEqual(form.allSections.count, 2)
}

func testReplaceAllSection() {
let form = Form() +++ Section("section1") {
$0.hidden = true
}
+++ Section("section2")
+++ Section("section3")

form.replaceSubrangeInAllSections(Range<Int>(uncheckedBounds: (lower: 0, upper: 2)), with: [Section("section0") { $0.hidden = true }])

XCTAssertEqual(form.allSections.count, 2)
XCTAssertEqual(form.count, 1)
XCTAssertEqual(form[0].header?.title, "section3")
XCTAssertEqual(form.allSections[0].header?.title, "section0")
XCTAssertEqual(form.allSections[1].header?.title, "section3")
}


private func hideAndShowSections(form: Form, expectedTitles titles: [String]) {
// Doesn't matter how rows were added to the form (using append, +++ or subscript index)
// next must work
Expand Down