Change to get currently selected unread count item from total number of articles shown in timeline

This commit is contained in:
Maurice Parker 2019-08-21 13:10:08 -05:00
parent eb8de035d7
commit c4e2420966
2 changed files with 50 additions and 14 deletions

View File

@ -21,7 +21,7 @@ public extension Notification.Name {
static let ArticleSelectionDidChange = Notification.Name(rawValue: "ArticleSelectionDidChange") static let ArticleSelectionDidChange = Notification.Name(rawValue: "ArticleSelectionDidChange")
} }
class AppCoordinator: NSObject, UndoableCommandRunner { class AppCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider {
var undoableCommands = [UndoableCommand]() var undoableCommands = [UndoableCommand]()
var undoManager: UndoManager? { var undoManager: UndoManager? {
@ -107,6 +107,7 @@ class AppCoordinator: NSObject, UndoableCommandRunner {
var timelineFetcher: ArticleFetcher? { var timelineFetcher: ArticleFetcher? {
didSet { didSet {
unreadCount = 0
currentArticleIndexPath = nil currentArticleIndexPath = nil
if timelineFetcher is Feed { if timelineFetcher is Feed {
showFeedNames = false showFeedNames = false
@ -181,11 +182,13 @@ class AppCoordinator: NSObject, UndoableCommandRunner {
if articles.representSameArticlesInSameOrder(as: oldValue) { if articles.representSameArticlesInSameOrder(as: oldValue) {
articleRowMap = [String: Int]() articleRowMap = [String: Int]()
NotificationCenter.default.post(name: .ArticleDataDidChange, object: self, userInfo: nil) NotificationCenter.default.post(name: .ArticleDataDidChange, object: self, userInfo: nil)
updateUnreadCount()
return return
} }
updateShowAvatars() updateShowAvatars()
articleRowMap = [String: Int]() articleRowMap = [String: Int]()
NotificationCenter.default.post(name: .ArticlesDidChange, object: self, userInfo: nil) NotificationCenter.default.post(name: .ArticlesDidChange, object: self, userInfo: nil)
updateUnreadCount()
} }
} }
@ -200,6 +203,14 @@ class AppCoordinator: NSObject, UndoableCommandRunner {
return appDelegate.unreadCount > 0 return appDelegate.unreadCount > 0
} }
var unreadCount: Int = 0 {
didSet {
if unreadCount != oldValue {
postUnreadCountDidChangeNotification()
}
}
}
override init() { override init() {
super.init() super.init()
@ -210,6 +221,7 @@ class AppCoordinator: NSObject, UndoableCommandRunner {
rebuildShadowTable() rebuildShadowTable()
NotificationCenter.default.addObserver(self, selector: #selector(statusesDidChange(_:)), name: .StatusesDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(containerChildrenDidChange(_:)), name: .ChildrenDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(containerChildrenDidChange(_:)), name: .ChildrenDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(batchUpdateDidPerform(_:)), name: .BatchUpdateDidPerform, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(batchUpdateDidPerform(_:)), name: .BatchUpdateDidPerform, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(displayNameDidChange(_:)), name: .DisplayNameDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(displayNameDidChange(_:)), name: .DisplayNameDidChange, object: nil)
@ -240,6 +252,10 @@ class AppCoordinator: NSObject, UndoableCommandRunner {
// MARK: Notifications // MARK: Notifications
@objc func statusesDidChange(_ note: Notification) {
updateUnreadCount()
}
@objc func containerChildrenDidChange(_ note: Notification) { @objc func containerChildrenDidChange(_ note: Notification) {
rebuildBackingStores() rebuildBackingStores()
} }
@ -336,6 +352,17 @@ class AppCoordinator: NSObject, UndoableCommandRunner {
return nil return nil
} }
func unreadCountFor(_ node: Node) -> Int {
// The coordinator supplies the unread count for the currently selected feed node
if let indexPath = currentMasterIndexPath, let selectedNode = nodeFor(indexPath), selectedNode == node {
return unreadCount
}
if let unreadCountProvider = node.representedObject as? UnreadCountProvider {
return unreadCountProvider.unreadCount
}
return 0
}
func expand(section: Int, completion: ([IndexPath]) -> ()) { func expand(section: Int, completion: ([IndexPath]) -> ()) {
guard let expandNode = treeController.rootNode.childAtIndex(section) else { guard let expandNode = treeController.rootNode.childAtIndex(section) else {
@ -713,6 +740,16 @@ extension AppCoordinator: UISplitViewControllerDelegate {
private extension AppCoordinator { private extension AppCoordinator {
func updateUnreadCount() {
var count = 0
for article in articles {
if !article.status.read {
count += 1
}
}
unreadCount = count
}
func rebuildBackingStores() { func rebuildBackingStores() {
if !animatingChanges && !BatchUpdate.shared.isPerforming { if !animatingChanges && !BatchUpdate.shared.isPerforming {
treeController.rebuild() treeController.rebuild()

View File

@ -93,8 +93,14 @@ class MasterFeedViewController: UITableViewController, UndoableCommandRunner {
return return
} }
guard let node = coordinator.rootNode.descendantNodeRepresentingObject(representedObject as AnyObject), var node: Node? = nil
let indexPath = coordinator.indexPathFor(node) else { if let coordinator = representedObject as? AppCoordinator, let fetcher = coordinator.timelineFetcher {
node = coordinator.rootNode.descendantNodeRepresentingObject(fetcher as AnyObject)
} else {
node = coordinator.rootNode.descendantNodeRepresentingObject(representedObject as AnyObject)
}
guard let unwrappedNode = node, let indexPath = coordinator.indexPathFor(unwrappedNode) else {
return return
} }
@ -559,7 +565,7 @@ private extension MasterFeedViewController {
cell.allowDisclosureSelection = node.canHaveChildNodes cell.allowDisclosureSelection = node.canHaveChildNodes
cell.name = nameFor(node) cell.name = nameFor(node)
cell.unreadCount = unreadCountFor(node) cell.unreadCount = coordinator.unreadCountFor(node)
configureFavicon(cell, node) configureFavicon(cell, node)
cell.shouldShowImage = node.representedObject is SmallIconProvider cell.shouldShowImage = node.representedObject is SmallIconProvider
@ -583,13 +589,6 @@ private extension MasterFeedViewController {
return "" return ""
} }
func unreadCountFor(_ node: Node) -> Int {
if let unreadCountProvider = node.representedObject as? UnreadCountProvider {
return unreadCountProvider.unreadCount
}
return 0
}
func configureCellsForRepresentedObject(_ representedObject: AnyObject) { func configureCellsForRepresentedObject(_ representedObject: AnyObject) {
applyToCellsForRepresentedObject(representedObject, configure) applyToCellsForRepresentedObject(representedObject, configure)
} }