Forgo notifications entirely, and use a delegate instead

This commit is contained in:
Nate Weaver 2019-10-21 17:17:07 -05:00
parent a762052264
commit e7278017d9
2 changed files with 10 additions and 13 deletions

View File

@ -12,13 +12,15 @@ import Account
class ExportOPMLAccessoryViewController: NSViewController { class ExportOPMLAccessoryViewController: NSViewController {
@IBOutlet weak var accountPopUpButton: NSPopUpButton! @IBOutlet weak var accountPopUpButton: NSPopUpButton!
weak var delegate: ExportOPMLAccessoryViewControllerDelegate?
var selectedAccount: Account? { var selectedAccount: Account? {
accountPopUpButton.selectedItem?.representedObject as? Account accountPopUpButton.selectedItem?.representedObject as? Account
} }
init() { init(delegate: ExportOPMLAccessoryViewControllerDelegate) {
super.init(nibName: "ExportOPMLAccessoryView", bundle: nil) super.init(nibName: "ExportOPMLAccessoryView", bundle: nil)
self.delegate = delegate
} }
// MARK: - NSViewController // MARK: - NSViewController
@ -48,11 +50,10 @@ class ExportOPMLAccessoryViewController: NSViewController {
} }
@IBAction func accountSelected(_ popUpButton: NSPopUpButton) { @IBAction func accountSelected(_ popUpButton: NSPopUpButton) {
NotificationCenter.default.post(name: .ExportOPMLSelectedAccountDidChange, object: self) delegate!.selectedAccountDidChange(self)
} }
} }
extension Notification.Name { protocol ExportOPMLAccessoryViewControllerDelegate: class {
static let ExportOPMLSelectedAccountDidChange = Notification.Name(rawValue: "SelectedAccountDidChange") func selectedAccountDidChange(_ accessoryViewController: ExportOPMLAccessoryViewController)
} }

View File

@ -9,13 +9,13 @@
import AppKit import AppKit
import Account import Account
class ExportOPMLController { class ExportOPMLController: ExportOPMLAccessoryViewControllerDelegate {
weak var savePanel: NSSavePanel? weak var savePanel: NSSavePanel?
func runSheetOnWindow(_ hostWindow: NSWindow) { func runSheetOnWindow(_ hostWindow: NSWindow) {
let accessoryViewController = ExportOPMLAccessoryViewController() let accessoryViewController = ExportOPMLAccessoryViewController(delegate: self)
let panel = NSSavePanel() let panel = NSSavePanel()
panel.allowedFileTypes = ["opml"] panel.allowedFileTypes = ["opml"]
panel.allowsOtherFileTypes = false panel.allowsOtherFileTypes = false
@ -26,8 +26,6 @@ class ExportOPMLController {
panel.isExtensionHidden = false panel.isExtensionHidden = false
panel.accessoryView = accessoryViewController.view panel.accessoryView = accessoryViewController.view
NotificationCenter.default.addObserver(self, selector: #selector(selectedAccountDidChange(_:)), name: .ExportOPMLSelectedAccountDidChange, object: accessoryViewController)
updateNameFieldStringValueIfAppropriate(savePanel: panel, from: accessoryViewController, force: true) updateNameFieldStringValueIfAppropriate(savePanel: panel, from: accessoryViewController, force: true)
savePanel = panel savePanel = panel
@ -46,8 +44,6 @@ class ExportOPMLController {
} }
} }
} }
NotificationCenter.default.removeObserver(self)
} }
} }
@ -62,8 +58,8 @@ class ExportOPMLController {
} }
@objc private func selectedAccountDidChange(_ notification: Notification) { internal func selectedAccountDidChange(_ accessoryViewController: ExportOPMLAccessoryViewController) {
if let savePanel = savePanel, let accessoryViewController = notification.object as? ExportOPMLAccessoryViewController { if let savePanel = savePanel {
self.updateNameFieldStringValueIfAppropriate(savePanel: savePanel, from: accessoryViewController) self.updateNameFieldStringValueIfAppropriate(savePanel: savePanel, from: accessoryViewController)
} }
} }