Optimize and fix unread count updates

This commit is contained in:
Maurice Parker 2019-08-29 14:35:18 -05:00
parent 8e583c2fad
commit 197fc0170e
1 changed files with 47 additions and 15 deletions

View File

@ -75,7 +75,31 @@ class MasterFeedViewController: UITableViewController, UndoableCommandRunner {
@objc func unreadCountDidChange(_ note: Notification) { @objc func unreadCountDidChange(_ note: Notification) {
updateUI() updateUI()
applyChanges(animate: false)
guard let representedObject = note.object else {
return
}
if let account = representedObject as? Account {
if let node = coordinator.rootNode.childNodeRepresentingObject(account) {
let sectionIndex = coordinator.rootNode.indexOfChild(node)!
if let headerView = tableView.headerView(forSection: sectionIndex) as? MasterFeedTableViewSectionHeader {
headerView.unreadCount = account.unreadCount
}
}
return
}
var node: Node? = nil
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)
}
if let node = node, coordinator.indexPathFor(node) != nil {
reloadNode(node)
}
} }
@objc func faviconDidBecomeAvailable(_ note: Notification) { @objc func faviconDidBecomeAvailable(_ note: Notification) {
@ -426,23 +450,19 @@ private extension MasterFeedViewController {
} }
func reloadNode(_ node: Node) { func reloadNode(_ node: Node) {
let savedNode = selectedNode()
var snapshot = dataSource.snapshot() var snapshot = dataSource.snapshot()
snapshot.reloadItems([node]) snapshot.reloadItems([node])
dataSource.apply(snapshot) dataSource.apply(snapshot, animatingDifferences: false) { [weak self] in
self?.selectRow(node: savedNode)
}
} }
func applyChanges(animate: Bool, completion: (() -> Void)? = nil) { func applyChanges(animate: Bool, completion: (() -> Void)? = nil) {
let savedNode = selectedNode()
let selectedNode: Node? = {
if let selectedIndexPath = tableView.indexPathForSelectedRow {
return coordinator.nodeFor(selectedIndexPath)
} else {
return nil
}
}()
var snapshot = NSDiffableDataSourceSnapshot<Int, Node>() var snapshot = NSDiffableDataSourceSnapshot<Int, Node>()
let sections = coordinator.allSections let sections = coordinator.allSections
snapshot.appendSections(sections) snapshot.appendSections(sections)
@ -451,12 +471,24 @@ private extension MasterFeedViewController {
} }
dataSource.apply(snapshot, animatingDifferences: animate) { [weak self] in dataSource.apply(snapshot, animatingDifferences: animate) { [weak self] in
if let nodeToSelect = selectedNode, let selectingIndexPath = self?.coordinator.indexPathFor(nodeToSelect) { self?.selectRow(node: savedNode)
self?.tableView.selectRow(at: selectingIndexPath, animated: false, scrollPosition: .none)
}
completion?() completion?()
} }
}
func selectedNode() -> Node? {
if let selectedIndexPath = tableView.indexPathForSelectedRow {
return coordinator.nodeFor(selectedIndexPath)
} else {
return nil
}
}
func selectRow(node: Node?) {
if let nodeToSelect = node, let selectingIndexPath = coordinator.indexPathFor(nodeToSelect) {
tableView.selectRow(at: selectingIndexPath, animated: false, scrollPosition: .none)
}
} }
func makeDataSource() -> UITableViewDiffableDataSource<Int, Node> { func makeDataSource() -> UITableViewDiffableDataSource<Int, Node> {