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

Remove by tag #2263

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ DerivedData
*.xcuserstate
*.xcscmblueprint
.LSOverride
.swiftpm/

# CocoaPods
#
Expand Down
13 changes: 13 additions & 0 deletions Source/Core/Section.swift
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,19 @@ extension Section /* Helpers */ {
newRow.wasAddedTo(section: self)
}

/// Remove a row for tag represented as enum
/// - Parameter type: tag represented as enum
public func remove<RowTagType: RawRepresentable>(tag: RowTagType) {
removeAll(where: { $0.tag == tag.rawValue as? String })
}

/// Remove all rows where tags are represented as `CaseIterable` enum cases
/// - Parameter type: type of the enum with tags
public func removeAllRowsWithIterableTags<RowTagType: CaseIterable & RawRepresentable>(type: RowTagType.Type) {
type.allCases.forEach { item in
remove(tag: item)
}
}
}

/**
Expand Down
35 changes: 35 additions & 0 deletions Tests/HelperMethodTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
import XCTest
@testable import Eureka

private enum RowTag: String, CaseIterable {
case first
case second
case third
}

class HelperMethodTests: BaseEurekaTests {

func testRowByTag() {
Expand Down Expand Up @@ -128,4 +134,33 @@ class HelperMethodTests: BaseEurekaTests {
XCTAssertEqual(allSections.count, 6)
}

func testRemoveForTag() {
let row = LabelRow(tag: RowTag.first.rawValue)
manySectionsForm[0] <<< row
XCTAssertEqual(manySectionsForm[0].rowBy(tag: RowTag.first.rawValue), row)

manySectionsForm[0].remove(tag: RowTag.first)
let rowAfterRemove: LabelRow? = manySectionsForm[0].rowBy(tag: RowTag.first.rawValue)
XCTAssertNil(rowAfterRemove)
}

func testRemoveAllRowsWithIterableTags() {
RowTag.allCases.forEach { tag in
manySectionsForm[0] <<< LabelRow(tag: tag.rawValue)
}
let someOtherRowTag = "someOtherRow"
let someOtherRow = LabelRow(tag: someOtherRowTag)
manySectionsForm[0] <<< someOtherRow
RowTag.allCases.forEach { tag in
let label: LabelRow? = manySectionsForm[0].rowBy(tag: tag.rawValue)
XCTAssertNotNil(label)
}
XCTAssertEqual(manySectionsForm[0].rowBy(tag: someOtherRowTag), someOtherRow)
manySectionsForm[0].removeAllRowsWithIterableTags(type: RowTag.self)
RowTag.allCases.forEach { tag in
let label: LabelRow? = manySectionsForm[0].rowBy(tag: tag.rawValue)
XCTAssertNil(label)
}
XCTAssertEqual(manySectionsForm[0].rowBy(tag: someOtherRowTag), someOtherRow)
}
}
Loading