diff --git a/iOS/Master/MasterViewController.swift b/iOS/Master/MasterViewController.swift index aaf4dca2f..57239a045 100644 --- a/iOS/Master/MasterViewController.swift +++ b/iOS/Master/MasterViewController.swift @@ -43,7 +43,7 @@ class MasterViewController: UITableViewController, UndoableCommandRunner { NotificationCenter.default.addObserver(self, selector: #selector(faviconDidBecomeAvailable(_:)), name: .FaviconDidBecomeAvailable, object: nil) 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(userDidAddFeed(_:)), name: .UserDidAddFeed, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(progressDidChange(_:)), name: .AccountRefreshProgressDidChange, object: nil) refreshControl = UIRefreshControl() @@ -88,11 +88,11 @@ class MasterViewController: UITableViewController, UndoableCommandRunner { } @objc func containerChildrenDidChange(_ note: Notification) { - rebuildTreeAndReloadDataIfNeeded() + rebuildBackingStoresAndReloadDataIfNeeded() } @objc func batchUpdateDidPerform(_ notification: Notification) { - rebuildTreeAndReloadDataIfNeeded() + rebuildBackingStoresAndReloadDataIfNeeded() } @objc func unreadCountDidChange(_ note: Notification) { @@ -136,11 +136,36 @@ class MasterViewController: UITableViewController, UndoableCommandRunner { return } - rebuildTreeAndReloadDataIfNeeded() + rebuildBackingStoresAndReloadDataIfNeeded() configureCellsForRepresentedObject(object as AnyObject) } + @objc func userDidAddFeed(_ notification: Notification) { + + guard let feed = notification.userInfo?[UserInfoKey.feed], + let node = treeController.rootNode.descendantNodeRepresentingObject(feed as AnyObject) else { + return + } + + if let indexPath = indexPathFor(node) { + tableView.scrollToRow(at: indexPath, at: .middle, animated: true) + return + } + + // It wasn't already visable, so expand its folder and try again + guard let parent = node.parent, let indexPath = indexPathFor(parent) else { + return + } + + expand(indexPath) + + if let indexPath = indexPathFor(node) { + tableView.scrollToRow(at: indexPath, at: .middle, animated: true) + } + + } + // MARK: Table View override func numberOfSections(in tableView: UITableView) -> Int { @@ -172,7 +197,7 @@ class MasterViewController: UITableViewController, UndoableCommandRunner { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! MasterTableViewCell - guard let node = nodeFor(indexPath: indexPath) else { + guard let node = nodeFor(indexPath) else { return cell } @@ -182,7 +207,7 @@ class MasterViewController: UITableViewController, UndoableCommandRunner { } override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { - guard let node = nodeFor(indexPath: indexPath), !(node.representedObject is PseudoFeed) else { + guard let node = nodeFor(indexPath), !(node.representedObject is PseudoFeed) else { return false } return true @@ -214,7 +239,7 @@ class MasterViewController: UITableViewController, UndoableCommandRunner { override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { - guard let node = nodeFor(indexPath: indexPath) else { + guard let node = nodeFor(indexPath) else { assertionFailure() return } @@ -381,7 +406,7 @@ class MasterViewController: UITableViewController, UndoableCommandRunner { func delete(indexPath: IndexPath) { guard let undoManager = undoManager, - let deleteNode = nodeFor(indexPath: indexPath), + let deleteNode = nodeFor(indexPath), let deleteCommand = DeleteCommand(nodesToDelete: [deleteNode], treeController: treeController, undoManager: undoManager) else { return @@ -399,7 +424,7 @@ class MasterViewController: UITableViewController, UndoableCommandRunner { func rename(indexPath: IndexPath) { - let name = (nodeFor(indexPath: indexPath)?.representedObject as? DisplayNameProvider)?.nameForDisplay ?? "" + let name = (nodeFor(indexPath)?.representedObject as? DisplayNameProvider)?.nameForDisplay ?? "" let formatString = NSLocalizedString("Rename “%@”", comment: "Feed finder") let title = NSString.localizedStringWithFormat(formatString as NSString, name) as String @@ -411,7 +436,7 @@ class MasterViewController: UITableViewController, UndoableCommandRunner { let renameTitle = NSLocalizedString("Rename", comment: "Rename") let renameAction = UIAlertAction(title: renameTitle, style: .default) { [weak self] action in - guard let node = self?.nodeFor(indexPath: indexPath), + guard let node = self?.nodeFor(indexPath), let name = alertController.textFields?[0].text, !name.isEmpty else { return @@ -437,10 +462,19 @@ class MasterViewController: UITableViewController, UndoableCommandRunner { } - func nodeFor(indexPath: IndexPath) -> Node? { + func nodeFor(_ indexPath: IndexPath) -> Node? { return shadowTable[indexPath.section][indexPath.row] } + func indexPathFor(_ node: Node) -> IndexPath? { + for i in 0.. Void) { tableView.visibleCells.forEach { cell in - guard let indexPath = tableView.indexPath(for: cell), let node = nodeFor(indexPath: indexPath) else { + guard let indexPath = tableView.indexPath(for: cell), let node = nodeFor(indexPath) else { return } callback(cell as! MasterTableViewCell, node) } } - func rebuildBackingStructures() { - treeController.rebuild() - rebuildShadowTable() - } - func rebuildShadowTable() { for i in 0..