Use the proper index sets and so on to make deleting work from the sidebar again. Fix #192.

This commit is contained in:
Brent Simmons 2017-11-25 17:44:54 -08:00
parent 7d579733f0
commit 1047fa5a02
2 changed files with 64 additions and 8 deletions

View File

@ -111,14 +111,17 @@ import RSCore
return
}
let selectedRows = outlineView.selectedRowIndexes
animatingChanges = true
outlineView.beginUpdates()
outlineView.removeItems(at: selectedRows, inParent: nil, withAnimation: [.slideDown])
outlineView.endUpdates()
runCommand(deleteCommand)
let indexSetsGroupedByParent = Node.indexSetsGroupedByParent(nodesToDelete)
for (parent, indexSet) in indexSetsGroupedByParent {
outlineView.removeItems(at: indexSet, inParent: parent.isRoot ? nil : parent, withAnimation: [.slideDown])
}
outlineView.endUpdates()
runCommand(deleteCommand)
animatingChanges = false
}
@ -465,6 +468,22 @@ private extension SidebarViewController {
deleteItemForNode(oneNode)
}
}
func commonParentItemForNodes(_ nodes: [Node]) -> Node? {
if nodes.isEmpty {
return nil
}
guard let parent = nodes.first!.parent else {
return nil
}
for node in nodes {
if node.parent !== parent {
return nil
}
}
return parent
}
}

View File

@ -8,14 +8,17 @@
import Foundation
public final class Node: Equatable {
public final class Node: Hashable {
public weak var parent: Node?
public let representedObject: AnyObject
public var canHaveChildNodes = false
public var isGroupItem = false
public var childNodes: [Node]?
public let hashValue: Int
private static var incrementingID = 0
private static var incrementingIDLock = NSLock()
public var isRoot: Bool {
get {
if let _ = parent {
@ -63,6 +66,11 @@ public final class Node: Equatable {
self.representedObject = representedObject
self.parent = parent
Node.incrementingIDLock.lock()
self.hashValue = Node.incrementingID
Node.incrementingID += 1
Node.incrementingIDLock.unlock()
}
public class func genericRootNode() -> Node {
@ -137,6 +145,35 @@ public final class Node: Equatable {
nomad = parent
}
}
public class func nodesOrganizedByParent(_ nodes: [Node]) -> [Node: [Node]] {
let nodesWithParents = nodes.filter { $0.parent != nil }
return Dictionary(grouping: nodesWithParents, by: { $0.parent! })
}
public class func indexSetsGroupedByParent(_ nodes: [Node]) -> [Node: IndexSet] {
let d = nodesOrganizedByParent(nodes)
let indexSetDictionary = d.mapValues { (nodes) -> IndexSet in
var indexSet = IndexSet()
if nodes.isEmpty {
return indexSet
}
let parent = nodes.first!.parent!
for node in nodes {
if let index = parent.indexOfChild(node) {
indexSet.insert(index)
}
}
return indexSet
}
return indexSetDictionary
}
}