Clean activities when the associated data is deleted

This commit is contained in:
Maurice Parker 2019-08-28 11:30:40 -05:00
parent ba9f7ac426
commit 9c66f6160e
4 changed files with 75 additions and 3 deletions

View File

@ -41,7 +41,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: "folder.\(folder.nameForDisplay)")
selectingActivity = makeSelectingActivity(type: ActivityType.selectFolder, title: title, identifier: identifer(for: folder))
selectingActivity!.userInfo = [
ActivityID.accountID.rawValue: folder.account?.accountID ?? "",
@ -55,7 +55,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: feed.url)
selectingActivity = makeSelectingActivity(type: ActivityType.selectFeed, title: title, identifier: identifer(for: feed))
selectingActivity!.userInfo = [
ActivityID.accountID.rawValue: feed.account?.accountID ?? "",
@ -63,6 +63,16 @@ class ActivityManager {
ActivityID.feedID.rawValue: feed.feedID
]
let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeItem as String)
attributeSet.title = feed.nameForDisplay
attributeSet.keywords = makeKeywords(feed.nameForDisplay)
if let image = appDelegate.feedIconDownloader.icon(for: feed) {
attributeSet.thumbnailData = image.pngData()
} else if let image = appDelegate.faviconDownloader.faviconAsAvatar(for: feed) {
attributeSet.thumbnailData = image.pngData()
}
selectingActivity!.contentAttributeSet = attributeSet
selectingActivity!.becomeCurrent()
}
@ -74,6 +84,37 @@ class ActivityManager {
readingActivity?.becomeCurrent()
}
func cleanUp(_ account: Account) {
var ids = [String]()
if let folders = account.folders {
for folder in folders {
ids.append(identifer(for: folder))
}
}
for feed in account.flattenedFeeds() {
ids.append(contentsOf: identifers(for: feed))
}
NSUserActivity.deleteSavedUserActivities(withPersistentIdentifiers: ids) {}
}
func cleanUp(_ folder: Folder) {
var ids = [String]()
ids.append(identifer(for: folder))
for feed in folder.flattenedFeeds() {
ids.append(contentsOf: identifers(for: feed))
}
NSUserActivity.deleteSavedUserActivities(withPersistentIdentifiers: ids) {}
}
func cleanUp(_ feed: Feed) {
NSUserActivity.deleteSavedUserActivities(withPersistentIdentifiers: identifers(for: feed)) {}
}
}
// MARK: Private
@ -110,13 +151,13 @@ private extension ActivityManager {
activity.isEligibleForSearch = true
activity.isEligibleForPrediction = false
activity.isEligibleForHandoff = true
activity.persistentIdentifier = identifer(for: article)
// CoreSpotlight
let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeCompositeContent as String)
attributeSet.title = article.title
attributeSet.contentDescription = article.summary
attributeSet.keywords = keywords
attributeSet.relatedUniqueIdentifier = article.url
if let image = article.avatarImage() {
attributeSet.thumbnailData = image.pngData()
@ -131,4 +172,27 @@ 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
}
}

View File

@ -859,6 +859,12 @@ private extension MasterFeedViewController {
return
}
if let folder = deleteNode.representedObject as? Folder {
ActivityManager.shared.cleanUp(folder)
} else if let feed = deleteNode.representedObject as? Feed {
ActivityManager.shared.cleanUp(feed)
}
var deleteIndexPaths = [indexPath]
if coordinator.isExpanded(deleteNode) {
for i in 0..<deleteNode.numberOfChildNodes {

View File

@ -114,6 +114,7 @@ struct SettingsDetailAccountView : View {
func delete() {
AccountManager.shared.deleteAccount(account)
ActivityManager.shared.cleanUp(account)
}
}
}

View File

@ -121,6 +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)
self?.navigationController?.popViewController(animated: true)
}
alertController.addAction(markAction)