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
import RSTree
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
class TimelineViewController : NSViewController , NSTableViewDelegate , NSTableViewDataSource , KeyboardDelegate {
@IBOutlet var tableView : TimelineTableView !
var didRegisterForNotifications = false
2017-09-24 21:24:44 +02:00
var fontSize : FontSize = AppDefaults . shared . timelineFontSize {
2017-05-27 19:43:27 +02:00
didSet {
fontSizeDidChange ( )
}
}
var cellAppearance : TimelineCellAppearance !
private var articles = [ Article ] ( ) {
didSet {
tableView . reloadData ( )
}
}
private var representedObjects : [ AnyObject ] ? {
didSet {
if ! representedObjectArraysAreEqual ( oldValue , representedObjects ) {
fetchArticles ( )
if articles . count > 0 {
tableView . scrollRowToVisible ( 0 )
}
}
}
}
private var showFeedNames : Bool {
// i f l e t _ = n o d e ? . r e p r e s e n t e d O b j e c t a s ? F e e d {
return false
// }
// r e t u r n t r u e
}
var selectedArticles : [ Article ] {
get {
2017-09-24 21:24:44 +02:00
return Array ( articlesForIndexes ( tableView . selectedRowIndexes ) )
2017-05-27 19:43:27 +02:00
}
}
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 )
tableView . rowHeight = calculateRowHeight ( )
tableView . target = self
tableView . doubleAction = #selector ( openArticleInBrowser ( _ : ) )
tableView . keyboardDelegate = self
if ! didRegisterForNotifications {
NotificationCenter . default . addObserver ( self , selector : #selector ( sidebarSelectionDidChange ( _ : ) ) , name : . SidebarSelectionDidChange , object : nil )
NotificationCenter . default . addObserver ( self , selector : #selector ( articleStatusesDidChange ( _ : ) ) , name : . ArticleStatusesDidChange , object : nil )
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 ( )
}
}
// MARK: A P I
func markAllAsRead ( ) {
if articles . isEmpty {
return
}
2017-09-18 02:03:58 +02:00
markArticles ( Set ( articles ) , statusKey : ArticleStatusKey . read . rawValue , flag : true )
2017-05-27 19:43:27 +02:00
reloadCellsForArticles ( articles )
}
// 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-05-27 19:43:27 +02:00
openInBrowser ( link )
}
}
@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-05-27 19:43:27 +02:00
2017-09-18 02:03:58 +02:00
markArticles ( Set ( articles ) , statusKey : ArticleStatusKey . read . rawValue , flag : markAsRead )
2017-05-27 19:43:27 +02:00
}
@IBAction func markSelectedArticlesAsRead ( _ sender : AnyObject ) {
2017-09-18 02:03:58 +02:00
markArticles ( Set ( selectedArticles ) , statusKey : ArticleStatusKey . read . rawValue , flag : true )
2017-05-27 19:43:27 +02:00
}
@IBAction func markSelectedArticlesAsUnread ( _ sender : AnyObject ) {
2017-09-18 02:03:58 +02:00
markArticles ( Set ( selectedArticles ) , statusKey : ArticleStatusKey . read . rawValue , flag : false )
2017-05-27 19:43:27 +02:00
}
// MARK: N a v i g a t i o n
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-09-18 02:03:58 +02:00
for article in articles {
2017-09-24 21:24:44 +02:00
if ! article . status . read {
2017-05-27 19:43:27 +02:00
return true
}
}
2017-09-18 02:03:58 +02:00
2017-05-27 19:43:27 +02:00
return false
}
func indexOfNextUnreadArticle ( ) -> Int ? {
if articles . isEmpty {
return nil
}
var ix = tableView . selectedRow
while ( true ) {
ix = ix + 1
if ix >= articles . count {
break
}
let article = articleAtRow ( ix ) !
2017-09-24 21:24:44 +02:00
if ! article . status . read {
2017-05-27 19:43:27 +02:00
return ix
}
}
return nil
}
// MARK: N o t i f i c a t i o n s
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-09-17 21:54:08 +02:00
@objc func articleStatusesDidChange ( _ note : Notification ) {
2017-09-24 21:24:44 +02:00
guard let articles = note . appInfo ? . articles else {
2017-05-27 19:43:27 +02:00
return
}
2017-09-25 22:31:36 +02:00
reloadCellsForArticles ( Array ( articles ) )
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
}
}
// MARK: K e y b o a r d D e l e g a t e
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
}
// MARK: R e l o a d i n g D a t a
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
let indexes = indexesForArticles ( articles )
tableView . reloadData ( forRowIndexes : indexes , columnIndexes : NSIndexSet ( index : 0 ) as IndexSet )
}
// MARK: A r t i c l e s
2017-09-25 22:31:36 +02:00
private func indexesForArticles ( _ articles : [ Article ] ) -> IndexSet {
2017-05-27 19:43:27 +02:00
var indexes = IndexSet ( )
articles . forEach { ( article ) in
let oneIndex = rowForArticle ( article )
if oneIndex != NSNotFound {
indexes . insert ( oneIndex )
}
}
return indexes
}
2017-09-24 21:24:44 +02:00
private func articlesForIndexes ( _ indexes : IndexSet ) -> Set < Article > {
2017-05-27 19:43:27 +02:00
2017-09-24 21:24:44 +02:00
return Set ( indexes . flatMap { ( oneIndex ) -> Article ? in
2017-05-27 19:43:27 +02:00
return articleAtRow ( oneIndex )
2017-09-24 21:24:44 +02:00
} )
2017-05-27 19:43:27 +02:00
}
private func articleAtRow ( _ row : Int ) -> Article ? {
if row < 0 || row = = NSNotFound || row > articles . count - 1 {
return nil
}
return articles [ row ]
}
private func rowForArticle ( _ article : Article ) -> Int {
2017-09-18 02:12:42 +02:00
if let index = articles . index ( where : { $0 . articleID = = article . articleID } ) {
2017-05-27 19:43:27 +02:00
return index
}
return NSNotFound
}
func selectedArticle ( ) -> Article ? {
return articleAtRow ( tableView . selectedRow )
}
// MARK: S o r t i n g A r t i c l e s
private func articleComparator ( _ article1 : Article , article2 : Article ) -> Bool {
return article1 . logicalDatePublished . compare ( article2 . logicalDatePublished ) = = . orderedDescending
}
// MARK: F e t c h i n g A r t i c l e s
private func fetchArticles ( ) {
2017-09-24 21:24:44 +02:00
// g u a r d l e t r e p r e s e n t e d O b j e c t s = r e p r e s e n t e d O b j e c t s e l s e {
// i f ! a r t i c l e s . i s E m p t y {
// a r t i c l e s = [ A r t i c l e ] ( )
// }
// r e t u r n
// }
//
// v a r a c c o u n t s D i c t i o n a r y = [ S t r i n g : [ A n y O b j e c t ] ] ( )
//
// f u n c a d d T o A c c o u n t A r r a y ( a c c o u n t I D : S t r i n g , o b j e c t : A n y O b j e c t ) {
//
// i f l e t a c c o u n t A r r a y = a c c o u n t s D i c t i o n a r y [ a c c o u n t I D ] {
// i f ! a c c o u n t A r r a y . c o n t a i n s ( w h e r e : { $ 0 = = = o b j e c t } ) {
// a c c o u n t s D i c t i o n a r y [ a c c o u n t I D ] = a c c o u n t A r r a y + [ o b j e c t ]
// }
// }
// e l s e {
// a c c o u n t s D i c t i o n a r y [ a c c o u n t I D ] = [ o b j e c t ]
// }
// }
//
// f o r o n e O b j e c t i n r e p r e s e n t e d O b j e c t s {
//
// i f l e t o n e F e e d = o n e O b j e c t a s ? F e e d {
// a d d T o A c c o u n t A r r a y ( a c c o u n t I D : o n e F e e d . a c c o u n t . a c c o u n t I D , o b j e c t : o n e F e e d )
// }
// e l s e i f l e t o n e F o l d e r = o n e O b j e c t a s ? F o l d e r , l e t a c c o u n t I D = o n e F o l d e r . a c c o u n t ? . a c c o u n t I D {
// a d d T o A c c o u n t A r r a y ( a c c o u n t I D : a c c o u n t I D , o b j e c t : o n e F o l d e r )
// }
// }
//
// v a r f e t c h e d A r t i c l e s = [ A r t i c l e ] ( )
// f o r ( a c c o u n t I D , o b j e c t s ) i n a c c o u n t s D i c t i o n a r y {
//
// g u a r d l e t o n e A c c o u n t = a c c o u n t W i t h I D ( a c c o u n t I D ) e l s e {
// c o n t i n u e
// }
//
// l e t o n e F e t c h e d A r t i c l e s = o n e A c c o u n t . f e t c h A r t i c l e s ( f o r : o b j e c t s )
// f o r o n e F e t c h e d A r t i c l e i n o n e F e t c h e d A r t i c l e s {
// i f ! f e t c h e d A r t i c l e s . c o n t a i n s ( w h e r e : { $ 0 = = = o n e F e t c h e d A r t i c l e } ) {
// f e t c h e d A r t i c l e s + = [ o n e F e t c h e d A r t i c l e ]
// }
// }
// }
//
// f e t c h e d A r t i c l e s . s o r t ( b y : a r t i c l e C o m p a r a t o r )
//
// i f a r t i c l e s ! = f e t c h e d A r t i c l e s {
// a r t i c l e s = f e t c h e d A r t i c l e s
// }
2017-05-27 19:43:27 +02:00
}
// MARK: C e l l C o n f i g u r i n g
private func calculateRowHeight ( ) -> CGFloat {
2017-09-18 02:56:04 +02: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? "
2017-09-24 21:24:44 +02:00
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
let prototypeCellData = TimelineCellData ( article : prototypeArticle , appearance : cellAppearance , showFeedName : false )
let height = timelineCellHeight ( 100 , cellData : prototypeCellData , appearance : cellAppearance )
return height
}
private func configureTimelineCell ( _ cell : TimelineTableCellView , article : Article ) {
cell . objectValue = article
cell . cellData = TimelineCellData ( article : article , appearance : cellAppearance , showFeedName : showFeedNames )
}
private func makeTimelineCellEmpty ( _ cell : TimelineTableCellView ) {
cell . objectValue = nil
cell . cellData = emptyCellData
}
// MARK: N S T a b l e V i e w D a t a S o u r c e
func numberOfRows ( in tableView : NSTableView ) -> Int {
return articles . count
}
func tableView ( _ tableView : NSTableView , objectValueFor tableColumn : NSTableColumn ? , row : Int ) -> Any ? {
return articleAtRow ( row )
}
// MARK: N S T a b l e V i e w D e l e g a t e
func tableView ( _ tableView : NSTableView , rowViewForRow row : Int ) -> NSTableRowView ? {
2017-09-19 07:00:35 +02:00
let rowView : TimelineTableRowView = tableView . makeView ( withIdentifier : NSUserInterfaceItemIdentifier ( rawValue : " timelineRow " ) , owner : self ) as ! TimelineTableRowView
2017-05-27 19:43:27 +02:00
rowView . cellAppearance = cellAppearance
return rowView
}
func tableView ( _ tableView : NSTableView , viewFor tableColumn : NSTableColumn ? , row : Int ) -> NSView ? {
2017-09-19 07:00:35 +02:00
let cell : TimelineTableCellView = tableView . makeView ( withIdentifier : NSUserInterfaceItemIdentifier ( rawValue : " timelineCell " ) , owner : self ) as ! TimelineTableCellView
2017-05-27 19:43:27 +02:00
cell . cellAppearance = cellAppearance
if let article = articleAtRow ( row ) {
configureTimelineCell ( cell , article : article )
}
else {
makeTimelineCellEmpty ( cell )
}
return cell
}
private func postTimelineSelectionDidChangeNotification ( _ selectedArticle : Article ? ) {
2017-09-24 21:24:44 +02:00
let appInfo = AppInfo ( )
2017-05-27 19:43:27 +02:00
if let article = selectedArticle {
2017-09-24 21:24:44 +02:00
appInfo . article = article
2017-05-27 19:43:27 +02:00
}
2017-09-24 21:24:44 +02:00
appInfo . view = tableView
2017-05-27 19:43:27 +02:00
2017-09-24 21:24:44 +02:00
NotificationCenter . default . post ( name : . TimelineSelectionDidChange , object : self , userInfo : appInfo . userInfo )
2017-05-27 19:43:27 +02:00
}
func tableViewSelectionDidChange ( _ notification : Notification ) {
tableView . redrawGrid ( )
let selectedRow = tableView . selectedRow
if selectedRow < 0 || selectedRow = = NSNotFound || tableView . numberOfSelectedRows != 1 {
postTimelineSelectionDidChangeNotification ( nil )
return
}
if let selectedArticle = articleAtRow ( selectedRow ) {
2017-09-24 21:24:44 +02:00
if ( ! selectedArticle . status . read ) {
2017-09-18 02:03:58 +02:00
markArticles ( Set ( [ selectedArticle ] ) , statusKey : ArticleStatusKey . read . rawValue , flag : true )
2017-05-27 19:43:27 +02:00
}
postTimelineSelectionDidChangeNotification ( selectedArticle )
}
else {
postTimelineSelectionDidChangeNotification ( nil )
}
}
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
}
}
private extension TimelineViewController {
var hasAtLeastOneSelectedArticle : Bool {
get {
return self . tableView . selectedRow != - 1
}
}
}
private extension NSTableView {
func scrollTo ( row : Int ) {
guard let scrollView = self . enclosingScrollView else {
return
}
let documentVisibleRect = scrollView . documentVisibleRect
let r = rect ( ofRow : row )
if NSContainsRect ( documentVisibleRect , r ) {
return
}
let rMidY = NSMidY ( r )
var scrollPoint = NSZeroPoint ;
let extraHeight = 150
scrollPoint . y = floor ( rMidY - ( documentVisibleRect . size . height / 2.0 ) ) + CGFloat ( extraHeight )
scrollPoint . y = max ( scrollPoint . y , 0 )
let maxScrollPointY = frame . size . height - documentVisibleRect . size . height
scrollPoint . y = min ( maxScrollPointY , scrollPoint . y )
let clipView = scrollView . contentView
let rClipView = NSMakeRect ( scrollPoint . x , scrollPoint . y , NSWidth ( clipView . bounds ) , NSHeight ( clipView . bounds ) )
clipView . animator ( ) . bounds = rClipView
}
func visibleRowViews ( ) -> [ TimelineTableRowView ] ? {
guard let scrollView = self . enclosingScrollView , numberOfRows > 0 else {
return nil
}
let range = rows ( in : scrollView . documentVisibleRect )
let ixMax = numberOfRows - 1
let ixStart = min ( range . location , ixMax )
let ixEnd = min ( ( ( range . location + range . length ) - 1 ) , ixMax )
var visibleRows = [ TimelineTableRowView ] ( )
for ixRow in ixStart . . . ixEnd {
if let oneRowView = rowView ( atRow : ixRow , makeIfNecessary : false ) as ? TimelineTableRowView {
visibleRows += [ oneRowView ]
}
}
return visibleRows . isEmpty ? nil : visibleRows
}
func redrawGrid ( ) {
visibleRowViews ( ) ? . forEach { $0 . invalidateGridRect ( ) }
}
}