NetNewsWire/Shared/Activity/ActivityManager.swift

289 lines
8.4 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 Account
import Articles
2019-08-26 00:04:15 +02:00
import Intents
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 readingActivity != nil {
return readingActivity
}
return selectingActivity
}
init() {
NotificationCenter.default.addObserver(self, selector: #selector(feedIconDidBecomeAvailable(_:)), name: .FeedIconDidBecomeAvailable, object: nil)
}
func invalidateCurrentActivities() {
invalidateReading()
invalidateSelecting()
invalidateNextUnread()
}
2019-08-26 00:04:15 +02:00
func selectingToday() {
invalidateCurrentActivities()
let title = NSLocalizedString("See articles for “Today”", comment: "Today")
selectingActivity = makeSelectingActivity(type: ActivityType.selectToday, title: title, identifier: "smartfeed.today")
2019-08-26 00:04:15 +02:00
selectingActivity!.becomeCurrent()
}
func selectingAllUnread() {
invalidateCurrentActivities()
let title = NSLocalizedString("See articles in “All Unread”", comment: "All Unread")
selectingActivity = makeSelectingActivity(type: ActivityType.selectAllUnread, title: title, identifier: "smartfeed.allUnread")
2019-08-26 00:04:15 +02:00
selectingActivity!.becomeCurrent()
}
func selectingStarred() {
invalidateCurrentActivities()
let title = NSLocalizedString("See articles in “Starred”", comment: "Starred")
selectingActivity = makeSelectingActivity(type: ActivityType.selectStarred, title: title, identifier: "smartfeed.starred")
selectingActivity!.becomeCurrent()
}
func selectingFolder(_ folder: Folder) {
invalidateCurrentActivities()
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: ActivityManager.identifer(for: folder))
2019-10-03 16:53:21 +02:00
selectingActivity!.userInfo = folder.deepLinkUserInfo
selectingActivity!.becomeCurrent()
}
func selectingFeed(_ feed: Feed) {
invalidateCurrentActivities()
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: ActivityManager.identifer(for: feed))
2019-10-03 16:53:21 +02:00
selectingActivity!.userInfo = feed.deepLinkUserInfo
updateSelectingActivityFeedSearchAttributes(with: feed)
2019-08-26 00:04:15 +02:00
selectingActivity!.becomeCurrent()
}
func invalidateSelecting() {
selectingActivity?.invalidate()
selectingActivity = nil
}
func selectingNextUnread() {
guard nextUnreadActivity == nil else { return }
let title = NSLocalizedString("See first unread article", comment: "First Unread")
nextUnreadActivity = makeSelectingActivity(type: ActivityType.nextUnread, title: title, identifier: "action.nextUnread")
nextUnreadActivity!.becomeCurrent()
}
func invalidateNextUnread() {
nextUnreadActivity?.invalidate()
nextUnreadActivity = nil
}
2019-08-26 00:04:15 +02:00
func reading(_ article: Article?) {
invalidateReading()
invalidateNextUnread()
2019-08-26 00:04:15 +02:00
guard let article = article else { return }
readingActivity = makeReadArticleActivity(article)
#if os(iOS)
updateReadArticleSearchAttributes(with: article)
#endif
2019-08-26 00:04:15 +02:00
readingActivity?.becomeCurrent()
}
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 {
ids.append(identifer(for: folder))
}
}
for feed in account.flattenedFeeds() {
ids.append(contentsOf: identifers(for: feed))
}
NSUserActivity.deleteSavedUserActivities(withPersistentIdentifiers: ids) {}
}
static 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) {}
}
static func cleanUp(_ feed: Feed) {
NSUserActivity.deleteSavedUserActivities(withPersistentIdentifiers: identifers(for: feed)) {}
}
#endif
@objc func feedIconDidBecomeAvailable(_ note: Notification) {
2019-10-03 16:53:21 +02:00
guard let feed = note.userInfo?[UserInfoKey.feed] as? Feed, let activityFeedId = selectingActivity?.userInfo?[DeepLinkKey.feedID.rawValue] 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 {
func makeSelectingActivity(type: ActivityType, title: String, identifier: String) -> NSUserActivity {
2019-08-26 00:04:15 +02:00
let activity = NSUserActivity(activityType: type.rawValue)
activity.title = title
activity.keywords = Set(makeKeywords(title))
activity.isEligibleForSearch = true
#if os(iOS)
activity.suggestedInvocationPhrase = title
activity.isEligibleForPrediction = true
2019-08-26 00:04:15 +02:00
activity.persistentIdentifier = identifier
#endif
2019-08-26 00:04:15 +02:00
return activity
}
func makeReadArticleActivity(_ article: Article) -> NSUserActivity {
let activity = NSUserActivity(activityType: ActivityType.readArticle.rawValue)
activity.title = article.title
activity.userInfo = article.deepLinkUserInfo
activity.isEligibleForHandoff = true
#if os(iOS)
activity.keywords = Set(makeKeywords(article))
activity.isEligibleForSearch = true
activity.isEligibleForPrediction = false
activity.persistentIdentifier = ActivityManager.identifer(for: article)
updateReadArticleSearchAttributes(with: article)
#endif
readingArticle = article
return activity
}
#if os(iOS)
func updateReadArticleSearchAttributes(with article: Article) {
let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeCompositeContent as String)
attributeSet.title = article.title
attributeSet.contentDescription = article.summary
attributeSet.keywords = makeKeywords(article)
if let image = article.avatarImage() {
attributeSet.thumbnailData = image.pngData()
}
readingActivity?.contentAttributeSet = attributeSet
readingActivity?.needsSave = true
}
#endif
func makeKeywords(_ article: Article) -> [String] {
let feedNameKeywords = makeKeywords(article.feed?.nameForDisplay)
let articleTitleKeywords = makeKeywords(article.title)
return feedNameKeywords + articleTitleKeywords
}
2019-08-26 00:04:15 +02:00
func makeKeywords(_ value: String?) -> [String] {
return value?.components(separatedBy: " ").filter { $0.count > 2 } ?? []
}
func updateSelectingActivityFeedSearchAttributes(with feed: Feed) {
let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeItem as String)
attributeSet.title = feed.nameForDisplay
attributeSet.keywords = makeKeywords(feed.nameForDisplay)
if let image = appDelegate.feedIconDownloader.icon(for: feed) {
#if os(iOS)
attributeSet.thumbnailData = image.pngData()
#else
attributeSet.thumbnailData = image.tiffRepresentation
#endif
} else if let image = appDelegate.faviconDownloader.faviconAsAvatar(for: feed) {
#if os(iOS)
attributeSet.thumbnailData = image.pngData()
#else
attributeSet.thumbnailData = image.tiffRepresentation
#endif
}
selectingActivity!.contentAttributeSet = attributeSet
selectingActivity!.needsSave = true
}
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
}
}