Skip to content

Commit 7ec00ac

Browse files
committed
updated
1 parent e473ecd commit 7ec00ac

File tree

7 files changed

+127
-79
lines changed

7 files changed

+127
-79
lines changed

Sources/AdvancedCollectionTableView/DiffableDataSource/NSOutlineView/OutlineViewDiffableDataSource+Delegate.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ extension OutlineViewDiffableDataSource {
6262
rowView = outlineView.makeView(withIdentifier: "_RowView", owner: self) as? NSTableRowView ?? NSTableRowView()
6363
rowView.identifier = "_RowView"
6464
}
65-
if dataSource.currentSnapshot.groupItemsAreExpandable, self.outlineView(dataSource.outlineView, isGroupItem: item) {
65+
if !dataSource.currentSnapshot.groupItems.isAlwaysExpanded, self.outlineView(dataSource.outlineView, isGroupItem: item) {
6666
var isExpanded = false
6767
if let item = item as? ItemIdentifierType {
6868
isExpanded = dataSource.currentSnapshot.isExpanded(item)
@@ -131,8 +131,8 @@ extension OutlineViewDiffableDataSource {
131131
func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
132132
let isGroupItem = self.outlineView(outlineView, isGroupItem: item)
133133
let cellView: NSView
134-
if isGroupItem, let groupRowCellProvider = dataSource.groupRowCellProvider {
135-
cellView = groupRowCellProvider(outlineView, tableColumn, item as! ItemIdentifierType)
134+
if isGroupItem, let groupItemCellProvider = dataSource.groupItemCellProvider {
135+
cellView = groupItemCellProvider(outlineView, tableColumn, item as! ItemIdentifierType)
136136
} else {
137137
cellView = dataSource.cellProvider(outlineView, tableColumn, item as! ItemIdentifierType)
138138
}
@@ -144,7 +144,7 @@ extension OutlineViewDiffableDataSource {
144144
}
145145

146146
func outlineView(_ outlineView: NSOutlineView, isGroupItem item: Any) -> Bool {
147-
guard let item = item as? ItemIdentifierType, dataSource.currentSnapshot.usesGroupItems else { return false }
147+
guard let item = item as? ItemIdentifierType, dataSource.currentSnapshot.groupItems.isEnabled else { return false }
148148
return dataSource.currentSnapshot.rootItems.contains(item)
149149
}
150150

Sources/AdvancedCollectionTableView/DiffableDataSource/NSOutlineView/OutlineViewDiffableDataSource.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ public class OutlineViewDiffableDataSource<ItemIdentifierType: Hashable>: NSObje
7575
}
7676
}
7777

78-
/// The closure that configures and returns cell views for the outline view’s group rows.
79-
open var groupRowCellProvider: GroupRowCellProvider?
78+
/// The closure that configures and returns cell views for the outline view’s group items.
79+
open var groupItemCellProvider: GroupItemCellProvider?
8080

81-
/// Applies the specified cell registration to configures and returns cell views for the outline view’s group rows.
82-
open func applyGroupRowCellRegistration<Cell: NSTableCellView>(_ registration: NSTableView.CellRegistration<Cell, ItemIdentifierType>) {
83-
groupRowCellProvider = { outlineView, column, item in
81+
/// Applies the specified cell registration to configures and returns cell views for the outline view’s group items.
82+
open func applyGroupItemCellRegistration<Cell: NSTableCellView>(_ registration: NSTableView.CellRegistration<Cell, ItemIdentifierType>) {
83+
groupItemCellProvider = { outlineView, column, item in
8484
outlineView.makeCellView(using: registration, forColumn: column ?? .outline, row: 0, item: item)!
8585
}
8686
}
@@ -95,7 +95,7 @@ public class OutlineViewDiffableDataSource<ItemIdentifierType: Hashable>: NSObje
9595

9696
- Returns: A configured cell object.
9797
*/
98-
public typealias GroupRowCellProvider = (_ outlineView: NSOutlineView, _ tableColumn: NSTableColumn?, _ identifier: ItemIdentifierType) -> NSView
98+
public typealias GroupItemCellProvider = (_ outlineView: NSOutlineView, _ tableColumn: NSTableColumn?, _ identifier: ItemIdentifierType) -> NSView
9999

100100

101101
/**

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,16 @@ extension NSOutlineView {
9191
}
9292

9393
func expandCollapseItems() {
94-
let oldExpanded = Set(currentSnapshot.nodes.filter { $0.value.isExpanded }.map { $0.key } + currentSnapshot.groupItems)
95-
let newExpanded = Set(snapshot.nodes.filter { $0.value.isExpanded }.map { $0.key } + snapshot.groupItems)
94+
let oldExpanded = Set(currentSnapshot.nodes.filter { $0.value.isExpanded }.map { $0.key } + currentSnapshot._groupItems)
95+
let newExpanded = Set(snapshot.nodes.filter { $0.value.isExpanded }.map { $0.key } + snapshot._groupItems)
9696
let collapse = Array(oldExpanded.subtracting(newExpanded))
9797
var expand = Array(newExpanded.subtracting(oldExpanded))
9898
collapse.forEach({ animator().collapseItem($0) })
9999
expand.forEach({ animator().expandItem($0) })
100100
}
101101

102+
floatsGroupRows = snapshot.groupItems.isFloating
103+
102104
if option.isReloadData {
103105
reloadData()
104106
expandCollapseItems()

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

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -42,49 +42,6 @@ public struct OutlineNode<ItemIdentifierType: Hashable> {
4242
self.children = children
4343
self.isExpanded = isExpanded
4444
}
45-
46-
/// A function builder type that produces an array of nodes.
47-
@resultBuilder
48-
public enum Builder {
49-
public static func buildBlock(_ block: [OutlineNode]...) -> [OutlineNode] {
50-
block.flatMap { $0 }
51-
}
52-
53-
public static func buildOptional(_ item: [OutlineNode]?) -> [OutlineNode] {
54-
item ?? []
55-
}
56-
57-
public static func buildEither(first: [OutlineNode]?) -> [OutlineNode] {
58-
first ?? []
59-
}
60-
61-
public static func buildEither(second: [OutlineNode]?) -> [OutlineNode] {
62-
second ?? []
63-
}
64-
65-
public static func buildArray(_ components: [[OutlineNode]]) -> [OutlineNode] {
66-
components.flatMap { $0 }
67-
}
68-
69-
public static func buildExpression(_ expr: [OutlineNode]?) -> [OutlineNode] {
70-
expr ?? []
71-
}
72-
73-
public static func buildExpression(_ expr: OutlineNode?) -> [OutlineNode] {
74-
expr.map { [$0] } ?? []
75-
}
76-
77-
public static func buildExpression(_ expr: ItemIdentifierType?) -> [OutlineNode] {
78-
if let item = expr {
79-
return [.init(item)]
80-
}
81-
return []
82-
}
83-
84-
public static func buildExpression(_ expr: [ItemIdentifierType]?) -> [OutlineNode] {
85-
expr?.compactMap({.init($0)}) ?? []
86-
}
87-
}
8845
}
8946

9047
extension OutlineViewDiffableDataSourceSnapshot {
@@ -136,3 +93,48 @@ extension OutlineViewDiffableDataSourceSnapshot {
13693
nodes.forEach({ apply($0.children, to: $0.item) })
13794
}
13895
}
96+
97+
extension OutlineNode {
98+
/// A function builder type that produces an array of nodes.
99+
@resultBuilder
100+
public enum Builder {
101+
public static func buildBlock(_ block: [OutlineNode]...) -> [OutlineNode] {
102+
block.flatMap { $0 }
103+
}
104+
105+
public static func buildOptional(_ item: [OutlineNode]?) -> [OutlineNode] {
106+
item ?? []
107+
}
108+
109+
public static func buildEither(first: [OutlineNode]?) -> [OutlineNode] {
110+
first ?? []
111+
}
112+
113+
public static func buildEither(second: [OutlineNode]?) -> [OutlineNode] {
114+
second ?? []
115+
}
116+
117+
public static func buildArray(_ components: [[OutlineNode]]) -> [OutlineNode] {
118+
components.flatMap { $0 }
119+
}
120+
121+
public static func buildExpression(_ expr: [OutlineNode]?) -> [OutlineNode] {
122+
expr ?? []
123+
}
124+
125+
public static func buildExpression(_ expr: OutlineNode?) -> [OutlineNode] {
126+
expr.map { [$0] } ?? []
127+
}
128+
129+
public static func buildExpression(_ expr: ItemIdentifierType?) -> [OutlineNode] {
130+
if let item = expr {
131+
return [.init(item)]
132+
}
133+
return []
134+
}
135+
136+
public static func buildExpression(_ expr: [ItemIdentifierType]?) -> [OutlineNode] {
137+
expr?.compactMap({.init($0)}) ?? []
138+
}
139+
}
140+
}

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

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -54,28 +54,6 @@ public struct OutlineViewDiffableDataSourceSnapshot<ItemIdentifierType: Hashable
5454
public var items: [ItemIdentifierType] {
5555
Array(orderedItems)
5656
}
57-
58-
/**
59-
A Boolean value indicating whether the root items are group items.
60-
61-
The default value is `false`. The group items are expanded and can't be collapsed.
62-
63-
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(_:)``.
64-
*/
65-
public var usesGroupItems: Bool = false
66-
67-
/**
68-
A Boolean value indicating whether group items can be expanded/collapsed.
69-
70-
The default value is `false`. The group row items are expanded and can't be collapsed.
71-
72-
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(_:)``.
73-
*/
74-
public var groupItemsAreExpandable = false
75-
76-
var groupItems: [ItemIdentifierType] {
77-
usesGroupItems && !groupItemsAreExpandable ? rootItems : []
78-
}
7957

8058
/// The identifiers of the currently visible items in the snapshot.
8159
public var visibleItems: [ItemIdentifierType] {
@@ -334,6 +312,61 @@ public struct OutlineViewDiffableDataSourceSnapshot<ItemIdentifierType: Hashable
334312
public mutating func collapse(_ items: [ItemIdentifierType]) {
335313
items.forEach({ nodes[$0]?.isExpanded = false })
336314
}
315+
316+
// MARK: - Configurating group items
317+
318+
/// Properties for group items.
319+
public var groupItems = GroupItemProperties()
320+
321+
/// Properties for group items.
322+
public struct GroupItemProperties {
323+
/**
324+
A Boolean value indicating whether the root items are displayed as group items.
325+
326+
If set to `true`, you can optionally provide custom group item cell views using `OutlineViewDiffableDataSource's` ``OutlineViewDiffableDataSource/groupItemCellProvider-swift.property`` and ``OutlineViewDiffableDataSource/applyGroupItemCellRegistration(_:)``.
327+
*/
328+
public var isEnabled: Bool = false
329+
330+
/**
331+
A Boolean value indicating whether group items can be expanded/collapsed.
332+
333+
The default value is `true` and group items can't be collapsed.
334+
335+
If you set the value to `false`, the group item can be expanded and collapsed like regular items.
336+
*/
337+
public var isAlwaysExpanded: Bool = true
338+
339+
/// A Boolean value indicating whether the group items are floating.
340+
public var isFloating: Bool = false
341+
}
342+
343+
/*
344+
/**
345+
A Boolean value indicating whether the root items are group items.
346+
347+
The default value is `false`. The group items are expanded and can't be collapsed.
348+
349+
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(_:)``.
350+
*/
351+
public var usesGroupItems: Bool = false
352+
*/
353+
354+
/*
355+
/**
356+
A Boolean value indicating whether group items can be expanded/collapsed.
357+
358+
The default value is `false`. The group row items are expanded and can't be collapsed.
359+
360+
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(_:)``.
361+
*/
362+
public var groupItemsAreExpandable = false
363+
*/
364+
365+
var _groupItems: [ItemIdentifierType] {
366+
groupItems.isEnabled && groupItems.isAlwaysExpanded ? rootItems : []
367+
}
368+
369+
// MARK: - Debugging snapshots
337370

338371
/// Returns a string with an ASCII representation of the snapshot.
339372
public func visualDescription() -> String {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# ``OutlineViewDiffableDataSourceSnapshot/GroupItemProperties``
2+
3+
## Overview
4+
5+
## Topics
6+
7+
### Configurating group items
8+
9+
- ``isEnabled``
10+
- ``isAlwaysExpanded``
11+
- ``isFloating``

Sources/AdvancedCollectionTableView/Documentation/AdvancedCollectionTableView.docc/Extensions/DiffableDataSource/OutlineViewDiffableDataSource/OutlineViewDiffableDataSourceSnapshot.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555

5656
### Configurating group items
5757

58-
- ``usesGroupItems``
59-
- ``groupItemsAreExpandable``
58+
- ``groupItems``
59+
- ``GroupItemProperties``
6060

6161
### Debugging snapshots
6262

0 commit comments

Comments
 (0)