-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NSDiffableDataSources safe operation method set (#16)
- Loading branch information
Showing
4 changed files
with
95 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -608,3 +608,4 @@ extension EnvironmentValues { | |
set { self[CellContextKey.self] = newValue } | ||
} | ||
} | ||
|
61 changes: 61 additions & 0 deletions
61
Sources/DynamicList/NSDiffableDataSourceSnapshot+Unique.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
|
||
import UIKit | ||
|
||
public enum DiffableDataSourceError: Error { | ||
case duplicatedSectionIdentifiers(Set<AnyHashable>) | ||
case duplicatedItemIdentifiers(Set<AnyHashable>) | ||
} | ||
|
||
extension NSDiffableDataSourceSnapshot { | ||
|
||
public mutating func safeAppendSections( | ||
_ sections: [SectionIdentifierType] | ||
) throws { | ||
|
||
var sectionSet: Set<SectionIdentifierType> = [] | ||
|
||
for section in sections { | ||
let (inserted, _) = sectionSet.insert(section) | ||
guard inserted else { | ||
throw DiffableDataSourceError.duplicatedSectionIdentifiers([section]) | ||
} | ||
} | ||
|
||
|
||
var set = Set(sectionIdentifiers) | ||
set.formIntersection(sections) | ||
|
||
if set.isEmpty { | ||
appendSections(sections) | ||
} else { | ||
throw DiffableDataSourceError.duplicatedSectionIdentifiers(set) | ||
} | ||
|
||
} | ||
|
||
public mutating func safeAppendItems( | ||
_ items: [ItemIdentifierType], | ||
intoSection sectionIdentifier: SectionIdentifierType? = nil | ||
) throws { | ||
|
||
var itemSet: Set<ItemIdentifierType> = [] | ||
|
||
for item in items { | ||
let (inserted, _) = itemSet.insert(item) | ||
guard inserted else { | ||
throw DiffableDataSourceError.duplicatedItemIdentifiers([item]) | ||
} | ||
} | ||
|
||
var set = Set(itemIdentifiers) | ||
set.formIntersection(items) | ||
|
||
if set.isEmpty { | ||
appendItems(items, toSection: sectionIdentifier) | ||
} else { | ||
throw DiffableDataSourceError.duplicatedItemIdentifiers(set) | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,38 @@ | ||
import XCTest | ||
|
||
@testable import DynamicList | ||
|
||
final class swift_dynamic_listTests: XCTestCase { | ||
func testExample() throws { | ||
// XCTest Documenation | ||
// https://developer.apple.com/documentation/xctest | ||
func testDiffable_append_duplicated() throws { | ||
|
||
var snapshot = NSDiffableDataSourceSnapshot<String, Int>() | ||
snapshot.appendSections(["A"]) | ||
|
||
do { | ||
try snapshot.safeAppendItems([0, 0]) | ||
XCTFail() | ||
} catch { | ||
|
||
// Defining Test Cases and Test Methods | ||
// https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods | ||
} | ||
} | ||
|
||
func testDiffable_append_duplicated_2() throws { | ||
|
||
var snapshot = NSDiffableDataSourceSnapshot<String, Int>() | ||
snapshot.appendSections(["A"]) | ||
snapshot.appendItems([0]) | ||
|
||
do { | ||
try snapshot.safeAppendItems([0]) | ||
XCTFail() | ||
} catch { | ||
|
||
} | ||
} | ||
|
||
func testIntersect() throws { | ||
|
||
XCTAssertEqual(Set([1,2]).intersection([3,4]).isEmpty, true) | ||
|
||
} | ||
} |