2017-05-27 19:43:27 +02:00
//
// T i m e l i n e V i e w C o n t r o l l e r . s w i f t
// E v e r g r e e n
//
// C r e a t e d b y B r e n t S i m m o n s o n 7 / 2 6 / 1 5 .
// C o p y r i g h t © 2 0 1 5 R a n c h e r o S o f t w a r e , L L C . A l l r i g h t s r e s e r v e d .
//
import Foundation
import RSCore
import RSTextDrawing
2017-09-17 21:22:15 +02:00
import Data
2017-09-17 21:34:10 +02:00
import Account
2017-05-27 19:43:27 +02:00
2017-12-20 21:59:31 +01:00
class TimelineViewController : NSViewController , UndoableCommandRunner {
2017-05-27 19:43:27 +02:00
@IBOutlet var tableView : TimelineTableView !
2017-11-02 06:40:28 +01:00
var selectedArticles : [ Article ] {
2017-11-01 04:33:41 +01:00
get {
2017-11-02 06:40:28 +01:00
return Array ( articles . articlesForIndexes ( tableView . selectedRowIndexes ) )
2017-11-01 04:33:41 +01:00
}
}
2017-11-02 06:40:28 +01:00
2017-12-20 21:59:31 +01:00
var hasAtLeastOneSelectedArticle : Bool {
get {
return tableView . selectedRow != - 1
}
}
2017-11-05 06:51:14 +01:00
var undoableCommands = [ UndoableCommand ] ( )
2017-11-04 18:35:34 +01:00
private var cellAppearance : TimelineCellAppearance !
2018-01-05 06:20:09 +01:00
private var cellAppearanceWithAvatar : TimelineCellAppearance !
2017-12-30 21:45:10 +01:00
private var showFeedNames = false {
didSet {
if showFeedNames != oldValue {
2018-01-05 06:20:09 +01:00
updateShowAvatars ( )
2018-02-08 06:46:54 +01:00
updateTableViewRowHeight ( )
2017-12-30 21:45:10 +01:00
}
}
}
2018-01-05 06:20:09 +01:00
private var showAvatars = false
2017-12-30 21:45:10 +01:00
private var rowHeightWithFeedName : CGFloat = 0.0
private var rowHeightWithoutFeedName : CGFloat = 0.0
private var currentRowHeight : CGFloat {
return showFeedNames ? rowHeightWithFeedName : rowHeightWithoutFeedName
}
2017-11-04 18:35:34 +01:00
private var didRegisterForNotifications = false
2018-01-09 06:34:39 +01:00
private var reloadAvailableCellsTimer : Timer ?
2018-01-18 02:28:09 +01:00
private var fetchAndMergeArticlesTimer : Timer ?
2017-11-02 06:40:28 +01:00
2018-01-28 20:33:45 +01:00
private var sortDirection = AppDefaults . shared . timelineSortDirection {
didSet {
if sortDirection != oldValue {
sortDirectionDidChange ( )
}
}
}
2017-11-04 18:35:34 +01:00
private var articles = ArticleArray ( ) {
didSet {
if articles != oldValue {
clearUndoableCommands ( )
2018-01-05 06:20:09 +01:00
updateShowAvatars ( )
2017-11-04 18:35:34 +01:00
tableView . reloadData ( )
}
}
}
2017-11-02 06:40:28 +01:00
private var fontSize : FontSize = AppDefaults . shared . timelineFontSize {
2017-05-27 19:43:27 +02:00
didSet {
2018-01-28 20:33:45 +01:00
if fontSize != oldValue {
fontSizeDidChange ( )
}
2017-05-27 19:43:27 +02:00
}
}
2017-11-05 07:05:20 +01:00
private var representedObjects : [ AnyObject ] ? {
2017-05-27 19:43:27 +02:00
didSet {
if ! representedObjectArraysAreEqual ( oldValue , representedObjects ) {
2017-12-30 19:56:58 +01:00
if let representedObjects = representedObjects {
if representedObjects . count = = 1 && representedObjects . first is Feed {
showFeedNames = false
}
else {
showFeedNames = true
}
}
else {
showFeedNames = false
}
2017-11-06 05:59:30 +01:00
postTimelineSelectionDidChangeNotification ( nil )
2017-12-31 21:10:30 +01:00
articles = ArticleArray ( )
2017-05-27 19:43:27 +02:00
fetchArticles ( )
if articles . count > 0 {
tableView . scrollRowToVisible ( 0 )
}
}
}
}
private var oneSelectedArticle : Article ? {
get {
return selectedArticles . count = = 1 ? selectedArticles . first : nil
}
}
override func viewDidLoad ( ) {
2018-01-05 06:20:09 +01:00
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
2017-12-30 21:45:10 +01:00
updateRowHeights ( )
tableView . rowHeight = currentRowHeight
2017-05-27 19:43:27 +02:00
tableView . target = self
tableView . doubleAction = #selector ( openArticleInBrowser ( _ : ) )
2017-11-07 07:06:42 +01:00
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 )
2017-10-09 06:06:25 +02:00
NotificationCenter . default . addObserver ( self , selector : #selector ( statusesDidChange ( _ : ) ) , name : . StatusesDidChange , object : nil )
2018-01-05 06:20:09 +01:00
NotificationCenter . default . addObserver ( self , selector : #selector ( feedIconDidBecomeAvailable ( _ : ) ) , name : . FeedIconDidBecomeAvailable , object : nil )
NotificationCenter . default . addObserver ( self , selector : #selector ( avatarDidBecomeAvailable ( _ : ) ) , name : . AvatarDidBecomeAvailable , object : nil )
2018-01-09 06:34:39 +01:00
NotificationCenter . default . addObserver ( self , selector : #selector ( imageDidBecomeAvailable ( _ : ) ) , name : . ImageDidBecomeAvailable , object : nil )
2018-01-10 06:09:09 +01:00
NotificationCenter . default . addObserver ( self , selector : #selector ( imageDidBecomeAvailable ( _ : ) ) , name : . FaviconDidBecomeAvailable , object : nil )
2018-01-18 02:28:09 +01:00
NotificationCenter . default . addObserver ( self , selector : #selector ( accountDidDownloadArticles ( _ : ) ) , name : . AccountDidDownloadArticles , object : nil )
2018-01-28 20:33:45 +01:00
NotificationCenter . default . addObserver ( self , selector : #selector ( userDefaultsDidChange ( _ : ) ) , name : UserDefaults . didChangeNotification , object : nil )
2017-05-27 19:43:27 +02:00
2018-01-28 20:33:45 +01:00
didRegisterForNotifications = true
2017-05-27 19:43:27 +02:00
}
}
// MARK: A p p e a r a n c e C h a n g e
private func fontSizeDidChange ( ) {
2018-01-28 20:33:45 +01:00
TimelineCellData . emptyCache ( )
RSSingleLineRenderer . emptyCache ( )
RSMultiLineRenderer . emptyCache ( )
2018-01-05 06:20:09 +01:00
cellAppearance = TimelineCellAppearance ( theme : appDelegate . currentTheme , showAvatar : false , fontSize : fontSize )
cellAppearanceWithAvatar = TimelineCellAppearance ( theme : appDelegate . currentTheme , showAvatar : true , fontSize : fontSize )
2017-12-30 21:45:10 +01:00
updateRowHeights ( )
2018-01-28 20:33:45 +01:00
tableView . reloadData ( )
2017-05-27 19:43:27 +02:00
}
2017-10-09 06:06:25 +02:00
// MARK: - A P I
2017-05-27 19:43:27 +02:00
func markAllAsRead ( ) {
2017-10-29 19:14:10 +01:00
2017-10-29 20:09:56 +01:00
guard let undoManager = undoManager , let markReadCommand = MarkReadOrUnreadCommand ( initialArticles : articles , markingRead : true , undoManager : undoManager ) else {
2017-05-27 19:43:27 +02:00
return
}
2017-10-29 20:09:56 +01:00
runCommand ( markReadCommand )
2017-10-29 19:44:35 +01:00
}
2017-11-04 18:39:47 +01:00
func canMarkAllAsRead ( ) -> Bool {
2017-10-29 19:28:45 +01:00
2017-11-04 18:39:47 +01:00
return articles . canMarkAllAsRead ( )
2017-10-29 19:28:45 +01:00
}
2017-11-06 06:27:34 +01:00
func canMarkSelectedArticlesAsRead ( ) -> Bool {
return selectedArticles . canMarkAllAsRead ( )
}
2017-10-29 19:44:35 +01:00
// MARK: - A c t i o n s
2017-09-17 21:54:08 +02:00
@objc func openArticleInBrowser ( _ sender : AnyObject ) {
2017-05-27 19:43:27 +02:00
2017-09-18 01:30:45 +02:00
if let link = oneSelectedArticle ? . preferredLink {
2017-10-06 03:12:58 +02:00
Browser . open ( link )
2017-05-27 19:43:27 +02:00
}
}
@IBAction func toggleStatusOfSelectedArticles ( _ sender : AnyObject ) {
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
2017-10-29 20:09:56 +01:00
if markAsRead {
markSelectedArticlesAsRead ( sender )
}
else {
markSelectedArticlesAsUnread ( sender )
}
2017-05-27 19:43:27 +02:00
}
2017-12-20 21:59:31 +01:00
@IBAction func markSelectedArticlesAsRead ( _ sender : Any ? ) {
2017-10-29 20:09:56 +01:00
guard let undoManager = undoManager , let markReadCommand = MarkReadOrUnreadCommand ( initialArticles : selectedArticles , markingRead : true , undoManager : undoManager ) else {
return
}
runCommand ( markReadCommand )
2017-05-27 19:43:27 +02:00
}
2017-12-20 21:59:31 +01:00
@IBAction func markSelectedArticlesAsUnread ( _ sender : Any ? ) {
2017-05-27 19:43:27 +02:00
2017-10-29 20:09:56 +01:00
guard let undoManager = undoManager , let markUnreadCommand = MarkReadOrUnreadCommand ( initialArticles : selectedArticles , markingRead : false , undoManager : undoManager ) else {
return
}
runCommand ( markUnreadCommand )
2017-05-27 19:43:27 +02:00
}
2017-12-25 19:40:06 +01:00
2017-12-25 21:21:44 +01:00
func markOlderArticlesAsRead ( ) {
2017-12-25 19:40:06 +01:00
// M a r k a r t i c l e s t h e s a m e a g e o r o l d e r t h a n t h e s e l e c t e d a r t i c l e ( s ) a s r e a d .
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 = MarkReadOrUnreadCommand ( initialArticles : articlesToMark , markingRead : true , undoManager : undoManager ) else {
return
}
runCommand ( markReadCommand )
}
2017-12-25 21:21:44 +01:00
func canMarkOlderArticlesAsRead ( ) -> Bool {
return ! selectedArticles . isEmpty
}
2017-10-09 06:06:25 +02:00
// MARK: - N a v i g a t i o n
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
}
2017-12-20 00:24:38 +01:00
func focus ( ) {
guard let window = tableView . window else {
return
}
window . makeFirstResponderUnlessDescendantIsFirstResponder ( tableView )
if ! hasAtLeastOneSelectedArticle && articles . count > 0 {
tableView . rs_selectRowAndScrollToVisible ( 0 )
}
}
2017-10-09 06:06:25 +02:00
// MARK: - N o t i f i c a t i o n s
2017-05-27 19:43:27 +02:00
2017-12-18 21:34:07 +01:00
@objc func sidebarSelectionDidChange ( _ notification : Notification ) {
2017-05-27 19:43:27 +02:00
2017-12-18 21:34:07 +01: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
2017-12-18 21:34:07 +01:00
if let objects = userInfo [ UserInfoKey . objects ] as ? [ AnyObject ] {
representedObjects = objects
}
else {
representedObjects = nil
2017-05-27 19:43:27 +02:00
}
}
2017-10-09 06:06:25 +02:00
@objc func statusesDidChange ( _ note : Notification ) {
2017-09-24 21:24:44 +02:00
2017-10-10 06:54:08 +02:00
guard let articles = note . userInfo ? [ Account . UserInfoKey . articles ] as ? Set < Article > else {
2017-05-27 19:43:27 +02:00
return
}
2017-10-10 06:54:08 +02:00
reloadCellsForArticleIDs ( articles . articleIDs ( ) )
2017-05-27 19:43:27 +02:00
}
2018-01-05 06:20:09 +01: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 ) {
2018-01-06 20:18:03 +01:00
guard showAvatars , let avatarURL = note . userInfo ? [ UserInfoKey . url ] as ? String else {
2018-01-05 06:20:09 +01:00
return
}
2018-01-06 20:18:03 +01:00
2018-01-06 21:12:31 +01:00
let indexesToReload = tableView . indexesOfAvailableRowsPassingTest { ( row ) -> Bool in
2018-01-06 20:18:03 +01:00
guard let article = articles . articleAtRow ( row ) , let authors = article . authors , ! authors . isEmpty else {
2018-01-06 21:12:31 +01:00
return false
2018-01-05 06:20:09 +01:00
}
2018-01-06 20:18:03 +01:00
for author in authors {
if author . avatarURL = = avatarURL {
2018-01-06 21:12:31 +01:00
return true
2018-01-05 06:20:09 +01:00
}
}
2018-01-06 21:12:31 +01:00
return false
}
if let indexesToReload = indexesToReload {
reloadCells ( for : indexesToReload )
2018-01-05 06:20:09 +01:00
}
}
2018-01-09 06:34:39 +01:00
@objc func imageDidBecomeAvailable ( _ note : Notification ) {
2018-01-10 06:09:09 +01:00
if showAvatars {
queueReloadAvailableCells ( )
}
2018-01-09 06:34:39 +01:00
}
2018-01-18 02:28:09 +01:00
@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 ( )
}
}
2018-01-28 20:33:45 +01:00
@objc func userDefaultsDidChange ( _ note : Notification ) {
self . fontSize = AppDefaults . shared . timelineFontSize
self . sortDirection = AppDefaults . shared . timelineSortDirection
}
2017-10-09 06:06:25 +02:00
// MARK: - R e l o a d i n g D a t a
2018-01-06 20:56:32 +01:00
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
}
2017-09-25 22:31:36 +02:00
private func reloadCellsForArticles ( _ articles : [ Article ] ) {
2017-05-27 19:43:27 +02:00
2017-10-09 06:06:25 +02:00
reloadCellsForArticleIDs ( Set ( articles . articleIDs ( ) ) )
}
private func reloadCellsForArticleIDs ( _ articleIDs : Set < String > ) {
2018-01-06 20:18:03 +01:00
if articleIDs . isEmpty {
return
}
2017-11-02 06:40:28 +01:00
let indexes = articles . indexesForArticleIDs ( articleIDs )
2018-01-06 20:56:32 +01:00
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: - C e l l C o n f i g u r i n g
2017-05-27 19:43:27 +02:00
2017-12-30 21:45:10 +01:00
private func calculateRowHeight ( showingFeedNames : Bool ) -> CGFloat {
2017-10-09 06:06:25 +02:00
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
2018-01-05 06:20:09 +01:00
let prototypeCellData = TimelineCellData ( article : prototypeArticle , appearance : cellAppearance , showFeedName : showingFeedNames , feedName : " Prototype Feed Name " , avatar : nil , showAvatar : false , featuredImage : nil )
2017-11-02 06:40:28 +01:00
let height = timelineCellHeight ( 100 , cellData : prototypeCellData , appearance : cellAppearance )
return height
2017-05-27 19:43:27 +02:00
}
2017-12-30 21:45:10 +01:00
private func updateRowHeights ( ) {
rowHeightWithFeedName = calculateRowHeight ( showingFeedNames : true )
rowHeightWithoutFeedName = calculateRowHeight ( showingFeedNames : false )
2018-02-08 06:46:54 +01:00
updateTableViewRowHeight ( )
2017-12-30 21:45:10 +01:00
}
2017-11-02 06:40:28 +01:00
}
2017-05-27 19:43:27 +02:00
2017-11-04 18:35:34 +01:00
// MARK: - N S T a b l e V i e w D a t a S o u r c e
extension TimelineViewController : NSTableViewDataSource {
func numberOfRows ( in tableView : NSTableView ) -> Int {
return articles . count
}
func tableView ( _ tableView : NSTableView , objectValueFor tableColumn : NSTableColumn ? , row : Int ) -> Any ? {
return articles . articleAtRow ( row ) ? ? nil
}
2017-11-06 22:20:29 +01:00
func tableView ( _ tableView : NSTableView , pasteboardWriterForRow row : Int ) -> NSPasteboardWriting ? {
guard let article = articles . articleAtRow ( row ) else {
return nil
}
return ArticlePasteboardWriter ( article : article )
}
2017-11-04 18:35:34 +01:00
}
// MARK: - N S T a b l e V i e w D e l e g a t e
extension TimelineViewController : NSTableViewDelegate {
func tableView ( _ tableView : NSTableView , rowViewForRow row : Int ) -> NSTableRowView ? {
let rowView : TimelineTableRowView = tableView . makeView ( withIdentifier : NSUserInterfaceItemIdentifier ( rawValue : " timelineRow " ) , owner : self ) as ! TimelineTableRowView
2018-01-05 06:20:09 +01:00
rowView . cellAppearance = showAvatars ? cellAppearanceWithAvatar : cellAppearance
2017-11-04 18:35:34 +01:00
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
2018-01-05 06:20:09 +01:00
cell . cellAppearance = showAvatars ? cellAppearanceWithAvatar : cellAppearance
2017-11-04 18:35:34 +01:00
if let article = articles . articleAtRow ( row ) {
configureTimelineCell ( cell , article : article )
}
else {
makeTimelineCellEmpty ( cell )
}
return cell
}
func tableViewSelectionDidChange ( _ notification : Notification ) {
tableView . redrawGrid ( )
let selectedRow = tableView . selectedRow
if selectedRow < 0 || selectedRow = = NSNotFound || tableView . numberOfSelectedRows != 1 {
postTimelineSelectionDidChangeNotification ( nil )
return
}
if let selectedArticle = articles . articleAtRow ( selectedRow ) {
if ( ! selectedArticle . status . read ) {
markArticles ( Set ( [ selectedArticle ] ) , statusKey : . read , flag : true )
}
postTimelineSelectionDidChangeNotification ( selectedArticle )
}
else {
postTimelineSelectionDidChangeNotification ( nil )
}
}
private func postTimelineSelectionDidChangeNotification ( _ selectedArticle : Article ? ) {
2017-12-18 21:34:07 +01:00
var userInfo = UserInfoDictionary ( )
2017-11-04 18:35:34 +01:00
if let article = selectedArticle {
2017-12-18 21:34:07 +01:00
userInfo [ UserInfoKey . article ] = article
2017-11-04 18:35:34 +01:00
}
2017-12-18 21:34:07 +01:00
userInfo [ UserInfoKey . view ] = tableView
2017-11-04 18:35:34 +01:00
2017-12-18 21:34:07 +01:00
NotificationCenter . default . post ( name : . TimelineSelectionDidChange , object : self , userInfo : userInfo )
2017-11-04 18:35:34 +01:00
}
private func configureTimelineCell ( _ cell : TimelineTableCellView , article : Article ) {
cell . objectValue = article
2017-11-26 06:27:35 +01:00
2018-01-05 06:20:09 +01:00
// l e t f a v i c o n = s h o w F e e d N a m e s ? a r t i c l e . f e e d ? . s m a l l I c o n : n i l
var avatar = avatarFor ( article )
if avatar = = nil , let feed = article . feed {
avatar = appDelegate . faviconDownloader . favicon ( for : feed )
}
2017-11-26 06:27:35 +01:00
let featuredImage = featuredImageFor ( article )
2018-01-05 06:20:09 +01:00
cell . cellData = TimelineCellData ( article : article , appearance : cellAppearance , showFeedName : showFeedNames , feedName : article . feed ? . nameForDisplay , avatar : avatar , showAvatar : showAvatars , featuredImage : featuredImage )
2017-11-26 06:27:35 +01:00
}
private func avatarFor ( _ article : Article ) -> NSImage ? {
2018-01-05 06:20:09 +01:00
if ! showAvatars {
return nil
}
2017-11-26 06:27:35 +01:00
if let authors = article . authors {
for author in authors {
if let image = avatarForAuthor ( author ) {
return image
}
}
}
guard let feed = article . feed else {
return nil
}
// TODO: m a k e F e e d k n o w a b o u t i t s a u t h o r s .
// h t t p s : / / g i t h u b . c o m / b r e n t s i m m o n s / E v e r g r e e n / i s s u e s / 2 1 2
2017-11-27 04:57:45 +01:00
return appDelegate . feedIconDownloader . icon ( for : feed )
2017-11-26 06:27:35 +01:00
}
private func avatarForAuthor ( _ author : Author ) -> NSImage ? {
2017-11-26 22:16:32 +01:00
return appDelegate . authorAvatarDownloader . image ( for : author )
2017-11-26 06:27:35 +01:00
}
private func featuredImageFor ( _ article : Article ) -> NSImage ? {
if let url = article . imageURL {
return appDelegate . imageDownloader . image ( for : url )
}
return nil
2017-11-04 18:35:34 +01:00
}
private func makeTimelineCellEmpty ( _ cell : TimelineTableCellView ) {
cell . objectValue = nil
cell . cellData = emptyCellData
}
}
// MARK: - P r i v a t e
2017-11-02 06:40:28 +01:00
private extension TimelineViewController {
2018-01-05 06:20:09 +01:00
2018-01-09 06:34:39 +01:00
func reloadAvailableCells ( ) {
if let indexesToReload = tableView . indexesOfAvailableRows ( ) {
reloadCells ( for : indexesToReload )
}
}
func queueReloadAvailableCells ( ) {
invalidateReloadTimer ( )
reloadAvailableCellsTimer = Timer . scheduledTimer ( withTimeInterval : 0.1 , repeats : false ) { ( timer ) in
self . reloadAvailableCells ( )
self . invalidateReloadTimer ( )
}
}
func invalidateReloadTimer ( ) {
if let timer = reloadAvailableCellsTimer {
if timer . isValid {
timer . invalidate ( )
}
reloadAvailableCellsTimer = nil
}
}
2018-02-08 06:46:54 +01:00
func updateTableViewRowHeight ( ) {
tableView . rowHeight = currentRowHeight
}
2018-01-05 06:20:09 +01:00
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
2017-10-09 03:58:15 +02:00
if ! articles . isEmpty {
articles = [ Article ] ( )
}
}
2017-11-02 06:40:28 +01:00
2018-01-28 20:33:45 +01:00
func sortDirectionDidChange ( ) {
2018-02-08 06:46:54 +01:00
performBlockAndRestoreSelection {
let unsortedArticles = Set ( articles )
updateArticles ( with : unsortedArticles )
}
}
func selectedArticleIDs ( ) -> [ String ] {
return selectedArticles . articleIDs ( )
}
2018-01-28 20:45:18 +01:00
2018-02-08 06:46:54 +01:00
func restoreSelection ( _ articleIDs : [ String ] ) {
2018-01-28 20:45:18 +01:00
2018-02-08 06:46:54 +01:00
selectArticles ( articleIDs )
2018-01-28 20:45:18 +01:00
if tableView . selectedRow != - 1 {
tableView . scrollRowToVisible ( tableView . selectedRow )
}
2018-01-28 20:33:45 +01:00
}
2018-02-08 06:46:54 +01:00
func performBlockAndRestoreSelection ( _ block : ( ( ) -> Void ) ) {
let savedSelection = selectedArticleIDs ( )
block ( )
restoreSelection ( savedSelection )
}
2017-11-02 06:40:28 +01:00
// MARK: F e t c h i n g A r t i c l e s
func fetchArticles ( ) {
2017-05-27 19:43:27 +02:00
2017-10-09 03:58:15 +02:00
guard let representedObjects = representedObjects else {
emptyTheTimeline ( )
return
}
2017-11-02 06:40:28 +01:00
2018-01-18 07:03:13 +01:00
let fetchedArticles = fetchUnsortedArticles ( for : representedObjects )
2018-01-21 01:54:18 +01:00
updateArticles ( with : fetchedArticles )
}
func updateArticles ( with unsortedArticles : Set < Article > ) {
2018-01-28 20:33:45 +01:00
let sortedArticles = Array ( unsortedArticles ) . sortedByDate ( sortDirection )
2018-01-18 07:03:13 +01:00
if articles != sortedArticles {
articles = sortedArticles
}
}
func fetchUnsortedArticles ( for representedObjects : [ Any ] ) -> Set < Article > {
2017-10-09 03:58:15 +02:00
var fetchedArticles = Set < Article > ( )
2017-11-02 06:40:28 +01:00
2017-10-09 03:58:15 +02:00
for object in representedObjects {
2017-11-02 06:40:28 +01:00
2017-10-09 03:58:15 +02:00
if let feed = object as ? Feed {
fetchedArticles . formUnion ( feed . fetchArticles ( ) )
}
else if let folder = object as ? Folder {
fetchedArticles . formUnion ( folder . fetchArticles ( ) )
}
}
2017-11-02 06:40:28 +01:00
2018-01-18 07:03:13 +01:00
return fetchedArticles
2017-05-27 19:43:27 +02:00
}
2018-01-18 02:28:09 +01:00
func fetchAndMergeArticles ( ) {
2018-01-21 01:54:18 +01:00
guard let representedObjects = representedObjects else {
return
}
2018-02-08 06:46:54 +01:00
performBlockAndRestoreSelection {
var unsortedArticles = fetchUnsortedArticles ( for : representedObjects )
unsortedArticles . formUnion ( Set ( articles ) )
updateArticles ( with : unsortedArticles )
}
2018-01-18 02:28:09 +01:00
}
2018-02-08 06:46:54 +01:00
2018-01-18 02:28:09 +01:00
func selectArticles ( _ articleIDs : [ String ] ) {
2018-01-21 01:54:18 +01:00
let indexesToSelect = articles . indexesForArticleIDs ( Set ( articleIDs ) )
if indexesToSelect . isEmpty {
tableView . deselectAll ( self )
return
}
tableView . selectRowIndexes ( indexesToSelect , byExtendingSelection : false )
2018-01-18 02:28:09 +01:00
}
func invalidateFetchAndMergeArticlesTimer ( ) {
if let timer = fetchAndMergeArticlesTimer {
if timer . isValid {
timer . invalidate ( )
}
fetchAndMergeArticlesTimer = nil
}
}
func queueFetchAndMergeArticles ( ) {
invalidateFetchAndMergeArticlesTimer ( )
fetchAndMergeArticlesTimer = Timer . scheduledTimer ( withTimeInterval : 0.5 , repeats : false ) { ( timer ) in
self . fetchAndMergeArticles ( )
self . invalidateFetchAndMergeArticlesTimer ( )
}
}
2017-11-05 07:05:20 +01:00
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 {
2017-11-05 07:05:20 +01:00
if oneObject !== objects2 [ ix ] {
2017-05-27 19:43:27 +02:00
return false
}
ix += 1
}
return true
}
2018-01-18 02:28:09 +01:00
func representedObjectsContainsAnyFeed ( _ feeds : Set < Feed > ) -> Bool {
// R e t u r n t r u e i f t h e r e ’ s a m a t c h o r i f a f o l d e r c o n t a i n s ( r e c u r s i v e l y ) o n e o f f e e d s
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
}