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