// // UserActivities.swift // Reader // // Created by Shadowfacts on 1/15/22. // import Foundation extension NSUserActivity { static let preferencesType = "net.shadowfacts.Reader.activity.preferences" static let addAccountType = "net.shadowfacts.Reader.activity.add-account" static let activateAccountType = "net.shadowfacts.Reader.activity.activate-account" static let readUnreadType = "net.shadowfacts.Reader.activity.read-unread" static let readAllType = "net.shadowfacts.Reader.activity.read-all" static let readFeedType = "net.shadowfacts.Reader.activity.read-feed" static let readGroupType = "net.shadowfacts.Reader.activity.read-group" static let readItemType = "net.shadowfacts.Reader.activity.read-item" func accountID() -> Data? { let types = [ NSUserActivity.activateAccountType, NSUserActivity.readUnreadType, NSUserActivity.readAllType, ] if types.contains(self.activityType), let id = self.userInfo?["accountID"] as? Data { return id } else { return nil } } func feedID() -> String? { if activityType == NSUserActivity.readFeedType { return userInfo?["feedID"] as? String } else { return nil } } func groupID() -> String? { if activityType == NSUserActivity.readGroupType { return userInfo?["groupID"] as? String } else { return nil } } var itemID: String? { if activityType == NSUserActivity.readItemType { return userInfo?["itemID"] as? String } else { return nil } } var topElementSelector: String? { get { userInfo?["topElementSelector"] as? String } set { if let newValue = newValue { addUserInfoEntries(from: ["topElementSelector": newValue]) } else { userInfo?.removeValue(forKey: "topElementSelector") } } } static func preferences() -> NSUserActivity { return NSUserActivity(activityType: preferencesType) } static func addAccount() -> NSUserActivity { return NSUserActivity(activityType: addAccountType) } static func activateAccount(_ account: LocalData.Account) -> NSUserActivity { let activity = NSUserActivity(activityType: activateAccountType) activity.userInfo = [ "accountID": account.id, ] return activity } static func readUnread(account: LocalData.Account) -> NSUserActivity { let activity = NSUserActivity(activityType: readUnreadType) activity.isEligibleForHandoff = true activity.isEligibleForPrediction = true activity.title = "Show unread articles" activity.userInfo = [ "accountID": account.id ] activity.targetContentIdentifier = account.id.base64EncodedString() return activity } static func readAll(account: LocalData.Account) -> NSUserActivity { let activity = NSUserActivity(activityType: readAllType) activity.isEligibleForHandoff = true activity.isEligibleForPrediction = true activity.title = "Show all articles" activity.userInfo = [ "accountID": account.id ] activity.targetContentIdentifier = account.id.base64EncodedString() return activity } static func readFeed(_ feed: Feed, account: LocalData.Account) -> NSUserActivity { let activity = NSUserActivity(activityType: readFeedType) activity.isEligibleForHandoff = true activity.isEligibleForPrediction = true activity.title = "Show articles from \(feed.title!)" activity.userInfo = [ "accountID": account.id, "feedID": feed.id! ] activity.targetContentIdentifier = account.id.base64EncodedString() return activity } static func readGroup(_ group: Group, account: LocalData.Account) -> NSUserActivity { let activity = NSUserActivity(activityType: readGroupType) activity.isEligibleForHandoff = true activity.isEligibleForPrediction = true activity.title = "Show articles from \(group.title)" activity.userInfo = [ "accountID": account.id, "groupID": group.id! ] activity.targetContentIdentifier = account.id.base64EncodedString() return activity } static func readItem(_ item: Item, account: LocalData.Account) -> NSUserActivity { let activity = NSUserActivity(activityType: readItemType) activity.isEligibleForHandoff = true activity.userInfo = [ "accountID": account.id, "itemID": item.id!, ] return activity } }