NetNewsWire/Evergreen/MainWindow/Timeline/TimelineViewController.swift

774 lines
21 KiB
Swift
Raw Normal View History

2017-05-27 19:43:27 +02:00
//
// TimelineViewController.swift
// Evergreen
//
// Created by Brent Simmons on 7/26/15.
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
//
import Foundation
import RSCore
import Data
2017-09-17 21:34:10 +02:00
import Account
2017-05-27 19:43:27 +02:00
class TimelineViewController: NSViewController, UndoableCommandRunner {
2017-05-27 19:43:27 +02:00
@IBOutlet var tableView: TimelineTableView!
@IBOutlet var contextualMenuDelegate: TimelineContextualMenuDelegate?
@IBOutlet var dataSource: TimelineDataSource!
2017-11-02 06:40:28 +01:00
var selectedArticles: [Article] {
return Array(articles.articlesForIndexes(tableView.selectedRowIndexes))
2017-11-01 04:33:41 +01:00
}
2017-11-02 06:40:28 +01:00
var hasAtLeastOneSelectedArticle: Bool {
return tableView.selectedRow != -1
}
2018-02-10 08:16:12 +01:00
var articles = ArticleArray() {
didSet {
if articles != oldValue {
dataSource.articles = articles
2018-02-10 08:16:12 +01:00
updateShowAvatars()
tableView.reloadData()
}
}
}
var undoableCommands = [UndoableCommand]()
private var cellAppearance: TimelineCellAppearance!
private var cellAppearanceWithAvatar: TimelineCellAppearance!
private var showFeedNames = false {
didSet {
if showFeedNames != oldValue {
updateShowAvatars()
updateTableViewRowHeight()
}
}
}
private var showAvatars = false
private var rowHeightWithFeedName: CGFloat = 0.0
private var rowHeightWithoutFeedName: CGFloat = 0.0
private var currentRowHeight: CGFloat {
return showFeedNames ? rowHeightWithFeedName : rowHeightWithoutFeedName
}
private var didRegisterForNotifications = false
static let fetchAndMergeArticlesQueue = CoalescingQueue(name: "Fetch and Merge Articles", interval: 0.5)
2017-11-02 06:40:28 +01:00
private var sortDirection = AppDefaults.shared.timelineSortDirection {
didSet {
if sortDirection != oldValue {
sortDirectionDidChange()
}
}
}
2017-11-02 06:40:28 +01:00
private var fontSize: FontSize = AppDefaults.shared.timelineFontSize {
2017-05-27 19:43:27 +02:00
didSet {
if fontSize != oldValue {
fontSizeDidChange()
}
2017-05-27 19:43:27 +02:00
}
}
private var representedObjects: [AnyObject]? {
2017-05-27 19:43:27 +02:00
didSet {
if !representedObjectArraysAreEqual(oldValue, representedObjects) {
if let representedObjects = representedObjects {
if representedObjects.count == 1 && representedObjects.first is Feed {
showFeedNames = false
}
else {
showFeedNames = true
}
}
else {
showFeedNames = false
}
postTimelineSelectionDidChangeNotification(nil)
articles = ArticleArray()
2017-05-27 19:43:27 +02:00
fetchArticles()
if articles.count > 0 {
tableView.scrollRowToVisible(0)
}
}
}
}
private var oneSelectedArticle: Article? {
return selectedArticles.count == 1 ? selectedArticles.first : nil
2017-05-27 19:43:27 +02:00
}
override func viewDidLoad() {
cellAppearance = TimelineCellAppearance(theme: appDelegate.currentTheme, showAvatar: false, fontSize: fontSize)
cellAppearanceWithAvatar = TimelineCellAppearance(theme: appDelegate.currentTheme, showAvatar: true, fontSize: fontSize)
2017-05-27 19:43:27 +02:00
updateRowHeights()
tableView.rowHeight = currentRowHeight
2017-05-27 19:43:27 +02:00
tableView.target = self
tableView.doubleAction = #selector(openArticleInBrowser(_:))
tableView.setDraggingSourceOperationMask(.copy, forLocal: false)
2017-05-27 19:43:27 +02:00
if !didRegisterForNotifications {
NotificationCenter.default.addObserver(self, selector: #selector(sidebarSelectionDidChange(_:)), name: .SidebarSelectionDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(statusesDidChange(_:)), name: .StatusesDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(feedIconDidBecomeAvailable(_:)), name: .FeedIconDidBecomeAvailable, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(avatarDidBecomeAvailable(_:)), name: .AvatarDidBecomeAvailable, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(imageDidBecomeAvailable(_:)), name: .ImageDidBecomeAvailable, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(imageDidBecomeAvailable(_:)), name: .FaviconDidBecomeAvailable, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(accountDidDownloadArticles(_:)), name: .AccountDidDownloadArticles, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(userDefaultsDidChange(_:)), name: UserDefaults.didChangeNotification, object: nil)
2017-05-27 19:43:27 +02:00
didRegisterForNotifications = true
2017-05-27 19:43:27 +02:00
}
}
// MARK: Appearance Change
private func fontSizeDidChange() {
TimelineCellData.emptyCache()
cellAppearance = TimelineCellAppearance(theme: appDelegate.currentTheme, showAvatar: false, fontSize: fontSize)
cellAppearanceWithAvatar = TimelineCellAppearance(theme: appDelegate.currentTheme, showAvatar: true, fontSize: fontSize)
updateRowHeights()
performBlockAndRestoreSelection {
tableView.reloadData()
}
2017-05-27 19:43:27 +02:00
}
// MARK: - API
2017-05-27 19:43:27 +02:00
func markAllAsRead() {
guard let undoManager = undoManager, let markReadCommand = MarkStatusCommand(initialArticles: articles, markingRead: true, undoManager: undoManager) else {
2017-05-27 19:43:27 +02:00
return
}
runCommand(markReadCommand)
}
func canMarkAllAsRead() -> Bool {
return articles.canMarkAllAsRead()
}
2017-11-06 06:27:34 +01:00
func canMarkSelectedArticlesAsRead() -> Bool {
return selectedArticles.canMarkAllAsRead()
}
// MARK: - Actions
@objc func openArticleInBrowser(_ sender: Any?) {
2017-05-27 19:43:27 +02:00
2017-09-18 01:30:45 +02:00
if let link = oneSelectedArticle?.preferredLink {
Browser.open(link)
2017-05-27 19:43:27 +02:00
}
}
@IBAction func toggleStatusOfSelectedArticles(_ sender: Any?) {
2017-05-27 19:43:27 +02:00
guard !selectedArticles.isEmpty else {
return
}
let articles = selectedArticles
2017-09-24 21:24:44 +02:00
let status = articles.first!.status
let markAsRead = !status.read
if markAsRead {
markSelectedArticlesAsRead(sender)
}
else {
markSelectedArticlesAsUnread(sender)
}
2017-05-27 19:43:27 +02:00
}
@IBAction func markSelectedArticlesAsRead(_ sender: Any?) {
guard let undoManager = undoManager, let markReadCommand = MarkStatusCommand(initialArticles: selectedArticles, markingRead: true, undoManager: undoManager) else {
return
}
runCommand(markReadCommand)
2017-05-27 19:43:27 +02:00
}
@IBAction func markSelectedArticlesAsUnread(_ sender: Any?) {
2017-05-27 19:43:27 +02:00
guard let undoManager = undoManager, let markUnreadCommand = MarkStatusCommand(initialArticles: selectedArticles, markingRead: false, undoManager: undoManager) else {
return
}
runCommand(markUnreadCommand)
2017-05-27 19:43:27 +02:00
}
@IBAction func copy(_ sender: Any?) {
NSPasteboard.general.copyObjects(selectedArticles)
}
func toggleStarredStatusForSelectedArticles() {
// If any one of the selected articles is not starred, then star them.
// If all articles are starred, then unstar them.
let commandStatus = markStarredCommandStatus()
let starring: Bool
switch commandStatus {
case .canMark:
starring = true
case .canUnmark:
starring = false
case .canDoNothing:
return
}
guard let undoManager = undoManager, let markStarredCommand = MarkStatusCommand(initialArticles: selectedArticles, markingStarred: starring, undoManager: undoManager) else {
return
}
runCommand(markStarredCommand)
}
func markStarredCommandStatus() -> MarkCommandValidationStatus {
return MarkCommandValidationStatus.statusFor(selectedArticles) { $0.anyArticleIsUnstarred() }
}
func markReadCommandStatus() -> MarkCommandValidationStatus {
return MarkCommandValidationStatus.statusFor(selectedArticles) { $0.anyArticleIsUnread() }
}
func markOlderArticlesAsRead() {
// Mark articles the same age or older than the selected article(s) as read.
var cutoffDate: Date? = nil
for article in selectedArticles {
if cutoffDate == nil {
cutoffDate = article.logicalDatePublished
}
else if cutoffDate! < article.logicalDatePublished {
cutoffDate = article.logicalDatePublished
}
}
if cutoffDate == nil {
return
}
let articlesToMark = articles.filter { $0.logicalDatePublished <= cutoffDate! }
if articlesToMark.isEmpty {
return
}
guard let undoManager = undoManager, let markReadCommand = MarkStatusCommand(initialArticles: articlesToMark, markingRead: true, undoManager: undoManager) else {
return
}
runCommand(markReadCommand)
}
func canMarkOlderArticlesAsRead() -> Bool {
return !selectedArticles.isEmpty
}
// MARK: - Navigation
2017-05-27 19:43:27 +02:00
func goToNextUnread() {
guard let ix = indexOfNextUnreadArticle() else {
return
}
tableView.rs_selectRow(ix)
tableView.scrollTo(row: ix)
}
func canGoToNextUnread() -> Bool {
guard let _ = indexOfNextUnreadArticle() else {
return false
}
return true
}
func indexOfNextUnreadArticle() -> Int? {
2017-11-02 06:40:28 +01:00
return articles.rowOfNextUnreadArticle(tableView.selectedRow)
2017-05-27 19:43:27 +02:00
}
func focus() {
guard let window = tableView.window else {
return
}
window.makeFirstResponderUnlessDescendantIsFirstResponder(tableView)
if !hasAtLeastOneSelectedArticle && articles.count > 0 {
tableView.rs_selectRowAndScrollToVisible(0)
}
}
// MARK: - Notifications
2017-05-27 19:43:27 +02:00
@objc func sidebarSelectionDidChange(_ notification: Notification) {
2017-05-27 19:43:27 +02:00
guard let userInfo = notification.userInfo else {
return
}
guard let sidebarView = userInfo[UserInfoKey.view] as? NSView, sidebarView.window === tableView.window else {
return
}
2017-05-27 19:43:27 +02:00
if let objects = userInfo[UserInfoKey.objects] as? [AnyObject] {
representedObjects = objects
}
else {
representedObjects = nil
2017-05-27 19:43:27 +02:00
}
}
@objc func statusesDidChange(_ note: Notification) {
2017-09-24 21:24:44 +02:00
guard let articles = note.userInfo?[Account.UserInfoKey.articles] as? Set<Article> else {
2017-05-27 19:43:27 +02:00
return
}
reloadCellsForArticleIDs(articles.articleIDs())
2017-05-27 19:43:27 +02:00
}
@objc func feedIconDidBecomeAvailable(_ note: Notification) {
guard let feed = note.userInfo?[UserInfoKey.feed] as? Feed else {
return
}
let articlesToReload = articles.filter { (article) -> Bool in
return feed == article.feed
}
reloadCellsForArticles(articlesToReload)
}
@objc func avatarDidBecomeAvailable(_ note: Notification) {
guard showAvatars, let avatarURL = note.userInfo?[UserInfoKey.url] as? String else {
return
}
let indexesToReload = tableView.indexesOfAvailableRowsPassingTest { (row) -> Bool in
guard let article = articles.articleAtRow(row), let authors = article.authors, !authors.isEmpty else {
return false
}
for author in authors {
if author.avatarURL == avatarURL {
return true
}
}
return false
}
if let indexesToReload = indexesToReload {
reloadCells(for: indexesToReload)
}
}
@objc func imageDidBecomeAvailable(_ note: Notification) {
if showAvatars {
queueReloadAvailableCells()
}
}
@objc func accountDidDownloadArticles(_ note: Notification) {
guard let feeds = note.userInfo?[Account.UserInfoKey.feeds] as? Set<Feed> else {
return
}
let shouldFetchAndMergeArticles = representedObjectsContainsAnyFeed(feeds)
if shouldFetchAndMergeArticles {
queueFetchAndMergeArticles()
}
}
@objc func userDefaultsDidChange(_ note: Notification) {
self.fontSize = AppDefaults.shared.timelineFontSize
self.sortDirection = AppDefaults.shared.timelineSortDirection
}
// MARK: - Reloading Data
2017-05-27 19:43:27 +02:00
private func cellForRowView(_ rowView: NSView) -> NSView? {
for oneView in rowView.subviews where oneView is TimelineTableCellView {
return oneView
}
return nil
}
private func reloadCellsForArticles(_ articles: [Article]) {
2017-05-27 19:43:27 +02:00
reloadCellsForArticleIDs(Set(articles.articleIDs()))
}
private func reloadCellsForArticleIDs(_ articleIDs: Set<String>) {
if articleIDs.isEmpty {
return
}
2017-11-02 06:40:28 +01:00
let indexes = articles.indexesForArticleIDs(articleIDs)
reloadCells(for: indexes)
}
private func reloadCells(for indexes: IndexSet) {
if indexes.isEmpty {
return
}
2017-05-27 19:43:27 +02:00
tableView.reloadData(forRowIndexes: indexes, columnIndexes: NSIndexSet(index: 0) as IndexSet)
}
2017-11-02 06:40:28 +01:00
// MARK: - Cell Configuring
2017-05-27 19:43:27 +02:00
private func calculateRowHeight(showingFeedNames: Bool) -> CGFloat {
2017-11-02 06:40:28 +01:00
let longTitle = "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?"
let prototypeID = "prototype"
let status = ArticleStatus(articleID: prototypeID, read: false, starred: false, userDeleted: false, dateArrived: Date())
2017-12-19 03:20:13 +01:00
let prototypeArticle = Article(accountID: prototypeID, articleID: prototypeID, feedID: prototypeID, uniqueID: prototypeID, title: longTitle, contentHTML: nil, contentText: nil, url: nil, externalURL: nil, summary: nil, imageURL: nil, bannerImageURL: nil, datePublished: nil, dateModified: nil, authors: nil, attachments: nil, status: status)
2017-05-27 19:43:27 +02:00
let prototypeCellData = TimelineCellData(article: prototypeArticle, appearance: cellAppearance, showFeedName: showingFeedNames, feedName: "Prototype Feed Name", avatar: nil, showAvatar: false, featuredImage: nil)
let height = TimelineCellLayout.height(for: 100, cellData: prototypeCellData, appearance: cellAppearance)
2017-11-02 06:40:28 +01:00
return height
2017-05-27 19:43:27 +02:00
}
private func updateRowHeights() {
rowHeightWithFeedName = calculateRowHeight(showingFeedNames: true)
rowHeightWithoutFeedName = calculateRowHeight(showingFeedNames: false)
updateTableViewRowHeight()
}
@objc func fetchAndMergeArticles() {
guard let representedObjects = representedObjects else {
return
}
performBlockAndRestoreSelection {
var unsortedArticles = fetchUnsortedArticles(for: representedObjects)
// Merge articles by articleID. For any unique articleID in current articles, add to unsortedArticles.
let unsortedArticleIDs = unsortedArticles.articleIDs()
for article in articles {
if !unsortedArticleIDs.contains(article.articleID) {
unsortedArticles.insert(article)
}
}
updateArticles(with: unsortedArticles)
}
}
2017-11-02 06:40:28 +01:00
}
2017-05-27 19:43:27 +02:00
// MARK: NSUserInterfaceValidations
extension TimelineViewController: NSUserInterfaceValidations {
func validateUserInterfaceItem(_ item: NSValidatedUserInterfaceItem) -> Bool {
if item.action == #selector(copy(_:)) {
return NSPasteboard.general.canCopyAtLeastOneObject(selectedArticles)
}
return true
}
}
// MARK: - NSTableViewDelegate
extension TimelineViewController: NSTableViewDelegate {
func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
let rowView: TimelineTableRowView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "timelineRow"), owner: self) as! TimelineTableRowView
rowView.cellAppearance = showAvatars ? cellAppearanceWithAvatar: cellAppearance
return rowView
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
let cell: TimelineTableCellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "timelineCell"), owner: self) as! TimelineTableCellView
cell.cellAppearance = showAvatars ? cellAppearanceWithAvatar: cellAppearance
if let article = articles.articleAtRow(row) {
configureTimelineCell(cell, article: article)
}
else {
makeTimelineCellEmpty(cell)
}
return cell
}
func tableViewSelectionDidChange(_ notification: Notification) {
tableView.redrawGrid()
if selectedArticles.isEmpty {
postTimelineSelectionDidChangeNotification(nil)
return
}
if selectedArticles.count == 1 {
let article = selectedArticles.first!
if !article.status.read {
markArticles(Set([article]), statusKey: .read, flag: true)
}
}
postTimelineSelectionDidChangeNotification(selectedArticles)
}
private func postTimelineSelectionDidChangeNotification(_ selectedArticles: ArticleArray?) {
var userInfo = UserInfoDictionary()
if let selectedArticles = selectedArticles {
userInfo[UserInfoKey.articles] = selectedArticles
}
userInfo[UserInfoKey.view] = tableView
NotificationCenter.default.post(name: .TimelineSelectionDidChange, object: self, userInfo: userInfo)
}
private func configureTimelineCell(_ cell: TimelineTableCellView, article: Article) {
cell.objectValue = article
// let favicon = showFeedNames ? article.feed?.smallIcon : nil
var avatar = avatarFor(article)
if avatar == nil, let feed = article.feed {
avatar = appDelegate.faviconDownloader.favicon(for: feed)
}
let featuredImage = featuredImageFor(article)
cell.cellData = TimelineCellData(article: article, appearance: cellAppearance, showFeedName: showFeedNames, feedName: article.feed?.nameForDisplay, avatar: avatar, showAvatar: showAvatars, featuredImage: featuredImage)
}
private func avatarFor(_ article: Article) -> NSImage? {
if !showAvatars {
return nil
}
if let authors = article.authors {
for author in authors {
if let image = avatarForAuthor(author) {
return image
}
}
}
guard let feed = article.feed else {
return nil
}
2017-11-27 04:57:45 +01:00
return appDelegate.feedIconDownloader.icon(for: feed)
}
private func avatarForAuthor(_ author: Author) -> NSImage? {
2017-11-26 22:16:32 +01:00
return appDelegate.authorAvatarDownloader.image(for: author)
}
private func featuredImageFor(_ article: Article) -> NSImage? {
if let url = article.imageURL {
return appDelegate.imageDownloader.image(for: url)
}
return nil
}
private func makeTimelineCellEmpty(_ cell: TimelineTableCellView) {
cell.objectValue = nil
cell.cellData = emptyCellData
}
}
// MARK: - Private
2017-11-02 06:40:28 +01:00
private extension TimelineViewController {
@objc func reloadAvailableCells() {
if let indexesToReload = tableView.indexesOfAvailableRows() {
reloadCells(for: indexesToReload)
}
}
func queueReloadAvailableCells() {
CoalescingQueue.standard.add(self, #selector(reloadAvailableCells))
}
func updateTableViewRowHeight() {
tableView.rowHeight = currentRowHeight
}
func updateShowAvatars() {
if showFeedNames {
self.showAvatars = true
return
}
for article in articles {
if let authors = article.authors {
for author in authors {
if author.avatarURL != nil {
self.showAvatars = true
return
}
}
}
}
self.showAvatars = false
}
2017-11-02 06:40:28 +01:00
func emptyTheTimeline() {
2017-05-27 19:43:27 +02:00
if !articles.isEmpty {
articles = [Article]()
}
}
2017-11-02 06:40:28 +01:00
func sortDirectionDidChange() {
performBlockAndRestoreSelection {
let unsortedArticles = Set(articles)
updateArticles(with: unsortedArticles)
}
}
func selectedArticleIDs() -> [String] {
return selectedArticles.articleIDs()
}
func restoreSelection(_ articleIDs: [String]) {
selectArticles(articleIDs)
if tableView.selectedRow != -1 {
tableView.scrollRowToVisible(tableView.selectedRow)
}
}
func performBlockAndRestoreSelection(_ block: (() -> Void)) {
let savedSelection = selectedArticleIDs()
block()
restoreSelection(savedSelection)
}
2017-11-02 06:40:28 +01:00
// MARK: Fetching Articles
func fetchArticles() {
2017-05-27 19:43:27 +02:00
guard let representedObjects = representedObjects else {
emptyTheTimeline()
return
}
2017-11-02 06:40:28 +01:00
let fetchedArticles = fetchUnsortedArticles(for: representedObjects)
updateArticles(with: fetchedArticles)
}
func updateArticles(with unsortedArticles: Set<Article>) {
let sortedArticles = Array(unsortedArticles).sortedByDate(sortDirection)
if articles != sortedArticles {
articles = sortedArticles
}
}
func fetchUnsortedArticles(for representedObjects: [Any]) -> Set<Article> {
var fetchedArticles = Set<Article>()
2017-11-02 06:40:28 +01:00
for object in representedObjects {
2017-11-02 06:40:28 +01:00
if let articleFetcher = object as? ArticleFetcher {
fetchedArticles.formUnion(articleFetcher.fetchArticles())
}
}
2017-11-02 06:40:28 +01:00
return fetchedArticles
2017-05-27 19:43:27 +02:00
}
func selectArticles(_ articleIDs: [String]) {
let indexesToSelect = articles.indexesForArticleIDs(Set(articleIDs))
if indexesToSelect.isEmpty {
tableView.deselectAll(self)
return
}
tableView.selectRowIndexes(indexesToSelect, byExtendingSelection: false)
}
func queueFetchAndMergeArticles() {
TimelineViewController.fetchAndMergeArticlesQueue.add(self, #selector(fetchAndMergeArticles))
}
func representedObjectArraysAreEqual(_ objects1: [AnyObject]?, _ objects2: [AnyObject]?) -> Bool {
2017-05-27 19:43:27 +02:00
if objects1 == nil && objects2 == nil {
return true
}
guard let objects1 = objects1, let objects2 = objects2 else {
return false
}
if objects1.count != objects2.count {
return false
}
var ix = 0
for oneObject in objects1 {
if oneObject !== objects2[ix] {
2017-05-27 19:43:27 +02:00
return false
}
ix += 1
}
return true
}
func representedObjectsContainsAnyFeed(_ feeds: Set<Feed>) -> Bool {
// Return true if theres a match or if a folder contains (recursively) one of feeds
guard let representedObjects = representedObjects else {
return false
}
for representedObject in representedObjects {
if let feed = representedObject as? Feed {
for oneFeed in feeds {
if feed.feedID == oneFeed.feedID || feed.url == oneFeed.url {
return true
}
}
}
else if let folder = representedObject as? Folder {
for oneFeed in feeds {
if folder.hasFeed(with: oneFeed.feedID) || folder.hasFeed(withURL: oneFeed.url) {
return true
}
}
}
}
return false
}
2017-05-27 19:43:27 +02:00
}