frenzy-ios/Reader/UserActivities.swift

149 lines
4.8 KiB
Swift
Raw Normal View History

2022-01-15 19:09:30 +00:00
//
// UserActivities.swift
// Reader
//
// Created by Shadowfacts on 1/15/22.
//
import Foundation
extension NSUserActivity {
2022-01-16 16:58:28 +00:00
static let preferencesType = "net.shadowfacts.Reader.activity.preferences"
2022-01-15 19:09:30 +00:00
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"
2022-03-08 04:16:35 +00:00
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
}
}
2022-01-15 19:09:30 +00:00
2022-03-14 01:12:33 +00:00
var feedID: String? {
2022-03-08 04:16:35 +00:00
if activityType == NSUserActivity.readFeedType {
return userInfo?["feedID"] as? String
} else {
return nil
}
}
2022-03-14 01:12:33 +00:00
var groupID: String? {
2022-03-08 04:16:35 +00:00
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")
}
}
}
2022-03-08 04:16:35 +00:00
2022-01-16 16:58:28 +00:00
static func preferences() -> NSUserActivity {
return NSUserActivity(activityType: preferencesType)
}
2022-01-15 19:09:30 +00:00
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,
2022-01-15 19:09:30 +00:00
]
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
}
2022-03-08 04:16:35 +00:00
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
}
2022-01-15 19:09:30 +00:00
}