Add refreshing

This commit is contained in:
Shadowfacts 2020-02-09 21:13:22 -05:00
parent 3c4d07093d
commit feeb4e0370
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
4 changed files with 60 additions and 17 deletions

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="15702" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct"> <document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="16085" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies> <dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="15702"/> <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="16085"/>
</dependencies> </dependencies>
<objects> <objects>
<customObject id="-2" userLabel="File's Owner" customClass="NSApplication"> <customObject id="-2" userLabel="File's Owner" customClass="NSApplication">
@ -107,7 +107,8 @@
<action selector="saveDocumentAs:" target="-1" id="mDf-zr-I0C"/> <action selector="saveDocumentAs:" target="-1" id="mDf-zr-I0C"/>
</connections> </connections>
</menuItem> </menuItem>
<menuItem title="Revert to Saved" keyEquivalent="r" id="KaW-ft-85H"> <menuItem title="Revert to Saved" id="KaW-ft-85H">
<modifierMask key="keyEquivalentModifierMask"/>
<connections> <connections>
<action selector="revertDocumentToSaved:" target="-1" id="iJ3-Pv-kwq"/> <action selector="revertDocumentToSaved:" target="-1" id="iJ3-Pv-kwq"/>
</connections> </connections>
@ -362,6 +363,11 @@
<modifierMask key="keyEquivalentModifierMask"/> <modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="Database" id="nD3-Xo-LUZ"> <menu key="submenu" title="Database" id="nD3-Xo-LUZ">
<items> <items>
<menuItem title="Refresh" keyEquivalent="r" id="kpI-5J-eaC">
<connections>
<action selector="refresh:" target="-1" id="S5L-sX-Z2j"/>
</connections>
</menuItem>
<menuItem title="Run Query" id="RSP-nH-a1I"> <menuItem title="Run Query" id="RSP-nH-a1I">
<string key="keyEquivalent" base64-UTF8="YES"> <string key="keyEquivalent" base64-UTF8="YES">
DQ DQ

View File

@ -33,5 +33,13 @@ class MongoController {
client = try! MongoClient(connectionString, using: group) client = try! MongoClient(connectionString, using: group)
} }
func db(for collection: DatabaseCollection) -> MongoDatabase {
return client.db(collection.database)
}
func collection(_ collection: DatabaseCollection) -> MongoCollection<Document> {
return db(for: collection).collection(collection.name)
}
} }

View File

@ -150,10 +150,11 @@ class DatabaseViewController: NSViewController {
} }
@IBAction func runQuery(_ sender: Any) { @IBAction func runQuery(_ sender: Any) {
guard let queryViewController = queryViewController else { queryViewController?.runQuery()
return }
}
queryViewController.runQuery() @IBAction func refresh(_ sender: Any) {
queryViewController?.refresh()
} }
} }

View File

@ -27,6 +27,8 @@ class QueryViewController: NSViewController {
return queryTextView.string != defaultQuery return queryTextView.string != defaultQuery
} }
var mostRecentQuery: String? = nil
var rootNodes: [Node] = [] var rootNodes: [Node] = []
init(mongoController: MongoController, collection: DatabaseCollection) { init(mongoController: MongoController, collection: DatabaseCollection) {
@ -43,11 +45,8 @@ class QueryViewController: NSViewController {
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
let db = mongoController.client.db(collection.database) refresh()
let collection = db.collection(self.collection.name)
let documents = try! collection.find().all()
rootNodes = documents.map { Node(document: $0) }
verticalSplitView.delegate = self verticalSplitView.delegate = self
verticalSplitView.setHoldingPriority(.defaultHigh, forSubviewAt: 0) verticalSplitView.setHoldingPriority(.defaultHigh, forSubviewAt: 0)
@ -61,9 +60,6 @@ class QueryViewController: NSViewController {
outlineView.target = self outlineView.target = self
outlineView.doubleAction = #selector(outlineCellDoubleClicked) outlineView.doubleAction = #selector(outlineCellDoubleClicked)
title = "\(self.collection.database).\(self.collection.name)"
documentCountLabel.stringValue = "\(documents.count) document\(documents.count == 1 ? "" : "s")"
} }
override func viewWillAppear() { override func viewWillAppear() {
@ -80,9 +76,32 @@ class QueryViewController: NSViewController {
view.window!.makeFirstResponder(outlineView) view.window!.makeFirstResponder(outlineView)
} }
func refresh(reload: Bool = true) {
if let query = mostRecentQuery {
let connStr = "\(mongoController.connectionString)/\(collection.database)"
rootNodes = MongoEvaluator.eval(command: query, connectingTo: connStr).map {
Node(value: $0)
}
title = query
documentCountLabel.stringValue = "\(rootNodes.count) result\(rootNodes.count == 1 ? "" : "s")"
} else {
let documents = try! mongoController.collection(collection).find().all()
rootNodes = documents.map { Node(document: $0) }
title = "\(self.collection.database).\(self.collection.name)"
documentCountLabel.stringValue = "\(documents.count) document\(documents.count == 1 ? "" : "s")"
}
if reload {
outlineView.reloadData()
}
}
func runQuery() { func runQuery() {
let query = queryTextView.string mostRecentQuery = queryTextView.string
let connStr = "\(mongoController.connectionString)/\(collection.database)" refresh()
}
rootNodes = MongoEvaluator.eval(command: query, connectingTo: connStr).map { rootNodes = MongoEvaluator.eval(command: query, connectingTo: connStr).map {
Node(value: $0) Node(value: $0)
@ -102,6 +121,15 @@ class QueryViewController: NSViewController {
} }
extension QueryViewController: NSMenuItemValidation {
func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
if menuItem.action == #selector(deleteNode(_:)) {
return outlineView.clickedRow != -1
}
return true
}
}
extension QueryViewController: NSSplitViewDelegate { extension QueryViewController: NSSplitViewDelegate {
func splitView(_ splitView: NSSplitView, constrainSplitPosition proposedPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat { func splitView(_ splitView: NSSplitView, constrainSplitPosition proposedPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
return max(80, min(splitView.bounds.height / 2, proposedPosition)) return max(80, min(splitView.bounds.height / 2, proposedPosition))