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() {
super.windowDidLoad()
if mongoController == nil {
let setupMongo = mongoController == nil
if setupMongo {
mongoController = MongoController(connectionString: "mongodb://localhost:27017")
mongoController.setup()
}
mongoController.statusDidChange.append({ [weak self] (status) in
guard let self = self else { return }
self.updateStatusText(status)
if status == .success {
DispatchQueue.main.async {
self.initializeUI()
}
}
})
self.updateStatusText(mongoController.status)
databaseViewController = DatabaseViewController(mongoController: mongoController)
contentViewController = databaseViewController
if let initialCollection = initialCollection {
databaseViewController.showCollection(initialCollection)
if setupMongo {
mongoController.setup()
}
titleObservation = observe(\.contentViewController?.title) { [unowned self] (_, _) in
self.updateWindowTitle()
}
@ -60,7 +62,7 @@ class DatabaseWindowController: NSWindowController {
}
private func updateWindowTitle() {
window?.title = databaseViewController.title ?? "MongoView"
window?.title = databaseViewController?.title ?? "MongoView"
}
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 {