From e50055632bfcbb1cb1df85ade4e4b80ac48ff118 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sun, 21 Jan 2018 21:01:18 -0800 Subject: [PATCH] Continue progress on feed inspector. --- .../FeedInspectorViewController.swift | 102 ++++++++++-------- .../Inspector/InspectorWindowController.swift | 5 - 2 files changed, 58 insertions(+), 49 deletions(-) diff --git a/Evergreen/Inspector/FeedInspectorViewController.swift b/Evergreen/Inspector/FeedInspectorViewController.swift index accbbb551..cdbfb17a1 100644 --- a/Evergreen/Inspector/FeedInspectorViewController.swift +++ b/Evergreen/Inspector/FeedInspectorViewController.swift @@ -8,18 +8,27 @@ import AppKit import Data +import DB5 final class FeedInspectorViewController: NSViewController, Inspector { - @IBOutlet var imageView: NSImageView! - @IBOutlet var nameTextField: NSTextField! - @IBOutlet var homePageURLTextField: NSTextField! - @IBOutlet var urlTextField: NSTextField! + @IBOutlet var imageView: NSImageView? + @IBOutlet var nameTextField: NSTextField? + @IBOutlet var homePageURLTextField: NSTextField? + @IBOutlet var urlTextField: NSTextField? + + private var feed: Feed? { + didSet { + updateUI() + } + } + + // MARK: Inspector let isFallbackInspector = false var objects: [Any]? { didSet { - updateUI() + updateFeed() } } @@ -28,69 +37,74 @@ final class FeedInspectorViewController: NSViewController, Inspector { return objects.count == 1 && objects.first is Feed } - func willEndInspectingObjects() { - - makeUIEmpty() + // MARK: NSViewController + + override func viewDidLoad() { + + imageView!.wantsLayer = true + let cornerRadius = appDelegate.currentTheme.float(forKey: "MainWindow.Timeline.cell.avatarCornerRadius") + imageView!.layer?.cornerRadius = cornerRadius + + updateUI() + + NotificationCenter.default.addObserver(self, selector: #selector(imageDidBecomeAvailable(_:)), name: .ImageDidBecomeAvailable, object: nil) + } + + // MARK: Notifications + + @objc func imageDidBecomeAvailable(_ note: Notification) { + + updateImage() } } private extension FeedInspectorViewController { - private var feed: Feed? { - guard let objects = objects, objects.count == 1, let feed = objects.first as? Feed else { - return nil + func updateFeed() { + + guard let objects = objects, objects.count == 1, let singleFeed = objects.first as? Feed else { + feed = nil + return } - return feed + feed = singleFeed } func updateUI() { - view.needsLayout = true + updateImage() + updateName() + updateHomePageURL() + updateFeedURL() - guard let feed = feed else { - makeUIEmpty() + view.needsLayout = true + } + + func updateImage() { + + guard let feed = feed, let image = image(for: feed) else { + imageView?.image = nil return } - - updateImage(feed) - updateName(feed) - updateHomePageURL(feed) - updateFeedURL(feed) + imageView?.image = image } - func updateImage(_ feed: Feed) { + func image(for feed: Feed) -> NSImage? { - if let image = appDelegate.feedIconDownloader.icon(for: feed) { - imageView.image = image - } - else if let image = appDelegate.faviconDownloader.favicon(for: feed) { - imageView.image = image - } - else { - imageView.image = nil - } + return appDelegate.feedIconDownloader.icon(for: feed) ?? appDelegate.faviconDownloader.favicon(for: feed) } - func updateName(_ feed: Feed) { + func updateName() { - nameTextField.stringValue = feed.editedName ?? feed.name ?? "" + nameTextField?.stringValue = feed?.editedName ?? feed?.name ?? "" } - func updateHomePageURL(_ feed: Feed) { + func updateHomePageURL() { - homePageURLTextField.stringValue = feed.homePageURL ?? "" + homePageURLTextField?.stringValue = feed?.homePageURL ?? "" } - func updateFeedURL(_ feed: Feed) { + func updateFeedURL() { - urlTextField.stringValue = feed.url - } - - func makeUIEmpty() { - - imageView.image = nil - nameTextField.stringValue = "" - homePageURLTextField.stringValue = "" - urlTextField.stringValue = "" + urlTextField?.stringValue = feed?.url ?? "" } } diff --git a/Evergreen/Inspector/InspectorWindowController.swift b/Evergreen/Inspector/InspectorWindowController.swift index b166e4c95..ff5c1f510 100644 --- a/Evergreen/Inspector/InspectorWindowController.swift +++ b/Evergreen/Inspector/InspectorWindowController.swift @@ -14,8 +14,6 @@ protocol Inspector: class { var isFallbackInspector: Bool { get } // Can handle nothing-to-inspect or unexpected type of objects. func canInspect(_ objects: [Any]) -> Bool - - func willEndInspectingObjects() // Called on the current inspector right before objects is about to change. } typealias InspectorViewController = Inspector & NSViewController @@ -44,9 +42,6 @@ final class InspectorWindowController: NSWindowController { private var currentInspector: InspectorViewController! { didSet { - if let oldInspector = oldValue { - oldInspector.willEndInspectingObjects() - } currentInspector.objects = objects for inspector in inspectors { if inspector !== currentInspector {