From 3a8088033abb75acbc579c7ab7a463ffcdff9f9a Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 1 Apr 2020 21:42:32 -0400 Subject: [PATCH] Prevent nodes from collapsing when refreshing --- MongoView.xcodeproj/project.pbxproj | 4 +++- MongoView/Node.swift | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/MongoView.xcodeproj/project.pbxproj b/MongoView.xcodeproj/project.pbxproj index a9b22bd..6055219 100644 --- a/MongoView.xcodeproj/project.pbxproj +++ b/MongoView.xcodeproj/project.pbxproj @@ -224,7 +224,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 1130; - LastUpgradeCheck = 1130; + LastUpgradeCheck = 1140; ORGANIZATIONNAME = Shadowfacts; TargetAttributes = { D63CDEB923C837DC0012D658 = { @@ -418,6 +418,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_ENTITLEMENTS = MongoView/MongoView.entitlements; + CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; CURRENT_PROJECT_VERSION = 3; @@ -439,6 +440,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_ENTITLEMENTS = MongoView/MongoView.entitlements; + CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; CURRENT_PROJECT_VERSION = 3; diff --git a/MongoView/Node.swift b/MongoView/Node.swift index 5e106ce..f78414d 100644 --- a/MongoView/Node.swift +++ b/MongoView/Node.swift @@ -9,7 +9,9 @@ import Foundation import MongoSwift -class Node { +// Node needs to be an NSObject, since NSOutlineView uses NSObject.isEqual(_:) and NSObject.hash to determine item equality +// which is necessary to prevent items from collapsing when refreshing the view +class Node: NSObject { let key: Key? let value: BSON weak var parent: Node? @@ -57,10 +59,22 @@ class Node { self.init(key: nil, value: .document(document)) } } + + override func isEqual(_ object: Any?) -> Bool { + guard let object = object as? Node else { return false } + return self.parent == object.parent && self.key == object.key + } + + override var hash: Int { + var hasher = Hasher() + hasher.combine(parent) + hasher.combine(key) + return hasher.finalize() + } } extension Node { - enum Key { + enum Key: Equatable, Hashable { case index(Int) case name(String) case objectId(ObjectId)