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-02 06:40:28 +01:00
class TimelineViewController : NSViewController , KeyboardDelegate {
2017-05-27 19:43:27 +02:00
@IBOutlet var tableView : TimelineTableView !
2017-11-02 06:40:28 +01:00
var cellAppearance : TimelineCellAppearance !
var showFeedNames = false
var articles = ArticleArray ( ) {
2017-05-27 19:43:27 +02:00
didSet {
2017-11-02 06:40:28 +01:00
if articles != oldValue {
clearUndoableCommands ( )
tableView . reloadData ( )
}
2017-05-27 19:43:27 +02:00
}
}
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
private var undoableCommands = [ UndoableCommand ] ( )
private lazy var tableViewDataSource : TimelineTableViewDataSource ! = {
return TimelineTableViewDataSource ( timelineViewController : self )
} ( )
private lazy var tableViewDelegate : TimelineTableViewDelegate ! = {
return TimelineTableViewDelegate ( timelineViewController : self )
} ( )
private var didRegisterForNotifications = false
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
}
}
private var representedObjects : [ AnyObject ] ? {
didSet {
if ! representedObjectArraysAreEqual ( oldValue , representedObjects ) {
fetchArticles ( )
if articles . count > 0 {
tableView . scrollRowToVisible ( 0 )
}
}
}
}
private var oneSelectedArticle : Article ? {
get {
return selectedArticles . count = = 1 ? selectedArticles . first : nil
}
}
2017-09-24 21:24:44 +02:00
private let timelineFontSizeKVOKey = " values.{AppDefaults.Key.timelineFontSize} "
2017-05-27 19:43:27 +02:00
override func viewDidLoad ( ) {
cellAppearance = TimelineCellAppearance ( theme : currentTheme , fontSize : fontSize )
2017-11-02 06:40:28 +01:00
tableView . dataSource = tableViewDataSource
tableView . delegate = tableViewDelegate
tableView . rowHeight = calculateRowHeight ( )
2017-05-27 19:43:27 +02:00
tableView . target = self
tableView . doubleAction = #selector ( openArticleInBrowser ( _ : ) )
tableView . keyboardDelegate = self
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 ( ) {
cellAppearance = TimelineCellAppearance ( theme : currentTheme , fontSize : fontSize )
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
}
// MARK: - U n d o a b l e C o m m a n d s
private func runCommand ( _ undoableCommand : UndoableCommand ) {
pushUndoableCommand ( undoableCommand )
undoableCommand . perform ( )
2017-05-27 19:43:27 +02:00
}
2017-10-29 19:28:45 +01:00
private func pushUndoableCommand ( _ undoableCommand : UndoableCommand ) {
undoableCommands += [ undoableCommand ]
}
private func clearUndoableCommands ( ) {
2017-10-29 20:09:56 +01:00
// W h e n t h e t i m e l i n e i s r e l o a d e d a n d t h e l i s t o f a r t i c l e s c h a n g e s ,
2017-10-29 19:28:45 +01:00
// u n d o a b l e c o m m a n d s s h o u l d b e d r o p p e d — o t h e r w i s e t h i n g s l i k e
2017-10-29 20:09:56 +01:00
// R e d o M a r k R e a d a r e a m b i g u o u s . ( D o t h e y a p p l y t o t h e p r e v i o u s a r t i c l e s
2017-10-29 19:28:45 +01:00
// o r t o t h e c u r r e n t a r t i c l e s ? )
guard let undoManager = undoManager else {
return
}
undoableCommands . forEach { undoManager . removeAllActions ( withTarget : $0 ) }
undoableCommands = [ UndoableCommand ] ( )
}
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
}
@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 )
// t a b l e V i e w . r s _ s e l e c t R o w A n d S c r o l l T o V i s i b l e ( i x )
}
func canGoToNextUnread ( ) -> Bool {
guard let _ = indexOfNextUnreadArticle ( ) else {
return false
}
return true
}
func canMarkAllAsRead ( ) -> Bool {
2017-11-02 06:40:28 +01:00
return articles . canMarkAllAsRead ( )
2017-05-27 19:43:27 +02:00
}
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-02 06:40:28 +01:00
let prototypeCellData = TimelineCellData ( article : prototypeArticle , appearance : cellAppearance , showFeedName : false )
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-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
}
func representedObjectArraysAreEqual ( _ objects1 : [ AnyObject ] ? , _ objects2 : [ AnyObject ] ? ) -> Bool {
if objects1 = = nil && objects2 = = nil {
return true
}
guard let objects1 = objects1 , let objects2 = objects2 else {
return false
}
if objects1 . count != objects2 . count {
return false
}
var ix = 0
for oneObject in objects1 {
if oneObject !== objects2 [ ix ] {
return false
}
ix += 1
}
return true
}
}