Add feed/group user activities

This commit is contained in:
Shadowfacts 2022-03-07 23:16:35 -05:00
parent 9d22d4ef35
commit 533bc025f3
4 changed files with 66 additions and 2 deletions

View File

@ -6,6 +6,8 @@
<array>
<string>$(PRODUCT_BUNDLE_IDENTIFIER).activity.read-unread</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER).activity.read-all</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER).activity.read-feed</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER).activity.read-group</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER).activity.preferences</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER).activity.add-account</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER).activity.activate-account</string>

View File

@ -122,6 +122,24 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
split.selectHomeItem(.unread)
case NSUserActivity.readAllType:
split.selectHomeItem(.all)
case NSUserActivity.readFeedType:
guard let feedID = activity.feedID() else {
break
}
let req = Feed.fetchRequest()
req.predicate = NSPredicate(format: "id = %@", feedID)
if let feed = try? fervorController.persistentContainer.viewContext.fetch(req).first {
split.selectHomeItem(.feed(feed))
}
case NSUserActivity.readGroupType:
guard let groupID = activity.groupID() else {
break
}
let req = Group.fetchRequest()
req.predicate = NSPredicate(format: "id = %@", groupID)
if let group = try? fervorController.persistentContainer.viewContext.fetch(req).first {
split.selectHomeItem(.group(group))
}
default:
break
}

View File

@ -208,9 +208,9 @@ class HomeViewController: UIViewController {
case .unread:
vc.userActivity = .readUnread(account: fervorController.account!)
case .group(let group):
break
vc.userActivity = .readGroup(group, account: fervorController.account!)
case .feed(let feed):
break
vc.userActivity = .readFeed(feed, account: fervorController.account!)
}
return vc
}

View File

@ -14,6 +14,8 @@ extension NSUserActivity {
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"
func accountID() -> Data? {
let types = [
@ -29,6 +31,22 @@ extension NSUserActivity {
}
}
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
}
}
static func preferences() -> NSUserActivity {
return NSUserActivity(activityType: preferencesType)
}
@ -69,4 +87,30 @@ extension NSUserActivity {
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
}
}