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-11-05 06:51:14 +01:00
class TimelineViewController : NSViewController , KeyboardDelegate , 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-11-05 06:51:14 +01:00
var undoableCommands = [ UndoableCommand ] ( )
2017-11-04 18:35:34 +01:00
private var cellAppearance : TimelineCellAppearance !
private var showFeedNames = false
private var didRegisterForNotifications = false
private let timelineFontSizeKVOKey = " values.{AppDefaults.Key.timelineFontSize} "
2017-11-02 06:40:28 +01:00
2017-11-04 18:35:34 +01:00
private var articles = ArticleArray ( ) {
didSet {
if articles != oldValue {
clearUndoableCommands ( )
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 {
2017-11-02 06:40:28 +01:00
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-11-06 05:59:30 +01:00
postTimelineSelectionDidChangeNotification ( nil )
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 ( ) {
2017-11-25 06:39:59 +01:00
cellAppearance = TimelineCellAppearance ( theme : appDelegate . currentTheme , fontSize : fontSize )
2017-05-27 19:43:27 +02:00
2017-11-02 06:40:28 +01:00
tableView . rowHeight = calculateRowHeight ( )
2017-05-27 19:43:27 +02:00
tableView . target = self
tableView . doubleAction = #selector ( openArticleInBrowser ( _ : ) )
tableView . keyboardDelegate = self
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 )
2017-05-27 19:43:27 +02:00
2017-09-24 21:24:44 +02:00
NSUserDefaultsController . shared . addObserver ( self , forKeyPath : timelineFontSizeKVOKey , options : NSKeyValueObservingOptions ( rawValue : 0 ) , context : nil )
2017-05-27 19:43:27 +02:00
didRegisterForNotifications = true
}
}
// MARK: K V O
override func observeValue ( forKeyPath keyPath : String ? , of object : Any ? , change : [ NSKeyValueChangeKey : Any ] ? , context : UnsafeMutableRawPointer ? ) {
if let keyPath = keyPath {
switch ( keyPath ) {
case timelineFontSizeKVOKey :
fontSizeInDefaultsDidChange ( )
return
default :
break
}
}
super . observeValue ( forKeyPath : keyPath , of : object , change : change , context : context )
}
// MARK: A p p e a r a n c e C h a n g e
private func fontSizeDidChange ( ) {
2017-11-25 06:39:59 +01:00
cellAppearance = TimelineCellAppearance ( theme : appDelegate . currentTheme , fontSize : fontSize )
2017-05-27 19:43:27 +02:00
let updatedRowHeight = calculateRowHeight ( )
if tableView . rowHeight != updatedRowHeight {
tableView . rowHeight = updatedRowHeight
tableView . reloadData ( )
}
}
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-11-06 06:27:34 +01:00
@IBAction func markSelectedArticlesAsRead ( _ sender : AnyObject ? ) {
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
}
@IBAction func markSelectedArticlesAsUnread ( _ sender : AnyObject ) {
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-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-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-09-17 21:54:08 +02:00
@objc func sidebarSelectionDidChange ( _ note : Notification ) {
2017-05-27 19:43:27 +02:00
2017-09-24 21:24:44 +02:00
let sidebarView = note . appInfo ? . view
2017-05-27 19:43:27 +02:00
2017-09-24 21:24:44 +02:00
if sidebarView ? . window = = = tableView . window {
representedObjects = note . appInfo ? . objects
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
}
func fontSizeInDefaultsDidChange ( ) {
TimelineCellData . emptyCache ( )
RSSingleLineRenderer . emptyCache ( )
RSMultiLineRenderer . emptyCache ( )
2017-09-24 21:24:44 +02:00
let updatedFontSize = AppDefaults . shared . timelineFontSize
2017-05-27 19:43:27 +02:00
if updatedFontSize != self . fontSize {
self . fontSize = updatedFontSize
}
}
2017-10-09 06:06:25 +02:00
// MARK: - K e y b o a r d D e l e g a t e
2017-05-27 19:43:27 +02:00
func handleKeydownEvent ( _ event : NSEvent , sender : AnyObject ) -> Bool {
guard ! event . rs_keyIsModified ( ) else {
return false
}
guard let ch = event . rs_unmodifiedCharacterString ( ) else {
return false
}
let hasSelectedArticle = hasAtLeastOneSelectedArticle
var keyHandled = false
var shouldOpenInBrowser = false
switch ( ch ) {
case " \n " :
shouldOpenInBrowser = true
keyHandled = true
case " \r " :
shouldOpenInBrowser = true
keyHandled = true
case " r " :
markSelectedArticlesAsRead ( sender )
keyHandled = true
case " u " :
markSelectedArticlesAsUnread ( sender )
keyHandled = true
default :
keyHandled = false
}
if ! keyHandled {
let chUnichar = event . rs_unmodifiedCharacter ( )
switch ( chUnichar ) {
case keypadEnter :
shouldOpenInBrowser = true
keyHandled = true
default :
keyHandled = false
}
}
if shouldOpenInBrowser && hasSelectedArticle {
openArticleInBrowser ( self )
}
return keyHandled
}
2017-10-09 06:06:25 +02:00
// MARK: - R e l o a d i n g D a t a
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 > ) {
2017-11-02 06:40:28 +01:00
let indexes = articles . indexesForArticleIDs ( articleIDs )
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-11-02 06:40:28 +01:00
private func calculateRowHeight ( ) -> 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 ( ) )
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 , tags : nil , attachments : nil , status : status )
2017-05-27 19:43:27 +02:00
2017-11-26 06:27:35 +01:00
let prototypeCellData = TimelineCellData ( article : prototypeArticle , appearance : cellAppearance , showFeedName : false , favicon : nil , avatar : nil , 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-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
rowView . cellAppearance = 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 = cellAppearance
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 ? ) {
let appInfo = AppInfo ( )
if let article = selectedArticle {
appInfo . article = article
}
appInfo . view = tableView
NotificationCenter . default . post ( name : . TimelineSelectionDidChange , object : self , userInfo : appInfo . userInfo )
}
private func configureTimelineCell ( _ cell : TimelineTableCellView , article : Article ) {
cell . objectValue = article
2017-11-26 06:27:35 +01:00
let favicon = faviconFor ( article )
let avatar = avatarFor ( article )
let featuredImage = featuredImageFor ( article )
cell . cellData = TimelineCellData ( article : article , appearance : cellAppearance , showFeedName : showFeedNames , favicon : favicon , avatar : avatar , featuredImage : featuredImage )
}
private func faviconFor ( _ article : Article ) -> NSImage ? {
guard let feed = article . feed else {
return nil
}
return appDelegate . faviconDownloader . favicon ( for : feed )
}
private func avatarFor ( _ article : Article ) -> NSImage ? {
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 {
var hasAtLeastOneSelectedArticle : Bool {
get {
return tableView . selectedRow != - 1
}
2017-05-27 19:43:27 +02:00
}
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
// 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
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
let sortedArticles = Array ( fetchedArticles ) . sortedByDate ( )
2017-10-09 03:58:15 +02:00
if articles != sortedArticles {
articles = sortedArticles
}
2017-05-27 19:43:27 +02: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
}
}