2019-04-15 22:03:05 +02:00
//
// M a s t e r 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
// N e t N e w s W i r e
//
// C r e a t e d b y M a u r i c e P a r k e r o n 4 / 8 / 1 9 .
// C o p y r i g h t © 2 0 1 9 R a n c h e r o S o f t w a r e . A l l r i g h t s r e s e r v e d .
//
import UIKit
import RSCore
import Account
import Articles
2019-04-17 01:08:02 +02:00
class MasterTimelineViewController : UITableViewController , UndoableCommandRunner {
2019-04-15 22:03:05 +02:00
private var rowHeightWithFeedName : CGFloat = 0.0
private var rowHeightWithoutFeedName : CGFloat = 0.0
private var currentRowHeight : CGFloat {
2019-04-21 21:34:51 +02:00
return nmc . showFeedNames ? rowHeightWithFeedName : rowHeightWithoutFeedName
2019-04-15 22:03:05 +02:00
}
2019-04-21 21:34:51 +02:00
var nmc : NavigationModelController !
2019-04-21 20:57:23 +02:00
var undoableCommands = [ UndoableCommand ] ( )
2019-04-15 22:03:05 +02:00
var detailViewController : DetailViewController ? {
if let split = splitViewController {
let controllers = split . viewControllers
return ( controllers [ controllers . count - 1 ] as ! UINavigationController ) . topViewController as ? DetailViewController
}
return nil
}
2019-04-21 20:57:23 +02:00
2019-04-17 01:08:02 +02:00
override var canBecomeFirstResponder : Bool {
return true
}
2019-04-15 22:03:05 +02:00
override func viewDidLoad ( ) {
super . viewDidLoad ( )
updateRowHeights ( )
NotificationCenter . default . addObserver ( self , selector : #selector ( statusesDidChange ( _ : ) ) , name : . StatusesDidChange , object : nil )
NotificationCenter . default . addObserver ( self , selector : #selector ( feedIconDidBecomeAvailable ( _ : ) ) , name : . FeedIconDidBecomeAvailable , object : nil )
NotificationCenter . default . addObserver ( self , selector : #selector ( avatarDidBecomeAvailable ( _ : ) ) , name : . AvatarDidBecomeAvailable , object : nil )
NotificationCenter . default . addObserver ( self , selector : #selector ( imageDidBecomeAvailable ( _ : ) ) , name : . ImageDidBecomeAvailable , object : nil )
NotificationCenter . default . addObserver ( self , selector : #selector ( imageDidBecomeAvailable ( _ : ) ) , name : . FaviconDidBecomeAvailable , object : nil )
2019-04-18 21:36:22 +02:00
NotificationCenter . default . addObserver ( self , selector : #selector ( progressDidChange ( _ : ) ) , name : . AccountRefreshProgressDidChange , object : nil )
2019-04-15 22:03:05 +02:00
2019-04-21 21:34:51 +02:00
NotificationCenter . default . addObserver ( self , selector : #selector ( articlesReinitialized ( _ : ) ) , name : . ArticlesReinitialized , object : nmc )
NotificationCenter . default . addObserver ( self , selector : #selector ( showFeedNamesDidChange ( _ : ) ) , name : . ShowFeedNamesDidChange , object : nmc )
NotificationCenter . default . addObserver ( self , selector : #selector ( articleDataDidChange ( _ : ) ) , name : . ArticleDataDidChange , object : nmc )
NotificationCenter . default . addObserver ( self , selector : #selector ( articlesDidChange ( _ : ) ) , name : . ArticlesDidChange , object : nmc )
2019-04-21 20:57:23 +02:00
2019-04-18 21:36:22 +02:00
refreshControl = UIRefreshControl ( )
refreshControl ! . addTarget ( self , action : #selector ( refreshAccounts ( _ : ) ) , for : . valueChanged )
2019-04-15 22:03:05 +02:00
}
2019-04-17 01:08:02 +02:00
override func viewDidAppear ( _ animated : Bool ) {
super . viewDidAppear ( animated )
becomeFirstResponder ( )
}
override func viewWillDisappear ( _ animated : Bool ) {
super . viewWillDisappear ( animated )
resignFirstResponder ( )
}
2019-04-15 22:03:05 +02:00
override func prepare ( for segue : UIStoryboardSegue , sender : Any ? ) {
if segue . identifier = = " showDetail " {
if let indexPath = tableView . indexPathForSelectedRow {
let controller = ( segue . destination as ! UINavigationController ) . topViewController as ! DetailViewController
2019-04-21 21:34:51 +02:00
let article = nmc . articles [ indexPath . row ]
2019-04-15 22:03:05 +02:00
controller . article = article
controller . navigationItem . leftBarButtonItem = splitViewController ? . displayModeButtonItem
controller . navigationItem . leftItemsSupplementBackButton = true
2019-04-18 15:33:39 +02:00
splitViewController ? . toggleMasterView ( )
2019-04-15 22:03:05 +02:00
}
}
}
// M A R K A c t i o n s
@IBAction func markAllAsRead ( _ sender : Any ) {
let title = NSLocalizedString ( " Mark All Read " , comment : " Mark All Read " )
let message = NSLocalizedString ( " Mark all articles in this timeline as read? " , comment : " Mark all articles " )
let alertController = UIAlertController ( title : title , message : message , preferredStyle : . alert )
let cancelTitle = NSLocalizedString ( " Cancel " , comment : " Cancel " )
let cancelAction = UIAlertAction ( title : cancelTitle , style : . cancel )
alertController . addAction ( cancelAction )
let markTitle = NSLocalizedString ( " Mark All Read " , comment : " Mark All Read " )
let markAction = UIAlertAction ( title : markTitle , style : . default ) { [ weak self ] ( action ) in
2019-04-17 01:08:02 +02:00
2019-04-21 21:34:51 +02:00
guard let articles = self ? . nmc . articles ,
2019-04-17 01:08:02 +02:00
let undoManager = self ? . undoManager ,
let markReadCommand = MarkStatusCommand ( initialArticles : articles , markingRead : true , undoManager : undoManager ) else {
return
}
self ? . runCommand ( markReadCommand )
2019-04-15 22:03:05 +02:00
}
alertController . addAction ( markAction )
present ( alertController , animated : true )
}
// MARK: - T a b l e v i e w
override func numberOfSections ( in tableView : UITableView ) -> Int {
return 1
}
override func tableView ( _ tableView : UITableView , numberOfRowsInSection section : Int ) -> Int {
2019-04-21 21:34:51 +02:00
return nmc . articles . count
2019-04-15 22:03:05 +02:00
}
override func tableView ( _ tableView : UITableView , trailingSwipeActionsConfigurationForRowAt indexPath : IndexPath ) -> UISwipeActionsConfiguration ? {
2019-04-21 21:34:51 +02:00
let article = nmc . articles [ indexPath . row ]
2019-04-15 22:03:05 +02:00
// S e t u p t h e s t a r a c t i o n
let starTitle = article . status . starred ?
NSLocalizedString ( " Unstar " , comment : " Unstar " ) :
NSLocalizedString ( " Star " , comment : " Star " )
2019-04-17 01:08:02 +02:00
let starAction = UIContextualAction ( style : . normal , title : starTitle ) { [ weak self ] ( action , view , completionHandler ) in
guard let undoManager = self ? . undoManager ,
let markReadCommand = MarkStatusCommand ( initialArticles : [ article ] , markingStarred : ! article . status . starred , undoManager : undoManager ) else {
return
}
self ? . runCommand ( markReadCommand )
2019-04-15 22:03:05 +02:00
completionHandler ( true )
}
starAction . image = AppAssets . starClosedImage
starAction . backgroundColor = AppAssets . starColor
// S e t u p t h e r e a d a c t i o n
let readTitle = article . status . read ?
NSLocalizedString ( " Unread " , comment : " Unread " ) :
NSLocalizedString ( " Read " , comment : " Read " )
2019-04-17 01:08:02 +02:00
let readAction = UIContextualAction ( style : . normal , title : readTitle ) { [ weak self ] ( action , view , completionHandler ) in
guard let undoManager = self ? . undoManager ,
let markReadCommand = MarkStatusCommand ( initialArticles : [ article ] , markingRead : ! article . status . read , undoManager : undoManager ) else {
return
}
self ? . runCommand ( markReadCommand )
2019-04-15 22:03:05 +02:00
completionHandler ( true )
}
readAction . image = AppAssets . circleClosedImage
readAction . backgroundColor = AppAssets . timelineUnreadCircleColor
let configuration = UISwipeActionsConfiguration ( actions : [ starAction , readAction ] )
return configuration
}
override func tableView ( _ tableView : UITableView , cellForRowAt indexPath : IndexPath ) -> UITableViewCell {
let cell = tableView . dequeueReusableCell ( withIdentifier : " Cell " , for : indexPath ) as ! MasterTimelineTableViewCell
2019-04-21 21:34:51 +02:00
let article = nmc . articles [ indexPath . row ]
2019-04-15 22:03:05 +02:00
configureTimelineCell ( cell , article : article )
return cell
}
override func tableView ( _ tableView : UITableView , didSelectRowAt indexPath : IndexPath ) {
2019-04-21 21:34:51 +02:00
let article = nmc . articles [ indexPath . row ]
2019-04-15 22:03:05 +02:00
if ! article . status . read {
markArticles ( Set ( [ article ] ) , statusKey : . read , flag : true )
}
}
// MARK: N o t i f i c a t i o n s
2019-04-18 21:36:22 +02:00
@objc dynamic func progressDidChange ( _ notification : Notification ) {
if AccountManager . shared . combinedRefreshProgress . isComplete {
refreshControl ? . endRefreshing ( )
} else {
refreshControl ? . beginRefreshing ( )
}
}
2019-04-15 22:03:05 +02:00
@objc func statusesDidChange ( _ note : Notification ) {
guard let articles = note . userInfo ? [ Account . UserInfoKey . articles ] as ? Set < Article > else {
return
}
reloadVisibleCells ( for : articles )
}
@objc func feedIconDidBecomeAvailable ( _ note : Notification ) {
guard let feed = note . userInfo ? [ UserInfoKey . feed ] as ? Feed else {
return
}
performBlockAndRestoreSelection {
tableView . indexPathsForVisibleRows ? . forEach { indexPath in
2019-04-21 21:34:51 +02:00
guard let article = nmc . articles . articleAtRow ( indexPath . row ) else {
2019-04-15 22:03:05 +02:00
return
}
if feed = = article . feed {
tableView . reloadRows ( at : [ indexPath ] , with : . none )
return
}
}
}
}
@objc func avatarDidBecomeAvailable ( _ note : Notification ) {
2019-04-21 21:34:51 +02:00
guard nmc . showAvatars , let avatarURL = note . userInfo ? [ UserInfoKey . url ] as ? String else {
2019-04-15 22:03:05 +02:00
return
}
performBlockAndRestoreSelection {
tableView . indexPathsForVisibleRows ? . forEach { indexPath in
2019-04-21 21:34:51 +02:00
guard let article = nmc . articles . articleAtRow ( indexPath . row ) , let authors = article . authors , ! authors . isEmpty else {
2019-04-15 22:03:05 +02:00
return
}
for author in authors {
if author . avatarURL = = avatarURL {
tableView . reloadRows ( at : [ indexPath ] , with : . none )
}
}
}
}
}
@objc func imageDidBecomeAvailable ( _ note : Notification ) {
2019-04-21 21:34:51 +02:00
if nmc . showAvatars {
2019-04-15 22:03:05 +02:00
queueReloadVisableCells ( )
}
}
2019-04-21 20:57:23 +02:00
@objc func articlesReinitialized ( _ note : Notification ) {
2019-04-21 21:34:51 +02:00
if nmc . articles . count > 0 {
2019-04-21 20:57:23 +02:00
tableView . scrollToRow ( at : IndexPath ( row : 0 , section : 0 ) , at : . top , animated : false )
2019-04-15 22:03:05 +02:00
}
}
2019-04-21 20:57:23 +02:00
@objc func showFeedNamesDidChange ( _ note : Notification ) {
updateTableViewRowHeight ( )
2019-04-15 22:03:05 +02:00
}
2019-04-21 20:57:23 +02:00
@objc func articleDataDidChange ( _ note : Notification ) {
reloadAllVisibleCells ( )
}
@objc func articlesDidChange ( _ note : Notification ) {
tableView . reloadData ( )
}
2019-04-15 22:03:05 +02:00
// MARK: R e l o a d i n g
@objc func reloadAllVisibleCells ( ) {
tableView . beginUpdates ( )
performBlockAndRestoreSelection {
tableView . reloadRows ( at : tableView . indexPathsForVisibleRows ! , with : . none )
}
tableView . endUpdates ( )
}
private func reloadVisibleCells ( for articles : [ Article ] ) {
reloadVisibleCells ( for : Set ( articles . articleIDs ( ) ) )
}
private func reloadVisibleCells ( for articles : Set < Article > ) {
reloadVisibleCells ( for : articles . articleIDs ( ) )
}
private func reloadVisibleCells ( for articleIDs : Set < String > ) {
if articleIDs . isEmpty {
return
}
2019-04-21 21:34:51 +02:00
let indexes = nmc . indexesForArticleIDs ( articleIDs )
2019-04-15 22:03:05 +02:00
reloadVisibleCells ( for : indexes )
}
private func reloadVisibleCells ( for indexes : IndexSet ) {
performBlockAndRestoreSelection {
tableView . indexPathsForVisibleRows ? . forEach { indexPath in
if indexes . contains ( indexPath . row ) {
tableView . reloadRows ( at : [ indexPath ] , with : . none )
}
}
}
}
// MARK: C e l l C o n f i g u r i n g
private func calculateRowHeight ( showingFeedNames : Bool ) -> CGFloat {
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 , attachments : nil , status : status )
let prototypeCellData = MasterTimelineCellData ( article : prototypeArticle , showFeedName : showingFeedNames , feedName : " Prototype Feed Name " , avatar : nil , showAvatar : false , featuredImage : nil )
let height = MasterTimelineCellLayout . height ( for : 100 , cellData : prototypeCellData )
return height
}
private func updateRowHeights ( ) {
rowHeightWithFeedName = calculateRowHeight ( showingFeedNames : true )
rowHeightWithoutFeedName = calculateRowHeight ( showingFeedNames : false )
updateTableViewRowHeight ( )
}
}
// MARK: P r i v a t e
private extension MasterTimelineViewController {
2019-04-18 21:36:22 +02:00
@objc private func refreshAccounts ( _ sender : Any ) {
AccountManager . shared . refreshAll ( )
}
2019-04-15 22:03:05 +02:00
func configureTimelineCell ( _ cell : MasterTimelineTableViewCell , article : Article ) {
var avatar = avatarFor ( article )
if avatar = = nil , let feed = article . feed {
avatar = appDelegate . faviconDownloader . favicon ( for : feed )
}
let featuredImage = featuredImageFor ( article )
2019-04-21 21:34:51 +02:00
cell . cellData = MasterTimelineCellData ( article : article , showFeedName : nmc . showFeedNames , feedName : article . feed ? . nameForDisplay , avatar : avatar , showAvatar : nmc . showAvatars , featuredImage : featuredImage )
2019-04-15 22:03:05 +02:00
}
func avatarFor ( _ article : Article ) -> UIImage ? {
2019-04-21 21:34:51 +02:00
if ! nmc . showAvatars {
2019-04-15 22:03:05 +02:00
return nil
}
if let authors = article . authors {
for author in authors {
if let image = avatarForAuthor ( author ) {
return image
}
}
}
guard let feed = article . feed else {
return nil
}
return appDelegate . feedIconDownloader . icon ( for : feed )
}
func avatarForAuthor ( _ author : Author ) -> UIImage ? {
return appDelegate . authorAvatarDownloader . image ( for : author )
}
func featuredImageFor ( _ article : Article ) -> UIImage ? {
if let url = article . imageURL , let data = appDelegate . imageDownloader . image ( for : url ) {
return RSImage ( data : data )
}
return nil
}
func queueReloadVisableCells ( ) {
CoalescingQueue . standard . add ( self , #selector ( reloadAllVisibleCells ) )
}
func updateTableViewRowHeight ( ) {
tableView . rowHeight = currentRowHeight
tableView . estimatedRowHeight = currentRowHeight
}
func performBlockAndRestoreSelection ( _ block : ( ( ) -> Void ) ) {
let indexPaths = tableView . indexPathsForSelectedRows
block ( )
indexPaths ? . forEach { [ weak self ] indexPath in
self ? . tableView . selectRow ( at : indexPath , animated : false , scrollPosition : . none )
}
}
}