Skip to content

Commit

Permalink
Fix removeAll method in sections (#2141)
Browse files Browse the repository at this point in the history
* Fix removeAll method in sections

* Add override to Form

* Add some tests

* Refactor functions
  • Loading branch information
mats-claassen authored Feb 18, 2021
1 parent 019d800 commit 15ca571
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
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

0 comments on commit 15ca571

Please sign in to comment.