Skip to content

Commit

Permalink
[Fix] fix delete match indexes pattern IndexPaths update
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugene Orekhin committed Sep 28, 2020
1 parent d7680ee commit ef6817a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion G.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Pod::Spec.new do |s|
s.ios.deployment_target = '10.0'
s.tvos.deployment_target = '10.0'
s.name = "G"
s.version = "0.5.1"
s.version = "0.6"
s.summary = "Generic Grid |table/collection| system"
s.requires_arc = true

Expand Down
18 changes: 11 additions & 7 deletions G/Sources/GridSource/GridSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -264,18 +264,22 @@ public extension GridSource {
var buffer: [GCIndexPathable] = []
buffer.reserveCapacity(itemsCount - indexes.count)

var itemsIndex = 0
var shiftedIndex = 0

(0..<itemsCount).forEach {
(0..<itemsCount).forEach { i in

guard removeIndexedIndexes[$0] == nil else {
removeIndexPaths.append(IndexPath(item: $0, section: section))
itemsIndex += 1
guard removeIndexedIndexes[i] == nil else {
removeIndexPaths.append(IndexPath(item: i, section: section))
return
}

buffer.append(sections[section]!.items[itemsIndex])
itemsIndex += 1
let newItem = sections[section]!.items[i].copy(
type: .cell,
newIndexPath: IndexPath(item: shiftedIndex, section: section)
)

buffer.append(newItem)
shiftedIndex += 1
}

sections[section]!.items = buffer
Expand Down
10 changes: 5 additions & 5 deletions GTests/GridSourceRemoveTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,19 @@ final class GridSourceRemoveTests: XCTestCase {


// Test 1
let remove_ips1 = gridSource.deleteItems(section: 0, pattern: .matchIndexes([5, 6, 7, 8, 9]))
let remove_ips1 = gridSource.deleteItems(section: 0, pattern: .matchIndexes([5, 6, 7, 8]))

XCTAssertEqual(remove_ips1.count, 5)
XCTAssertEqual(gridSource.itemsCount(section: 0)!, 5)
XCTAssertEqual(remove_ips1.count, 4)
XCTAssertEqual(gridSource.itemsCount(section: 0)!, 6)
GTests.iterateCells(source: gridSource, section: 0) { idx, model in
XCTAssertEqual(idx, model.id)
XCTAssertEqual(idx, model.gcIndexPath.indexPath.item)
XCTAssertEqual(IndexPath(item: idx, section: 0), model.gcIndexPath.indexPath)
}

// Test 2
let remove_ips2 = gridSource.deleteItems(section: 0, pattern: .matchIndexes([5]))

XCTAssertEqual(remove_ips2.count, 0)
XCTAssertEqual(remove_ips2.count, 1)

// Test 3
let remove_ips3 = gridSource.deleteItems(section: 0, pattern: .matchIndexes([0, 1, 2, 3, 4]))
Expand Down

0 comments on commit ef6817a

Please sign in to comment.