Simplify NSUserActivity construction code

This commit is contained in:
Shadowfacts 2023-02-22 21:42:09 -05:00
parent 75f290ae8f
commit 71fa3910a1
4 changed files with 30 additions and 43 deletions

View File

@ -304,7 +304,7 @@ class MainSidebarViewController: UIViewController {
switch item { switch item {
case .tab(.notifications): case .tab(.notifications):
return UserActivityManager.checkNotificationsActivity(mode: Preferences.shared.defaultNotificationsMode) return UserActivityManager.checkNotificationsActivity(mode: Preferences.shared.defaultNotificationsMode, accountID: id)
case .tab(.compose): case .tab(.compose):
return UserActivityManager.newPostActivity(accountID: id) return UserActivityManager.newPostActivity(accountID: id)
case .explore: case .explore:

View File

@ -22,7 +22,7 @@ class NotificationsPageViewController: SegmentedPageViewController<Notifications
super.init(pages: [.all, .mentions]) { page in super.init(pages: [.all, .mentions]) { page in
let vc = NotificationsTableViewController(allowedTypes: page.allowedTypes, mastodonController: mastodonController) let vc = NotificationsTableViewController(allowedTypes: page.allowedTypes, mastodonController: mastodonController)
vc.title = page.title vc.title = page.title
vc.userActivity = page.userActivity vc.userActivity = page.userActivity(accountID: mastodonController.accountInfo!.id)
return vc return vc
} }
@ -79,12 +79,12 @@ class NotificationsPageViewController: SegmentedPageViewController<Notifications
} }
} }
var userActivity: NSUserActivity { func userActivity(accountID: String) -> NSUserActivity {
switch self { switch self {
case .all: case .all:
return UserActivityManager.checkNotificationsActivity(mode: .allNotifications) return UserActivityManager.checkNotificationsActivity(mode: .allNotifications, accountID: accountID)
case .mentions: case .mentions:
return UserActivityManager.checkNotificationsActivity(mode: .mentionsOnly) return UserActivityManager.checkNotificationsActivity(mode: .mentionsOnly, accountID: accountID)
} }
} }
} }
@ -93,7 +93,7 @@ class NotificationsPageViewController: SegmentedPageViewController<Notifications
extension NotificationsPageViewController: StateRestorableViewController { extension NotificationsPageViewController: StateRestorableViewController {
func stateRestorationActivity() -> NSUserActivity? { func stateRestorationActivity() -> NSUserActivity? {
return currentPage.userActivity return currentPage.userActivity(accountID: mastodonController.accountInfo!.id)
} }
func restoreActivity(_ activity: NSUserActivity) { func restoreActivity(_ activity: NSUserActivity) {

View File

@ -34,8 +34,11 @@ extension NSUserActivity {
} }
} }
convenience init(type: UserActivityType) { convenience init(type: UserActivityType, accountID: String) {
self.init(activityType: type.rawValue) self.init(activityType: type.rawValue)
self.userInfo = [
"accountID": accountID
]
} }
func handleResume(manager: UserActivityManager) -> Bool { func handleResume(manager: UserActivityManager) -> Bool {

View File

@ -47,7 +47,7 @@ class UserActivityManager {
// MARK: - Main Scene // MARK: - Main Scene
static func mainSceneActivity(accountID: String?) -> NSUserActivity { static func mainSceneActivity(accountID: String?) -> NSUserActivity {
let activity = NSUserActivity(type: .mainScene) let activity = NSUserActivity(activityType: UserActivityType.mainScene.rawValue)
if let accountID { if let accountID {
activity.userInfo = [ activity.userInfo = [
"accountID": accountID, "accountID": accountID,
@ -59,11 +59,8 @@ class UserActivityManager {
// MARK: - New Post // MARK: - New Post
static func newPostActivity(mentioning: Account? = nil, accountID: String) -> NSUserActivity { static func newPostActivity(mentioning: Account? = nil, accountID: String) -> NSUserActivity {
// todo: update to use managed objects // todo: update to use managed objects
let activity = NSUserActivity(type: .newPost) let activity = NSUserActivity(type: .newPost, accountID: accountID)
activity.isEligibleForPrediction = true activity.isEligibleForPrediction = true
activity.userInfo = [
"accountID": accountID,
]
if let mentioning = mentioning { if let mentioning = mentioning {
activity.userInfo!["mentioning"] = mentioning.acct activity.userInfo!["mentioning"] = mentioning.acct
activity.title = "Send a message to \(mentioning.displayName)" activity.title = "Send a message to \(mentioning.displayName)"
@ -85,11 +82,10 @@ class UserActivityManager {
} }
static func editDraftActivity(id: UUID, accountID: String) -> NSUserActivity { static func editDraftActivity(id: UUID, accountID: String) -> NSUserActivity {
let activity = NSUserActivity(type: .newPost) let activity = NSUserActivity(type: .newPost, accountID: accountID)
activity.userInfo = [ activity.addUserInfoEntries(from: [
"accountID": accountID,
"draftID": id.uuidString, "draftID": id.uuidString,
] ])
return activity return activity
} }
@ -130,8 +126,8 @@ class UserActivityManager {
} }
// MARK: - Check Notifications // MARK: - Check Notifications
static func checkNotificationsActivity(mode: NotificationsMode = .allNotifications) -> NSUserActivity { static func checkNotificationsActivity(mode: NotificationsMode = .allNotifications, accountID: String) -> NSUserActivity {
let activity = NSUserActivity(type: .checkNotifications) let activity = NSUserActivity(type: .checkNotifications, accountID: accountID)
activity.isEligibleForPrediction = true activity.isEligibleForPrediction = true
activity.addUserInfoEntries(from: [ activity.addUserInfoEntries(from: [
"notificationsMode": mode.rawValue "notificationsMode": mode.rawValue
@ -169,12 +165,11 @@ class UserActivityManager {
static func showTimelineActivity(timeline: Timeline, accountID: String) -> NSUserActivity? { static func showTimelineActivity(timeline: Timeline, accountID: String) -> NSUserActivity? {
guard let timelineData = try? encoder.encode(timeline) else { return nil } guard let timelineData = try? encoder.encode(timeline) else { return nil }
let activity = NSUserActivity(type: .showTimeline) let activity = NSUserActivity(type: .showTimeline, accountID: accountID)
activity.isEligibleForPrediction = true activity.isEligibleForPrediction = true
activity.userInfo = [ activity.addUserInfoEntries(from: [
"timelineData": timelineData, "timelineData": timelineData,
"accountID": accountID, ])
]
switch timeline { switch timeline {
case .home: case .home:
activity.title = NSLocalizedString("Show Home Timeline", comment: "home timeline shortcut title") activity.title = NSLocalizedString("Show Home Timeline", comment: "home timeline shortcut title")
@ -229,11 +224,10 @@ class UserActivityManager {
// MARK: - Show Conversation // MARK: - Show Conversation
static func showConversationActivity(mainStatusID: String, accountID: String, isEligibleForPrediction: Bool = false) -> NSUserActivity { static func showConversationActivity(mainStatusID: String, accountID: String, isEligibleForPrediction: Bool = false) -> NSUserActivity {
let activity = NSUserActivity(type: .showConversation) let activity = NSUserActivity(type: .showConversation, accountID: accountID)
activity.userInfo = [ activity.addUserInfoEntries(from: [
"mainStatusID": mainStatusID, "mainStatusID": mainStatusID,
"accountID": accountID, ])
]
activity.isEligibleForPrediction = isEligibleForPrediction activity.isEligibleForPrediction = isEligibleForPrediction
return activity return activity
} }
@ -245,10 +239,7 @@ class UserActivityManager {
// MARK: - Explore // MARK: - Explore
static func searchActivity(query: String?, accountID: String) -> NSUserActivity { static func searchActivity(query: String?, accountID: String) -> NSUserActivity {
let activity = NSUserActivity(type: .search) let activity = NSUserActivity(type: .search, accountID: accountID)
activity.userInfo = [
"accountID": accountID
]
if let query { if let query {
activity.userInfo!["query"] = query activity.userInfo!["query"] = query
} }
@ -280,10 +271,7 @@ class UserActivityManager {
} }
static func bookmarksActivity(accountID: String) -> NSUserActivity { static func bookmarksActivity(accountID: String) -> NSUserActivity {
let activity = NSUserActivity(type: .bookmarks) let activity = NSUserActivity(type: .bookmarks, accountID: accountID)
activity.userInfo = [
"accountID": accountID
]
activity.isEligibleForPrediction = true activity.isEligibleForPrediction = true
activity.title = NSLocalizedString("View Bookmarks", comment: "bookmarks shortcut title") activity.title = NSLocalizedString("View Bookmarks", comment: "bookmarks shortcut title")
activity.suggestedInvocationPhrase = NSLocalizedString("Show my bookmarks in Tusker", comment: "bookmarks shortcut invocation phrase") activity.suggestedInvocationPhrase = NSLocalizedString("Show my bookmarks in Tusker", comment: "bookmarks shortcut invocation phrase")
@ -301,10 +289,7 @@ class UserActivityManager {
// MARK: - My Profile // MARK: - My Profile
static func myProfileActivity(accountID: String) -> NSUserActivity { static func myProfileActivity(accountID: String) -> NSUserActivity {
let activity = NSUserActivity(type: .myProfile) let activity = NSUserActivity(type: .myProfile, accountID: accountID)
activity.userInfo = [
"accountID": accountID
]
activity.isEligibleForPrediction = true activity.isEligibleForPrediction = true
activity.title = NSLocalizedString("My Profile", comment: "my profile shortcut title") activity.title = NSLocalizedString("My Profile", comment: "my profile shortcut title")
activity.suggestedInvocationPhrase = NSLocalizedString("Show my Mastodon profile", comment: "my profile shortuct invocation phrase") activity.suggestedInvocationPhrase = NSLocalizedString("Show my Mastodon profile", comment: "my profile shortuct invocation phrase")
@ -318,12 +303,11 @@ class UserActivityManager {
// MARK: - Show Profile // MARK: - Show Profile
static func showProfileActivity(id profileID: String, accountID: String) -> NSUserActivity { static func showProfileActivity(id profileID: String, accountID: String) -> NSUserActivity {
let activity = NSUserActivity(type: .showProfile) let activity = NSUserActivity(type: .showProfile, accountID: accountID)
activity.userInfo = [ activity.addUserInfoEntries(from: [
"profileID": profileID, "profileID": profileID,
"accountID": accountID, ])
] activity.isEligibleForPrediction = true
// todo: should this be eligible for prediction?
return activity return activity
} }