Skip to content

Commit fb2c721

Browse files
committed
updated
1 parent 14ea444 commit fb2c721

File tree

7 files changed

+99
-99
lines changed

7 files changed

+99
-99
lines changed

Example/Example/MainViewController.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,26 @@ class MainViewController: NSViewController {
7777

7878
dataSource.selectElements([galleryItems.first!], scrollPosition: .top)
7979
collectionView.makeFirstResponder()
80+
81+
Swift.print(NSDiffableDataSourceSnapshotReference.classReflection())
82+
var object = dataSource.snapshot() as NSDiffableDataSourceSnapshotReference
83+
object.perform(NSSelectorFromString("reconfigureItemsWithIdentifiers:"), with: [galleryItems[0], galleryItems[1]])
84+
if let impl: NSObject = object.getIvarValue(for: "_impl") {
85+
Swift.print(impl.objectIdentifier)
86+
if let reconfiguredItemIdentifiers = impl.value(forKey: "reconfiguredItemIdentifiers") as? [GalleryItem] {
87+
Swift.print(reconfiguredItemIdentifiers)
88+
}
89+
}
90+
let snapshot = object as NSDiffableDataSourceSnapshot<Section, GalleryItem>
91+
object = snapshot as NSDiffableDataSourceSnapshotReference
92+
if let impl: NSObject = object.getIvarValue(for: "_impl") {
93+
Swift.print(impl.objectIdentifier)
94+
95+
if let reconfiguredItemIdentifiers = impl.value(forKey: "reconfiguredItemIdentifiers") as? [GalleryItem] {
96+
Swift.print(reconfiguredItemIdentifiers)
97+
}
98+
}
99+
80100
}
81101

82102
func applySnapshot(using items: [GalleryItem]) {

Sources/AdvancedCollectionTableView/DiffableDataSource/NSCollectionView/CollectionViewDiffableDataSource+Delegate.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,10 @@ extension CollectionViewDiffableDataSource {
256256
return true
257257
} else if dropOperation == .before, !droppingElements.isEmpty {
258258
let transaction: DiffableDataSourceTransaction<Section, Element> = dataSource.dropTransaction(droppingElements, indexPath: indexPath)
259-
dataSource.droppingHandlers.willDrop?(dropInfo, droppingElements, transaction)
259+
dataSource.droppingHandlers.willDrop?(dropInfo, transaction)
260260
dataSource.apply(transaction.finalSnapshot, dataSource.droppingHandlers.animates ? .animated : .withoutAnimation)
261261
dataSource.selectElements(droppingElements, scrollPosition: [])
262-
dataSource.droppingHandlers.didDrop?(dropInfo, droppingElements, transaction)
262+
dataSource.droppingHandlers.didDrop?(dropInfo, transaction)
263263
return true
264264
}
265265
return false

Sources/AdvancedCollectionTableView/DiffableDataSource/NSCollectionView/CollectionViewDiffableDataSource.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,20 +1052,18 @@ open class CollectionViewDiffableDataSource<Section: Identifiable & Hashable, El
10521052

10531053
- Parameters:
10541054
- dropInfo: The information about the drop.
1055-
- newElements: The new elements to be inserted for the drop.
10561055
- transaction: The transaction for the drop.
10571056
*/
1058-
public var willDrop: ((_ dropInfo: DropInfo, _ newElements: [Element], _ transaction: DiffableDataSourceTransaction<Section, Element>) -> ())?
1057+
public var willDrop: ((_ dropInfo: DropInfo, _ transaction: DiffableDataSourceTransaction<Section, Element>) -> ())?
10591058

10601059
/**
10611060
The handler that gets called when pasteboard content was dropped inside the collection view.
10621061

10631062
- Parameters:
10641063
- dropInfo: The information about the drop.
1065-
- newElements: The new elements that have be inserted for the drop.
10661064
- transaction: The transaction for the drop.
10671065
*/
1068-
public var didDrop: ((_ dropInfo: DropInfo, _ newElements: [Element], _ transaction: DiffableDataSourceTransaction<Section, Element>) -> ())?
1066+
public var didDrop: ((_ dropInfo: DropInfo, _ transaction: DiffableDataSourceTransaction<Section, Element>) -> ())?
10691067

10701068
/// The handler that determinates the elements for the proposed drop.
10711069
public var elements: ((_ dropInfo: DropInfo) -> ([Element]))?

Sources/AdvancedCollectionTableView/DiffableDataSource/NSOutlineView/OutlineViewDiffableDataSource.swift

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,19 @@ public class OutlineViewDiffableDataSource<ItemIdentifierType: Hashable>: NSObje
8383

8484
To use group items, enable group items in the snapshot you apply, by setting ``OutlineViewDiffableDataSourceSnapshot/groupItems`` `isEnabled` to `true`.
8585
*/
86-
open var groupItemCellProvider: GroupItemCellProvider?
86+
open var groupItemCellProvider: GroupItemCellProvider? {
87+
didSet { reapplySnapshot() }
88+
}
8789

8890
/**
8991
Applies the specified cell registration to configures and returnscellrow views for the outline view’s group items.
9092

9193
To use group items, enable group items in the snapshot you apply, by setting ``OutlineViewDiffableDataSourceSnapshot/groupItems`` `isEnabled` to `true`.
9294
*/
9395
open func applyGroupItemCellRegistration<Cell: NSTableCellView>(_ registration: NSTableView.CellRegistration<Cell, ItemIdentifierType>) {
94-
let previousUsesGroupItems = groupItemCellProvider != nil && !groupItemsAreCollapsable
9596
groupItemCellProvider = { outlineView, item in
9697
outlineView.makeCellView(using:registration, forColumn: self.groupRowTableColumn, row: 0, item: item)!
9798
}
98-
99-
reapplySnapshot(previousUsesGroupItems: previousUsesGroupItems)
10099
}
101100

102101
/**
@@ -642,27 +641,69 @@ public class OutlineViewDiffableDataSource<ItemIdentifierType: Hashable>: NSObje
642641
- completion: An optional completion handler which gets called after applying the snapshot. The system calls this closure from the main queue.
643642
*/
644643
public func apply(_ snapshot: OutlineViewDiffableDataSourceSnapshot<ItemIdentifierType>, _ option: NSDiffableDataSourceSnapshotApplyOption = .animated, completion: (() -> Void)? = nil) {
645-
apply(snapshot, option, completion: completion, previousUsesGroupItems: nil)
644+
_apply(snapshot, option, completion: completion)
646645
}
647646

648-
func apply(_ snapshot: OutlineViewDiffableDataSourceSnapshot<ItemIdentifierType>, _ option: NSDiffableDataSourceSnapshotApplyOption = .animated, completion: (() -> Void)? = nil, previousUsesGroupItems: Bool?, isReapplying: Bool = false) {
647+
func _apply(_ snapshot: OutlineViewDiffableDataSourceSnapshot<ItemIdentifierType>, _ option: NSDiffableDataSourceSnapshotApplyOption = .animated, completion: (() -> Void)? = nil, isReapplying: Bool = false) {
648+
var snapshot = snapshot
649+
snapshot.usesGroupItems = groupItemCellProvider != nil && !groupItemsAreCollapsable
649650
isApplyingSnapshot = true
650651
let usesGroupItems = groupItemCellProvider != nil && !groupItemsAreCollapsable
651652
let current = currentSnapshot
652653
let previousIsEmpty = currentSnapshot.items.isEmpty
653654
currentSnapshot = snapshot
654655
outlineView.hoveredRow = !isReapplying ? -1 : outlineView.hoveredRow
655-
outlineView.apply(snapshot, currentSnapshot: current, option: option, animation: defaultRowAnimation, completion: completion, previousGroupItems: (previousUsesGroupItems ?? usesGroupItems) ? current.rootItems : [], groupItems: usesGroupItems ? snapshot.rootItems : [])
656+
outlineView.apply(snapshot, currentSnapshot: current, option: option, animation: defaultRowAnimation, completion: completion)
656657
if !isReapplying {
657658
updateEmptyView(previousIsEmpty: previousIsEmpty)
658659
}
659660
isApplyingSnapshot = false
660661
}
661662

662-
func reapplySnapshot(previousUsesGroupItems: Bool? = nil) {
663+
func apply(_ snapshot: OutlineViewDiffableDataSourceSnapshot<ItemIdentifierType>, option: NSDiffableDataSourceSnapshotApplyOption, currentSnapshot: OutlineViewDiffableDataSourceSnapshot<ItemIdentifierType>, completion: (() -> Void)?) {
664+
func applySnapshot() {
665+
outlineView.beginUpdates()
666+
for instruction in currentSnapshot.instructions(forMorphingTo: snapshot) {
667+
switch instruction {
668+
case .insert(_, let index, let parent):
669+
outlineView.insertItems(at: IndexSet(integer: index), inParent: parent, withAnimation: defaultRowAnimation)
670+
case .remove(_, let index, let parent):
671+
outlineView.removeItems(at: IndexSet(integer: index), inParent: parent, withAnimation: defaultRowAnimation)
672+
case .move(_, let from, let fromParent, let to, let toParent):
673+
outlineView.moveItem(at: from, inParent: fromParent, to: to, inParent: toParent)
674+
}
675+
}
676+
expandCollapseItems()
677+
outlineView.endUpdates()
678+
}
679+
680+
func expandCollapseItems() {
681+
let oldExpanded = currentSnapshot.expandedItems
682+
let newExpanded = snapshot.expandedItems
683+
oldExpanded.subtracting(newExpanded).forEach({ outlineView.animator().collapseItem($0) })
684+
newExpanded.subtracting(oldExpanded).forEach({ outlineView.animator().expandItem($0) })
685+
}
686+
687+
if option.isReloadData {
688+
outlineView.reloadData()
689+
expandCollapseItems()
690+
completion?()
691+
} else if let duration = option.animationDuration, duration > 0.0 {
692+
NSView.animate(withDuration: duration, {
693+
applySnapshot()
694+
}, completion: completion)
695+
} else {
696+
NSView.performWithoutAnimation {
697+
applySnapshot()
698+
completion?()
699+
}
700+
}
701+
}
702+
703+
func reapplySnapshot() {
663704
let currentSnapshot = currentSnapshot
664-
apply(emptySnapshot(), .withoutAnimation, previousUsesGroupItems: previousUsesGroupItems, isReapplying: true)
665-
apply(currentSnapshot, .withoutAnimation, previousUsesGroupItems: previousUsesGroupItems, isReapplying: true)
705+
_apply(emptySnapshot(), .withoutAnimation, isReapplying: true)
706+
_apply(currentSnapshot, .withoutAnimation, isReapplying: true)
666707
}
667708

668709
public func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any {
@@ -860,9 +901,9 @@ public class OutlineViewDiffableDataSource<ItemIdentifierType: Hashable>: NSObje
860901
var snapshot = snapshot()
861902
snapshot.insert(items, atIndex: index, of: item)
862903
let transaction = OutlineViewDiffableDataSourceTransaction<ItemIdentifierType>.init(initial: currentSnapshot, final: snapshot)
863-
droppingHandlers.willDrop?(dropInfo, items, item, transaction)
904+
droppingHandlers.willDrop?(dropInfo, item, transaction)
864905
apply(transaction.finalSnapshot, droppingHandlers.animates ? .animated : .withoutAnimation)
865-
droppingHandlers.didDrop?(dropInfo, items, item, transaction)
906+
droppingHandlers.didDrop?(dropInfo, item, transaction)
866907
return true
867908
}
868909
return false
@@ -1051,10 +1092,10 @@ public class OutlineViewDiffableDataSource<ItemIdentifierType: Hashable>: NSObje
10511092
public var items: ((_ dropInfo: DropInfo) -> ([ItemIdentifierType]))?
10521093

10531094
/// The handler that gets called before new items are dropped.
1054-
public var willDrop: ((_ dropInfo: DropInfo, _ newItems: [ItemIdentifierType], _ target: ItemIdentifierType?, _ transaction: OutlineViewDiffableDataSourceTransaction<ItemIdentifierType>) -> ())?
1095+
public var willDrop: ((_ dropInfo: DropInfo, _ target: ItemIdentifierType?, _ transaction: OutlineViewDiffableDataSourceTransaction<ItemIdentifierType>) -> ())?
10551096

10561097
/// The handler that gets called after new items are dropped.
1057-
public var didDrop: ((_ dropInfo: DropInfo, _ newItems: [ItemIdentifierType], _ target: ItemIdentifierType?, _ transaction: OutlineViewDiffableDataSourceTransaction<ItemIdentifierType>) -> ())?
1098+
public var didDrop: ((_ dropInfo: DropInfo, _ target: ItemIdentifierType?, _ transaction: OutlineViewDiffableDataSourceTransaction<ItemIdentifierType>) -> ())?
10581099

10591100
/// A Boolean value that indicates whether dropping items is animated.
10601101
public var animates: Bool = false

Sources/AdvancedCollectionTableView/DiffableDataSource/NSOutlineView/Snapshot/OutlineChangeInstruction.swift

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ extension OutlineViewDiffableDataSourceSnapshot {
7575
}
7676

7777
extension NSOutlineView {
78-
func apply<Item: Hashable>(_ snapshot: OutlineViewDiffableDataSourceSnapshot<Item>, currentSnapshot: OutlineViewDiffableDataSourceSnapshot<Item>, option: NSDiffableDataSourceSnapshotApplyOption, animation: NSTableView.AnimationOptions, completion: (() -> Void)?, previousGroupItems: [Item], groupItems: [Item]) {
78+
func apply<Item: Hashable>(_ snapshot: OutlineViewDiffableDataSourceSnapshot<Item>, currentSnapshot: OutlineViewDiffableDataSourceSnapshot<Item>, option: NSDiffableDataSourceSnapshotApplyOption, animation: AnimationOptions, completion: (() -> Void)?) {
7979
func applySnapshot() {
8080
beginUpdates()
8181
for instruction in currentSnapshot.instructions(forMorphingTo: snapshot) {
@@ -93,14 +93,10 @@ extension NSOutlineView {
9393
}
9494

9595
func expandCollapseItems() {
96-
97-
// groupItems.isEnabled && groupItems.isAlwaysExpanded
98-
let oldExpanded = Set(currentSnapshot.nodes.filter { $0.value.isExpanded }.map { $0.key } + previousGroupItems)
99-
let newExpanded = Set(snapshot.nodes.filter { $0.value.isExpanded }.map { $0.key } + groupItems)
100-
let collapse = Array(oldExpanded.subtracting(newExpanded))
101-
let expand = Array(newExpanded.subtracting(oldExpanded))
102-
collapse.forEach({ animator().collapseItem($0) })
103-
expand.forEach({ animator().expandItem($0) })
96+
let oldExpanded = currentSnapshot.expandedItems
97+
let newExpanded = snapshot.expandedItems
98+
oldExpanded.subtracting(newExpanded).forEach({ animator().collapseItem($0) })
99+
newExpanded.subtracting(oldExpanded).forEach({ animator().expandItem($0) })
104100
}
105101

106102
if option.isReloadData {

Sources/AdvancedCollectionTableView/DiffableDataSource/NSOutlineView/Snapshot/OutlineViewDiffableDataSourceSnapshot.swift

Lines changed: 11 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public struct OutlineViewDiffableDataSourceSnapshot<ItemIdentifierType: Hashable
4949
/// The identifiers of the items at the top level of the snapshot’s hierarchy.
5050
public internal(set) var rootItems: [ItemIdentifierType] = []
5151

52+
var isCalculatingDiff = false
53+
var usesGroupItems: Bool = false
54+
5255
/// The identifiers of all items in the snapshot.
5356
public var items: [ItemIdentifierType] {
5457
Array(orderedItems)
@@ -64,8 +67,6 @@ public struct OutlineViewDiffableDataSourceSnapshot<ItemIdentifierType: Hashable
6467
return rootItems + rootItems.flatMap({ visibleChilds(for: $0) })
6568
}
6669

67-
var isCalculatingDiff = false
68-
6970
// MARK: - Adding ItemIdentifierTypes
7071

7172
/// Adds the specified items as child items of the specified parent item in the snapshot.
@@ -329,46 +330,6 @@ public struct OutlineViewDiffableDataSourceSnapshot<ItemIdentifierType: Hashable
329330
items.forEach({ nodes[$0]?.isExpanded = false })
330331
}
331332

332-
// MARK: - Configurating group items
333-
334-
/*
335-
/// Properties for group items.
336-
public var groupItems = GroupItemProperties()
337-
338-
/// Properties for group items.
339-
public struct GroupItemProperties {
340-
/**
341-
A Boolean value indicating whether the root items are displayed as group items.
342-
343-
If set to `true`, you can optionally provide custom group item cell views using `OutlineViewDiffableDataSource's` ``OutlineViewDiffableDataSource/groupItemCellProvider-swift.property`` and ``OutlineViewDiffableDataSource/applyGroupItemCellRegistration(_:)``.
344-
*/
345-
public var isEnabled: Bool = false
346-
347-
/**
348-
A Boolean value indicating whether group items are always expanded and can't be collapsed.
349-
350-
The default value is `false` and indicates that group items can be expanded and collapsed like regular items.
351-
352-
If set to `true`, the items are always expanded and can't be collapsed.
353-
354-
*/
355-
public var isAlwaysExpanded: Bool = false
356-
357-
/**
358-
A Boolean value indicating whether the group items are floating.
359-
360-
The default value is `false`.
361-
*/
362-
public var isFloating: Bool = false
363-
}
364-
*/
365-
366-
/*
367-
var _groupItems: [ItemIdentifierType] {
368-
groupItems.isEnabled && groupItems.isAlwaysExpanded ? rootItems : []
369-
}
370-
*/
371-
372333
// MARK: - Debugging snapshots
373334

374335
/// Returns a string with an ASCII representation of the snapshot.
@@ -472,6 +433,14 @@ public struct OutlineViewDiffableDataSourceSnapshot<ItemIdentifierType: Hashable
472433
orderedItems.append(contentsOf: descendants(of: root))
473434
}
474435
}
436+
437+
var expandedItems: Set<ItemIdentifierType> {
438+
var items = Set(nodes.filter({ $0.value.isExpanded }).compactMap({ $0.key }))
439+
if usesGroupItems {
440+
items += rootItems
441+
}
442+
return items
443+
}
475444
}
476445

477446
fileprivate extension Array where Element: Equatable {
@@ -490,25 +459,3 @@ fileprivate extension Array where Element: Equatable {
490459
insert(contentsOf: filteredItems, at: adjustedIndex)
491460
}
492461
}
493-
494-
/*
495-
/**
496-
A Boolean value indicating whether the root items are group items.
497-
498-
The default value is `false`. The group items are expanded and can't be collapsed.
499-
500-
If you set this value to `true`, the group rows display a disclosure button and you can manage the expansion state of the items via ``expand(_:)`` and ``collapse(_:)``.
501-
*/
502-
public var usesGroupItems: Bool = false
503-
*/
504-
505-
/*
506-
/**
507-
A Boolean value indicating whether group items can be expanded/collapsed.
508-
509-
The default value is `false`. The group row items are expanded and can't be collapsed.
510-
511-
If you set this value to `true`, the group rows display a disclosure button and you can manage the expansion state of the items via ``expand(_:)`` and ``collapse(_:)``.
512-
*/
513-
public var groupItemsAreExpandable = false
514-
*/

Sources/AdvancedCollectionTableView/DiffableDataSource/NSTableView/TableViewDiffableDataSource.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -587,10 +587,10 @@ open class TableViewDiffableDataSource<Section, Item>: NSObject, NSTableViewData
587587
let items = droppingHandlers.items?(dropInfo) ?? []
588588
if !items.isEmpty {
589589
let transaction = dropItemsTransaction(items, row: row)
590-
droppingHandlers.willDrop?(dropInfo,items, transaction)
590+
droppingHandlers.willDrop?(dropInfo, transaction)
591591
apply(transaction.finalSnapshot, droppingHandlers.animates ? .animated : .withoutAnimation)
592592
selectItems(items)
593-
droppingHandlers.didDrop?(dropInfo,items, transaction)
593+
droppingHandlers.didDrop?(dropInfo, transaction)
594594
return true
595595
}
596596
}
@@ -1285,20 +1285,18 @@ open class TableViewDiffableDataSource<Section, Item>: NSObject, NSTableViewData
12851285

12861286
- Parameters:
12871287
- dropInfo: The information about the drop.
1288-
- newItems: The new items to be inserted for the drop.
12891288
- transaction: The transaction for the drop.
12901289
*/
1291-
public var willDrop: ((_ dropInfo: DropInfo, _ newItems: [Item], _ transaction: DiffableDataSourceTransaction<Section, Item>) -> ())?
1290+
public var willDrop: ((_ dropInfo: DropInfo, _ transaction: DiffableDataSourceTransaction<Section, Item>) -> ())?
12921291

12931292
/**
12941293
The handler that gets called when pasteboard content was dropped inside the collection view.
12951294

12961295
- Parameters:
12971296
- dropInfo: The information about the drop.
1298-
- newItems: The new items that have be inserted for the drop.
12991297
- transaction: The transaction for the drop.
13001298
*/
1301-
public var didDrop: ((_ dropInfo: DropInfo, _ newItems: [Item], _ transaction: DiffableDataSourceTransaction<Section, Item>) -> ())?
1299+
public var didDrop: ((_ dropInfo: DropInfo, _ transaction: DiffableDataSourceTransaction<Section, Item>) -> ())?
13021300

13031301
public var updateDragItems: ((_ dropInfo: DropInfo)->())?
13041302

0 commit comments

Comments
 (0)