2022-01-15 19:09:30 +00:00
|
|
|
//
|
|
|
|
// UserActivities.swift
|
|
|
|
// Reader
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 1/15/22.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2022-06-19 18:56:56 +00:00
|
|
|
import Persistence
|
2022-01-15 19:09:30 +00:00
|
|
|
|
|
|
|
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"
|
2022-03-08 02:54:41 +00:00
|
|
|
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"
|
2022-03-14 01:12:05 +00:00
|
|
|
static let readItemType = "net.shadowfacts.Reader.activity.read-item"
|
2022-03-08 02:54:41 +00:00
|
|
|
|
|
|
|
func accountID() -> Data? {
|
2022-03-08 04:06:05 +00:00
|
|
|
let types = [
|
|
|
|
NSUserActivity.activateAccountType,
|
|
|
|
NSUserActivity.readUnreadType,
|
|
|
|
NSUserActivity.readAllType,
|
|
|
|
]
|
|
|
|
if types.contains(self.activityType),
|
2022-03-08 02:54:41 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2022-03-14 01:12:05 +00:00
|
|
|
|
|
|
|
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 = [
|
2022-03-08 02:54:41 +00:00
|
|
|
"accountID": account.id,
|
2022-01-15 19:09:30 +00:00
|
|
|
]
|
|
|
|
return activity
|
|
|
|
}
|
|
|
|
|
2022-03-08 04:06:05 +00:00
|
|
|
static func readUnread(account: LocalData.Account) -> NSUserActivity {
|
2022-03-08 02:54:41 +00:00
|
|
|
let activity = NSUserActivity(activityType: readUnreadType)
|
|
|
|
activity.isEligibleForHandoff = true
|
|
|
|
activity.isEligibleForPrediction = true
|
|
|
|
activity.title = "Show unread articles"
|
2022-03-08 04:06:05 +00:00
|
|
|
activity.userInfo = [
|
|
|
|
"accountID": account.id
|
|
|
|
]
|
|
|
|
activity.targetContentIdentifier = account.id.base64EncodedString()
|
2022-03-08 02:54:41 +00:00
|
|
|
return activity
|
|
|
|
}
|
|
|
|
|
2022-03-08 04:06:05 +00:00
|
|
|
static func readAll(account: LocalData.Account) -> NSUserActivity {
|
2022-03-08 02:54:41 +00:00
|
|
|
let activity = NSUserActivity(activityType: readAllType)
|
|
|
|
activity.isEligibleForHandoff = true
|
|
|
|
activity.isEligibleForPrediction = true
|
|
|
|
activity.title = "Show all articles"
|
2022-03-08 04:06:05 +00:00
|
|
|
activity.userInfo = [
|
|
|
|
"accountID": account.id
|
|
|
|
]
|
|
|
|
activity.targetContentIdentifier = account.id.base64EncodedString()
|
2022-03-08 02:54:41 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-03-14 01:12:05 +00:00
|
|
|
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
|
|
|
}
|