Skip to content

Commit

Permalink
NSDiffableDataSources safe operation method set (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii authored Mar 29, 2024
1 parent 9161ea1 commit adfe78a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 16 deletions.
13 changes: 2 additions & 11 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
{
"pins" : [
{
"identity" : "swiftui-gesture-velocity",
"kind" : "remoteSourceControl",
"location" : "https://github.com/FluidGroup/swiftui-gesture-velocity",
"state" : {
"revision" : "9c83f8995f9e5efc29db2fca4b9ff058283f1603",
"version" : "1.0.0"
}
},
{
"identity" : "swiftui-support",
"kind" : "remoteSourceControl",
"location" : "https://github.com/FluidGroup/swiftui-support",
"state" : {
"revision" : "8ef53190c33bd345e7a95ef504dafe0f85ad9c4d",
"version" : "0.4.1"
"revision" : "c610c1e46c14c4660beb4ef7fe0241941dbecdc6",
"version" : "0.5.0"
}
}
],
Expand Down
1 change: 1 addition & 0 deletions Sources/DynamicList/DynamicListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -608,3 +608,4 @@ extension EnvironmentValues {
set { self[CellContextKey.self] = newValue }
}
}

61 changes: 61 additions & 0 deletions Sources/DynamicList/NSDiffableDataSourceSnapshot+Unique.swift
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)
}

}

}
36 changes: 31 additions & 5 deletions Tests/DynamicListTests/swift_dynamic_listTests.swift
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)

}
}

0 comments on commit adfe78a

Please sign in to comment.