NetNewsWire/Evergreen/Inspector/FolderInspectorViewControll...

101 lines
1.8 KiB
Swift
Raw Normal View History

2018-01-21 06:48:27 +01:00
//
// FolderInspectorViewController.swift
// Evergreen
//
// 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
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?
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 {
updateFolder()
}
}
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()
NotificationCenter.default.addObserver(self, selector: #selector(displayNameDidChange(_:)), name: .DisplayNameDidChange, object: nil)
}
// MARK: Notifications
@objc func displayNameDidChange(_ note: Notification) {
guard let updatedFolder = note.object as? Folder, updatedFolder == folder else {
return
}
updateUI()
}
}
extension FolderInspectorViewController: NSTextFieldDelegate {
override func controlTextDidChange(_ note: Notification) {
guard let folder = folder, let nameTextField = nameTextField else {
return
}
folder.name = nameTextField.stringValue
2018-01-21 07:36:17 +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() {
guard let nameTextField = nameTextField else {
return
}
2018-01-22 06:24:25 +01:00
let name = folder?.nameForDisplay ?? ""
if nameTextField.stringValue != name {
nameTextField.stringValue = name
}
}
2018-01-22 06:24:25 +01:00
}