diff --git a/Mac/MainWindow/MainWindowController.swift b/Mac/MainWindow/MainWindowController.swift index de5bdfa30..bc00c98d1 100644 --- a/Mac/MainWindow/MainWindowController.swift +++ b/Mac/MainWindow/MainWindowController.swift @@ -381,12 +381,25 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations { extension MainWindowController: SidebarDelegate { func sidebarSelectionDidChange(_: SidebarViewController, selectedObjects: [AnyObject]?) { - // TODO: if searching, cancel search - timelineContainerViewController?.setRepresentedObjects(selectedObjects, mode: .regular) - forceSearchToEnd() + // Don’t update the timeline if it already has those objects. + let representedObjectsAreTheSame = timelineContainerViewController?.regularTimelineViewControllerHasRepresentedObjects(selectedObjects) ?? false + if !representedObjectsAreTheSame { + timelineContainerViewController?.setRepresentedObjects(selectedObjects, mode: .regular) + forceSearchToEnd() + } updateWindowTitle() NotificationCenter.default.post(name: .InspectableObjectsDidChange, object: nil) } + + func unreadCount(for representedObject: AnyObject) -> Int { + guard let timelineViewController = regularTimelineViewController else { + return 0 + } + guard timelineViewController.representsThisObjectOnly(representedObject) else { + return 0 + } + return timelineViewController.unreadCount + } } // MARK: - TimelineContainerViewControllerDelegate @@ -545,6 +558,10 @@ private extension MainWindowController { return timelineContainerViewController?.currentTimelineViewController } + var regularTimelineViewController: TimelineViewController? { + return timelineContainerViewController?.regularTimelineViewController + } + var sidebarSplitViewItem: NSSplitViewItem? { return splitViewController?.splitViewItems[0] } diff --git a/Mac/MainWindow/Sidebar/SidebarViewController.swift b/Mac/MainWindow/Sidebar/SidebarViewController.swift index b3a0c3dca..ef724bccd 100644 --- a/Mac/MainWindow/Sidebar/SidebarViewController.swift +++ b/Mac/MainWindow/Sidebar/SidebarViewController.swift @@ -14,6 +14,7 @@ import RSCore protocol SidebarDelegate: class { func sidebarSelectionDidChange(_: SidebarViewController, selectedObjects: [AnyObject]?) + func unreadCount(for: AnyObject) -> Int } @objc class SidebarViewController: NSViewController, NSOutlineViewDelegate, NSOutlineViewDataSource, NSMenuDelegate, UndoableCommandRunner { @@ -43,7 +44,6 @@ protocol SidebarDelegate: class { // MARK: - NSViewController override func viewDidLoad() { - sidebarCellAppearance = SidebarCellAppearance(fontSize: AppDefaults.sidebarFontSize) outlineView.dataSource = dataSource @@ -60,7 +60,6 @@ protocol SidebarDelegate: class { NotificationCenter.default.addObserver(self, selector: #selector(feedSettingDidChange(_:)), name: .FeedSettingDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(displayNameDidChange(_:)), name: .DisplayNameDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(userDidRequestSidebarSelection(_:)), name: .UserDidRequestSidebarSelection, object: nil) - NotificationCenter.default.addObserver(self, selector: #selector(calendarDayChanged(_:)), name: .NSCalendarDayChanged, object: nil) outlineView.reloadData() @@ -78,30 +77,9 @@ protocol SidebarDelegate: class { } } - // MARK: State Restoration - -// private static let stateRestorationSelectedRowIndexes = "selectedRowIndexes" -// -// override func encodeRestorableState(with coder: NSCoder) { -// -// super.encodeRestorableState(with: coder) -// -// coder.encode(outlineView.selectedRowIndexes, forKey: SidebarViewController.stateRestorationSelectedRowIndexes) -// } -// -// override func restoreState(with coder: NSCoder) { -// -// super.restoreState(with: coder) -// -// if let restoredRowIndexes = coder.decodeObject(of: [NSIndexSet.self], forKey: SidebarViewController.stateRestorationSelectedRowIndexes) as? IndexSet { -// outlineView.selectRowIndexes(restoredRowIndexes, byExtendingSelection: false) -// } -// } - // MARK: - Notifications @objc func unreadCountDidChange(_ note: Notification) { - guard let representedObject = note.object else { return } @@ -125,7 +103,6 @@ protocol SidebarDelegate: class { } @objc func userDidAddFeed(_ notification: Notification) { - guard let feed = notification.userInfo?[UserInfoKey.feed] else { return } @@ -133,12 +110,10 @@ protocol SidebarDelegate: class { } @objc func faviconDidBecomeAvailable(_ note: Notification) { - applyToAvailableCells(configureFavicon) } @objc func feedSettingDidChange(_ note: Notification) { - guard let feed = note.object as? Feed, let key = note.userInfo?[Feed.FeedSettingUserInfoKey] as? String else { return } @@ -148,7 +123,6 @@ protocol SidebarDelegate: class { } @objc func displayNameDidChange(_ note: Notification) { - guard let object = note.object else { return } @@ -159,23 +133,15 @@ protocol SidebarDelegate: class { } @objc func userDidRequestSidebarSelection(_ note: Notification) { - guard let feed = note.userInfo?[UserInfoKey.feed] else { return } revealAndSelectRepresentedObject(feed as AnyObject) } - @objc func calendarDayChanged(_ note: Notification) { - DispatchQueue.main.async { - SmartFeedsController.shared.todayFeed.fetchUnreadCounts() - } - } - // MARK: - Actions @IBAction func delete(_ sender: AnyObject?) { - if outlineView.selectionIsEmpty { return } @@ -183,7 +149,6 @@ protocol SidebarDelegate: class { } @IBAction func openInBrowser(_ sender: Any?) { - guard let feed = singleSelectedFeed, let homePageURL = feed.homePageURL else { return } @@ -191,29 +156,24 @@ protocol SidebarDelegate: class { } @IBAction func gotoToday(_ sender: Any?) { - outlineView.revealAndSelectRepresentedObject(SmartFeedsController.shared.todayFeed, treeController) } @IBAction func gotoAllUnread(_ sender: Any?) { - outlineView.revealAndSelectRepresentedObject(SmartFeedsController.shared.unreadFeed, treeController) } @IBAction func gotoStarred(_ sender: Any?) { - outlineView.revealAndSelectRepresentedObject(SmartFeedsController.shared.starredFeed, treeController) } @IBAction func copy(_ sender: Any?) { - NSPasteboard.general.copyObjects(selectedObjects) } // MARK: - Navigation func canGoToNextUnread() -> Bool { - if let _ = nextSelectableRowWithUnreadArticle() { return true } @@ -221,7 +181,6 @@ protocol SidebarDelegate: class { } func goToNextUnread() { - guard let row = nextSelectableRowWithUnreadArticle() else { assertionFailure("goToNextUnread called before checking if there is a next unread.") return @@ -230,26 +189,19 @@ protocol SidebarDelegate: class { NSCursor.setHiddenUntilMouseMoves(true) outlineView.selectRowIndexes(IndexSet([row]), byExtendingSelection: false) outlineView.scrollTo(row: row) - } func focus() { - - guard let window = outlineView.window else { - return - } - window.makeFirstResponderUnlessDescendantIsFirstResponder(outlineView) + outlineView.window?.makeFirstResponderUnlessDescendantIsFirstResponder(outlineView) } // MARK: - Contextual Menu func contextualMenuForSelectedObjects() -> NSMenu? { - return menu(for: selectedObjects) } func contextualMenuForClickedRows() -> NSMenu? { - let row = outlineView.clickedRow guard row != -1, let node = nodeForRow(row) else { return nil @@ -264,7 +216,7 @@ protocol SidebarDelegate: class { return menu(for: [object]) } - // MARK: NSMenuDelegate + // MARK: - NSMenuDelegate public func menuNeedsUpdate(_ menu: NSMenu) { menu.removeAllItems() @@ -278,7 +230,6 @@ protocol SidebarDelegate: class { // MARK: - NSOutlineViewDelegate func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? { - let node = item as! Node if node.isGroupItem { @@ -294,13 +245,11 @@ protocol SidebarDelegate: class { } func outlineView(_ outlineView: NSOutlineView, isGroupItem item: Any) -> Bool { - let node = item as! Node return node.isGroupItem } func outlineView(_ outlineView: NSOutlineView, selectionIndexesForProposedSelection proposedSelectionIndexes: IndexSet) -> IndexSet { - // Don’t allow selecting group items. // If any index in IndexSet contains a group item, // return the current selection (not a modified version of the proposed selection). @@ -315,19 +264,16 @@ protocol SidebarDelegate: class { } func outlineView(_ outlineView: NSOutlineView, shouldSelectItem item: Any) -> Bool { - return !self.outlineView(outlineView, isGroupItem: item) } func outlineViewSelectionDidChange(_ notification: Notification) { selectionDidChange(selectedObjects.isEmpty ? nil : selectedObjects) -// self.invalidateRestorableState() } //MARK: - Node Manipulation func deleteNodes(_ nodes: [Node]) { - let nodesToDelete = treeController.normalizedSelectedNodes(nodes) guard let undoManager = undoManager, let deleteCommand = DeleteCommand(nodesToDelete: nodesToDelete, treeController: treeController, undoManager: undoManager, errorHandler: ErrorHandler.present) else { @@ -374,7 +320,6 @@ protocol SidebarDelegate: class { extension SidebarViewController: NSUserInterfaceValidations { func validateUserInterfaceItem(_ item: NSValidatedUserInterfaceItem) -> Bool { - if item.action == #selector(copy(_:)) { return NSPasteboard.general.canCopyAtLeastOneObject(selectedObjects) } @@ -412,10 +357,10 @@ private extension SidebarViewController { } func rebuildTreeAndReloadDataIfNeeded() { - if !animatingChanges && !BatchUpdate.shared.isPerforming { - treeController.rebuild() - outlineView.reloadData() + if treeController.rebuild() { + outlineView.reloadData() + } } } @@ -444,7 +389,6 @@ private extension SidebarViewController { } func updateUnreadCounts(for objects: [AnyObject]) { - // On selection, update unread counts for folders and feeds. // For feeds, actually fetch from database. @@ -459,7 +403,6 @@ private extension SidebarViewController { } func nodeForItem(_ item: AnyObject?) -> Node { - if item == nil { return treeController.rootNode } @@ -467,7 +410,6 @@ private extension SidebarViewController { } func nodeForRow(_ row: Int) -> Node? { - if row < 0 || row >= outlineView.numberOfRows { return nil } @@ -479,7 +421,6 @@ private extension SidebarViewController { } func rowHasAtLeastOneUnreadArticle(_ row: Int) -> Bool { - if let oneNode = nodeForRow(row) { if let unreadCountProvider = oneNode.representedObject as? UnreadCountProvider { if unreadCountProvider.unreadCount > 0 { @@ -491,7 +432,6 @@ private extension SidebarViewController { } func rowIsGroupItem(_ row: Int) -> Bool { - if let node = nodeForRow(row), outlineView.isGroupItem(node) { return true } @@ -499,7 +439,6 @@ private extension SidebarViewController { } func nextSelectableRowWithUnreadArticle() -> Int? { - // Skip group items, because they should never be selected. let selectedRow = outlineView.selectedRow @@ -533,12 +472,10 @@ private extension SidebarViewController { } func configureUnreadCount(_ cell: SidebarCell, _ node: Node) { - cell.unreadCount = unreadCountFor(node) } func configureFavicon(_ cell: SidebarCell, _ node: Node) { - cell.image = imageFor(node) } @@ -547,7 +484,6 @@ private extension SidebarViewController { } func imageFor(_ node: Node) -> NSImage? { - if let smallIconProvider = node.representedObject as? SmallIconProvider { return smallIconProvider.smallIcon } @@ -555,7 +491,6 @@ private extension SidebarViewController { } func nameFor(_ node: Node) -> String { - if let displayNameProvider = node.representedObject as? DisplayNameProvider { return displayNameProvider.nameForDisplay } @@ -563,6 +498,13 @@ private extension SidebarViewController { } func unreadCountFor(_ node: Node) -> Int { + // If this node is the one and only selection, + // then the unread count comes from the timeline. + // This ensures that any transients in the timeline + // are accounted for in the unread count. + if selectedNodes.count == 1 && node === selectedNodes.first! { + return delegate?.unreadCount(for: node.representedObject) ?? 0 + } if let unreadCountProvider = node.representedObject as? UnreadCountProvider { return unreadCountProvider.unreadCount @@ -571,14 +513,11 @@ private extension SidebarViewController { } func cellForRowView(_ rowView: NSTableRowView) -> SidebarCell? { - return rowView.view(atColumn: 0) as? SidebarCell } func applyToAvailableCells(_ callback: (SidebarCell, Node) -> Void) { - outlineView.enumerateAvailableRowViews { (rowView: NSTableRowView, row: Int) -> Void in - guard let cell = cellForRowView(rowView), let node = nodeForRow(row) else { return } @@ -595,23 +534,19 @@ private extension SidebarViewController { } func configureCellsForRepresentedObject(_ representedObject: AnyObject) { - applyToCellsForRepresentedObject(representedObject, configure) } func configureUnreadCountForCellsForRepresentedObject(_ representedObject: AnyObject) { - applyToCellsForRepresentedObject(representedObject, configureUnreadCount) } @discardableResult func revealAndSelectRepresentedObject(_ representedObject: AnyObject) -> Bool { - return outlineView.revealAndSelectRepresentedObject(representedObject, treeController) } } - private extension Node { func representsSidebarObject(_ object: AnyObject) -> Bool { diff --git a/Mac/MainWindow/Timeline/TimelineContainerViewController.swift b/Mac/MainWindow/Timeline/TimelineContainerViewController.swift index 12f6d62d2..b4f3b3a27 100644 --- a/Mac/MainWindow/Timeline/TimelineContainerViewController.swift +++ b/Mac/MainWindow/Timeline/TimelineContainerViewController.swift @@ -30,7 +30,7 @@ final class TimelineContainerViewController: NSViewController { weak var delegate: TimelineContainerViewControllerDelegate? - private lazy var regularTimelineViewController = { + lazy var regularTimelineViewController = { return TimelineViewController(delegate: self) }() private lazy var searchTimelineViewController: TimelineViewController = { @@ -54,6 +54,31 @@ final class TimelineContainerViewController: NSViewController { func showTimeline(for mode: TimelineSourceMode) { currentTimelineViewController = timelineViewController(for: mode) } + + func regularTimelineViewControllerHasRepresentedObjects(_ representedObjects: [AnyObject]?) -> Bool { + // Use this to find out if the regular timeline view already has the specified representedObjects. + // This is used in determining whether a search should end. + // The sidebar may think that the selection has changed, and therefore search should end — + // but it could be that the regular timeline already has these representedObjects, + // and therefore the selection hasn’t actually changed, + // and therefore search shouldn’t end. + // https://github.com/brentsimmons/NetNewsWire/issues/791 + if representedObjects == nil && regularTimelineViewController.representedObjects == nil { + return true + } + guard let currentObjects = regularTimelineViewController.representedObjects, let representedObjects = representedObjects else { + return false + } + if currentObjects.count != representedObjects.count { + return false + } + for object in representedObjects { + guard let _ = currentObjects.firstIndex(where: { $0 === object } ) else { + return false + } + } + return true + } } extension TimelineContainerViewController: TimelineDelegate { diff --git a/Mac/MainWindow/Timeline/TimelineViewController.swift b/Mac/MainWindow/Timeline/TimelineViewController.swift index 7249da5bb..26750287d 100644 --- a/Mac/MainWindow/Timeline/TimelineViewController.swift +++ b/Mac/MainWindow/Timeline/TimelineViewController.swift @@ -19,14 +19,14 @@ protocol TimelineDelegate: class { func timelineSelectionDidChange(_: TimelineViewController, selectedArticles: [Article]?) } -final class TimelineViewController: NSViewController, UndoableCommandRunner { +final class TimelineViewController: NSViewController, UndoableCommandRunner, UnreadCountProvider { @IBOutlet var tableView: TimelineTableView! var representedObjects: [AnyObject]? { didSet { if !representedObjectArraysAreEqual(oldValue, representedObjects) { - + unreadCount = 0 if let representedObjects = representedObjects { if representedObjects.count == 1 && representedObjects.first is Feed { showFeedNames = false @@ -76,11 +76,21 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner { // Just reload visible cells in this case: don’t call reloadData. articleRowMap = [String: Int]() reloadVisibleCells() + updateUnreadCount() return } updateShowAvatars() articleRowMap = [String: Int]() tableView.reloadData() + updateUnreadCount() + } + } + + var unreadCount: Int = 0 { + didSet { + if unreadCount != oldValue { + postUnreadCountDidChangeNotification() + } } } @@ -221,6 +231,16 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner { return selectedArticles.canMarkAllAsRead() } + func representsThisObjectOnly(_ object: AnyObject) -> Bool { + guard let representedObjects = representedObjects else { + return false + } + if representedObjects.count != 1 { + return false + } + return representedObjects.first! === object + } + // MARK: - Actions @objc func openArticleInBrowser(_ sender: Any?) { @@ -448,6 +468,7 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner { return } reloadVisibleCells(for: articles) + updateUnreadCount() } @objc func feedIconDidBecomeAvailable(_ note: Notification) { @@ -810,6 +831,16 @@ private extension TimelineViewController { } } + func updateUnreadCount() { + var count = 0 + for article in articles { + if !article.status.read { + count += 1 + } + } + unreadCount = count + } + func queueReloadAvailableCells() { CoalescingQueue.standard.add(self, #selector(reloadAvailableCells))