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-07-27 21:49:07 +02:00
class MasterTimelineViewController : UITableViewController , UndoableCommandRunner {
2019-04-17 01:08:02 +02:00
2019-09-24 03:53:09 +02:00
private var titleView : MasterTimelineTitleView ?
2019-04-29 22:29:00 +02:00
private var numberOfTextLines = 0
2019-11-17 02:44:01 +01:00
private var iconSize = IconSize . medium
2019-04-15 22:03:05 +02:00
2019-11-22 01:22:43 +01:00
@IBOutlet weak var filterButton : UIBarButtonItem !
2019-04-23 11:35:48 +02:00
@IBOutlet weak var markAllAsReadButton : UIBarButtonItem !
@IBOutlet weak var firstUnreadButton : UIBarButtonItem !
2019-04-22 22:31:34 +02:00
2019-08-30 21:17:05 +02:00
private lazy var dataSource = makeDataSource ( )
2019-08-31 18:50:34 +02:00
private let searchController = UISearchController ( searchResultsController : nil )
2019-09-01 19:43:07 +02:00
weak var coordinator : SceneCoordinator !
2019-04-21 20:57:23 +02:00
var undoableCommands = [ UndoableCommand ] ( )
2019-11-11 23:59:42 +01:00
let scrollPositionQueue = CoalescingQueue ( name : " Scroll Position " , interval : 0.3 , maxInterval : 1.0 )
2019-08-31 18:50:34 +02:00
2019-09-05 21:37:07 +02:00
private let keyboardManager = KeyboardManager ( type : . timeline )
2019-09-04 23:24:16 +02:00
override var keyCommands : [ UIKeyCommand ] ? {
return keyboardManager . keyCommands
}
2019-04-17 01:08:02 +02:00
override var canBecomeFirstResponder : Bool {
return true
}
2019-09-04 23:24:16 +02:00
2019-04-15 22:03:05 +02:00
override func viewDidLoad ( ) {
super . viewDidLoad ( )
2019-08-30 21:17:05 +02:00
2019-04-23 11:35:48 +02:00
NotificationCenter . default . addObserver ( self , selector : #selector ( unreadCountDidChange ( _ : ) ) , name : . UnreadCountDidChange , object : nil )
2019-04-15 22:03:05 +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 )
2019-04-15 22:03:05 +02:00
NotificationCenter . default . addObserver ( self , selector : #selector ( avatarDidBecomeAvailable ( _ : ) ) , name : . AvatarDidBecomeAvailable , object : nil )
2019-08-22 02:37:19 +02:00
NotificationCenter . default . addObserver ( self , selector : #selector ( faviconDidBecomeAvailable ( _ : ) ) , name : . FaviconDidBecomeAvailable , object : nil )
2019-04-29 22:29:00 +02:00
NotificationCenter . default . addObserver ( self , selector : #selector ( userDefaultsDidChange ( _ : ) ) , name : UserDefaults . didChangeNotification , object : nil )
2019-04-29 21:50:56 +02:00
NotificationCenter . default . addObserver ( self , selector : #selector ( contentSizeCategoryDidChange ) , name : UIContentSizeCategory . didChangeNotification , object : nil )
2019-09-28 02:45:09 +02:00
NotificationCenter . default . addObserver ( self , selector : #selector ( displayNameDidChange ) , name : . DisplayNameDidChange , object : nil )
2019-10-29 03:12:09 +01:00
// S e t u p t h e S e a r c h C o n t r o l l e r
searchController . delegate = self
searchController . searchResultsUpdater = self
searchController . obscuresBackgroundDuringPresentation = false
searchController . searchBar . delegate = self
searchController . searchBar . placeholder = NSLocalizedString ( " Search Articles " , comment : " Search Articles " )
searchController . searchBar . scopeButtonTitles = [
NSLocalizedString ( " Here " , comment : " Here " ) ,
NSLocalizedString ( " All Articles " , comment : " All Articles " )
]
2019-11-02 17:49:44 +01:00
navigationItem . searchController = searchController
2019-10-29 03:12:09 +01:00
definesPresentationContext = true
2019-08-31 18:50:34 +02:00
// C o n f i g u r e t h e t a b l e
tableView . dataSource = dataSource
2019-04-29 22:29:00 +02:00
numberOfTextLines = AppDefaults . timelineNumberOfLines
2019-11-09 00:16:09 +01:00
iconSize = AppDefaults . timelineIconSize
2019-04-29 23:29:53 +02:00
resetEstimatedRowHeight ( )
2019-04-29 22:29:00 +02:00
2019-04-23 01:00:26 +02:00
resetUI ( )
2019-11-19 18:16:43 +01:00
applyChanges ( animated : false )
2019-11-11 23:59:42 +01:00
2019-11-24 17:33:13 +01:00
title = coordinator . timelineFeed ? . nameForDisplay ? ? " Timeline "
2019-11-20 01:16:03 +01:00
2019-11-11 23:59:42 +01:00
// R e s t o r e t h e s c r o l l p o s i t i o n i f w e h a v e o n e s t o r e d
if let restoreIndexPath = coordinator . timelineMiddleIndexPath {
tableView . scrollToRow ( at : restoreIndexPath , at : . middle , animated : false )
}
2019-11-12 03:45:14 +01:00
}
2019-11-24 01:00:51 +01:00
override func viewWillAppear ( _ animated : Bool ) {
// I f t h e n a v b a r i s h i d d e n , f a d e i t i n t o a v o i d i t s h o w i n g s t u f f a s i t i s g e t t i n g l a i d o u t
if navigationController ? . navigationBar . isHidden ? ? false {
navigationController ? . navigationBar . alpha = 0
}
}
2019-11-19 02:12:24 +01:00
override func viewDidAppear ( _ animated : Bool ) {
super . viewDidAppear ( true )
coordinator . isTimelineViewControllerPending = false
2019-11-24 01:00:51 +01:00
if navigationController ? . navigationBar . alpha = = 0 {
UIView . animate ( withDuration : 0.5 ) {
self . navigationController ? . navigationBar . alpha = 1
}
}
2019-11-19 02:12:24 +01:00
}
2019-09-05 04:06:29 +02:00
// MARK: A c t i o n s
2019-11-22 01:22:43 +01:00
@IBAction func toggleFilter ( _ sender : Any ) {
2019-11-22 16:40:39 +01:00
switch coordinator . articleReadFilterType {
2019-11-22 02:54:35 +01:00
case . none :
2019-11-22 01:22:43 +01:00
filterButton . image = AppAssets . filterActiveImage
coordinator . hideUnreadArticles ( )
case . read :
filterButton . image = AppAssets . filterInactiveImage
coordinator . showAllArticles ( )
2019-11-22 16:32:27 +01:00
case . alwaysRead :
2019-11-24 10:47:29 +01:00
break
2019-11-22 01:22:43 +01:00
}
}
2019-04-15 22:03:05 +02:00
@IBAction func markAllAsRead ( _ sender : Any ) {
2019-10-10 22:13:20 +02:00
if coordinator . displayUndoAvailableTip {
let alertController = UndoAvailableAlertController . alert { [ weak self ] _ in
self ? . coordinator . displayUndoAvailableTip = false
2019-10-08 02:33:30 +02:00
self ? . coordinator . markAllAsReadInTimeline ( )
}
present ( alertController , animated : true )
} else {
coordinator . markAllAsReadInTimeline ( )
2019-04-15 22:03:05 +02:00
}
}
2019-04-23 11:35:48 +02:00
@IBAction func firstUnread ( _ sender : Any ) {
2019-10-10 04:39:11 +02:00
coordinator . selectFirstUnread ( )
2019-04-23 01:00:26 +02:00
}
2019-09-05 04:06:29 +02:00
// MARK: K e y b o a r d s h o r t c u t s
@objc func selectNextUp ( _ sender : Any ? ) {
coordinator . selectPrevArticle ( )
}
@objc func selectNextDown ( _ sender : Any ? ) {
coordinator . selectNextArticle ( )
}
@objc func navigateToSidebar ( _ sender : Any ? ) {
coordinator . navigateToFeeds ( )
}
@objc func navigateToDetail ( _ sender : Any ? ) {
coordinator . navigateToDetail ( )
}
2019-09-28 02:45:09 +02:00
@objc func showFeedInspector ( _ sender : UITapGestureRecognizer ) {
coordinator . showFeedInspector ( )
}
2019-08-25 18:38:04 +02:00
// MARK: A P I
2019-09-29 22:53:50 +02:00
func restoreSelectionIfNecessary ( adjustScroll : Bool ) {
2019-09-11 16:11:33 +02:00
if let article = coordinator . currentArticle , let indexPath = dataSource . indexPath ( for : article ) {
2019-09-29 22:53:50 +02:00
if adjustScroll {
2019-10-23 02:20:35 +02:00
tableView . selectRowAndScrollIfNotVisible ( at : indexPath , animated : false )
2019-09-29 22:53:50 +02:00
} else {
tableView . selectRow ( at : indexPath , animated : false , scrollPosition : . none )
}
2019-09-10 15:06:43 +02:00
}
}
2019-08-25 18:38:04 +02:00
func reinitializeArticles ( ) {
resetUI ( )
}
2019-11-19 18:16:43 +01:00
func reloadArticles ( animated : Bool ) {
applyChanges ( animated : animated )
2019-08-25 18:38:04 +02:00
}
2019-10-10 04:24:56 +02:00
func updateArticleSelection ( animated : Bool ) {
2019-09-11 16:11:33 +02:00
if let article = coordinator . currentArticle , let indexPath = dataSource . indexPath ( for : article ) {
2019-08-25 18:38:04 +02:00
if tableView . indexPathForSelectedRow != indexPath {
2019-10-23 02:20:35 +02:00
tableView . selectRowAndScrollIfNotVisible ( at : indexPath , animated : true )
2019-08-25 18:38:04 +02:00
}
2019-09-05 04:06:29 +02:00
} else {
2019-10-10 04:24:56 +02:00
tableView . selectRow ( at : nil , animated : animated , scrollPosition : . none )
2019-08-25 18:38:04 +02:00
}
2019-09-06 17:38:02 +02:00
2019-08-25 18:38:04 +02:00
updateUI ( )
}
2019-09-02 00:41:46 +02:00
func showSearchAll ( ) {
navigationItem . searchController ? . isActive = true
navigationItem . searchController ? . searchBar . selectedScopeButtonIndex = 1
2019-09-06 17:29:00 +02:00
navigationItem . searchController ? . searchBar . becomeFirstResponder ( )
2019-09-02 00:41:46 +02:00
}
2019-09-05 04:06:29 +02:00
func focus ( ) {
becomeFirstResponder ( )
}
2019-04-15 22:03:05 +02:00
// MARK: - T a b l e v i e w
2019-10-03 22:55:16 +02:00
override func tableView ( _ tableView : UITableView , leadingSwipeActionsConfigurationForRowAt indexPath : IndexPath ) -> UISwipeActionsConfiguration ? {
2019-09-11 16:11:33 +02:00
guard let article = dataSource . itemIdentifier ( for : indexPath ) else { return nil }
2019-04-15 22:03:05 +02:00
2019-04-29 13:01:53 +02:00
// 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-15 22:03:05 +02:00
2019-04-29 13:01:53 +02:00
let readAction = UIContextualAction ( style : . normal , title : readTitle ) { [ weak self ] ( action , view , completionHandler ) in
2019-09-11 16:11:33 +02:00
self ? . coordinator . toggleRead ( article )
2019-04-15 22:03:05 +02:00
completionHandler ( true )
}
2019-09-18 09:57:32 +02:00
readAction . image = article . status . read ? AppAssets . circleClosedImage : AppAssets . circleOpenImage
2019-09-18 09:49:57 +02:00
readAction . backgroundColor = AppAssets . primaryAccentColor
2019-04-15 22:03:05 +02:00
2019-10-03 22:55:16 +02:00
return UISwipeActionsConfiguration ( actions : [ readAction ] )
}
override func tableView ( _ tableView : UITableView , trailingSwipeActionsConfigurationForRowAt indexPath : IndexPath ) -> UISwipeActionsConfiguration ? {
guard let article = dataSource . itemIdentifier ( for : indexPath ) else { return nil }
2019-04-29 13:01:53 +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-15 22:03:05 +02:00
2019-04-29 13:01:53 +02:00
let starAction = UIContextualAction ( style : . normal , title : starTitle ) { [ weak self ] ( action , view , completionHandler ) in
2019-09-11 16:11:33 +02:00
self ? . coordinator . toggleStar ( article )
2019-04-15 22:03:05 +02:00
completionHandler ( true )
}
2019-09-18 09:57:32 +02:00
starAction . image = article . status . starred ? AppAssets . starOpenImage : AppAssets . starClosedImage
2019-04-29 13:01:53 +02:00
starAction . backgroundColor = AppAssets . starColor
2019-04-15 22:03:05 +02:00
2019-08-19 00:34:53 +02:00
// S e t u p t h e r e a d a c t i o n
let moreTitle = NSLocalizedString ( " More " , comment : " More " )
let moreAction = UIContextualAction ( style : . normal , title : moreTitle ) { [ weak self ] ( action , view , completionHandler ) in
if let self = self {
let alert = UIAlertController ( title : nil , message : nil , preferredStyle : . actionSheet )
if let popoverController = alert . popoverPresentationController {
popoverController . sourceView = view
popoverController . sourceRect = CGRect ( x : view . frame . size . width / 2 , y : view . frame . size . height / 2 , width : 1 , height : 1 )
}
2019-09-11 16:11:33 +02:00
alert . addAction ( self . markOlderAsReadAlertAction ( article , completionHandler : completionHandler ) )
2019-08-19 00:34:53 +02:00
2019-09-11 16:11:33 +02:00
if let action = self . discloseFeedAlertAction ( article , completionHandler : completionHandler ) {
2019-08-19 22:45:52 +02:00
alert . addAction ( action )
}
2019-09-11 16:11:33 +02:00
if let action = self . markAllInFeedAsReadAlertAction ( article , completionHandler : completionHandler ) {
2019-08-20 00:26:09 +02:00
alert . addAction ( action )
}
2019-09-11 16:11:33 +02:00
if let action = self . openInBrowserAlertAction ( article , completionHandler : completionHandler ) {
2019-08-20 00:38:30 +02:00
alert . addAction ( action )
}
2019-09-11 16:11:33 +02:00
if let action = self . shareAlertAction ( article , indexPath : indexPath , completionHandler : completionHandler ) {
2019-08-20 01:09:38 +02:00
alert . addAction ( action )
}
2019-08-19 00:34:53 +02:00
let cancelTitle = NSLocalizedString ( " Cancel " , comment : " Cancel " )
alert . addAction ( UIAlertAction ( title : cancelTitle , style : . cancel ) { _ in
completionHandler ( true )
} )
self . present ( alert , animated : true )
}
}
2019-08-19 22:45:52 +02:00
moreAction . image = AppAssets . moreImage
2019-08-19 00:34:53 +02:00
moreAction . backgroundColor = UIColor . systemGray
2019-10-03 22:55:16 +02:00
return UISwipeActionsConfiguration ( actions : [ starAction , moreAction ] )
2019-04-15 22:03:05 +02:00
}
2019-08-16 20:19:06 +02:00
override func tableView ( _ tableView : UITableView , contextMenuConfigurationForRowAt indexPath : IndexPath , point : CGPoint ) -> UIContextMenuConfiguration ? {
2019-09-11 16:11:33 +02:00
guard let article = dataSource . itemIdentifier ( for : indexPath ) else { return nil }
2019-11-24 05:15:29 +01:00
return UIContextMenuConfiguration ( identifier : indexPath . row as NSCopying , previewProvider : nil , actionProvider : { [ weak self ] suggestedActions in
2019-08-16 20:19:06 +02:00
2019-08-19 22:45:52 +02:00
guard let self = self else { return nil }
2019-08-16 20:19:06 +02:00
var actions = [ UIAction ] ( )
2019-09-11 16:11:33 +02:00
actions . append ( self . toggleArticleReadStatusAction ( article ) )
actions . append ( self . toggleArticleStarStatusAction ( article ) )
actions . append ( self . markOlderAsReadAction ( article ) )
2019-08-16 20:19:06 +02:00
2019-09-11 16:11:33 +02:00
if let action = self . discloseFeedAction ( article ) {
2019-08-19 22:45:52 +02:00
actions . append ( action )
}
2019-09-11 16:11:33 +02:00
if let action = self . markAllInFeedAsReadAction ( article ) {
2019-08-20 00:26:09 +02:00
actions . append ( action )
}
2019-09-11 16:11:33 +02:00
if let action = self . openInBrowserAction ( article ) {
2019-08-20 00:38:30 +02:00
actions . append ( action )
}
2019-09-11 16:11:33 +02:00
if let action = self . shareAction ( article , indexPath : indexPath ) {
2019-08-20 01:09:38 +02:00
actions . append ( action )
}
2019-08-19 22:49:42 +02:00
return UIMenu ( title : " " , children : actions )
2019-08-16 20:19:06 +02:00
} )
}
2019-11-24 05:15:29 +01:00
override func tableView ( _ tableView : UITableView , previewForHighlightingContextMenuWithConfiguration configuration : UIContextMenuConfiguration ) -> UITargetedPreview ? {
guard let row = configuration . identifier as ? Int ,
let cell = tableView . cellForRow ( at : IndexPath ( row : row , section : 0 ) ) else {
return nil
}
return UITargetedPreview ( view : cell , parameters : CroppingPreviewParameters ( view : cell ) )
}
2019-04-15 22:03:05 +02:00
override func tableView ( _ tableView : UITableView , didSelectRowAt indexPath : IndexPath ) {
2019-09-04 23:24:16 +02:00
becomeFirstResponder ( )
2019-09-11 16:11:33 +02:00
let article = dataSource . itemIdentifier ( for : indexPath )
2019-10-10 04:24:56 +02:00
coordinator . selectArticle ( article , animated : true )
2019-04-15 22:03:05 +02:00
}
2019-11-11 23:59:42 +01:00
override func scrollViewDidScroll ( _ scrollView : UIScrollView ) {
scrollPositionQueue . add ( self , #selector ( scrollPositionDidChange ) )
}
2019-04-15 22:03:05 +02:00
// MARK: N o t i f i c a t i o n s
2019-04-23 11:35:48 +02:00
@objc dynamic func unreadCountDidChange ( _ notification : Notification ) {
updateUI ( )
}
2019-04-15 22:03:05 +02:00
@objc func statusesDidChange ( _ note : Notification ) {
2019-09-06 20:45:45 +02:00
guard let updatedArticles = note . userInfo ? [ Account . UserInfoKey . articles ] as ? Set < Article > else {
2019-04-15 22:03:05 +02:00
return
}
2019-09-06 20:45:45 +02:00
2019-09-11 03:32:03 +02:00
let visibleArticles = tableView . indexPathsForVisibleRows ! . compactMap { return dataSource . itemIdentifier ( for : $0 ) }
2019-09-06 20:45:45 +02:00
let visibleUpdatedArticles = visibleArticles . filter { updatedArticles . contains ( $0 ) }
for article in visibleUpdatedArticles {
2019-09-11 16:11:33 +02:00
if let indexPath = dataSource . indexPath ( for : article ) {
if let cell = tableView . cellForRow ( at : indexPath ) as ? MasterTimelineTableViewCell {
2019-09-06 20:45:45 +02:00
configure ( cell , article : article )
}
}
}
2019-04-15 22:03:05 +02:00
}
2019-11-15 03:11:41 +01:00
@objc func webFeedIconDidBecomeAvailable ( _ note : Notification ) {
2019-11-06 01:05:57 +01:00
titleView ? . iconView . iconImage = coordinator . timelineIconImage
2019-11-15 03:11:41 +01:00
guard let feed = note . userInfo ? [ UserInfoKey . webFeed ] as ? WebFeed else {
2019-04-15 22:03:05 +02:00
return
}
2019-08-30 21:17:05 +02:00
tableView . indexPathsForVisibleRows ? . forEach { indexPath in
2019-09-11 03:32:03 +02:00
guard let article = dataSource . itemIdentifier ( for : indexPath ) else {
2019-08-30 21:17:05 +02:00
return
}
2019-11-15 03:11:41 +01:00
if article . webFeed = = feed , let cell = tableView . cellForRow ( at : indexPath ) as ? MasterTimelineTableViewCell , let image = iconImageFor ( article ) {
2019-11-06 01:05:57 +01:00
cell . setIconImage ( image )
2019-04-15 22:03:05 +02:00
}
}
}
@objc func avatarDidBecomeAvailable ( _ note : Notification ) {
2019-11-06 01:05:57 +01:00
guard coordinator . showIcons , let avatarURL = note . userInfo ? [ UserInfoKey . url ] as ? String else {
2019-04-15 22:03:05 +02:00
return
}
2019-08-30 21:17:05 +02:00
tableView . indexPathsForVisibleRows ? . forEach { indexPath in
2019-09-11 03:32:03 +02:00
guard let article = dataSource . itemIdentifier ( for : indexPath ) , let authors = article . authors , ! authors . isEmpty else {
2019-08-30 21:17:05 +02:00
return
}
for author in authors {
2019-11-06 01:05:57 +01:00
if author . avatarURL = = avatarURL , let cell = tableView . cellForRow ( at : indexPath ) as ? MasterTimelineTableViewCell , let image = iconImageFor ( article ) {
cell . setIconImage ( image )
2019-04-15 22:03:05 +02:00
}
}
}
}
2019-08-22 02:37:19 +02:00
@objc func faviconDidBecomeAvailable ( _ note : Notification ) {
2019-11-06 01:05:57 +01:00
titleView ? . iconView . iconImage = coordinator . timelineIconImage
if coordinator . showIcons {
2019-09-07 00:22:12 +02:00
queueReloadAvailableCells ( )
2019-04-15 22:03:05 +02:00
}
}
2019-04-29 22:29:00 +02:00
@objc func userDefaultsDidChange ( _ note : Notification ) {
2019-11-09 00:16:09 +01:00
if numberOfTextLines != AppDefaults . timelineNumberOfLines || iconSize != AppDefaults . timelineIconSize {
2019-04-29 22:29:00 +02:00
numberOfTextLines = AppDefaults . timelineNumberOfLines
2019-11-09 00:16:09 +01:00
iconSize = AppDefaults . timelineIconSize
2019-04-29 23:29:53 +02:00
resetEstimatedRowHeight ( )
2019-08-30 21:17:05 +02:00
reloadAllVisibleCells ( )
2019-04-29 22:29:00 +02:00
}
}
2019-04-29 21:50:56 +02:00
@objc func contentSizeCategoryDidChange ( _ note : Notification ) {
2019-08-30 21:17:05 +02:00
reloadAllVisibleCells ( )
2019-04-29 21:50:56 +02:00
}
2019-09-28 02:45:09 +02:00
@objc func displayNameDidChange ( _ note : Notification ) {
2019-11-15 13:19:14 +01:00
titleView ? . label . text = coordinator . timelineFeed ? . nameForDisplay
2019-09-28 02:45:09 +02:00
}
2019-11-11 23:59:42 +01:00
@objc func scrollPositionDidChange ( ) {
coordinator . timelineMiddleIndexPath = tableView . middleVisibleRow ( )
}
2019-04-15 22:03:05 +02:00
// MARK: R e l o a d i n g
2019-09-07 00:22:12 +02:00
func queueReloadAvailableCells ( ) {
CoalescingQueue . standard . add ( self , #selector ( reloadAllVisibleCells ) )
}
@objc private func reloadAllVisibleCells ( ) {
2019-09-11 03:32:03 +02:00
let visibleArticles = tableView . indexPathsForVisibleRows ! . compactMap { return dataSource . itemIdentifier ( for : $0 ) }
2019-08-30 21:17:05 +02:00
reloadCells ( visibleArticles )
2019-04-15 22:03:05 +02:00
}
2019-08-30 21:17:05 +02:00
private func reloadCells ( _ articles : [ Article ] ) {
var snapshot = dataSource . snapshot ( )
snapshot . reloadItems ( articles )
dataSource . apply ( snapshot , animatingDifferences : false ) { [ weak self ] in
2019-09-29 22:53:50 +02:00
self ? . restoreSelectionIfNecessary ( adjustScroll : false )
2019-08-30 21:17:05 +02:00
}
2019-04-15 22:03:05 +02:00
}
2019-04-29 23:29:53 +02:00
// MARK: C e l l C o n f i g u r i n g
private func resetEstimatedRowHeight ( ) {
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-11-15 03:11:41 +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 , bannerImageURL : nil , datePublished : nil , dateModified : nil , authors : nil , attachments : nil , status : status )
2019-04-29 23:29:53 +02:00
2019-11-09 00:16:09 +01:00
let prototypeCellData = MasterTimelineCellData ( article : prototypeArticle , showFeedName : true , feedName : " Prototype Feed Name " , iconImage : nil , showIcon : false , featuredImage : nil , numberOfLines : numberOfTextLines , iconSize : iconSize )
2019-04-29 23:29:53 +02:00
2019-04-30 00:19:08 +02:00
if UIApplication . shared . preferredContentSizeCategory . isAccessibilityCategory {
let layout = MasterTimelineAccessibilityCellLayout ( width : tableView . bounds . width , insets : tableView . safeAreaInsets , cellData : prototypeCellData )
tableView . estimatedRowHeight = layout . height
} else {
let layout = MasterTimelineDefaultCellLayout ( width : tableView . bounds . width , insets : tableView . safeAreaInsets , cellData : prototypeCellData )
tableView . estimatedRowHeight = layout . height
}
2019-04-29 23:29:53 +02:00
}
2019-04-15 22:03:05 +02:00
}
2019-08-31 22:53:47 +02:00
// MARK: S e a r c h i n g
2019-08-31 18:50:34 +02:00
2019-09-02 19:40:14 +02:00
extension MasterTimelineViewController : UISearchControllerDelegate {
func willPresentSearchController ( _ searchController : UISearchController ) {
coordinator . beginSearching ( )
searchController . searchBar . showsScopeBar = true
}
func willDismissSearchController ( _ searchController : UISearchController ) {
coordinator . endSearching ( )
searchController . searchBar . showsScopeBar = false
}
}
2019-08-31 18:50:34 +02:00
extension MasterTimelineViewController : UISearchResultsUpdating {
2019-08-31 22:53:47 +02:00
2019-08-31 18:50:34 +02:00
func updateSearchResults ( for searchController : UISearchController ) {
2019-08-31 22:53:47 +02:00
let searchScope = SearchScope ( rawValue : searchController . searchBar . selectedScopeButtonIndex ) !
coordinator . searchArticles ( searchController . searchBar . text ! , searchScope )
2019-08-31 18:50:34 +02:00
}
2019-08-31 22:53:47 +02:00
}
extension MasterTimelineViewController : UISearchBarDelegate {
func searchBar ( _ searchBar : UISearchBar , selectedScopeButtonIndexDidChange selectedScope : Int ) {
let searchScope = SearchScope ( rawValue : selectedScope ) !
coordinator . searchArticles ( searchBar . text ! , searchScope )
}
2019-08-31 18:50:34 +02:00
}
2019-04-15 22:03:05 +02:00
// MARK: P r i v a t e
private extension MasterTimelineViewController {
2019-04-22 23:25:16 +02:00
2019-04-23 01:00:26 +02:00
func resetUI ( ) {
2019-09-28 02:45:09 +02:00
2019-09-22 00:59:58 +02:00
if let titleView = Bundle . main . loadNibNamed ( " MasterTimelineTitleView " , owner : self , options : nil ) ? [ 0 ] as ? MasterTimelineTitleView {
2019-09-24 03:53:09 +02:00
self . titleView = titleView
2019-09-30 09:54:19 +02:00
2019-11-06 01:05:57 +01:00
titleView . iconView . iconImage = coordinator . timelineIconImage
2019-11-15 13:19:14 +01:00
titleView . label . text = coordinator . timelineFeed ? . nameForDisplay
2019-10-25 22:03:13 +02:00
updateTitleUnreadCount ( )
2019-10-01 03:01:02 +02:00
2019-11-15 13:19:14 +01:00
if coordinator . timelineFeed is WebFeed {
2019-09-28 02:45:09 +02:00
titleView . heightAnchor . constraint ( equalToConstant : 44.0 ) . isActive = true
let tap = UITapGestureRecognizer ( target : self , action : #selector ( showFeedInspector ( _ : ) ) )
titleView . addGestureRecognizer ( tap )
}
2019-09-22 00:59:58 +02:00
navigationItem . titleView = titleView
}
2019-11-22 16:40:39 +01:00
switch coordinator . articleReadFilterType {
2019-11-22 02:54:35 +01:00
case . none :
2019-11-24 10:47:29 +01:00
filterButton . isHidden = false
2019-11-22 01:22:43 +01:00
filterButton . image = AppAssets . filterInactiveImage
2019-11-24 10:47:29 +01:00
case . read :
filterButton . isHidden = false
2019-11-22 03:31:58 +01:00
filterButton . image = AppAssets . filterActiveImage
2019-11-24 10:47:29 +01:00
case . alwaysRead :
filterButton . isHidden = true
2019-11-22 01:22:43 +01:00
}
2019-04-22 23:25:16 +02:00
2019-07-27 21:36:01 +02:00
tableView . selectRow ( at : nil , animated : false , scrollPosition : . top )
2019-09-11 03:32:03 +02:00
if dataSource . snapshot ( ) . itemIdentifiers ( inSection : 0 ) . count > 0 {
2019-04-22 23:25:16 +02:00
tableView . scrollToRow ( at : IndexPath ( row : 0 , section : 0 ) , at : . top , animated : false )
}
2019-10-08 16:19:50 +02:00
updateToolbar ( )
2019-04-23 01:00:26 +02:00
}
2019-04-23 11:35:48 +02:00
func updateUI ( ) {
2019-10-25 22:03:13 +02:00
updateTitleUnreadCount ( )
2019-10-08 16:19:50 +02:00
updateToolbar ( )
}
func updateToolbar ( ) {
2019-06-29 20:35:12 +02:00
markAllAsReadButton . isEnabled = coordinator . isTimelineUnreadAvailable
firstUnreadButton . isEnabled = coordinator . isTimelineUnreadAvailable
2019-04-22 23:25:16 +02:00
}
2019-04-18 21:36:22 +02:00
2019-10-25 22:03:13 +02:00
func updateTitleUnreadCount ( ) {
2019-11-19 18:16:43 +01:00
self . titleView ? . unreadCountView . unreadCount = coordinator . unreadCount
2019-10-01 03:01:02 +02:00
}
2019-11-19 18:16:43 +01:00
func applyChanges ( animated : Bool , completion : ( ( ) -> Void ) ? = nil ) {
2019-08-30 21:17:05 +02:00
var snapshot = NSDiffableDataSourceSnapshot < Int , Article > ( )
snapshot . appendSections ( [ 0 ] )
snapshot . appendItems ( coordinator . articles , toSection : 0 )
2019-11-19 18:16:43 +01:00
dataSource . apply ( snapshot , animatingDifferences : animated ) { [ weak self ] in
2019-09-29 22:53:50 +02:00
self ? . restoreSelectionIfNecessary ( adjustScroll : false )
2019-08-30 21:17:05 +02:00
completion ? ( )
}
}
func makeDataSource ( ) -> UITableViewDiffableDataSource < Int , Article > {
2019-11-19 18:16:43 +01:00
let dataSource : UITableViewDiffableDataSource < Int , Article > =
2019-11-22 22:23:21 +01:00
MasterTimelineDataSource ( tableView : tableView , cellProvider : { [ weak self ] tableView , indexPath , article in
2019-11-19 18:16:43 +01:00
let cell = tableView . dequeueReusableCell ( withIdentifier : " Cell " , for : indexPath ) as ! MasterTimelineTableViewCell
self ? . configure ( cell , article : article )
return cell
} )
2019-11-22 02:54:35 +01:00
dataSource . defaultRowAnimation = . middle
2019-11-19 18:16:43 +01:00
return dataSource
2019-08-30 21:17:05 +02:00
}
2019-07-27 21:49:07 +02:00
2019-08-30 21:17:05 +02:00
func configure ( _ cell : MasterTimelineTableViewCell , article : Article ) {
2019-04-15 22:03:05 +02:00
2019-11-06 01:05:57 +01:00
let iconImage = iconImageFor ( article )
2019-04-15 22:03:05 +02:00
let featuredImage = featuredImageFor ( article )
2019-06-29 20:35:12 +02:00
let showFeedNames = coordinator . showFeedNames
2019-11-06 01:05:57 +01:00
let showIcon = coordinator . showIcons && iconImage != nil
2019-11-15 03:11:41 +01:00
cell . cellData = MasterTimelineCellData ( article : article , showFeedName : showFeedNames , feedName : article . webFeed ? . nameForDisplay , iconImage : iconImage , showIcon : showIcon , featuredImage : featuredImage , numberOfLines : numberOfTextLines , iconSize : iconSize )
2019-04-15 22:03:05 +02:00
}
2019-11-06 01:05:57 +01:00
func iconImageFor ( _ article : Article ) -> IconImage ? {
if ! coordinator . showIcons {
2019-04-15 22:03:05 +02:00
return nil
}
2019-11-06 01:05:57 +01:00
return article . iconImage ( )
2019-04-15 22:03:05 +02:00
}
func featuredImageFor ( _ article : Article ) -> UIImage ? {
if let url = article . imageURL , let data = appDelegate . imageDownloader . image ( for : url ) {
return RSImage ( data : data )
}
return nil
}
2019-09-11 16:11:33 +02:00
func toggleArticleReadStatusAction ( _ article : Article ) -> UIAction {
2019-08-16 20:19:06 +02:00
let title = article . status . read ?
NSLocalizedString ( " Mark as Unread " , comment : " Mark as Unread " ) :
NSLocalizedString ( " Mark as Read " , comment : " Mark as Read " )
let image = article . status . read ? AppAssets . circleClosedImage : AppAssets . circleOpenImage
2019-08-19 00:34:53 +02:00
let action = UIAction ( title : title , image : image ) { [ weak self ] action in
2019-09-11 16:11:33 +02:00
self ? . coordinator . toggleRead ( article )
2019-08-16 20:19:06 +02:00
}
return action
}
2019-09-11 16:11:33 +02:00
func toggleArticleStarStatusAction ( _ article : Article ) -> UIAction {
2019-08-16 20:19:06 +02:00
let title = article . status . starred ?
NSLocalizedString ( " Mark as Unstarred " , comment : " Mark as Unstarred " ) :
NSLocalizedString ( " Mark as Starred " , comment : " Mark as Starred " )
let image = article . status . starred ? AppAssets . starOpenImage : AppAssets . starClosedImage
2019-08-19 00:34:53 +02:00
let action = UIAction ( title : title , image : image ) { [ weak self ] action in
2019-09-11 16:11:33 +02:00
self ? . coordinator . toggleStar ( article )
2019-08-16 20:19:06 +02:00
}
return action
}
2019-09-11 16:11:33 +02:00
func markOlderAsReadAction ( _ article : Article ) -> UIAction {
2019-08-19 00:34:53 +02:00
let title = NSLocalizedString ( " Mark Older as Read " , comment : " Mark Older as Read " )
let image = coordinator . sortDirection = = . orderedDescending ? AppAssets . markOlderAsReadDownImage : AppAssets . markOlderAsReadUpImage
let action = UIAction ( title : title , image : image ) { [ weak self ] action in
2019-09-11 16:11:33 +02:00
self ? . coordinator . markAsReadOlderArticlesInTimeline ( article )
2019-08-19 00:34:53 +02:00
}
return action
}
2019-09-11 16:11:33 +02:00
func markOlderAsReadAlertAction ( _ article : Article , completionHandler : @ escaping ( Bool ) -> Void ) -> UIAlertAction {
2019-08-19 00:34:53 +02:00
let title = NSLocalizedString ( " Mark Older as Read " , comment : " Mark Older as Read " )
let action = UIAlertAction ( title : title , style : . default ) { [ weak self ] action in
2019-09-11 16:11:33 +02:00
self ? . coordinator . markAsReadOlderArticlesInTimeline ( article )
2019-08-19 00:34:53 +02:00
completionHandler ( true )
}
return action
}
2019-09-11 16:11:33 +02:00
func discloseFeedAction ( _ article : Article ) -> UIAction ? {
2019-11-15 03:11:41 +01:00
guard let webFeed = article . webFeed else { return nil }
2019-09-11 16:11:33 +02:00
2019-11-13 22:22:22 +01:00
let title = NSLocalizedString ( " Go to Feed " , comment : " Go to Feed " )
2019-08-19 22:45:52 +02:00
let action = UIAction ( title : title , image : AppAssets . openInSidebarImage ) { [ weak self ] action in
2019-11-15 03:11:41 +01:00
self ? . coordinator . discloseFeed ( webFeed , animated : true )
2019-08-19 22:45:52 +02:00
}
return action
}
2019-09-11 16:11:33 +02:00
func discloseFeedAlertAction ( _ article : Article , completionHandler : @ escaping ( Bool ) -> Void ) -> UIAlertAction ? {
2019-11-15 03:11:41 +01:00
guard let webFeed = article . webFeed else { return nil }
2019-09-11 16:11:33 +02:00
2019-11-13 22:22:22 +01:00
let title = NSLocalizedString ( " Go to Feed " , comment : " Go to Feed " )
2019-08-19 22:45:52 +02:00
let action = UIAlertAction ( title : title , style : . default ) { [ weak self ] action in
2019-11-15 03:11:41 +01:00
self ? . coordinator . discloseFeed ( webFeed , animated : true )
2019-08-19 22:45:52 +02:00
completionHandler ( true )
}
return action
}
2019-09-11 16:11:33 +02:00
func markAllInFeedAsReadAction ( _ article : Article ) -> UIAction ? {
2019-11-15 03:11:41 +01:00
guard let webFeed = article . webFeed else { return nil }
2019-09-11 16:11:33 +02:00
2019-11-15 03:11:41 +01:00
let articles = Array ( webFeed . fetchArticles ( ) )
2019-08-20 00:26:09 +02:00
guard articles . canMarkAllAsRead ( ) else {
return nil
}
let localizedMenuText = NSLocalizedString ( " Mark All as Read in “%@” " , comment : " Command " )
2019-11-15 03:11:41 +01:00
let title = NSString . localizedStringWithFormat ( localizedMenuText as NSString , webFeed . nameForDisplay ) as String
2019-08-20 00:26:09 +02:00
let action = UIAction ( title : title , image : AppAssets . markAllInFeedAsReadImage ) { [ weak self ] action in
self ? . coordinator . markAllAsRead ( articles )
}
return action
}
2019-09-11 16:11:33 +02:00
func markAllInFeedAsReadAlertAction ( _ article : Article , completionHandler : @ escaping ( Bool ) -> Void ) -> UIAlertAction ? {
2019-11-15 03:11:41 +01:00
guard let webFeed = article . webFeed else { return nil }
2019-09-11 16:11:33 +02:00
2019-11-15 03:11:41 +01:00
let articles = Array ( webFeed . fetchArticles ( ) )
2019-08-20 00:26:09 +02:00
guard articles . canMarkAllAsRead ( ) else {
return nil
}
2019-08-20 00:38:30 +02:00
let localizedMenuText = NSLocalizedString ( " Mark All as Read in “%@” " , comment : " Mark All as Read in Feed " )
2019-11-15 03:11:41 +01:00
let title = NSString . localizedStringWithFormat ( localizedMenuText as NSString , webFeed . nameForDisplay ) as String
2019-08-20 00:26:09 +02:00
let action = UIAlertAction ( title : title , style : . default ) { [ weak self ] action in
self ? . coordinator . markAllAsRead ( articles )
completionHandler ( true )
}
return action
}
2019-08-20 00:38:30 +02:00
2019-09-11 16:11:33 +02:00
func openInBrowserAction ( _ article : Article ) -> UIAction ? {
guard let preferredLink = article . preferredLink , let _ = URL ( string : preferredLink ) else {
2019-08-20 00:38:30 +02:00
return nil
}
let title = NSLocalizedString ( " Open in Browser " , comment : " Open in Browser " )
let action = UIAction ( title : title , image : AppAssets . safariImage ) { [ weak self ] action in
2019-09-11 16:11:33 +02:00
self ? . coordinator . showBrowserForArticle ( article )
2019-08-20 00:38:30 +02:00
}
return action
}
2019-09-11 16:11:33 +02:00
func openInBrowserAlertAction ( _ article : Article , completionHandler : @ escaping ( Bool ) -> Void ) -> UIAlertAction ? {
guard let preferredLink = article . preferredLink , let _ = URL ( string : preferredLink ) else {
2019-08-20 00:38:30 +02:00
return nil
}
let title = NSLocalizedString ( " Open in Browser " , comment : " Open in Browser " )
let action = UIAlertAction ( title : title , style : . default ) { [ weak self ] action in
2019-09-11 16:11:33 +02:00
self ? . coordinator . showBrowserForArticle ( article )
2019-08-20 00:38:30 +02:00
completionHandler ( true )
}
return action
}
2019-08-20 00:26:09 +02:00
2019-08-20 01:09:38 +02:00
func shareDialogForTableCell ( indexPath : IndexPath , url : URL , title : String ? ) {
let itemSource = ArticleActivityItemSource ( url : url , subject : title )
let activityViewController = UIActivityViewController ( activityItems : [ itemSource ] , applicationActivities : nil )
guard let cell = tableView . cellForRow ( at : indexPath ) else { return }
let popoverController = activityViewController . popoverPresentationController
popoverController ? . sourceView = cell
popoverController ? . sourceRect = CGRect ( x : 0 , y : 0 , width : cell . frame . size . width , height : cell . frame . size . height )
present ( activityViewController , animated : true )
}
2019-09-11 16:11:33 +02:00
func shareAction ( _ article : Article , indexPath : IndexPath ) -> UIAction ? {
2019-08-20 01:09:38 +02:00
guard let preferredLink = article . preferredLink , let url = URL ( string : preferredLink ) else {
return nil
}
let title = NSLocalizedString ( " Share " , comment : " Share " )
let action = UIAction ( title : title , image : AppAssets . shareImage ) { [ weak self ] action in
self ? . shareDialogForTableCell ( indexPath : indexPath , url : url , title : article . title )
}
return action
}
2019-09-11 16:11:33 +02:00
func shareAlertAction ( _ article : Article , indexPath : IndexPath , completionHandler : @ escaping ( Bool ) -> Void ) -> UIAlertAction ? {
2019-08-20 01:09:38 +02:00
guard let preferredLink = article . preferredLink , let url = URL ( string : preferredLink ) else {
return nil
}
let title = NSLocalizedString ( " Share " , comment : " Share " )
let action = UIAlertAction ( title : title , style : . default ) { [ weak self ] action in
completionHandler ( true )
self ? . shareDialogForTableCell ( indexPath : indexPath , url : url , title : article . title )
}
return action
}
2019-04-15 22:03:05 +02:00
}