From 91fbbedc8d59d6071ae450f7e34f7a0ca767ce90 Mon Sep 17 00:00:00 2001 From: Mathias Claassen Date: Mon, 15 Feb 2021 17:33:22 -0300 Subject: [PATCH 1/4] Fix removeAll method in sections --- Source/Core/Section.swift | 15 +++++++++++++++ Tests/SectionsInsertionTests.swift | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/Source/Core/Section.swift b/Source/Core/Section.swift index 67db7bbd1..2dfd9d02a 100644 --- a/Source/Core/Section.swift +++ b/Source/Core/Section.swift @@ -324,6 +324,21 @@ extension Section: RangeReplaceableCollection { } } + public func removeAll(where shouldBeRemoved: (BaseRow) throws -> Bool) rethrows { + let indices = try kvoWrapper._allRows.enumerated().filter { (element) -> Bool in + return try shouldBeRemoved(element.element) + }.map { $0.offset } + var removedRows: [BaseRow] = [] + for index in indices.reversed() { + removedRows.append(kvoWrapper._allRows.remove(at: index)) + } + kvoWrapper.rows.removeObjects(in: removedRows) + + for row in removedRows { + row.willBeRemovedFromSection() + } + } + @discardableResult public func remove(at position: Int) -> BaseRow { let row = kvoWrapper.rows.object(at: position) as! BaseRow diff --git a/Tests/SectionsInsertionTests.swift b/Tests/SectionsInsertionTests.swift index 3c13dcc58..cbc059765 100644 --- a/Tests/SectionsInsertionTests.swift +++ b/Tests/SectionsInsertionTests.swift @@ -201,6 +201,21 @@ 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) + } + 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 From 5841988dd09308167018ce8bc3771b497ecdcf34 Mon Sep 17 00:00:00 2001 From: Mathias Claassen Date: Tue, 16 Feb 2021 11:08:02 -0300 Subject: [PATCH 2/4] Add override to Form --- Source/Core/Form.swift | 12 ++++++++++++ Source/Core/Section.swift | 4 +--- Tests/SectionsInsertionTests.swift | 11 +++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/Source/Core/Form.swift b/Source/Core/Form.swift index 068b82636..ba9912e35 100644 --- a/Source/Core/Form.swift +++ b/Source/Core/Form.swift @@ -266,7 +266,19 @@ 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 { (element) -> Bool in + return try shouldBeRemoved(element.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 { diff --git a/Source/Core/Section.swift b/Source/Core/Section.swift index 2dfd9d02a..c4663ddf2 100644 --- a/Source/Core/Section.swift +++ b/Source/Core/Section.swift @@ -334,9 +334,7 @@ extension Section: RangeReplaceableCollection { } kvoWrapper.rows.removeObjects(in: removedRows) - for row in removedRows { - row.willBeRemovedFromSection() - } + removedRows.forEach { $0.willBeRemovedFromSection() } } @discardableResult diff --git a/Tests/SectionsInsertionTests.swift b/Tests/SectionsInsertionTests.swift index cbc059765..e73f5d2a6 100644 --- a/Tests/SectionsInsertionTests.swift +++ b/Tests/SectionsInsertionTests.swift @@ -216,6 +216,17 @@ class SectionInsertionTests: XCTestCase { 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) + } + 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 From c8d0bf0b65b2c393f8b802c99cbcea0b1b24d594 Mon Sep 17 00:00:00 2001 From: Mathias Claassen Date: Tue, 16 Feb 2021 11:13:31 -0300 Subject: [PATCH 3/4] Add some tests --- Tests/SectionsInsertionTests.swift | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Tests/SectionsInsertionTests.swift b/Tests/SectionsInsertionTests.swift index e73f5d2a6..5c6495c16 100644 --- a/Tests/SectionsInsertionTests.swift +++ b/Tests/SectionsInsertionTests.swift @@ -227,6 +227,23 @@ class SectionInsertionTests: XCTestCase { XCTAssertEqual(form.allSections.count, 2) } + func testReplaceAllSection() { + let form = Form() +++ Section("section1") { + $0.hidden = true + } + +++ Section("section2") + +++ Section("section3") + + form.replaceSubrangeInAllSections(Range(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 From 5ad3bcc90c3c2f6f68be5b4cd4cc18c6d8666f27 Mon Sep 17 00:00:00 2001 From: Mathias Claassen Date: Wed, 17 Feb 2021 09:31:00 -0300 Subject: [PATCH 4/4] Refactor functions --- Source/Core/Form.swift | 9 +++++---- Source/Core/Section.swift | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Source/Core/Form.swift b/Source/Core/Form.swift index ba9912e35..94bb7bc6c 100644 --- a/Source/Core/Form.swift +++ b/Source/Core/Form.swift @@ -269,10 +269,11 @@ extension Form : RangeReplaceableCollection { } public func removeAll(where shouldBeRemoved: (Section) throws -> Bool) rethrows { - let indices = try kvoWrapper._allSections.enumerated().filter { (element) -> Bool in - return try shouldBeRemoved(element.element) - }.map { $0.offset } - var removedSections: [Section] = [] + 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)) } diff --git a/Source/Core/Section.swift b/Source/Core/Section.swift index c4663ddf2..389549bd3 100644 --- a/Source/Core/Section.swift +++ b/Source/Core/Section.swift @@ -325,10 +325,11 @@ extension Section: RangeReplaceableCollection { } public func removeAll(where shouldBeRemoved: (BaseRow) throws -> Bool) rethrows { - let indices = try kvoWrapper._allRows.enumerated().filter { (element) -> Bool in - return try shouldBeRemoved(element.element) - }.map { $0.offset } - var removedRows: [BaseRow] = [] + 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)) }