From 8fe2810a90fecf3b9edba32edccd1998e1ea223c Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sat, 25 Nov 2017 11:33:07 -0800 Subject: [PATCH] Show favicons in the Feed Directory. Fix #109. --- Evergreen/Base.lproj/FeedList.storyboard | 5 +- .../FeedList/FeedListViewController.swift | 54 ++++++++++++++++++- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/Evergreen/Base.lproj/FeedList.storyboard b/Evergreen/Base.lproj/FeedList.storyboard index 72e565a74..728514183 100644 --- a/Evergreen/Base.lproj/FeedList.storyboard +++ b/Evergreen/Base.lproj/FeedList.storyboard @@ -206,7 +206,7 @@ - + @@ -251,6 +251,9 @@ + + + diff --git a/Evergreen/FeedList/FeedListViewController.swift b/Evergreen/FeedList/FeedListViewController.swift index 00760eaf6..351b7da50 100644 --- a/Evergreen/FeedList/FeedListViewController.swift +++ b/Evergreen/FeedList/FeedListViewController.swift @@ -13,11 +13,26 @@ import RSCore final class FeedListViewController: NSViewController { @IBOutlet var outlineView: NSOutlineView! + private var sidebarCellAppearance: SidebarCellAppearance! private let treeControllerDelegate = FeedListTreeControllerDelegate() lazy var treeController: TreeController = { TreeController(delegate: treeControllerDelegate) }() + // MARK: NSViewController + + override func viewDidLoad() { + + sidebarCellAppearance = SidebarCellAppearance(theme: appDelegate.currentTheme, fontSize: AppDefaults.shared.sidebarFontSize) + NotificationCenter.default.addObserver(self, selector: #selector(faviconDidBecomeAvailable(_:)), name: .FaviconDidBecomeAvailable, object: nil) + } + + // MARK: - Notifications + + @objc func faviconDidBecomeAvailable(_ note: Notification) { + + configureAvailableCells() + } } // MARK: - NSOutlineViewDataSource @@ -80,14 +95,19 @@ extension FeedListViewController: NSOutlineViewDelegate { private func configure(_ cell: SidebarCell, _ node: Node) { + cell.cellAppearance = sidebarCellAppearance cell.objectValue = node cell.name = nameFor(node) cell.image = imageFor(node) + cell.shouldShowImage = node.representedObject is FeedListFeed } func imageFor(_ node: Node) -> NSImage? { - return nil + guard let feed = node.representedObject as? FeedListFeed else { + return nil + } + return appDelegate.faviconDownloader.favicon(withHomePageURL: feed.homePageURL) } func nameFor(_ node: Node) -> String { @@ -97,5 +117,35 @@ extension FeedListViewController: NSOutlineViewDelegate { } return "" } - +} + +private extension FeedListViewController { + + func nodeForRow(_ row: Int) -> Node? { + + if row < 0 || row >= outlineView.numberOfRows { + return nil + } + + if let node = outlineView.item(atRow: row) as? Node { + return node + } + return nil + } + + func cellForRowView(_ rowView: NSTableRowView) -> SidebarCell? { + + return rowView.view(atColumn: 0) as? SidebarCell + } + + func configureAvailableCells() { + + outlineView.enumerateAvailableRowViews { (rowView: NSTableRowView, row: Int) -> Void in + + guard let cell = cellForRowView(rowView), let node = nodeForRow(row) else { + return + } + configure(cell, node) + } + } }