Fix crash when MongoController connection fails

This commit is contained in:
Shadowfacts 2020-07-06 10:36:44 -04:00
parent 98179767ca
commit bb695fbf53
1 changed files with 21 additions and 10 deletions

View File

@ -28,23 +28,25 @@ class DatabaseWindowController: NSWindowController {
override func windowDidLoad() { override func windowDidLoad() {
super.windowDidLoad() super.windowDidLoad()
if mongoController == nil { let setupMongo = mongoController == nil
if setupMongo {
mongoController = MongoController(connectionString: "mongodb://localhost:27017") mongoController = MongoController(connectionString: "mongodb://localhost:27017")
mongoController.setup()
} }
mongoController.statusDidChange.append({ [weak self] (status) in mongoController.statusDidChange.append({ [weak self] (status) in
guard let self = self else { return } guard let self = self else { return }
self.updateStatusText(status) self.updateStatusText(status)
if status == .success {
DispatchQueue.main.async {
self.initializeUI()
}
}
}) })
self.updateStatusText(mongoController.status) self.updateStatusText(mongoController.status)
if setupMongo {
databaseViewController = DatabaseViewController(mongoController: mongoController) mongoController.setup()
contentViewController = databaseViewController
if let initialCollection = initialCollection {
databaseViewController.showCollection(initialCollection)
} }
titleObservation = observe(\.contentViewController?.title) { [unowned self] (_, _) in titleObservation = observe(\.contentViewController?.title) { [unowned self] (_, _) in
self.updateWindowTitle() self.updateWindowTitle()
} }
@ -60,7 +62,7 @@ class DatabaseWindowController: NSWindowController {
} }
private func updateWindowTitle() { private func updateWindowTitle() {
window?.title = databaseViewController.title ?? "MongoView" window?.title = databaseViewController?.title ?? "MongoView"
} }
private func updateStatusText(_ status: MongoController.Status) { private func updateStatusText(_ status: MongoController.Status) {
@ -75,6 +77,15 @@ class DatabaseWindowController: NSWindowController {
} }
} }
private func initializeUI() {
databaseViewController = DatabaseViewController(mongoController: mongoController)
contentViewController = databaseViewController
if let initialCollection = initialCollection {
databaseViewController.showCollection(initialCollection)
}
}
} }
extension NSToolbarItem.Identifier { extension NSToolbarItem.Identifier {