NetNewsWire/Evergreen/Inspector/FeedInspectorViewController...

120 lines
2.3 KiB
Swift
Raw Normal View History

//
// FeedInspectorViewController.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 Data
2018-01-22 06:01:18 +01:00
import DB5
2018-01-21 07:36:17 +01:00
final class FeedInspectorViewController: NSViewController, Inspector {
2018-01-22 06:01:18 +01:00
@IBOutlet var imageView: NSImageView?
@IBOutlet var nameTextField: NSTextField?
@IBOutlet var homePageURLTextField: NSTextField?
@IBOutlet var urlTextField: NSTextField?
private var feed: Feed? {
didSet {
updateUI()
}
}
// MARK: Inspector
2018-01-21 07:36:17 +01:00
let isFallbackInspector = false
var objects: [Any]? {
didSet {
2018-01-22 06:01:18 +01:00
updateFeed()
}
}
2018-01-21 07:36:17 +01:00
func canInspect(_ objects: [Any]) -> Bool {
return objects.count == 1 && objects.first is Feed
}
2018-01-22 06:01:18 +01:00
// 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 {
2018-01-22 06:01:18 +01:00
func updateFeed() {
guard let objects = objects, objects.count == 1, let singleFeed = objects.first as? Feed else {
feed = nil
return
}
2018-01-22 06:01:18 +01:00
feed = singleFeed
}
func updateUI() {
2018-01-22 06:01:18 +01:00
updateImage()
updateName()
updateHomePageURL()
updateFeedURL()
2018-01-22 06:01:18 +01:00
view.needsLayout = true
}
2018-01-22 06:01:18 +01:00
func updateImage() {
guard let feed = feed else {
2018-01-22 06:01:18 +01:00
imageView?.image = nil
return
}
if let feedIcon = appDelegate.feedIconDownloader.icon(for: feed) {
imageView?.image = feedIcon
return
}
if let favicon = appDelegate.faviconDownloader.favicon(for: feed) {
if favicon.size.height < 16.0 && favicon.size.width < 16.0 {
favicon.size = NSSize(width: 16, height: 16)
}
imageView?.image = favicon
return
}
imageView?.image = nil
}
2018-01-22 06:01:18 +01:00
func updateName() {
2018-01-22 06:01:18 +01:00
nameTextField?.stringValue = feed?.editedName ?? feed?.name ?? ""
}
2018-01-22 06:01:18 +01:00
func updateHomePageURL() {
2018-01-22 06:01:18 +01:00
homePageURLTextField?.stringValue = feed?.homePageURL ?? ""
}
2018-01-22 06:01:18 +01:00
func updateFeedURL() {
2018-01-21 07:36:17 +01:00
2018-01-22 06:01:18 +01:00
urlTextField?.stringValue = feed?.url ?? ""
2018-01-21 07:36:17 +01:00
}
2018-01-21 06:35:59 +01:00
}