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 {
case .tab(.notifications):
return UserActivityManager.checkNotificationsActivity(mode: Preferences.shared.defaultNotificationsMode)
return UserActivityManager.checkNotificationsActivity(mode: Preferences.shared.defaultNotificationsMode, accountID: id)
case .tab(.compose):
return UserActivityManager.newPostActivity(accountID: id)
case .explore:

View File

@ -22,7 +22,7 @@ class NotificationsPageViewController: SegmentedPageViewController<Notifications
super.init(pages: [.all, .mentions]) { page in
let vc = NotificationsTableViewController(allowedTypes: page.allowedTypes, mastodonController: mastodonController)
vc.title = page.title
vc.userActivity = page.userActivity
vc.userActivity = page.userActivity(accountID: mastodonController.accountInfo!.id)
return vc
}
@ -79,12 +79,12 @@ class NotificationsPageViewController: SegmentedPageViewController<Notifications
}
}
var userActivity: NSUserActivity {
func userActivity(accountID: String) -> NSUserActivity {
switch self {
case .all:
return UserActivityManager.checkNotificationsActivity(mode: .allNotifications)
return UserActivityManager.checkNotificationsActivity(mode: .allNotifications, accountID: accountID)
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 {
func stateRestorationActivity() -> NSUserActivity? {
return currentPage.userActivity
return currentPage.userActivity(accountID: mastodonController.accountInfo!.id)
}
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.userInfo = [
"accountID": accountID
]
}
func handleResume(manager: UserActivityManager) -> Bool {

View File

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