NetNewsWire/Shared/Activity/ActivityManager.swift

301 lines
8.9 KiB
Swift
Raw Normal View History

//
// ActivityManager.swift
// NetNewsWire-iOS
//
// Created by Maurice Parker on 8/23/19.
// Copyright © 2019 Ranchero Software. All rights reserved.
//
import Foundation
import CoreSpotlight
import CoreServices
import RSCore
import Account
import Articles
2019-08-26 00:04:15 +02:00
import Intents
import UniformTypeIdentifiers
class ActivityManager {
private var nextUnreadActivity: NSUserActivity?
private var selectingActivity: NSUserActivity?
private var readingActivity: NSUserActivity?
private var readingArticle: Article?
2019-08-26 00:04:15 +02:00
var stateRestorationActivity: NSUserActivity {
if let activity = readingActivity {
return activity
}
if let activity = selectingActivity {
return activity
}
let activity = NSUserActivity(activityType: ActivityType.restoration.rawValue)
#if os(iOS)
activity.persistentIdentifier = UUID().uuidString
#endif
activity.becomeCurrent()
return activity
}
init() {
NotificationCenter.default.addObserver(self, selector: #selector(feedIconDidBecomeAvailable(_:)), name: .FeedIconDidBecomeAvailable, object: nil)
}
func invalidateCurrentActivities() {
invalidateReading()
invalidateSelecting()
invalidateNextUnread()
}
2024-02-26 06:14:10 +01:00
func selecting(feed: SidebarItem) {
invalidateCurrentActivities()
2019-11-15 13:19:14 +01:00
selectingActivity = makeSelectFeedActivity(feed: feed)
if let feed = feed as? Feed {
updateSelectingActivityFeedSearchAttributes(with: feed)
}
2019-10-18 20:01:28 +02:00
donate(selectingActivity!)
2019-08-26 00:04:15 +02:00
}
func invalidateSelecting() {
selectingActivity?.invalidate()
selectingActivity = nil
}
func selectingNextUnread() {
guard nextUnreadActivity == nil else { return }
nextUnreadActivity = NSUserActivity(activityType: ActivityType.nextUnread.rawValue)
nextUnreadActivity!.title = NSLocalizedString("See first unread article", comment: "First Unread")
#if os(iOS)
nextUnreadActivity!.suggestedInvocationPhrase = nextUnreadActivity!.title
nextUnreadActivity!.isEligibleForPrediction = true
nextUnreadActivity!.persistentIdentifier = "nextUnread:"
nextUnreadActivity!.contentAttributeSet?.relatedUniqueIdentifier = "nextUnread:"
#endif
2019-10-18 20:01:28 +02:00
donate(nextUnreadActivity!)
}
func invalidateNextUnread() {
nextUnreadActivity?.invalidate()
nextUnreadActivity = nil
}
2024-02-26 06:14:10 +01:00
func reading(feed: SidebarItem?, article: Article?) {
invalidateReading()
invalidateNextUnread()
guard let article = article else { return }
2019-11-15 13:19:14 +01:00
readingActivity = makeReadArticleActivity(feed: feed, article: article)
#if os(iOS)
updateReadArticleSearchAttributes(with: article)
#endif
2019-10-18 20:01:28 +02:00
donate(readingActivity!)
2019-08-26 00:04:15 +02:00
}
func invalidateReading() {
readingActivity?.invalidate()
readingActivity = nil
readingArticle = nil
}
#if os(iOS)
static func cleanUp(_ account: Account) {
var ids = [String]()
if let folders = account.folders {
for folder in folders {
2022-01-04 23:25:20 +01:00
ids.append(identifier(for: folder))
}
}
for feed in account.flattenedFeeds() {
ids.append(contentsOf: identifiers(for: feed))
}
2019-10-18 20:01:28 +02:00
CSSearchableIndex.default().deleteSearchableItems(withIdentifiers: ids)
}
static func cleanUp(_ folder: Folder) {
var ids = [String]()
2022-01-04 23:25:20 +01:00
ids.append(identifier(for: folder))
for feed in folder.flattenedFeeds() {
ids.append(contentsOf: identifiers(for: feed))
}
2019-10-18 20:01:28 +02:00
CSSearchableIndex.default().deleteSearchableItems(withIdentifiers: ids)
}
static func cleanUp(_ feed: Feed) {
CSSearchableIndex.default().deleteSearchableItems(withIdentifiers: identifiers(for: feed))
}
#endif
@objc func feedIconDidBecomeAvailable(_ note: Notification) {
guard let feed = note.userInfo?[UserInfoKey.feed] as? Feed, let activityFeedId = selectingActivity?.userInfo?[ArticlePathKey.feedID] as? String else {
return
}
#if os(iOS)
if let article = readingArticle, activityFeedId == article.feedID {
updateReadArticleSearchAttributes(with: article)
}
#endif
if activityFeedId == feed.feedID {
updateSelectingActivityFeedSearchAttributes(with: feed)
}
}
2019-08-26 00:04:15 +02:00
}
// MARK: Private
private extension ActivityManager {
2024-02-26 06:14:10 +01:00
func makeSelectFeedActivity(feed: SidebarItem) -> NSUserActivity {
let activity = NSUserActivity(activityType: ActivityType.selectFeed.rawValue)
let localizedText = NSLocalizedString("See articles in “%@”", comment: "See articles in Folder")
2019-11-15 13:19:14 +01:00
let title = NSString.localizedStringWithFormat(localizedText as NSString, feed.nameForDisplay) as String
2019-08-26 00:04:15 +02:00
activity.title = title
2019-08-26 00:04:15 +02:00
activity.keywords = Set(makeKeywords(title))
activity.isEligibleForSearch = true
2024-02-26 06:34:22 +01:00
let articleFetcherIdentifierUserInfo = feed.sidebarItemID?.userInfo ?? [AnyHashable: Any]()
activity.userInfo = [UserInfoKey.feedIdentifier: articleFetcherIdentifierUserInfo]
activity.requiredUserInfoKeys = Set(activity.userInfo!.keys.map { $0 as! String })
2024-02-26 06:34:22 +01:00
activity.persistentIdentifier = feed.sidebarItemID?.description ?? ""
#if os(iOS)
activity.suggestedInvocationPhrase = title
activity.isEligibleForPrediction = true
2024-02-26 06:34:22 +01:00
activity.contentAttributeSet?.relatedUniqueIdentifier = feed.sidebarItemID?.description ?? ""
#endif
2019-08-26 00:04:15 +02:00
return activity
}
2024-02-26 06:14:10 +01:00
func makeReadArticleActivity(feed: SidebarItem?, article: Article) -> NSUserActivity {
let activity = NSUserActivity(activityType: ActivityType.readArticle.rawValue)
activity.title = ArticleStringFormatter.truncatedTitle(article)
2019-11-15 13:19:14 +01:00
if let feed = feed {
2024-02-26 06:34:22 +01:00
let articleFetcherIdentifierUserInfo = feed.sidebarItemID?.userInfo ?? [AnyHashable: Any]()
let articlePathUserInfo = article.pathUserInfo
activity.userInfo = [UserInfoKey.feedIdentifier: articleFetcherIdentifierUserInfo, UserInfoKey.articlePath: articlePathUserInfo]
} else {
activity.userInfo = [UserInfoKey.articlePath: article.pathUserInfo]
}
activity.requiredUserInfoKeys = Set(activity.userInfo!.keys.map { $0 as! String })
activity.isEligibleForHandoff = true
2022-01-04 23:25:20 +01:00
activity.persistentIdentifier = ActivityManager.identifier(for: article)
#if os(iOS)
activity.keywords = Set(makeKeywords(article))
activity.isEligibleForSearch = true
activity.isEligibleForPrediction = false
updateReadArticleSearchAttributes(with: article)
#endif
readingArticle = article
return activity
}
#if os(iOS)
func updateReadArticleSearchAttributes(with article: Article) {
let attributeSet = CSSearchableItemAttributeSet(itemContentType: UTType.compositeContent.identifier)
attributeSet.title = ArticleStringFormatter.truncatedTitle(article)
attributeSet.contentDescription = article.summary
attributeSet.keywords = makeKeywords(article)
2022-01-04 23:25:20 +01:00
attributeSet.relatedUniqueIdentifier = ActivityManager.identifier(for: article)
2019-10-18 20:01:28 +02:00
if let iconImage = article.iconImage() {
attributeSet.thumbnailData = iconImage.image.pngData()
}
readingActivity?.contentAttributeSet = attributeSet
readingActivity?.needsSave = true
}
#endif
func makeKeywords(_ article: Article) -> [String] {
let feedNameKeywords = makeKeywords(article.feed?.nameForDisplay)
let articleTitleKeywords = makeKeywords(ArticleStringFormatter.truncatedTitle(article))
return feedNameKeywords + articleTitleKeywords
}
2019-08-26 00:04:15 +02:00
func makeKeywords(_ value: String?) -> [String] {
return value?.components(separatedBy: " ").filter { $0.count > 2 } ?? []
}
2024-02-26 06:41:18 +01:00
func updateSelectingActivityFeedSearchAttributes(with feed: Feed) {
2024-02-25 04:03:20 +01:00
let attributeSet = CSSearchableItemAttributeSet(contentType: UTType.item)
attributeSet.title = feed.nameForDisplay
attributeSet.keywords = makeKeywords(feed.nameForDisplay)
2022-01-04 23:25:20 +01:00
attributeSet.relatedUniqueIdentifier = ActivityManager.identifier(for: feed)
if let iconImage = IconImageCache.shared.imageForFeed(feed) {
attributeSet.thumbnailData = iconImage.image.dataRepresentation()
}
selectingActivity!.contentAttributeSet = attributeSet
selectingActivity!.needsSave = true
}
2019-10-18 20:01:28 +02:00
func donate(_ activity: NSUserActivity) {
// You have to put the search item in the index or the activity won't index
// itself because the relatedUniqueIdentifier on the activity attributeset is populated.
if let attributeSet = activity.contentAttributeSet {
let identifier = attributeSet.relatedUniqueIdentifier
2024-02-25 04:03:20 +01:00
let tempAttributeSet = CSSearchableItemAttributeSet(contentType: UTType.item)
2019-10-18 20:01:28 +02:00
let searchableItem = CSSearchableItem(uniqueIdentifier: identifier, domainIdentifier: nil, attributeSet: tempAttributeSet)
CSSearchableIndex.default().indexSearchableItems([searchableItem])
}
activity.becomeCurrent()
}
2022-01-04 23:25:20 +01:00
static func identifier(for folder: Folder) -> String {
return "account_\(folder.account!.accountID)_folder_\(folder.nameForDisplay)"
}
2024-02-26 06:41:18 +01:00
static func identifier(for feed: Feed) -> String {
return "account_\(feed.account!.accountID)_feed_\(feed.feedID)"
}
2022-01-04 23:25:20 +01:00
static func identifier(for article: Article) -> String {
return "account_\(article.accountID)_feed_\(article.feedID)_article_\(article.articleID)"
}
2024-02-26 06:41:18 +01:00
static func identifiers(for feed: Feed) -> [String] {
var ids = [String]()
2022-01-04 23:25:20 +01:00
ids.append(identifier(for: feed))
if let articles = try? feed.fetchArticles() {
for article in articles {
2022-01-04 23:25:20 +01:00
ids.append(identifier(for: article))
}
}
return ids
}
}