Skip to content

Commit 15ca571

Browse files
Fix removeAll method in sections (#2141)
* Fix removeAll method in sections * Add override to Form * Add some tests * Refactor functions
1 parent 019d800 commit 15ca571

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

Source/Core/Form.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,20 @@ extension Form : RangeReplaceableCollection {
266266
for section in sections {
267267
section.willBeRemovedFromForm()
268268
}
269+
}
270+
271+
public func removeAll(where shouldBeRemoved: (Section) throws -> Bool) rethrows {
272+
let indices = try kvoWrapper._allSections.enumerated()
273+
.filter { try shouldBeRemoved($0.element)}
274+
.map { $0.offset }
275+
276+
var removedSections = [Section]()
277+
for index in indices.reversed() {
278+
removedSections.append(kvoWrapper._allSections.remove(at: index))
279+
}
280+
kvoWrapper.sections.removeObjects(in: removedSections)
269281

282+
removedSections.forEach { $0.willBeRemovedFromForm() }
270283
}
271284

272285
private func indexForInsertion(at index: Int) -> Int {

Source/Core/Section.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,20 @@ extension Section: RangeReplaceableCollection {
324324
}
325325
}
326326

327+
public func removeAll(where shouldBeRemoved: (BaseRow) throws -> Bool) rethrows {
328+
let indices = try kvoWrapper._allRows.enumerated()
329+
.filter { try shouldBeRemoved($0.element)}
330+
.map { $0.offset }
331+
332+
var removedRows = [BaseRow]()
333+
for index in indices.reversed() {
334+
removedRows.append(kvoWrapper._allRows.remove(at: index))
335+
}
336+
kvoWrapper.rows.removeObjects(in: removedRows)
337+
338+
removedRows.forEach { $0.willBeRemovedFromSection() }
339+
}
340+
327341
@discardableResult
328342
public func remove(at position: Int) -> BaseRow {
329343
let row = kvoWrapper.rows.object(at: position) as! BaseRow

Tests/SectionsInsertionTests.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,49 @@ class SectionInsertionTests: XCTestCase {
201201
XCTAssertEqual(form.allRows[2], bRow)
202202
}
203203

204+
func testDeletingRows() {
205+
let form = Form()
206+
let section = Section("section_01")
207+
form.append(section)
208+
209+
section.append(NameRow(tag: "row_01"))
210+
section.append(NameRow(tag: "row_2"))
211+
section.append(NameRow("row_03") { $0.hidden = true })
212+
section.append(NameRow("row_04") { $0.hidden = true })
213+
214+
section.removeAll(where: { row in row.tag?.hasPrefix("row_0") ?? false })
215+
XCTAssertNotNil(form.rowBy(tag: "row_2"))
216+
XCTAssertEqual(form.allRows.count, 1)
217+
}
218+
219+
func testDeletingSections() {
220+
let form = Form()
221+
form +++ Section("section_0")
222+
+++ Section("section_1") { $0.hidden = true }
223+
+++ Section("section_22")
224+
+++ Section("section_32")
225+
226+
form.removeAll(where: { section in section.header?.title?.hasSuffix("2") ?? false })
227+
XCTAssertEqual(form.allSections.count, 2)
228+
}
229+
230+
func testReplaceAllSection() {
231+
let form = Form() +++ Section("section1") {
232+
$0.hidden = true
233+
}
234+
+++ Section("section2")
235+
+++ Section("section3")
236+
237+
form.replaceSubrangeInAllSections(Range<Int>(uncheckedBounds: (lower: 0, upper: 2)), with: [Section("section0") { $0.hidden = true }])
238+
239+
XCTAssertEqual(form.allSections.count, 2)
240+
XCTAssertEqual(form.count, 1)
241+
XCTAssertEqual(form[0].header?.title, "section3")
242+
XCTAssertEqual(form.allSections[0].header?.title, "section0")
243+
XCTAssertEqual(form.allSections[1].header?.title, "section3")
244+
}
245+
246+
204247
private func hideAndShowSections(form: Form, expectedTitles titles: [String]) {
205248
// Doesn't matter how rows were added to the form (using append, +++ or subscript index)
206249
// next must work

0 commit comments

Comments
 (0)