From 76bad13ae173cfe89a8222b539729b3406db0f14 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Tue, 4 Feb 2020 11:00:55 -0800 Subject: [PATCH] Rebuild the backing stores to except spotlight search result restoration. Issue #1759 --- iOS/SceneCoordinator.swift | 71 +++++++++++++++++++++++++------------- iOS/SceneDelegate.swift | 8 ++--- 2 files changed, 51 insertions(+), 28 deletions(-) diff --git a/iOS/SceneCoordinator.swift b/iOS/SceneCoordinator.swift index c5a533170..5636a2c37 100644 --- a/iOS/SceneCoordinator.swift +++ b/iOS/SceneCoordinator.swift @@ -360,27 +360,27 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider { } } - func handle(_ activity: NSUserActivity) { + func handle(_ activity: NSUserActivity, initialLoad: Bool) { selectFeed(nil) { guard let activityType = ActivityType(rawValue: activity.activityType) else { return } switch activityType { case .restoration: break case .selectFeed: - self.handleSelectFeed(activity.userInfo) + self.handleSelectFeed(activity.userInfo, initialLoad: initialLoad) case .nextUnread: self.selectFirstUnreadInAllUnread() case .readArticle: - self.handleReadArticle(activity.userInfo) + self.handleReadArticle(activity.userInfo, initialLoad: initialLoad) case .addFeedIntent: self.showAdd(.feed) } } } - func handle(_ response: UNNotificationResponse) { + func handle(_ response: UNNotificationResponse, initialLoad: Bool) { let userInfo = response.notification.request.content.userInfo - handleReadArticle(userInfo) + handleReadArticle(userInfo, initialLoad: initialLoad) } func configurePanelMode(for size: CGSize) { @@ -1920,7 +1920,7 @@ private extension SceneCoordinator { ] } - func handleSelectFeed(_ userInfo: [AnyHashable : Any]?) { + func handleSelectFeed(_ userInfo: [AnyHashable : Any]?, initialLoad: Bool) { guard let userInfo = userInfo, let feedIdentifierUserInfo = userInfo[UserInfoKey.feedIdentifier] as? [AnyHashable : AnyHashable], let feedIdentifier = FeedIdentifier(userInfo: feedIdentifierUserInfo) else { @@ -1930,11 +1930,20 @@ private extension SceneCoordinator { treeControllerDelegate.addFilterException(feedIdentifier) masterFeedViewController.restoreSelection = true + func rebuildIfNecessary() { + if !initialLoad && isReadFeedsFiltered { + rebuildBackingStores() + treeControllerDelegate.resetFilterExceptions() + } + } switch feedIdentifier { case .smartFeed: guard let smartFeed = SmartFeedsController.shared.find(by: feedIdentifier) else { return } + + rebuildIfNecessary() + if let indexPath = indexPathFor(smartFeed) { selectFeed(indexPath) { self.masterFeedViewController.focus() @@ -1945,6 +1954,8 @@ private extension SceneCoordinator { break case .folder(let accountID, let folderName): + rebuildIfNecessary() + guard let accountNode = findAccountNode(accountID: accountID), let folderNode = findFolderNode(folderName: folderName, beginningAt: accountNode) else { return } @@ -1955,22 +1966,29 @@ private extension SceneCoordinator { } case .webFeed(let accountID, let webFeedID): - guard let accountNode = findAccountNode(accountID: accountID), let feedNode = findWebFeedNode(webFeedID: webFeedID, beginningAt: accountNode) else { + guard let accountNode = findAccountNode(accountID: accountID), + let account = accountNode.representedObject as? Account, + let webFeed = account.existingWebFeed(withWebFeedID: webFeedID) else { return } - if let folder = feedNode.parent?.representedObject as? Folder, let folderFeedID = folder.feedID { - treeControllerDelegate.addFilterException(folderFeedID) - } - if let feed = feedNode.representedObject as? WebFeed { - discloseFeed(feed) { - self.masterFeedViewController.focus() + + for folder in account.sortedFolders ?? [Folder]() { + if folder.objectIsChild(webFeed), let folderFeedID = folder.feedID { + treeControllerDelegate.addFilterException(folderFeedID) + break } } + rebuildIfNecessary() + + discloseFeed(webFeed) { + self.masterFeedViewController.focus() + } + } } - func handleReadArticle(_ userInfo: [AnyHashable : Any]?) { + func handleReadArticle(_ userInfo: [AnyHashable : Any]?, initialLoad: Bool) { guard let userInfo = userInfo else { return } guard let articlePathUserInfo = userInfo[UserInfoKey.articlePath] as? [AnyHashable : Any], @@ -1989,15 +2007,24 @@ private extension SceneCoordinator { return } - guard let webFeedNode = findWebFeedNode(webFeedID: webFeedID, beginningAt: accountNode), - let webFeed = webFeedNode.representedObject as? WebFeed, + guard let webFeed = account.existingWebFeed(withWebFeedID: webFeedID), let webFeedFeedID = webFeed.feedID else { return } treeControllerDelegate.addFilterException(webFeedFeedID) - addParentFolderToFilterExceptions(webFeedNode) + for folder in account.sortedFolders ?? [Folder]() { + if folder.objectIsChild(webFeed), let folderFeedID = folder.feedID { + treeControllerDelegate.addFilterException(folderFeedID) + break + } + } + if !initialLoad && isReadFeedsFiltered { + rebuildBackingStores() + treeControllerDelegate.resetFilterExceptions() + } + discloseFeed(webFeed) { self.selectArticleInCurrentFeed(articleID) } @@ -2044,7 +2071,9 @@ private extension SceneCoordinator { let found = selectFeedAndArticle(feedNode: webFeedNode, articleID: articleID) if found { treeControllerDelegate.addFilterException(feedIdentifier) - addParentFolderToFilterExceptions(webFeedNode) + if let folder = webFeedNode.parent?.representedObject as? Folder, let folderFeedID = folder.feedID { + treeControllerDelegate.addFilterException(folderFeedID) + } } return found @@ -2095,10 +2124,4 @@ private extension SceneCoordinator { } } - func addParentFolderToFilterExceptions(_ feedNode: Node) { - if let folder = feedNode.parent?.representedObject as? Folder, let folderFeedID = folder.feedID { - treeControllerDelegate.addFilterException(folderFeedID) - } - } - } diff --git a/iOS/SceneDelegate.swift b/iOS/SceneDelegate.swift index 6c6447e5d..dd00e8f4b 100644 --- a/iOS/SceneDelegate.swift +++ b/iOS/SceneDelegate.swift @@ -33,12 +33,12 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { if let notificationResponse = connectionOptions.notificationResponse { window!.makeKeyAndVisible() - coordinator.handle(notificationResponse) + coordinator.handle(notificationResponse, initialLoad: true) return } if let userActivity = connectionOptions.userActivities.first ?? session.stateRestorationActivity { - coordinator.handle(userActivity) + coordinator.handle(userActivity, initialLoad: true) } window!.makeKeyAndVisible() @@ -52,7 +52,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { func scene(_ scene: UIScene, continue userActivity: NSUserActivity) { appDelegate.resumeDatabaseProcessingIfNecessary() - coordinator.handle(userActivity) + coordinator.handle(userActivity, initialLoad: false) } func sceneDidEnterBackground(_ scene: UIScene) { @@ -74,7 +74,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { func handle(_ response: UNNotificationResponse) { appDelegate.resumeDatabaseProcessingIfNecessary() - coordinator.handle(response) + coordinator.handle(response, initialLoad: false) } func suspend() {