frenzy-ios/Reader/UserActivities.swift

73 lines
2.3 KiB
Swift

//
// 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"
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
}
}
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
}
}