2018-01-21 06:48:27 +01:00
|
|
|
//
|
|
|
|
// FolderInspectorViewController.swift
|
2018-08-29 07:18:24 +02:00
|
|
|
// NetNewsWire
|
2018-01-21 06:48:27 +01:00
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 1/20/18.
|
|
|
|
// Copyright © 2018 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import AppKit
|
2018-01-21 07:36:17 +01:00
|
|
|
import Account
|
2018-01-23 07:01:25 +01:00
|
|
|
import RSCore
|
2018-01-21 06:48:27 +01:00
|
|
|
|
2018-01-21 07:36:17 +01:00
|
|
|
final class FolderInspectorViewController: NSViewController, Inspector {
|
2018-01-21 06:48:27 +01:00
|
|
|
|
2018-01-22 06:24:25 +01:00
|
|
|
@IBOutlet var nameTextField: NSTextField?
|
2020-12-10 00:24:55 +01:00
|
|
|
@IBOutlet weak var folderImageView: NSImageView!
|
|
|
|
|
2018-01-22 06:24:25 +01:00
|
|
|
private var folder: Folder? {
|
|
|
|
didSet {
|
|
|
|
if folder != oldValue {
|
|
|
|
updateUI()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Inspector
|
|
|
|
|
2018-01-21 07:36:17 +01:00
|
|
|
let isFallbackInspector = false
|
2018-01-22 06:24:25 +01:00
|
|
|
var objects: [Any]? {
|
|
|
|
didSet {
|
2020-12-10 00:09:28 +01:00
|
|
|
renameFolderIfNecessary()
|
2018-01-22 06:24:25 +01:00
|
|
|
updateFolder()
|
|
|
|
}
|
|
|
|
}
|
2020-09-07 19:34:37 +02:00
|
|
|
var windowTitle: String = NSLocalizedString("Folder Inspector", comment: "Folder Inspector window title")
|
2018-01-21 07:36:17 +01:00
|
|
|
|
|
|
|
func canInspect(_ objects: [Any]) -> Bool {
|
|
|
|
|
2018-01-22 06:24:25 +01:00
|
|
|
guard let _ = singleFolder(from: objects) else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2018-01-21 07:36:17 +01:00
|
|
|
}
|
|
|
|
|
2018-01-22 06:24:25 +01:00
|
|
|
// MARK: NSViewController
|
2018-01-21 07:36:17 +01:00
|
|
|
|
2018-01-22 06:24:25 +01:00
|
|
|
override func viewDidLoad() {
|
|
|
|
updateUI()
|
2020-12-10 00:24:55 +01:00
|
|
|
|
2024-11-02 19:32:52 +01:00
|
|
|
let image = NSImage(systemSymbolName: "folder", accessibilityDescription: nil)!
|
|
|
|
folderImageView.image = image
|
|
|
|
folderImageView.contentTintColor = NSColor.controlAccentColor
|
2020-12-10 00:24:55 +01:00
|
|
|
|
2018-01-23 07:01:25 +01:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(displayNameDidChange(_:)), name: .DisplayNameDidChange, object: nil)
|
|
|
|
}
|
|
|
|
|
2020-12-10 00:09:28 +01:00
|
|
|
override func viewDidDisappear() {
|
|
|
|
renameFolderIfNecessary()
|
|
|
|
}
|
|
|
|
|
2018-01-23 07:01:25 +01:00
|
|
|
// MARK: Notifications
|
|
|
|
|
|
|
|
@objc func displayNameDidChange(_ note: Notification) {
|
|
|
|
guard let updatedFolder = note.object as? Folder, updatedFolder == folder else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
updateUI()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension FolderInspectorViewController: NSTextFieldDelegate {
|
|
|
|
|
2019-05-06 17:53:20 +02:00
|
|
|
func controlTextDidEndEditing(_ obj: Notification) {
|
2020-12-10 00:09:28 +01:00
|
|
|
renameFolderIfNecessary()
|
2018-01-21 07:36:17 +01:00
|
|
|
}
|
2020-12-10 00:09:28 +01:00
|
|
|
|
2018-01-21 06:48:27 +01:00
|
|
|
}
|
2018-01-22 06:24:25 +01:00
|
|
|
|
|
|
|
private extension FolderInspectorViewController {
|
|
|
|
|
|
|
|
func singleFolder(from objects: [Any]?) -> Folder? {
|
|
|
|
|
|
|
|
guard let objects = objects, objects.count == 1, let singleFolder = objects.first as? Folder else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return singleFolder
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateFolder() {
|
|
|
|
|
|
|
|
folder = singleFolder(from: objects)
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateUI() {
|
|
|
|
|
2018-01-23 07:01:25 +01:00
|
|
|
guard let nameTextField = nameTextField else {
|
|
|
|
return
|
|
|
|
}
|
2018-01-22 06:24:25 +01:00
|
|
|
|
2018-01-23 07:01:25 +01:00
|
|
|
let name = folder?.nameForDisplay ?? ""
|
|
|
|
if nameTextField.stringValue != name {
|
|
|
|
nameTextField.stringValue = name
|
|
|
|
}
|
2020-09-07 19:59:14 +02:00
|
|
|
windowTitle = folder?.nameForDisplay ?? NSLocalizedString("Folder Inspector", comment: "Folder Inspector window title")
|
2018-01-23 07:01:25 +01:00
|
|
|
}
|
2020-12-10 00:09:28 +01:00
|
|
|
|
|
|
|
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)
|
2020-12-10 00:12:58 +01:00
|
|
|
} else {
|
|
|
|
self?.windowTitle = folder.nameForDisplay
|
2020-12-10 00:09:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-22 06:24:25 +01:00
|
|
|
}
|