Implement stateRestorationActivity to save most relevant current user activity

This commit is contained in:
Maurice Parker 2019-08-31 19:30:21 -05:00
parent ecec048716
commit 87030a5921
5 changed files with 54 additions and 43 deletions

View File

@ -15,11 +15,16 @@ import Intents
class ActivityManager {
public static var shared = ActivityManager()
private var selectingActivity: NSUserActivity? = nil
private var readingActivity: NSUserActivity? = nil
var stateRestorationActivity: NSUserActivity? {
if readingActivity != nil {
return readingActivity
}
return selectingActivity
}
init() {
NotificationCenter.default.addObserver(self, selector: #selector(feedIconDidBecomeAvailable(_:)), name: .FeedIconDidBecomeAvailable, object: nil)
}
@ -45,7 +50,7 @@ class ActivityManager {
func selectingFolder(_ folder: Folder) {
let localizedText = NSLocalizedString("See articles in “%@”", comment: "See articles in Folder")
let title = NSString.localizedStringWithFormat(localizedText as NSString, folder.nameForDisplay) as String
selectingActivity = makeSelectingActivity(type: ActivityType.selectFolder, title: title, identifier: identifer(for: folder))
selectingActivity = makeSelectingActivity(type: ActivityType.selectFolder, title: title, identifier: ActivityManager.identifer(for: folder))
selectingActivity!.userInfo = [
ActivityID.accountID.rawValue: folder.account?.accountID ?? "",
@ -59,7 +64,7 @@ class ActivityManager {
func selectingFeed(_ feed: Feed) {
let localizedText = NSLocalizedString("See articles in “%@”", comment: "See articles in Feed")
let title = NSString.localizedStringWithFormat(localizedText as NSString, feed.nameForDisplay) as String
selectingActivity = makeSelectingActivity(type: ActivityType.selectFeed, title: title, identifier: identifer(for: feed))
selectingActivity = makeSelectingActivity(type: ActivityType.selectFeed, title: title, identifier: ActivityManager.identifer(for: feed))
selectingActivity!.userInfo = [
ActivityID.accountID.rawValue: feed.account?.accountID ?? "",
@ -79,7 +84,7 @@ class ActivityManager {
readingActivity?.becomeCurrent()
}
func cleanUp(_ account: Account) {
static func cleanUp(_ account: Account) {
var ids = [String]()
if let folders = account.folders {
@ -95,7 +100,7 @@ class ActivityManager {
NSUserActivity.deleteSavedUserActivities(withPersistentIdentifiers: ids) {}
}
func cleanUp(_ folder: Folder) {
static func cleanUp(_ folder: Folder) {
var ids = [String]()
ids.append(identifer(for: folder))
@ -106,7 +111,7 @@ class ActivityManager {
NSUserActivity.deleteSavedUserActivities(withPersistentIdentifiers: ids) {}
}
func cleanUp(_ feed: Feed) {
static func cleanUp(_ feed: Feed) {
NSUserActivity.deleteSavedUserActivities(withPersistentIdentifiers: identifers(for: feed)) {}
}
@ -155,7 +160,7 @@ private extension ActivityManager {
activity.isEligibleForSearch = true
activity.isEligibleForPrediction = false
activity.isEligibleForHandoff = true
activity.persistentIdentifier = identifer(for: article)
activity.persistentIdentifier = ActivityManager.identifer(for: article)
// CoreSpotlight
let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeCompositeContent as String)
@ -176,29 +181,6 @@ private extension ActivityManager {
return value?.components(separatedBy: " ").filter { $0.count > 2 } ?? []
}
func identifer(for folder: Folder) -> String {
return "account_\(folder.account!.accountID)_folder_\(folder.nameForDisplay)"
}
func identifer(for feed: Feed) -> String {
return "account_\(feed.account!.accountID)_feed_\(feed.feedID)"
}
func identifer(for article: Article) -> String {
return "account_\(article.accountID)_feed_\(article.feedID)_article_\(article.articleID)"
}
func identifers(for feed: Feed) -> [String] {
var ids = [String]()
ids.append(identifer(for: feed))
for article in feed.fetchArticles() {
ids.append(identifer(for: article))
}
return ids
}
func updateSelectingActivityFeedSearchAttributes(with feed: Feed) {
let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeItem as String)
@ -215,4 +197,27 @@ private extension ActivityManager {
}
static func identifer(for folder: Folder) -> String {
return "account_\(folder.account!.accountID)_folder_\(folder.nameForDisplay)"
}
static func identifer(for feed: Feed) -> String {
return "account_\(feed.account!.accountID)_feed_\(feed.feedID)"
}
static func identifer(for article: Article) -> String {
return "account_\(article.accountID)_feed_\(article.feedID)_article_\(article.articleID)"
}
static func identifers(for feed: Feed) -> [String] {
var ids = [String]()
ids.append(identifer(for: feed))
for article in feed.fetchArticles() {
ids.append(identifer(for: article))
}
return ids
}
}

View File

@ -24,6 +24,8 @@ class AppCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider {
return rootSplitViewController.undoManager
}
private var activityManager = ActivityManager()
private var rootSplitViewController: UISplitViewController!
private var masterNavigationController: UINavigationController!
private var masterFeedViewController: MasterFeedViewController!
@ -71,6 +73,10 @@ class AppCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider {
return TreeController(delegate: treeControllerDelegate)
}()
var stateRestorationActivity: NSUserActivity? {
return activityManager.stateRestorationActivity
}
var isRootSplitCollapsed: Bool {
return rootSplitViewController.isCollapsed
}
@ -499,7 +505,7 @@ class AppCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider {
func selectArticle(_ indexPath: IndexPath?) {
currentArticleIndexPath = indexPath
ActivityManager.shared.reading(currentArticle)
activityManager.reading(currentArticle)
if indexPath == nil {
if !rootSplitViewController.isCollapsed {
@ -1234,15 +1240,15 @@ private extension AppCoordinator {
func updateSelectingActivity(with node: Node) {
switch true {
case node.representedObject === SmartFeedsController.shared.todayFeed:
ActivityManager.shared.selectingToday()
activityManager.selectingToday()
case node.representedObject === SmartFeedsController.shared.unreadFeed:
ActivityManager.shared.selectingAllUnread()
activityManager.selectingAllUnread()
case node.representedObject === SmartFeedsController.shared.starredFeed:
ActivityManager.shared.selectingStarred()
activityManager.selectingStarred()
case node.representedObject is Folder:
ActivityManager.shared.selectingFolder(node.representedObject as! Folder)
activityManager.selectingFolder(node.representedObject as! Folder)
case node.representedObject is Feed:
ActivityManager.shared.selectingFeed(node.representedObject as! Feed)
activityManager.selectingFeed(node.representedObject as! Feed)
default:
break
}

View File

@ -815,9 +815,9 @@ private extension MasterFeedViewController {
}
if let folder = deleteNode.representedObject as? Folder {
ActivityManager.shared.cleanUp(folder)
ActivityManager.cleanUp(folder)
} else if let feed = deleteNode.representedObject as? Feed {
ActivityManager.shared.cleanUp(feed)
ActivityManager.cleanUp(feed)
}
pushUndoableCommand(deleteCommand)

View File

@ -40,8 +40,8 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
appDelegate.prepareAccountsForForeground()
}
// func stateRestorationActivity(for scene: UIScene) -> NSUserActivity? {
// return scene.userActivity
// }
func stateRestorationActivity(for scene: UIScene) -> NSUserActivity? {
return coordinator.stateRestorationActivity
}
}

View File

@ -121,7 +121,7 @@ private extension DetailAccountViewController {
let markAction = UIAlertAction(title: markTitle, style: .default) { [weak self] (action) in
guard let account = self?.account else { return }
AccountManager.shared.deleteAccount(account)
ActivityManager.shared.cleanUp(account)
ActivityManager.cleanUp(account)
self?.navigationController?.popViewController(animated: true)
}
alertController.addAction(markAction)