// // MainSplitViewController.swift // MongoView // // Created by Shadowfacts on 8/11/20. // Copyright © 2020 Shadowfacts. All rights reserved. // import Cocoa import Combine class MainSplitViewController: NSSplitViewController { let mongoController: MongoController let initialCollection: DatabaseCollection? private var detailViewController = DetailViewController() private var currentQueryViewController: QueryViewController? { detailViewController.children.first as? QueryViewController } private var collectionSubscriber: Cancellable? private var titleObservation: NSKeyValueObservation? init(mongoController: MongoController, initialCollection: DatabaseCollection?) { self.mongoController = mongoController self.initialCollection = initialCollection super.init(nibName: nil, bundle: nil) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewDidLoad() { super.viewDidLoad() let listVC = DatabaseCollectionListViewController(mongoController: mongoController) let sourceListItem = NSSplitViewItem(sidebarWithViewController: listVC) let detailItem = NSSplitViewItem(viewController: detailViewController) detailItem.maximumThickness = NSSplitViewItem.unspecifiedDimension addSplitViewItem(sourceListItem) addSplitViewItem(detailItem) NSLayoutConstraint.activate([ sourceListItem.viewController.view.widthAnchor.constraint(greaterThanOrEqualToConstant: 200), sourceListItem.viewController.view.widthAnchor.constraint(lessThanOrEqualToConstant: 500), detailItem.viewController.view.widthAnchor.constraint(greaterThanOrEqualToConstant: 200), ]) collectionSubscriber = listVC.selectedCollection.sink(receiveValue: self.selectCollection) if let initialCollection = initialCollection { selectCollection(initialCollection) } } private func selectCollection(_ collection: DatabaseCollection) { if currentQueryViewController?.hasFilterChanged ?? false { (NSApp.delegate as! AppDelegate).newWindow(mongoController: mongoController, collection: collection) } else { let queryVC = QueryViewController(mongoController: mongoController, collection: collection) setDetailChild(queryVC) } } private func setDetailChild(_ vc: NSViewController) { if let child = detailViewController.children.first { child.removeFromParent() child.view.removeFromSuperview() } vc.view.translatesAutoresizingMaskIntoConstraints = false detailViewController.addChild(vc) detailViewController.view.addSubview(vc.view) NSLayoutConstraint.activate([ detailViewController.view.leadingAnchor.constraint(equalTo: vc.view.leadingAnchor), detailViewController.view.trailingAnchor.constraint(equalTo: vc.view.trailingAnchor), detailViewController.view.topAnchor.constraint(equalTo: vc.view.topAnchor), detailViewController.view.bottomAnchor.constraint(equalTo: vc.view.bottomAnchor), ]) titleObservation = vc.observe(\.title, options: .initial, changeHandler: { [unowned self] (_, _) in self.title = vc.title }) } @IBAction func refresh(_ sender: Any) { currentQueryViewController?.refresh() } } extension MainSplitViewController: NSMenuItemValidation { func validateMenuItem(_ menuItem: NSMenuItem) -> Bool { if menuItem.action == #selector(refresh(_:)) { return currentQueryViewController != nil } return true } }