Browse Source

Add refreshing

master
Shadowfacts 4 months ago
parent
commit
feeb4e0370
Signed by: Shadowfacts <me@shadowfacts.net> GPG Key ID: 94A5AB95422746E5

+ 9
- 3
MongoView/Base.lproj/MainMenu.xib View File

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

+ 8
- 0
MongoView/MongoController.swift View File

@@ -33,5 +33,13 @@ class MongoController {
33 33
         
34 34
         client = try! MongoClient(connectionString, using: group)
35 35
     }
36
+    
37
+    func db(for collection: DatabaseCollection) -> MongoDatabase {
38
+        return client.db(collection.database)
39
+    }
40
+    
41
+    func collection(_ collection: DatabaseCollection) -> MongoCollection<Document> {
42
+        return db(for: collection).collection(collection.name)
43
+    }
36 44
 
37 45
 }

+ 5
- 4
MongoView/View Controllers/DatabaseViewController.swift View File

@@ -150,10 +150,11 @@ class DatabaseViewController: NSViewController {
150 150
     }
151 151
 
152 152
     @IBAction func runQuery(_ sender: Any) {
153
-        guard let queryViewController = queryViewController else {
154
-            return
155
-        }
156
-        queryViewController.runQuery()
153
+        queryViewController?.runQuery()
154
+    }
155
+    
156
+    @IBAction func refresh(_ sender: Any) {
157
+        queryViewController?.refresh()
157 158
     }
158 159
 }
159 160
 

+ 38
- 10
MongoView/View Controllers/QueryViewController.swift View File

@@ -27,6 +27,8 @@ class QueryViewController: NSViewController {
27 27
         return queryTextView.string != defaultQuery
28 28
     }
29 29
     
30
+    var mostRecentQuery: String? = nil
31
+    
30 32
     var rootNodes: [Node] = []
31 33
     
32 34
     init(mongoController: MongoController, collection: DatabaseCollection) {
@@ -43,11 +45,8 @@ class QueryViewController: NSViewController {
43 45
     override func viewDidLoad() {
44 46
         super.viewDidLoad()
45 47
         
46
-        let db = mongoController.client.db(collection.database)
47
-        let collection = db.collection(self.collection.name)
48
-        let documents = try! collection.find().all()
49
-        rootNodes = documents.map { Node(document: $0) }
50
-        
48
+        refresh()
49
+
51 50
         verticalSplitView.delegate = self
52 51
         verticalSplitView.setHoldingPriority(.defaultHigh, forSubviewAt: 0)
53 52
         
@@ -61,9 +60,6 @@ class QueryViewController: NSViewController {
61 60
         outlineView.target = self
62 61
         outlineView.doubleAction = #selector(outlineCellDoubleClicked)
63 62
         
64
-        title = "\(self.collection.database).\(self.collection.name)"
65
-
66
-        documentCountLabel.stringValue = "\(documents.count) document\(documents.count == 1 ? "" : "s")"
67 63
     }
68 64
     
69 65
     override func viewWillAppear() {
@@ -80,9 +76,32 @@ class QueryViewController: NSViewController {
80 76
         view.window!.makeFirstResponder(outlineView)
81 77
     }
82 78
     
79
+    func refresh(reload: Bool = true) {
80
+        if let query = mostRecentQuery {
81
+            let connStr = "\(mongoController.connectionString)/\(collection.database)"
82
+            
83
+            rootNodes = MongoEvaluator.eval(command: query, connectingTo: connStr).map {
84
+                Node(value: $0)
85
+            }
86
+            
87
+            title = query
88
+            documentCountLabel.stringValue = "\(rootNodes.count) result\(rootNodes.count == 1 ? "" : "s")"
89
+        } else {
90
+            let documents = try! mongoController.collection(collection).find().all()
91
+            rootNodes = documents.map { Node(document: $0) }
92
+
93
+            title = "\(self.collection.database).\(self.collection.name)"
94
+            documentCountLabel.stringValue = "\(documents.count) document\(documents.count == 1 ? "" : "s")"
95
+        }
96
+        if reload {
97
+            outlineView.reloadData()
98
+        }
99
+    }
100
+    
83 101
     func runQuery() {
84
-        let query = queryTextView.string
85
-        let connStr = "\(mongoController.connectionString)/\(collection.database)"
102
+        mostRecentQuery = queryTextView.string
103
+        refresh()
104
+    }
86 105
 
87 106
         rootNodes = MongoEvaluator.eval(command: query, connectingTo: connStr).map {
88 107
             Node(value: $0)
@@ -102,6 +121,15 @@ class QueryViewController: NSViewController {
102 121
     
103 122
 }
104 123
 
124
+extension QueryViewController: NSMenuItemValidation {
125
+    func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
126
+        if menuItem.action == #selector(deleteNode(_:)) {
127
+            return outlineView.clickedRow != -1
128
+        }
129
+        return true
130
+    }
131
+}
132
+
105 133
 extension QueryViewController: NSSplitViewDelegate {
106 134
     func splitView(_ splitView: NSSplitView, constrainSplitPosition proposedPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
107 135
         return max(80, min(splitView.bounds.height / 2, proposedPosition))

Loading…
Cancel
Save