Make minor code formatting changes so that TimelineViewController matches our current thinking. Also delete some commented-out and unused code.

This commit is contained in:
Brent Simmons 2019-09-16 22:27:17 -07:00
parent f7c9cc1dbd
commit 66d9333999
1 changed files with 13 additions and 99 deletions

View File

@ -148,7 +148,6 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr
}
override func viewDidLoad() {
cellAppearance = TimelineCellAppearance(showAvatar: false, fontSize: fontSize)
cellAppearanceWithAvatar = TimelineCellAppearance(showAvatar: true, fontSize: fontSize)
@ -160,7 +159,6 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr
tableView.keyboardDelegate = keyboardDelegate
if !didRegisterForNotifications {
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)
@ -179,42 +177,9 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr
sharingServiceDelegate = SharingServiceDelegate(self.view.window)
}
// MARK: State Restoration
// private static let stateRestorationSelectedArticles = "selectedArticles"
//
// override func encodeRestorableState(with coder: NSCoder) {
//
// super.encodeRestorableState(with: coder)
//
// coder.encode(self.selectedArticleIDs(), forKey: TimelineViewController.stateRestorationSelectedArticles)
// }
//
// override func restoreState(with coder: NSCoder) {
//
// super.restoreState(with: coder)
//
// if let restoredArticleIDs = (try? coder.decodeTopLevelObject(forKey: TimelineViewController.stateRestorationSelectedArticles)) as? [String] {
// self.restoreSelection(restoredArticleIDs)
// }
// }
// MARK: Appearance Change
private func fontSizeDidChange() {
cellAppearance = TimelineCellAppearance(showAvatar: false, fontSize: fontSize)
cellAppearanceWithAvatar = TimelineCellAppearance(showAvatar: true, fontSize: fontSize)
updateRowHeights()
performBlockAndRestoreSelection {
tableView.reloadData()
}
}
// MARK: - API
func markAllAsRead() {
guard let undoManager = undoManager, let markReadCommand = MarkStatusCommand(initialArticles: articles, markingRead: true, undoManager: undoManager) else {
return
}
@ -222,12 +187,10 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr
}
func canMarkAllAsRead() -> Bool {
return articles.canMarkAllAsRead()
}
func canMarkSelectedArticlesAsRead() -> Bool {
return selectedArticles.canMarkAllAsRead()
}
@ -244,14 +207,12 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr
// MARK: - Actions
@objc func openArticleInBrowser(_ sender: Any?) {
if let link = oneSelectedArticle?.preferredLink {
Browser.open(link)
}
}
@IBAction func toggleStatusOfSelectedArticles(_ sender: Any?) {
guard !selectedArticles.isEmpty else {
return
}
@ -268,7 +229,6 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr
}
@IBAction func markSelectedArticlesAsRead(_ sender: Any?) {
guard let undoManager = undoManager, let markReadCommand = MarkStatusCommand(initialArticles: selectedArticles, markingRead: true, undoManager: undoManager) else {
return
}
@ -276,7 +236,6 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr
}
@IBAction func markSelectedArticlesAsUnread(_ sender: Any?) {
guard let undoManager = undoManager, let markUnreadCommand = MarkStatusCommand(initialArticles: selectedArticles, markingRead: false, undoManager: undoManager) else {
return
}
@ -284,12 +243,10 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr
}
@IBAction func copy(_ sender: Any?) {
NSPasteboard.general.copyObjects(selectedArticles)
}
@IBAction func selectNextUp(_ sender: Any?) {
guard let lastSelectedRow = tableView.selectedRowIndexes.last else {
return
}
@ -311,7 +268,6 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr
}
@IBAction func selectNextDown(_ sender: Any?) {
guard let firstSelectedRow = tableView.selectedRowIndexes.first else {
return
}
@ -334,7 +290,6 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr
}
func toggleReadStatusForSelectedArticles() {
// If any one of the selected articles is unread, then mark them as read.
// If all articles are read, then mark them as unread them.
@ -379,12 +334,10 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr
}
func markStarredCommandStatus() -> MarkCommandValidationStatus {
return MarkCommandValidationStatus.statusFor(selectedArticles) { $0.anyArticleIsUnstarred() }
}
func markReadCommandStatus() -> MarkCommandValidationStatus {
return MarkCommandValidationStatus.statusFor(selectedArticles) { $0.anyArticleIsUnread() }
}
@ -426,7 +379,6 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr
// MARK: - Navigation
func goToNextUnread() {
guard let ix = indexOfNextUnreadArticle() else {
return
}
@ -436,7 +388,6 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr
}
func canGoToNextUnread() -> Bool {
guard let _ = indexOfNextUnreadArticle() else {
return false
}
@ -444,12 +395,10 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr
}
func indexOfNextUnreadArticle() -> Int? {
return articles.rowOfNextUnreadArticle(tableView.selectedRow)
}
func focus() {
guard let window = tableView.window else {
return
}
@ -463,7 +412,6 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr
// MARK: - Notifications
@objc func statusesDidChange(_ note: Notification) {
guard let articles = note.userInfo?[Account.UserInfoKey.articles] as? Set<Article> else {
return
}
@ -472,7 +420,6 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr
}
@objc func feedIconDidBecomeAvailable(_ note: Notification) {
guard showAvatars, let feed = note.userInfo?[UserInfoKey.feed] as? Feed else {
return
}
@ -488,7 +435,6 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr
}
@objc func avatarDidBecomeAvailable(_ note: Notification) {
guard showAvatars, let avatarURL = note.userInfo?[UserInfoKey.url] as? String else {
return
}
@ -516,7 +462,6 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr
}
@objc func accountDidDownloadArticles(_ note: Notification) {
guard let feeds = note.userInfo?[Account.UserInfoKey.feeds] as? Set<Feed> else {
return
}
@ -546,7 +491,6 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr
}
@objc func userDefaultsDidChange(_ note: Notification) {
self.fontSize = AppDefaults.timelineFontSize
self.sortDirection = AppDefaults.timelineSortDirection
}
@ -554,7 +498,6 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr
// MARK: - Reloading Data
private func cellForRowView(_ rowView: NSView) -> NSView? {
for oneView in rowView.subviews where oneView is TimelineTableCellView {
return oneView
}
@ -603,7 +546,6 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr
// MARK: - Cell Configuring
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())
@ -615,7 +557,6 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr
}
private func updateRowHeights() {
rowHeightWithFeedName = calculateRowHeight(showingFeedNames: true)
rowHeightWithoutFeedName = calculateRowHeight(showingFeedNames: false)
updateTableViewRowHeight()
@ -663,7 +604,6 @@ extension TimelineViewController: NSMenuDelegate {
extension TimelineViewController: NSUserInterfaceValidations {
func validateUserInterfaceItem(_ item: NSValidatedUserInterfaceItem) -> Bool {
if item.action == #selector(openArticleInBrowser(_:)) {
let currentLink = oneSelectedArticle?.preferredLink
return currentLink != nil
@ -680,7 +620,6 @@ extension TimelineViewController: NSUserInterfaceValidations {
// MARK: - NSTableViewDataSource
extension TimelineViewController: NSTableViewDataSource {
func numberOfRows(in tableView: NSTableView) -> Int {
return articles.count
}
@ -700,7 +639,6 @@ extension TimelineViewController: NSTableViewDataSource {
// MARK: - NSTableViewDelegate
extension TimelineViewController: NSTableViewDelegate {
private static let rowViewIdentifier = NSUserInterfaceItemIdentifier(rawValue: "timelineRow")
func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
@ -751,8 +689,6 @@ extension TimelineViewController: NSTableViewDelegate {
}
selectionDidChange(selectedArticles)
// self.invalidateRestorableState()
}
private func selectionDidChange(_ selectedArticles: ArticleArray?) {
@ -761,11 +697,8 @@ extension TimelineViewController: NSTableViewDelegate {
private func configureTimelineCell(_ cell: TimelineTableCellView, article: Article) {
cell.objectValue = article
let avatar = avatarFor(article)
let featuredImage = featuredImageFor(article)
cell.cellData = TimelineCellData(article: article, showFeedName: showFeedNames, feedName: article.feed?.nameForDisplay, avatar: avatar, showAvatar: showAvatars, featuredImage: featuredImage)
cell.cellData = TimelineCellData(article: article, showFeedName: showFeedNames, feedName: article.feed?.nameForDisplay, avatar: avatar, showAvatar: showAvatars, featuredImage: nil)
}
private func avatarFor(_ article: Article) -> NSImage? {
@ -800,22 +733,6 @@ extension TimelineViewController: NSTableViewDelegate {
return appDelegate.authorAvatarDownloader.image(for: author)
}
private func featuredImageFor(_ article: Article) -> NSImage? {
// At this writing (17 June 2019) were not displaying featured images anywhere,
// so lets skip downloading them even if we find them.
//
// Well revisit this later.
// if let url = article.imageURL {
// if let imageData = appDelegate.imageDownloader.image(for: url) {
// return NSImage(data: imageData)
// }
// }
return nil
}
private func makeTimelineCellEmpty(_ cell: TimelineTableCellView) {
cell.objectValue = nil
cell.cellData = TimelineCellData()
@ -827,7 +744,6 @@ extension TimelineViewController: NSTableViewDelegate {
private extension TimelineViewController {
func startObservingUserDefaults() {
assert(timelineShowsSeparatorsObserver == nil)
timelineShowsSeparatorsObserver = UserDefaults.standard.observe(\UserDefaults.CorreiaSeparators) { [weak self] (_, _) in
guard let self = self, self.isViewLoaded else { return }
@ -840,7 +756,6 @@ private extension TimelineViewController {
}
@objc func reloadAvailableCells() {
if let indexesToReload = tableView.indexesOfAvailableRows() {
reloadCells(for: indexesToReload)
}
@ -857,17 +772,14 @@ private extension TimelineViewController {
}
func queueReloadAvailableCells() {
CoalescingQueue.standard.add(self, #selector(reloadAvailableCells))
}
func updateTableViewRowHeight() {
tableView.rowHeight = currentRowHeight
}
func updateShowAvatars() {
if showFeedNames {
self.showAvatars = true
return
@ -894,7 +806,6 @@ private extension TimelineViewController {
}
func sortDirectionDidChange() {
performBlockAndRestoreSelection {
let unsortedArticles = Set(articles)
replaceArticles(with: unsortedArticles)
@ -902,12 +813,10 @@ private extension TimelineViewController {
}
func selectedArticleIDs() -> [String] {
return selectedArticles.articleIDs()
}
func restoreSelection(_ articleIDs: [String]) {
selectArticles(articleIDs)
if tableView.selectedRow != -1 {
tableView.scrollRowToVisible(tableView.selectedRow)
@ -915,7 +824,6 @@ private extension TimelineViewController {
}
func performBlockAndRestoreSelection(_ block: (() -> Void)) {
let savedSelection = selectedArticleIDs()
block()
restoreSelection(savedSelection)
@ -947,7 +855,6 @@ private extension TimelineViewController {
}
func indexesForArticleIDs(_ articleIDs: Set<String>) -> IndexSet {
var indexes = IndexSet()
articleIDs.forEach { (articleID) in
@ -962,7 +869,18 @@ private extension TimelineViewController {
return indexes
}
// MARK: Fetching Articles
// MARK: - Appearance Change
private func fontSizeDidChange() {
cellAppearance = TimelineCellAppearance(showAvatar: false, fontSize: fontSize)
cellAppearanceWithAvatar = TimelineCellAppearance(showAvatar: true, fontSize: fontSize)
updateRowHeights()
performBlockAndRestoreSelection {
tableView.reloadData()
}
}
// MARK: - Fetching Articles
func fetchAndReplaceArticlesSync() {
// To be called when the user has made a change of selection in the sidebar.
@ -1030,7 +948,6 @@ private extension TimelineViewController {
}
func selectArticles(_ articleIDs: [String]) {
let indexesToSelect = indexesForArticleIDs(Set(articleIDs))
if indexesToSelect.isEmpty {
tableView.deselectAll(self)
@ -1040,12 +957,10 @@ private extension TimelineViewController {
}
func queueFetchAndMergeArticles() {
TimelineViewController.fetchAndMergeArticlesQueue.add(self, #selector(fetchAndMergeArticles))
}
func representedObjectArraysAreEqual(_ objects1: [AnyObject]?, _ objects2: [AnyObject]?) -> Bool {
if objects1 == nil && objects2 == nil {
return true
}
@ -1079,7 +994,6 @@ private extension TimelineViewController {
}
func representedObjectsContainsAnyFeed(_ feeds: Set<Feed>) -> Bool {
// Return true if theres a match or if a folder contains (recursively) one of feeds
guard let representedObjects = representedObjects else {