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
2018-08-29 07:18:24 +02:00
// N e t N e w s W i r e
2017-05-27 19:43:27 +02:00
//
// 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
2018-07-24 03:29:08 +02:00
import Articles
2017-09-17 21:34:10 +02:00
import Account
2019-10-02 18:18:52 +02:00
import os . log
2017-05-27 19:43:27 +02:00
2019-02-14 06:33:50 +01:00
protocol TimelineDelegate : class {
2019-02-18 06:43:51 +01:00
func timelineSelectionDidChange ( _ : TimelineViewController , selectedArticles : [ Article ] ? )
2020-03-01 00:50:13 +01:00
func timelineRequestedWebFeedSelection ( _ : TimelineViewController , webFeed : WebFeed )
2020-03-03 19:54:37 +01:00
func timelineInvalidatedRestorationState ( _ : TimelineViewController )
2019-02-14 06:33:50 +01:00
}
2019-07-28 07:53:27 +02:00
final class TimelineViewController : NSViewController , UndoableCommandRunner , UnreadCountProvider {
2017-05-27 19:43:27 +02:00
@IBOutlet var tableView : TimelineTableView !
2018-02-17 21:21:15 +01:00
2020-03-04 03:13:43 +01:00
private var readFilterEnabledTable = [ FeedIdentifier : Bool ] ( )
2019-11-24 11:29:15 +01:00
var isReadFiltered : Bool ? {
2020-03-04 03:13:43 +01:00
guard representedObjects ? . count = = 1 , let timelineFeed = representedObjects ? . first as ? Feed else {
return nil
}
guard timelineFeed . defaultReadFilterType != . alwaysRead else {
return nil
}
if let feedID = timelineFeed . feedID , let readFilterEnabled = readFilterEnabledTable [ feedID ] {
return readFilterEnabled
} else {
return timelineFeed . defaultReadFilterType = = . read
}
2019-11-22 18:47:03 +01:00
}
2019-02-18 04:38:17 +01:00
var representedObjects : [ AnyObject ] ? {
2019-02-17 07:14:06 +01:00
didSet {
2019-02-18 04:38:17 +01:00
if ! representedObjectArraysAreEqual ( oldValue , representedObjects ) {
2019-07-28 07:53:27 +02:00
unreadCount = 0
2019-02-18 04:38:17 +01:00
if let representedObjects = representedObjects {
2019-11-15 03:11:41 +01:00
if representedObjects . count = = 1 && representedObjects . first is WebFeed {
2019-02-18 04:38:17 +01:00
showFeedNames = false
}
else {
showFeedNames = true
}
}
else {
showFeedNames = false
}
2019-02-18 23:54:38 +01:00
selectionDidChange ( nil )
2019-07-06 05:06:31 +02:00
if showsSearchResults {
fetchAndReplaceArticlesAsync ( )
}
else {
fetchAndReplaceArticlesSync ( )
if articles . count > 0 {
tableView . scrollRowToVisible ( 0 )
}
2019-09-11 05:28:16 +02:00
updateUnreadCount ( )
2019-02-18 04:38:17 +01:00
}
2019-02-17 07:22:26 +01:00
}
2019-02-17 07:14:06 +01:00
}
}
2020-03-01 00:50:13 +01:00
weak var delegate : TimelineDelegate ?
2018-09-26 05:20:59 +02:00
var sharingServiceDelegate : NSSharingServiceDelegate ?
2019-07-06 05:06:31 +02:00
var showsSearchResults = false
2017-11-02 06:40:28 +01:00
var selectedArticles : [ Article ] {
2018-02-14 22:14:25 +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 {
2018-02-14 22:14:25 +01:00
return tableView . selectedRow != - 1
2017-12-20 21:59:31 +01:00
}
2018-02-10 08:16:12 +01:00
var articles = ArticleArray ( ) {
didSet {
2019-08-21 22:23:46 +02:00
defer {
updateUnreadCount ( )
}
2019-02-09 04:53:41 +01:00
if articles = = oldValue {
return
}
if articles . representSameArticlesInSameOrder ( as : oldValue ) {
// W h e n t h e a r r a y i s t h e s a m e — s a m e a r t i c l e s , s a m e o r d e r —
// b u t s o m e d a t a i n s o m e o f t h e a r t i c l e s m a y h a v e c h a n g e d .
// J u s t r e l o a d v i s i b l e c e l l s i n t h i s c a s e : d o n ’ t c a l l r e l o a d D a t a .
2018-09-26 22:23:21 +02:00
articleRowMap = [ String : Int ] ( )
2019-02-09 04:53:41 +01:00
reloadVisibleCells ( )
return
2018-02-10 08:16:12 +01:00
}
2019-11-06 01:05:57 +01:00
updateShowIcons ( )
2019-02-09 04:53:41 +01:00
articleRowMap = [ String : Int ] ( )
tableView . reloadData ( )
2019-07-28 07:53:27 +02:00
}
}
var unreadCount : Int = 0 {
didSet {
if unreadCount != oldValue {
postUnreadCountDidChangeNotification ( )
}
2018-02-10 08:16:12 +01:00
}
}
2017-11-05 06:51:14 +01:00
var undoableCommands = [ UndoableCommand ] ( )
2019-07-06 05:06:31 +02:00
private var fetchSerialNumber = 0
private let fetchRequestQueue = FetchRequestQueue ( )
2020-03-01 02:05:56 +01:00
private var exceptionArticleFetcher : ArticleFetcher ?
2018-09-26 22:23:21 +02:00
private var articleRowMap = [ String : Int ] ( ) // a r t i c l e I D : r o w I n d e x
2017-11-04 18:35:34 +01:00
private var cellAppearance : TimelineCellAppearance !
2019-11-06 01:05:57 +01:00
private var cellAppearanceWithIcon : TimelineCellAppearance !
2017-12-30 21:45:10 +01:00
private var showFeedNames = false {
didSet {
if showFeedNames != oldValue {
2019-11-06 01:05:57 +01:00
updateShowIcons ( )
2018-02-08 06:46:54 +01:00
updateTableViewRowHeight ( )
2019-09-11 05:28:16 +02:00
reloadVisibleCells ( )
2017-12-30 21:45:10 +01:00
}
}
}
2019-11-06 01:05:57 +01:00
private var showIcons = 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
2019-07-06 05:06:31 +02:00
static let fetchAndMergeArticlesQueue = CoalescingQueue ( name : " Fetch and Merge Articles " , interval : 0.5 , maxInterval : 2.0 )
2017-11-02 06:40:28 +01:00
2018-12-28 06:34:21 +01:00
private var sortDirection = AppDefaults . timelineSortDirection {
2018-01-28 20:33:45 +01:00
didSet {
if sortDirection != oldValue {
2019-09-13 15:29:56 +02:00
sortParametersDidChange ( )
2018-01-28 20:33:45 +01:00
}
}
}
2019-09-09 00:09:26 +02:00
private var groupByFeed = AppDefaults . timelineGroupByFeed {
didSet {
if groupByFeed != oldValue {
2019-09-13 15:29:56 +02:00
sortParametersDidChange ( )
2019-09-09 00:09:26 +02:00
}
}
}
2018-12-28 06:34:21 +01:00
private var fontSize : FontSize = AppDefaults . 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
}
}
private var oneSelectedArticle : Article ? {
2018-02-14 22:14:25 +01:00
return selectedArticles . count = = 1 ? selectedArticles . first : nil
2017-05-27 19:43:27 +02:00
}
2019-02-18 07:00:04 +01:00
private let keyboardDelegate = TimelineKeyboardDelegate ( )
2019-09-02 18:13:21 +02:00
private var timelineShowsSeparatorsObserver : NSKeyValueObservation ?
2019-02-18 07:00:04 +01:00
2019-02-17 07:14:06 +01:00
convenience init ( delegate : TimelineDelegate ) {
self . init ( nibName : " TimelineTableView " , bundle : nil )
self . delegate = delegate
2019-09-02 18:13:21 +02:00
self . startObservingUserDefaults ( )
2019-02-17 07:14:06 +01:00
}
2019-09-02 18:13:21 +02:00
2017-05-27 19:43:27 +02:00
override func viewDidLoad ( ) {
2019-11-06 01:05:57 +01:00
cellAppearance = TimelineCellAppearance ( showIcon : false , fontSize : fontSize )
cellAppearanceWithIcon = TimelineCellAppearance ( showIcon : 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 )
2019-02-18 07:00:04 +01:00
tableView . keyboardDelegate = keyboardDelegate
2017-05-27 19:43:27 +02:00
if ! didRegisterForNotifications {
2017-10-09 06:06:25 +02:00
NotificationCenter . default . addObserver ( self , selector : #selector ( statusesDidChange ( _ : ) ) , name : . StatusesDidChange , object : nil )
2019-11-15 03:11:41 +01:00
NotificationCenter . default . addObserver ( self , selector : #selector ( webFeedIconDidBecomeAvailable ( _ : ) ) , name : . WebFeedIconDidBecomeAvailable , object : nil )
2018-01-05 06:20:09 +01:00
NotificationCenter . default . addObserver ( self , selector : #selector ( avatarDidBecomeAvailable ( _ : ) ) , name : . AvatarDidBecomeAvailable , object : nil )
2019-08-26 19:54:23 +02:00
NotificationCenter . default . addObserver ( self , selector : #selector ( faviconDidBecomeAvailable ( _ : ) ) , name : . FaviconDidBecomeAvailable , object : nil )
2018-01-18 02:28:09 +01:00
NotificationCenter . default . addObserver ( self , selector : #selector ( accountDidDownloadArticles ( _ : ) ) , name : . AccountDidDownloadArticles , object : nil )
2019-05-21 23:36:47 +02:00
NotificationCenter . default . addObserver ( self , selector : #selector ( accountStateDidChange ( _ : ) ) , name : . AccountStateDidChange , object : nil )
2019-09-08 16:43:51 +02:00
NotificationCenter . default . addObserver ( self , selector : #selector ( accountsDidChange ( _ : ) ) , name : . UserDidAddAccount , object : nil )
NotificationCenter . default . addObserver ( self , selector : #selector ( accountsDidChange ( _ : ) ) , name : . UserDidDeleteAccount , object : nil )
2019-08-22 06:41:56 +02:00
NotificationCenter . default . addObserver ( self , selector : #selector ( containerChildrenDidChange ( _ : ) ) , name : . ChildrenDidChange , 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
2019-09-17 07:09:58 +02:00
didRegisterForNotifications = true
2017-05-27 19:43:27 +02:00
}
}
2018-09-26 05:20:59 +02:00
override func viewDidAppear ( ) {
sharingServiceDelegate = SharingServiceDelegate ( self . view . window )
}
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 ( ) {
2018-02-11 21:59:35 +01:00
guard let undoManager = undoManager , let markReadCommand = MarkStatusCommand ( 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 {
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 ( )
}
2019-07-28 07:53:27 +02:00
func representsThisObjectOnly ( _ object : AnyObject ) -> Bool {
guard let representedObjects = representedObjects else {
return false
}
if representedObjects . count != 1 {
return false
}
return representedObjects . first ! = = = object
}
2020-03-16 00:02:55 +01:00
func cleanUp ( ) {
fetchAndReplacePreservingSelection ( )
}
2019-07-28 07:53:27 +02:00
2019-11-22 18:47:03 +01:00
func toggleReadFilter ( ) {
2020-03-04 03:13:43 +01:00
guard let filter = isReadFiltered , let feedID = ( representedObjects ? . first as ? Feed ) ? . feedID else { return }
readFilterEnabledTable [ feedID ] = ! filter
delegate ? . timelineInvalidatedRestorationState ( self )
2020-03-16 00:02:55 +01:00
fetchAndReplacePreservingSelection ( )
2019-11-22 18:47:03 +01:00
}
2020-03-03 19:54:37 +01:00
// MARK: S t a t e R e s t o r a t i o n
2020-03-06 00:42:54 +01:00
func saveState ( to state : inout [ AnyHashable : Any ] ) {
2020-03-12 18:07:01 +01:00
state [ UserInfoKey . readArticlesFilterStateKeys ] = readFilterEnabledTable . keys . compactMap { $0 . userInfo }
state [ UserInfoKey . readArticlesFilterStateValues ] = readFilterEnabledTable . values . compactMap ( { $0 } )
2020-03-05 02:16:58 +01:00
if selectedArticles . count = = 1 {
2020-03-06 00:42:54 +01:00
state [ UserInfoKey . articlePath ] = selectedArticles . first ! . pathUserInfo
2020-03-05 02:16:58 +01:00
}
2020-03-03 19:54:37 +01:00
}
2020-03-06 00:42:54 +01:00
func restoreState ( from state : [ AnyHashable : Any ] ) {
2020-03-12 18:07:01 +01:00
guard let readArticlesFilterStateKeys = state [ UserInfoKey . readArticlesFilterStateKeys ] as ? [ [ AnyHashable : AnyHashable ] ] ,
let readArticlesFilterStateValues = state [ UserInfoKey . readArticlesFilterStateValues ] as ? [ Bool ] else {
return
}
for i in 0. . < readArticlesFilterStateKeys . count {
if let feedIdentifier = FeedIdentifier ( userInfo : readArticlesFilterStateKeys [ i ] ) {
readFilterEnabledTable [ feedIdentifier ] = readArticlesFilterStateValues [ i ]
2020-03-04 03:13:43 +01:00
}
}
2020-03-05 02:16:58 +01:00
2020-03-12 18:07:01 +01:00
if let articlePathUserInfo = state [ UserInfoKey . articlePath ] as ? [ AnyHashable : Any ] ,
2020-03-05 02:16:58 +01:00
let accountID = articlePathUserInfo [ ArticlePathKey . accountID ] as ? String ,
let account = AccountManager . shared . existingAccount ( with : accountID ) ,
2020-03-12 18:07:01 +01:00
let articleID = articlePathUserInfo [ ArticlePathKey . articleID ] as ? String {
2020-03-05 02:16:58 +01:00
exceptionArticleFetcher = SingleArticleFetcher ( account : account , articleID : articleID )
fetchAndReplaceArticlesSync ( )
2020-03-12 18:07:01 +01:00
if let selectedIndex = articles . firstIndex ( where : { $0 . articleID = = articleID } ) {
tableView . selectRow ( selectedIndex )
2020-03-17 08:05:50 +01:00
DispatchQueue . main . async {
self . tableView . scrollTo ( row : selectedIndex )
}
2020-03-12 18:07:01 +01:00
focus ( )
}
} else {
fetchAndReplaceArticlesSync ( )
2020-03-05 02:16:58 +01:00
}
2020-03-03 19:54:37 +01:00
}
2017-10-29 19:44:35 +01:00
// MARK: - A c t i o n s
2018-02-12 03:58:50 +01:00
@objc func openArticleInBrowser ( _ sender : Any ? ) {
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
}
}
2018-02-12 03:58:50 +01: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
2017-10-29 20:09:56 +01:00
if markAsRead {
markSelectedArticlesAsRead ( sender )
}
else {
markSelectedArticlesAsUnread ( sender )
}
2017-05-27 19:43:27 +02:00
}
2018-02-17 07:35:04 +01:00
2017-12-20 21:59:31 +01:00
@IBAction func markSelectedArticlesAsRead ( _ sender : Any ? ) {
2018-02-11 21:59:35 +01:00
guard let undoManager = undoManager , let markReadCommand = MarkStatusCommand ( initialArticles : selectedArticles , markingRead : true , undoManager : undoManager ) else {
2017-10-29 20:09:56 +01:00
return
}
runCommand ( markReadCommand )
2017-05-27 19:43:27 +02:00
}
2017-12-20 21:59:31 +01:00
@IBAction func markSelectedArticlesAsUnread ( _ sender : Any ? ) {
2018-02-11 21:59:35 +01:00
guard let undoManager = undoManager , let markUnreadCommand = MarkStatusCommand ( initialArticles : selectedArticles , markingRead : false , undoManager : undoManager ) else {
2017-10-29 20:09:56 +01:00
return
}
runCommand ( markUnreadCommand )
2017-05-27 19:43:27 +02:00
}
2017-12-25 19:40:06 +01:00
2018-02-12 22:04:07 +01:00
@IBAction func copy ( _ sender : Any ? ) {
NSPasteboard . general . copyObjects ( selectedArticles )
}
2019-04-12 18:13:19 +02:00
@IBAction func selectNextUp ( _ sender : Any ? ) {
guard let lastSelectedRow = tableView . selectedRowIndexes . last else {
return
}
let nextRowIndex = lastSelectedRow - 1
if nextRowIndex <= 0 {
tableView . scrollTo ( row : 0 , extraHeight : 0 )
}
2020-01-13 01:38:04 +01:00
tableView . selectRow ( nextRowIndex )
2019-04-12 18:13:19 +02:00
let followingRowIndex = nextRowIndex - 1
if followingRowIndex < 0 {
return
}
2019-04-13 20:20:03 +02:00
tableView . scrollToRowIfNotVisible ( followingRowIndex )
2019-04-12 18:13:19 +02:00
}
@IBAction func selectNextDown ( _ sender : Any ? ) {
guard let firstSelectedRow = tableView . selectedRowIndexes . first else {
return
}
let tableMaxIndex = tableView . numberOfRows - 1
let nextRowIndex = firstSelectedRow + 1
if nextRowIndex >= tableMaxIndex {
tableView . scrollTo ( row : tableMaxIndex , extraHeight : 0 )
}
2020-01-13 01:38:04 +01:00
tableView . selectRow ( nextRowIndex )
2019-04-12 18:13:19 +02:00
let followingRowIndex = nextRowIndex + 1
if followingRowIndex > tableMaxIndex {
return
}
2019-04-13 20:20:03 +02:00
tableView . scrollToRowIfNotVisible ( followingRowIndex )
2019-04-12 18:13:19 +02:00
}
2018-09-13 22:00:33 +02:00
func toggleReadStatusForSelectedArticles ( ) {
// I f a n y o n e o f t h e s e l e c t e d a r t i c l e s i s u n r e a d , t h e n m a r k t h e m a s r e a d .
// I f a l l a r t i c l e s a r e r e a d , t h e n m a r k t h e m a s u n r e a d t h e m .
let commandStatus = markReadCommandStatus ( )
let markingRead : Bool
switch commandStatus {
case . canMark :
markingRead = true
case . canUnmark :
markingRead = false
case . canDoNothing :
return
}
guard let undoManager = undoManager , let markStarredCommand = MarkStatusCommand ( initialArticles : selectedArticles , markingRead : markingRead , undoManager : undoManager ) else {
return
}
runCommand ( markStarredCommand )
}
2018-02-17 07:35:04 +01:00
func toggleStarredStatusForSelectedArticles ( ) {
// I f a n y o n e o f t h e s e l e c t e d a r t i c l e s i s n o t s t a r r e d , t h e n s t a r t h e m .
// I f a l l a r t i c l e s a r e s t a r r e d , t h e n u n s t a r t h e m .
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 {
2020-02-29 19:30:35 +01:00
let articles = selectedArticles
if articles . anyArticleIsUnread ( ) {
return . canMark
}
if articles . anyArticleIsReadAndCanMarkUnread ( ) {
return . canUnmark
}
return . canDoNothing
2018-02-17 07:35:04 +01:00
}
2020-03-09 00:15:17 +01:00
func markAboveArticlesRead ( ) {
markAboveArticlesRead ( selectedArticles )
2018-09-05 06:34:06 +02:00
}
2020-03-09 00:15:17 +01:00
func markBelowArticlesRead ( ) {
markBelowArticlesRead ( selectedArticles )
2018-09-05 06:34:06 +02:00
}
2017-12-25 19:40:06 +01:00
2020-03-09 00:15:17 +01:00
func canMarkAboveArticlesAsRead ( ) -> Bool {
guard let first = selectedArticles . first else { return false }
return articles . articlesAbove ( article : first ) . canMarkAllAsRead ( )
}
2017-12-25 19:40:06 +01:00
2020-03-09 00:15:17 +01:00
func canMarkBelowArticlesAsRead ( ) -> Bool {
guard let last = selectedArticles . last else { return false }
return articles . articlesBelow ( article : last ) . canMarkAllAsRead ( )
}
2017-12-25 19:40:06 +01:00
2020-03-09 00:15:17 +01:00
func markAboveArticlesRead ( _ selectedArticles : [ Article ] ) {
guard let first = selectedArticles . first else { return }
let articlesToMark = articles . articlesAbove ( article : first )
guard ! articlesToMark . isEmpty else { return }
guard let undoManager = undoManager , let markReadCommand = MarkStatusCommand ( initialArticles : articlesToMark , markingRead : true , undoManager : undoManager ) else {
2017-12-25 19:40:06 +01:00
return
}
2020-03-09 00:15:17 +01:00
runCommand ( markReadCommand )
}
2017-12-25 19:40:06 +01:00
2020-03-09 00:15:17 +01:00
func markBelowArticlesRead ( _ selectedArticles : [ Article ] ) {
guard let last = selectedArticles . last else { return }
let articlesToMark = articles . articlesBelow ( article : last )
guard ! articlesToMark . isEmpty else { return }
2018-02-11 21:59:35 +01:00
guard let undoManager = undoManager , let markReadCommand = MarkStatusCommand ( initialArticles : articlesToMark , markingRead : true , undoManager : undoManager ) else {
2017-12-25 19:40:06 +01:00
return
}
runCommand ( markReadCommand )
}
2017-12-25 21:21:44 +01:00
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
2019-10-03 18:39:48 +02:00
func goToDeepLink ( for userInfo : [ AnyHashable : Any ] ) {
2019-11-14 22:35:19 +01:00
guard let articleID = userInfo [ ArticlePathKey . articleID ] as ? String else { return }
2020-03-01 02:05:56 +01:00
if isReadFiltered ? ? false {
if let accountName = userInfo [ ArticlePathKey . accountName ] as ? String ,
let account = AccountManager . shared . findActiveAccount ( forDisplayName : accountName ) {
exceptionArticleFetcher = SingleArticleFetcher ( account : account , articleID : articleID )
fetchAndReplaceArticlesSync ( )
}
}
2019-10-03 18:39:48 +02:00
guard let ix = articles . firstIndex ( where : { $0 . articleID = = articleID } ) else { return }
NSCursor . setHiddenUntilMouseMoves ( true )
2020-01-13 01:38:04 +01:00
tableView . selectRow ( ix )
2019-10-03 18:39:48 +02:00
tableView . scrollTo ( row : ix )
}
2017-05-27 19:43:27 +02:00
func goToNextUnread ( ) {
guard let ix = indexOfNextUnreadArticle ( ) else {
return
}
2018-09-05 06:18:59 +02:00
NSCursor . setHiddenUntilMouseMoves ( true )
2020-01-13 01:38:04 +01:00
tableView . selectRow ( ix )
2017-05-27 19:43:27 +02:00
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
}
2019-03-03 22:51:52 +01:00
2017-12-20 00:24:38 +01:00
window . makeFirstResponderUnlessDescendantIsFirstResponder ( tableView )
2019-03-05 01:08:53 +01:00
if ! hasAtLeastOneSelectedArticle && articles . count > 0 {
2020-01-13 01:38:04 +01:00
tableView . selectRowAndScrollToVisible ( 0 )
2019-03-05 01:08:53 +01:00
}
2017-12-20 00:24:38 +01:00
}
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-10-09 06:06:25 +02:00
@objc func statusesDidChange ( _ note : Notification ) {
2019-12-17 07:45:59 +01:00
guard let articleIDs = note . userInfo ? [ Account . UserInfoKey . articleIDs ] as ? Set < String > else {
2017-05-27 19:43:27 +02:00
return
}
2019-12-17 07:45:59 +01:00
reloadVisibleCells ( for : articleIDs )
2019-07-28 07:53:27 +02:00
updateUnreadCount ( )
2017-05-27 19:43:27 +02:00
}
2019-11-15 03:11:41 +01:00
@objc func webFeedIconDidBecomeAvailable ( _ note : Notification ) {
guard showIcons , let feed = note . userInfo ? [ UserInfoKey . webFeed ] as ? WebFeed else {
2018-01-05 06:20:09 +01:00
return
}
2018-09-27 06:33:55 +02:00
let indexesToReload = tableView . indexesOfAvailableRowsPassingTest { ( row ) -> Bool in
guard let article = articles . articleAtRow ( row ) else {
return false
}
2019-11-15 03:11:41 +01:00
return feed = = article . webFeed
2018-01-05 06:20:09 +01:00
}
2018-09-27 06:33:55 +02:00
if let indexesToReload = indexesToReload {
reloadCells ( for : indexesToReload )
}
2018-01-05 06:20:09 +01:00
}
@objc func avatarDidBecomeAvailable ( _ note : Notification ) {
2019-11-06 01:05:57 +01:00
guard showIcons , 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
}
}
2019-08-26 19:54:23 +02:00
@objc func faviconDidBecomeAvailable ( _ note : Notification ) {
2019-11-06 01:05:57 +01:00
if showIcons {
2019-09-06 23:52:31 +02:00
queueReloadAvailableCells ( )
2018-01-10 06:09:09 +01:00
}
2018-01-09 06:34:39 +01:00
}
2018-01-18 02:28:09 +01:00
@objc func accountDidDownloadArticles ( _ note : Notification ) {
2019-11-15 03:11:41 +01:00
guard let feeds = note . userInfo ? [ Account . UserInfoKey . webFeeds ] as ? Set < WebFeed > else {
2018-01-18 02:28:09 +01:00
return
}
2019-11-15 03:11:41 +01:00
let shouldFetchAndMergeArticles = representedObjectsContainsAnyWebFeed ( feeds ) || representedObjectsContainsAnyPseudoFeed ( )
2018-01-18 02:28:09 +01:00
if shouldFetchAndMergeArticles {
queueFetchAndMergeArticles ( )
}
}
2019-05-21 23:36:47 +02:00
@objc func accountStateDidChange ( _ note : Notification ) {
if representedObjectsContainsAnyPseudoFeed ( ) {
2019-07-06 05:06:31 +02:00
fetchAndReplaceArticlesAsync ( )
2019-05-21 23:36:47 +02:00
}
}
2019-05-21 23:47:32 +02:00
@objc func accountsDidChange ( _ note : Notification ) {
if representedObjectsContainsAnyPseudoFeed ( ) {
2019-07-06 05:06:31 +02:00
fetchAndReplaceArticlesAsync ( )
2019-05-21 23:47:32 +02:00
}
}
2019-08-22 06:41:56 +02:00
@objc func containerChildrenDidChange ( _ note : Notification ) {
if representedObjectsContainsAnyPseudoFeed ( ) || representedObjectsContainAnyFolder ( ) {
fetchAndReplaceArticlesAsync ( )
}
}
2018-01-28 20:33:45 +01:00
@objc func userDefaultsDidChange ( _ note : Notification ) {
2018-12-28 06:34:21 +01:00
self . fontSize = AppDefaults . timelineFontSize
self . sortDirection = AppDefaults . timelineSortDirection
2019-09-09 00:09:26 +02:00
self . groupByFeed = AppDefaults . timelineGroupByFeed
2018-01-28 20:33:45 +01:00
}
2019-05-29 17:24:30 +02:00
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
}
2019-02-09 04:53:41 +01:00
private func reloadVisibleCells ( ) {
guard let indexes = tableView . indexesOfAvailableRows ( ) else {
return
}
reloadVisibleCells ( for : indexes )
}
2017-05-27 19:43:27 +02:00
2018-09-27 06:33:55 +02:00
private func reloadVisibleCells ( for articles : [ Article ] ) {
reloadVisibleCells ( for : Set ( articles . articleIDs ( ) ) )
2017-10-09 06:06:25 +02:00
}
2018-09-27 06:33:55 +02:00
private func reloadVisibleCells ( for articles : Set < Article > ) {
reloadVisibleCells ( for : articles . articleIDs ( ) )
}
private func reloadVisibleCells ( for articleIDs : Set < String > ) {
2018-01-06 20:18:03 +01:00
if articleIDs . isEmpty {
return
}
2018-09-27 06:33:55 +02:00
let indexes = indexesForArticleIDs ( articleIDs )
reloadVisibleCells ( for : indexes )
2018-01-06 20:56:32 +01:00
}
2018-09-27 06:33:55 +02:00
private func reloadVisibleCells ( for indexes : IndexSet ) {
let indexesToReload = tableView . indexesOfAvailableRowsPassingTest { ( row ) -> Bool in
return indexes . contains ( row )
}
if let indexesToReload = indexesToReload {
reloadCells ( for : indexesToReload )
}
}
2018-01-06 20:56:32 +01:00
2018-09-27 06:33:55 +02:00
private func reloadCells ( for indexes : IndexSet ) {
2018-01-06 20:56:32 +01:00
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-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 ( ) )
2019-12-29 05:53:36 +01:00
let prototypeArticle = Article ( accountID : prototypeID , articleID : prototypeID , webFeedID : prototypeID , uniqueID : prototypeID , title : longTitle , contentHTML : nil , contentText : nil , url : nil , externalURL : nil , summary : nil , imageURL : nil , datePublished : nil , dateModified : nil , authors : nil , status : status )
2017-05-27 19:43:27 +02:00
2019-11-06 01:05:57 +01:00
let prototypeCellData = TimelineCellData ( article : prototypeArticle , showFeedName : showingFeedNames , feedName : " Prototype Feed Name " , iconImage : nil , showIcon : false , featuredImage : nil )
2018-02-19 00:13:47 +01:00
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
}
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
}
2018-02-18 00:29:55 +01:00
@objc func fetchAndMergeArticles ( ) {
guard let representedObjects = representedObjects else {
return
}
2019-07-06 05:06:31 +02:00
fetchUnsortedArticlesAsync ( for : representedObjects ) { [ weak self ] ( unsortedArticles ) in
2018-02-18 00:29:55 +01:00
// M e r g e a r t i c l e s b y a r t i c l e I D . F o r a n y u n i q u e a r t i c l e I D i n c u r r e n t a r t i c l e s , a d d t o u n s o r t e d A r t i c l e s .
2019-07-06 05:06:31 +02:00
guard let strongSelf = self else {
return
}
2018-02-18 00:29:55 +01:00
let unsortedArticleIDs = unsortedArticles . articleIDs ( )
2019-07-06 05:06:31 +02:00
var updatedArticles = unsortedArticles
for article in strongSelf . articles {
2018-02-18 00:29:55 +01:00
if ! unsortedArticleIDs . contains ( article . articleID ) {
2019-07-06 05:06:31 +02:00
updatedArticles . insert ( article )
2018-02-18 00:29:55 +01:00
}
}
2019-07-06 05:06:31 +02:00
strongSelf . performBlockAndRestoreSelection {
strongSelf . replaceArticles ( with : updatedArticles )
}
2018-02-18 00:29:55 +01:00
}
}
2017-11-02 06:40:28 +01:00
}
2017-05-27 19:43:27 +02:00
2019-02-11 01:47:46 +01:00
// MARK: - N S M e n u D e l e g a t e
extension TimelineViewController : NSMenuDelegate {
public func menuNeedsUpdate ( _ menu : NSMenu ) {
menu . removeAllItems ( )
guard let contextualMenu = contextualMenuForClickedRows ( ) else {
return
}
menu . takeItems ( from : contextualMenu )
}
}
// MARK: - N S U s e r I n t e r f a c e V a l i d a t i o n s
2018-02-12 22:04:07 +01:00
extension TimelineViewController : NSUserInterfaceValidations {
func validateUserInterfaceItem ( _ item : NSValidatedUserInterfaceItem ) -> Bool {
2019-09-01 00:46:15 +02:00
if item . action = = #selector ( openArticleInBrowser ( _ : ) ) {
let currentLink = oneSelectedArticle ? . preferredLink
return currentLink != nil
}
2018-02-12 22:04:07 +01:00
if item . action = = #selector ( copy ( _ : ) ) {
return NSPasteboard . general . canCopyAtLeastOneObject ( selectedArticles )
}
2019-09-01 00:46:15 +02:00
2018-02-12 22:04:07 +01:00
return true
}
}
2018-12-28 07:16:38 +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
}
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 {
2019-02-14 06:33:50 +01:00
private static let rowViewIdentifier = NSUserInterfaceItemIdentifier ( rawValue : " timelineRow " )
2017-11-04 18:35:34 +01:00
2019-02-14 06:33:50 +01:00
func tableView ( _ tableView : NSTableView , rowViewForRow row : Int ) -> NSTableRowView ? {
if let rowView : TimelineTableRowView = tableView . makeView ( withIdentifier : TimelineViewController . rowViewIdentifier , owner : nil ) as ? TimelineTableRowView {
return rowView
}
let rowView = TimelineTableRowView ( )
rowView . identifier = TimelineViewController . rowViewIdentifier
2017-11-04 18:35:34 +01:00
return rowView
}
2019-02-14 06:33:50 +01:00
private static let timelineCellIdentifier = NSUserInterfaceItemIdentifier ( rawValue : " timelineCell " )
2017-11-04 18:35:34 +01:00
2019-02-14 06:33:50 +01:00
func tableView ( _ tableView : NSTableView , viewFor tableColumn : NSTableColumn ? , row : Int ) -> NSView ? {
2017-11-04 18:35:34 +01:00
2019-02-14 06:33:50 +01:00
func configure ( _ cell : TimelineTableCellView ) {
2019-11-06 01:05:57 +01:00
cell . cellAppearance = showIcons ? cellAppearanceWithIcon : cellAppearance
2019-02-14 06:33:50 +01:00
if let article = articles . articleAtRow ( row ) {
configureTimelineCell ( cell , article : article )
}
else {
makeTimelineCellEmpty ( cell )
}
2017-11-04 18:35:34 +01:00
}
2019-02-14 06:33:50 +01:00
if let cell = tableView . makeView ( withIdentifier : TimelineViewController . timelineCellIdentifier , owner : nil ) as ? TimelineTableCellView {
configure ( cell )
return cell
2017-11-04 18:35:34 +01:00
}
2019-02-14 06:33:50 +01:00
let cell = TimelineTableCellView ( )
cell . identifier = TimelineViewController . timelineCellIdentifier
configure ( cell )
2017-11-04 18:35:34 +01:00
return cell
}
func tableViewSelectionDidChange ( _ notification : Notification ) {
2018-02-19 06:49:46 +01:00
if selectedArticles . isEmpty {
2019-02-18 23:54:38 +01:00
selectionDidChange ( nil )
2017-11-04 18:35:34 +01:00
return
}
2018-02-19 06:49:46 +01:00
if selectedArticles . count = = 1 {
let article = selectedArticles . first !
if ! article . status . read {
markArticles ( Set ( [ article ] ) , statusKey : . read , flag : true )
2017-11-04 18:35:34 +01:00
}
}
2018-02-19 06:49:46 +01:00
2019-02-18 23:54:38 +01:00
selectionDidChange ( selectedArticles )
2017-11-04 18:35:34 +01:00
}
2019-02-18 23:54:38 +01:00
private func selectionDidChange ( _ selectedArticles : ArticleArray ? ) {
2019-02-18 07:00:04 +01:00
delegate ? . timelineSelectionDidChange ( self , selectedArticles : selectedArticles )
2020-03-05 02:16:58 +01:00
delegate ? . timelineInvalidatedRestorationState ( self )
2017-11-04 18:35:34 +01:00
}
private func configureTimelineCell ( _ cell : TimelineTableCellView , article : Article ) {
cell . objectValue = article
2019-11-06 01:05:57 +01:00
let iconImage = article . iconImage ( )
2019-11-15 03:11:41 +01:00
cell . cellData = TimelineCellData ( article : article , showFeedName : showFeedNames , feedName : article . webFeed ? . nameForDisplay , iconImage : iconImage , showIcon : showIcons , featuredImage : nil )
2017-11-26 06:27:35 +01:00
}
2019-11-06 01:05:57 +01:00
private func iconFor ( _ article : Article ) -> IconImage ? {
if ! showIcons {
2018-01-05 06:20:09 +01:00
return nil
}
2019-04-12 00:53:03 +02:00
2017-11-26 06:27:35 +01:00
if let authors = article . authors {
for author in authors {
if let image = avatarForAuthor ( author ) {
return image
}
}
}
2019-06-18 06:47:17 +02:00
2019-11-15 03:11:41 +01:00
guard let feed = article . webFeed else {
2017-11-26 06:27:35 +01:00
return nil
}
2019-11-15 03:11:41 +01:00
if let feedIcon = appDelegate . webFeedIconDownloader . icon ( for : feed ) {
2019-05-21 12:42:40 +02:00
return feedIcon
}
2019-11-06 01:05:57 +01:00
if let favicon = appDelegate . faviconDownloader . faviconAsIcon ( for : feed ) {
2019-05-21 12:42:40 +02:00
return favicon
}
2019-04-12 00:53:03 +02:00
2019-05-21 12:42:40 +02:00
return FaviconGenerator . favicon ( feed )
2017-11-26 06:27:35 +01:00
}
2019-11-06 01:05:57 +01:00
private func avatarForAuthor ( _ author : Author ) -> IconImage ? {
2017-11-26 22:16:32 +01:00
return appDelegate . authorAvatarDownloader . image ( for : author )
2017-11-04 18:35:34 +01:00
}
private func makeTimelineCellEmpty ( _ cell : TimelineTableCellView ) {
cell . objectValue = nil
2018-02-22 07:48:34 +01:00
cell . cellData = TimelineCellData ( )
2017-11-04 18:35:34 +01:00
}
2019-09-22 16:55:19 +02:00
private func toggleArticleRead ( _ article : Article ) {
guard let undoManager = undoManager , let markUnreadCommand = MarkStatusCommand ( initialArticles : [ article ] , markingRead : ! article . status . read , undoManager : undoManager ) else {
return
}
self . runCommand ( markUnreadCommand )
}
2019-09-24 15:50:15 +02:00
private func toggleArticleStarred ( _ article : Article ) {
guard let undoManager = undoManager , let markUnreadCommand = MarkStatusCommand ( initialArticles : [ article ] , markingStarred : ! article . status . starred , undoManager : undoManager ) else {
return
}
self . runCommand ( markUnreadCommand )
}
2019-09-22 16:55:19 +02:00
func tableView ( _ tableView : NSTableView , rowActionsForRow row : Int , edge : NSTableView . RowActionEdge ) -> [ NSTableViewRowAction ] {
2019-09-24 15:50:15 +02:00
guard let article = articles . articleAtRow ( row ) else {
return [ ]
}
switch edge {
case . leading :
2019-10-22 20:39:15 +02:00
let action = NSTableViewRowAction ( style : . regular , title : " " ) { ( action , row ) in
2019-09-24 15:50:15 +02:00
self . toggleArticleRead ( article ) ;
tableView . rowActionsVisible = false
}
2019-10-22 20:39:15 +02:00
action . image = article . status . read ? AppAssets . swipeMarkUnreadImage : AppAssets . swipeMarkReadImage
2019-09-24 15:50:15 +02:00
return [ action ]
2019-09-22 16:55:19 +02:00
2019-09-24 15:50:15 +02:00
case . trailing :
2019-10-22 20:39:15 +02:00
let action = NSTableViewRowAction ( style : . regular , title : " " ) { ( action , row ) in
2019-09-24 15:50:15 +02:00
self . toggleArticleStarred ( article ) ;
tableView . rowActionsVisible = false
}
2020-02-28 01:27:12 +01:00
action . backgroundColor = AppAssets . swipeMarkUnstarredColor
2019-10-22 20:39:15 +02:00
action . image = article . status . starred ? AppAssets . swipeMarkUnstarredImage : AppAssets . swipeMarkStarredImage
2019-09-24 15:50:15 +02:00
return [ action ]
2019-09-22 16:55:19 +02:00
2019-09-24 15:50:15 +02:00
@ unknown default :
2019-10-02 18:18:52 +02:00
os_log ( . error , " Unknown table row edge: %ld " , edge . rawValue )
2019-09-22 16:55:19 +02:00
}
return [ ]
}
2017-11-04 18:35:34 +01:00
}
// 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
2019-09-02 18:13:21 +02:00
func startObservingUserDefaults ( ) {
assert ( timelineShowsSeparatorsObserver = = nil )
timelineShowsSeparatorsObserver = UserDefaults . standard . observe ( \ UserDefaults . CorreiaSeparators ) { [ weak self ] ( _ , _ ) in
guard let self = self , self . isViewLoaded else { return }
self . tableView . enumerateAvailableRowViews { ( rowView , index ) in
if let cellView = rowView . view ( atColumn : 0 ) as ? TimelineTableCellView {
cellView . timelineShowsSeparatorsDefaultDidChange ( )
}
}
}
}
2020-03-16 00:02:55 +01:00
func fetchAndReplacePreservingSelection ( ) {
if let article = oneSelectedArticle , let account = article . account {
exceptionArticleFetcher = SingleArticleFetcher ( account : account , articleID : article . articleID )
}
performBlockAndRestoreSelection {
fetchAndReplaceArticlesSync ( )
}
}
2018-02-18 00:16:30 +01:00
@objc func reloadAvailableCells ( ) {
2018-01-09 06:34:39 +01:00
if let indexesToReload = tableView . indexesOfAvailableRows ( ) {
reloadCells ( for : indexesToReload )
}
}
2019-07-28 07:53:27 +02:00
func updateUnreadCount ( ) {
var count = 0
for article in articles {
if ! article . status . read {
count += 1
}
}
unreadCount = count
}
2018-01-09 06:34:39 +01:00
func queueReloadAvailableCells ( ) {
2018-02-18 02:45:05 +01:00
CoalescingQueue . standard . add ( self , #selector ( reloadAvailableCells ) )
2018-01-09 06:34:39 +01:00
}
2018-02-08 06:46:54 +01:00
func updateTableViewRowHeight ( ) {
tableView . rowHeight = currentRowHeight
}
2019-11-06 01:05:57 +01:00
func updateShowIcons ( ) {
2018-01-05 06:20:09 +01:00
if showFeedNames {
2019-11-06 01:05:57 +01:00
self . showIcons = true
2018-01-05 06:20:09 +01:00
return
}
for article in articles {
if let authors = article . authors {
for author in authors {
if author . avatarURL != nil {
2019-11-06 01:05:57 +01:00
self . showIcons = true
2018-01-05 06:20:09 +01:00
return
}
}
}
}
2019-11-06 01:05:57 +01:00
self . showIcons = false
2018-01-05 06:20:09 +01:00
}
2017-11-02 06:40:28 +01:00
func emptyTheTimeline ( ) {
2017-10-09 03:58:15 +02:00
if ! articles . isEmpty {
articles = [ Article ] ( )
}
}
2017-11-02 06:40:28 +01:00
2019-09-13 15:29:56 +02:00
func sortParametersDidChange ( ) {
2019-09-09 00:09:26 +02:00
performBlockAndRestoreSelection {
let unsortedArticles = Set ( articles )
replaceArticles ( with : unsortedArticles )
}
}
2018-02-08 06:46:54 +01:00
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 ] ) {
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 )
}
2018-09-26 22:23:21 +02:00
func row ( for articleID : String ) -> Int ? {
updateArticleRowMapIfNeeded ( )
return articleRowMap [ articleID ]
}
func row ( for article : Article ) -> Int ? {
return row ( for : article . articleID )
}
func updateArticleRowMap ( ) {
var rowMap = [ String : Int ] ( )
var index = 0
articles . forEach { ( article ) in
rowMap [ article . articleID ] = index
index += 1
}
articleRowMap = rowMap
}
func updateArticleRowMapIfNeeded ( ) {
if articleRowMap . isEmpty {
updateArticleRowMap ( )
}
}
func indexesForArticleIDs ( _ articleIDs : Set < String > ) -> IndexSet {
var indexes = IndexSet ( )
articleIDs . forEach { ( articleID ) in
guard let oneIndex = row ( for : articleID ) else {
return
}
if oneIndex != NSNotFound {
indexes . insert ( oneIndex )
}
}
return indexes
}
2019-09-17 07:27:17 +02:00
// MARK: - A p p e a r a n c e C h a n g e
private func fontSizeDidChange ( ) {
2019-11-06 01:05:57 +01:00
cellAppearance = TimelineCellAppearance ( showIcon : false , fontSize : fontSize )
cellAppearanceWithIcon = TimelineCellAppearance ( showIcon : true , fontSize : fontSize )
2019-09-17 07:27:17 +02:00
updateRowHeights ( )
performBlockAndRestoreSelection {
tableView . reloadData ( )
}
}
// MARK: - F e t c h i n g A r t i c l e s
2019-11-22 18:47:03 +01:00
2019-07-06 05:06:31 +02:00
func fetchAndReplaceArticlesSync ( ) {
// T o b e c a l l e d w h e n t h e u s e r h a s m a d e a c h a n g e o f s e l e c t i o n i n t h e s i d e b a r .
// I t b l o c k s t h e m a i n t h r e a d , s o t h a t t h e r e ’ s n o a s y n c d e l a y ,
// s o t h a t t h e e n t i r e d i s p l a y r e f r e s h e s a t o n c e .
// I t ’ s a b e t t e r u s e r e x p e r i e n c e t h i s w a y .
cancelPendingAsyncFetches ( )
2020-03-01 02:05:56 +01:00
guard var representedObjects = representedObjects else {
2019-07-06 05:06:31 +02:00
emptyTheTimeline ( )
return
}
2020-03-01 02:05:56 +01:00
if exceptionArticleFetcher != nil {
representedObjects . append ( exceptionArticleFetcher as AnyObject )
exceptionArticleFetcher = nil
}
2019-07-06 05:06:31 +02:00
let fetchedArticles = fetchUnsortedArticlesSync ( for : representedObjects )
replaceArticles ( with : fetchedArticles )
}
2017-05-27 19:43:27 +02:00
2019-07-06 05:06:31 +02:00
func fetchAndReplaceArticlesAsync ( ) {
// T o b e c a l l e d w h e n w e n e e d t o d o a n e n t i r e f e t c h , b u t a n a s y n c d e l a y i s o k a y .
// E x a m p l e : w e h a v e t h e T o d a y f e e d s e l e c t e d , a n d t h e c a l e n d a r d a y j u s t c h a n g e d .
cancelPendingAsyncFetches ( )
2020-03-01 02:05:56 +01:00
guard var representedObjects = representedObjects else {
2017-10-09 03:58:15 +02:00
emptyTheTimeline ( )
return
}
2020-03-01 02:05:56 +01:00
if exceptionArticleFetcher != nil {
representedObjects . append ( exceptionArticleFetcher as AnyObject )
exceptionArticleFetcher = nil
}
2019-07-06 05:06:31 +02:00
fetchUnsortedArticlesAsync ( for : representedObjects ) { [ weak self ] ( articles ) in
self ? . replaceArticles ( with : articles )
}
}
2017-11-02 06:40:28 +01:00
2019-07-06 05:06:31 +02:00
func cancelPendingAsyncFetches ( ) {
fetchSerialNumber += 1
fetchRequestQueue . cancelAllRequests ( )
2018-01-21 01:54:18 +01:00
}
2019-07-06 05:06:31 +02:00
func replaceArticles ( with unsortedArticles : Set < Article > ) {
2019-09-13 15:23:31 +02:00
articles = Array ( unsortedArticles ) . sortedByDate ( sortDirection , groupByFeed : groupByFeed )
2018-01-18 07:03:13 +01:00
}
2019-07-06 05:06:31 +02:00
func fetchUnsortedArticlesSync ( for representedObjects : [ Any ] ) -> Set < Article > {
cancelPendingAsyncFetches ( )
let articleFetchers = representedObjects . compactMap { $0 as ? ArticleFetcher }
if articleFetchers . isEmpty {
return Set < Article > ( )
}
2018-01-18 07:03:13 +01:00
2017-10-09 03:58:15 +02:00
var fetchedArticles = Set < Article > ( )
2019-07-06 05:06:31 +02:00
for articleFetcher in articleFetchers {
2020-03-04 03:13:43 +01:00
if isReadFiltered ? ? true {
2019-12-17 07:45:59 +01:00
if let articles = try ? articleFetcher . fetchUnreadArticles ( ) {
fetchedArticles . formUnion ( articles )
}
2019-11-22 18:47:03 +01:00
} else {
2019-12-17 07:45:59 +01:00
if let articles = try ? articleFetcher . fetchArticles ( ) {
fetchedArticles . formUnion ( articles )
}
2019-11-22 18:47:03 +01:00
}
2019-07-06 05:06:31 +02:00
}
return fetchedArticles
}
2017-11-02 06:40:28 +01:00
2019-12-15 02:01:34 +01:00
func fetchUnsortedArticlesAsync ( for representedObjects : [ Any ] , completion : @ escaping ArticleSetBlock ) {
2019-07-06 05:06:31 +02:00
// T h e c a l l b a c k w i l l * n o t * b e c a l l e d i f t h e f e t c h i s n o l o n g e r r e l e v a n t — t h a t i s ,
// i f i t ’ s b e e n s u p e r s e d e d b y a n e w e r f e t c h , o r t h e t i m e l i n e w a s e m p t i e d , e t c . , i t w o n ’ t g e t c a l l e d .
precondition ( Thread . isMainThread )
cancelPendingAsyncFetches ( )
2020-03-04 03:13:43 +01:00
let fetchOperation = FetchRequestOperation ( id : fetchSerialNumber , readFilter : isReadFiltered ? ? true , representedObjects : representedObjects ) { [ weak self ] ( articles , operation ) in
2019-07-06 05:06:31 +02:00
precondition ( Thread . isMainThread )
guard ! operation . isCanceled , let strongSelf = self , operation . id = = strongSelf . fetchSerialNumber else {
return
2017-10-09 03:58:15 +02:00
}
2019-12-15 02:01:34 +01:00
completion ( articles )
2017-10-09 03:58:15 +02:00
}
2019-07-06 05:06:31 +02:00
fetchRequestQueue . add ( fetchOperation )
2017-05-27 19:43:27 +02:00
}
2018-01-18 02:28:09 +01:00
func selectArticles ( _ articleIDs : [ String ] ) {
2018-09-27 06:33:55 +02:00
let indexesToSelect = indexesForArticleIDs ( Set ( articleIDs ) )
2018-01-21 01:54:18 +01:00
if indexesToSelect . isEmpty {
tableView . deselectAll ( self )
return
}
tableView . selectRowIndexes ( indexesToSelect , byExtendingSelection : false )
2018-01-18 02:28:09 +01:00
}
func queueFetchAndMergeArticles ( ) {
2018-02-18 00:29:55 +01:00
TimelineViewController . fetchAndMergeArticlesQueue . add ( self , #selector ( fetchAndMergeArticles ) )
2018-01-18 02:28:09 +01:00
}
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
2019-02-03 21:06:11 +01:00
func representedObjectsContainsAnyPseudoFeed ( ) -> Bool {
2019-05-20 05:49:24 +02:00
return representedObjects ? . contains ( where : { $0 is PseudoFeed } ) ? ? false
2019-02-03 21:06:11 +01:00
}
2019-05-29 17:24:30 +02:00
func representedObjectsContainsTodayFeed ( ) -> Bool {
return representedObjects ? . contains ( where : { $0 = = = SmartFeedsController . shared . todayFeed } ) ? ? false
}
2019-08-22 06:41:56 +02:00
func representedObjectsContainAnyFolder ( ) -> Bool {
return representedObjects ? . contains ( where : { $0 is Folder } ) ? ? false
}
2019-11-15 03:11:41 +01:00
func representedObjectsContainsAnyWebFeed ( _ webFeeds : Set < WebFeed > ) -> Bool {
2018-01-18 02:28:09 +01:00
// 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 {
2019-11-15 03:11:41 +01:00
if let feed = representedObject as ? WebFeed {
for oneFeed in webFeeds {
if feed . webFeedID = = oneFeed . webFeedID || feed . url = = oneFeed . url {
2018-01-18 02:28:09 +01:00
return true
}
}
}
else if let folder = representedObject as ? Folder {
2019-11-15 03:11:41 +01:00
for oneFeed in webFeeds {
if folder . hasWebFeed ( with : oneFeed . webFeedID ) || folder . hasWebFeed ( withURL : oneFeed . url ) {
2018-01-18 02:28:09 +01:00
return true
}
}
}
}
return false
}
2017-05-27 19:43:27 +02:00
}