Skip to content
Merged
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
10 changes: 9 additions & 1 deletion MagazineLayout/LayoutCore/ModelState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -660,20 +660,27 @@ final class ModelState {
switch update {
case let .sectionReload(sectionIndex, newSection):
sectionModelReloadIndexPairs.append((newSection, sectionIndex))

case let .itemReload(itemIndexPath, newItem):
itemModelReloadIndexPathPairs.append((newItem, itemIndexPath))

case let .sectionDelete(sectionIndex):
sectionIndicesToDelete.append(sectionIndex)
self.sectionIndicesToDelete.insert(sectionIndex)

case let .itemDelete(itemIndexPath):
itemIndexPathsToDelete.append(itemIndexPath)
self.itemIndexPathsToDelete.insert(itemIndexPath)

case let .sectionMove(initialSectionIndex, finalSectionIndex):
sectionIndicesToDelete.append(initialSectionIndex)
let sectionModelToMove = sectionModelsBeforeBatchUpdates[initialSectionIndex]
var sectionModelToMove = sectionModelsBeforeBatchUpdates[initialSectionIndex]
// Item moves are handled separately, so we need to clear out existing items
for itemIndex in (0..<sectionModelToMove.numberOfItems).reversed() {
sectionModelToMove.deleteItemModel(atIndex: itemIndex)
}
sectionModelInsertIndexPairs.append((sectionModelToMove, finalSectionIndex))

case let .itemMove(initialItemIndexPath, finalItemIndexPath):
itemIndexPathsToDelete.append(initialItemIndexPath)
let sectionContainingItemModelToMove = sectionModelsBeforeBatchUpdates[initialItemIndexPath.section]
Expand All @@ -684,6 +691,7 @@ final class ModelState {
case let .sectionInsert(sectionIndex, newSection):
sectionModelInsertIndexPairs.append((newSection, sectionIndex))
sectionIndicesToInsert.insert(sectionIndex)

case let .itemInsert(itemIndexPath, newItem):
itemModelInsertIndexPathPairs.append((newItem, itemIndexPath))
itemIndexPathsToInsert.insert(itemIndexPath)
Expand Down
18 changes: 17 additions & 1 deletion Tests/ModelStateUpdateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,18 @@ final class ModelStateUpdateTests: XCTestCase {
func testSectionMoves() {
let initialSections = ModelHelpers.basicSectionModels(
numberOfSections: 3,
numberOfItemsPerSection: 0)
numberOfItemsPerSection: 2)
modelState.setSections(initialSections)

modelState.applyUpdates(
[
.sectionMove(initialSectionIndex: 0, finalSectionIndex: 1),
.itemMove(initialItemIndexPath: .init(item: 0, section: 0), finalItemIndexPath: .init(item: 0, section: 1)),
.itemMove(initialItemIndexPath: .init(item: 1, section: 0), finalItemIndexPath: .init(item: 1, section: 1)),

.sectionMove(initialSectionIndex: 2, finalSectionIndex: 0),
.itemMove(initialItemIndexPath: .init(item: 0, section: 2), finalItemIndexPath: .init(item: 0, section: 0)),
.itemMove(initialItemIndexPath: .init(item: 1, section: 2), finalItemIndexPath: .init(item: 1, section: 0)),
])

XCTAssert(
Expand All @@ -222,6 +227,17 @@ final class ModelStateUpdateTests: XCTestCase {
modelState.indexForSectionModel(withID: initialSections[2].id, .afterUpdates) == 0
),
"The model state's section models before / after updates are in an incorrect state")

XCTAssert(
(
modelState.numberOfItems(inSectionAtIndex: 0, .beforeUpdates) ==
modelState.numberOfItems(inSectionAtIndex: 1, .afterUpdates) &&
modelState.numberOfItems(inSectionAtIndex: 1, .beforeUpdates) ==
modelState.numberOfItems(inSectionAtIndex: 2, .afterUpdates) &&
modelState.numberOfItems(inSectionAtIndex: 2, .beforeUpdates) ==
modelState.numberOfItems(inSectionAtIndex: 0, .afterUpdates)
),
"The model state's section models before / after updates are in an incorrect state")
}

func testItemMoves() {
Expand Down