diff --git a/Mac/Inspector/FolderInspectorViewController.swift b/Mac/Inspector/FolderInspectorViewController.swift index 708802d9b..125eca634 100644 --- a/Mac/Inspector/FolderInspectorViewController.swift +++ b/Mac/Inspector/FolderInspectorViewController.swift @@ -27,6 +27,7 @@ final class FolderInspectorViewController: NSViewController, Inspector { let isFallbackInspector = false var objects: [Any]? { didSet { + renameFolderIfNecessary() updateFolder() } } @@ -43,16 +44,17 @@ final class FolderInspectorViewController: NSViewController, Inspector { // MARK: NSViewController override func viewDidLoad() { - updateUI() - NotificationCenter.default.addObserver(self, selector: #selector(displayNameDidChange(_:)), name: .DisplayNameDidChange, object: nil) } + override func viewDidDisappear() { + renameFolderIfNecessary() + } + // MARK: Notifications @objc func displayNameDidChange(_ note: Notification) { - guard let updatedFolder = note.object as? Folder, updatedFolder == folder else { return } @@ -63,19 +65,9 @@ final class FolderInspectorViewController: NSViewController, Inspector { extension FolderInspectorViewController: NSTextFieldDelegate { func controlTextDidEndEditing(_ obj: Notification) { - - guard let folder = folder, let nameTextField = nameTextField else { - return - } - folder.rename(to: nameTextField.stringValue) { result in - switch result { - case .success: - break - case .failure(let error): - NSApplication.shared.presentError(error) - } - } + renameFolderIfNecessary() } + } private extension FolderInspectorViewController { @@ -106,4 +98,20 @@ private extension FolderInspectorViewController { } windowTitle = folder?.nameForDisplay ?? NSLocalizedString("Folder Inspector", comment: "Folder Inspector window title") } + + func renameFolderIfNecessary() { + guard let folder = folder, + let account = folder.account, + let nameTextField = nameTextField, + folder.nameForDisplay != nameTextField.stringValue else { + return + } + + account.renameFolder(folder, to: nameTextField.stringValue) { [weak self] result in + if case .failure(let error) = result { + self?.presentError(error) + } + } + } + } diff --git a/Mac/Inspector/WebFeedInspectorViewController.swift b/Mac/Inspector/WebFeedInspectorViewController.swift index c1586f05a..feb073350 100644 --- a/Mac/Inspector/WebFeedInspectorViewController.swift +++ b/Mac/Inspector/WebFeedInspectorViewController.swift @@ -35,6 +35,7 @@ final class WebFeedInspectorViewController: NSViewController, Inspector { let isFallbackInspector = false var objects: [Any]? { didSet { + renameWebFeedIfNecessary() updateFeed() } } @@ -55,6 +56,10 @@ final class WebFeedInspectorViewController: NSViewController, Inspector { updateNotificationSettings() } + override func viewDidDisappear() { + renameWebFeedIfNecessary() + } + // MARK: Actions @IBAction func isNotifyAboutNewArticlesChanged(_ sender: Any) { guard userNotificationSettings != nil else { @@ -108,12 +113,8 @@ final class WebFeedInspectorViewController: NSViewController, Inspector { extension WebFeedInspectorViewController: NSTextFieldDelegate { - func controlTextDidChange(_ note: Notification) { - guard let feed = feed, let nameTextField = nameTextField else { - return - } - feed.editedName = nameTextField.stringValue - windowTitle = feed.editedName ?? NSLocalizedString("Feed Inspector", comment: "Feed Inspector window title") + func controlTextDidEndEditing(_ note: Notification) { + renameWebFeedIfNecessary() } } @@ -208,4 +209,19 @@ private extension WebFeedInspectorViewController { } } + func renameWebFeedIfNecessary() { + guard let feed = feed, + let account = feed.account, + let nameTextField = nameTextField, + feed.nameForDisplay != nameTextField.stringValue else { + return + } + + account.renameWebFeed(feed, to: nameTextField.stringValue) { [weak self] result in + if case .failure(let error) = result { + self?.presentError(error) + } + } + } + }