Skip to content

Commit 4d3c351

Browse files
committed
Fixed diffing bug where item's parentID wasn't taken into account. Moving items would otherwise remain broken.
Reload parent items when removing / inserting
1 parent 67d2e71 commit 4d3c351

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

Sources/DiffableDataSourceSnapshot.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,13 @@ extension DiffableDataSourceSnapshot {
302302

303303
/// Only IDs should be equal.
304304
static func == (lhs: Self, rhs: Self) -> Bool {
305-
lhs.itemId == rhs.itemId
305+
lhs.itemId == rhs.itemId && lhs.parentId == rhs.parentId
306306
}
307307

308308
/// Only ID means as hash.
309309
func hash(into hasher: inout Hasher) {
310310
hasher.combine(itemId)
311+
hasher.combine(parentId)
311312
}
312313
}
313314

Sources/OutlineViewDiffableDataSource.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,15 +280,23 @@ public extension OutlineViewDiffableDataSource {
280280
// Insert outline view item
281281
let insertionIndexes = IndexSet(integer: inserted.itemPath.last.unsafelyUnwrapped)
282282
let parentItem = inserted.parentId.flatMap(newSnapshot.itemForId)
283+
283284
outlineView?.insertItems(at: insertionIndexes, inParent: parentItem, withAnimation: [.effectFade, .slideDown])
285+
286+
// Reload new parent so their expansion states can be reloaded
287+
outlineView?.reloadItem(parentItem, reloadChildren: true)
284288
}
285289

286290
case .remove(_, let before, let indexAfter):
287291
if indexAfter == nil {
288292
// Delete outline view item
289293
let deletionIndexes = IndexSet(integer: before.itemPath.last.unsafelyUnwrapped)
290294
let oldParentItem = before.parentId.flatMap(oldSnapshot.itemForId)
291-
outlineView?.removeItems(at: deletionIndexes, inParent: oldParentItem, withAnimation: [.effectFade, .slideUp])
295+
296+
outlineView?.removeItems(at: deletionIndexes, inParent: oldParentItem, withAnimation: [.effectFade, .slideDown])
297+
298+
// Reload old parent so their expansion states can be reloaded
299+
outlineView?.reloadItem(oldParentItem, reloadChildren: true)
292300
}
293301
else {
294302
// the item moved since it's got a valid "index after". We handle moves in `.insert` so this can be

0 commit comments

Comments
 (0)