2018-10-20 14:54:59 +00:00
|
|
|
//
|
|
|
|
// UserActivityManager.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 10/19/18.
|
|
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2020-08-31 23:28:50 +00:00
|
|
|
import Intents
|
2018-10-20 14:54:59 +00:00
|
|
|
import Pachyderm
|
|
|
|
|
|
|
|
class UserActivityManager {
|
|
|
|
|
|
|
|
// MARK: - Utils
|
2019-09-16 00:43:06 +00:00
|
|
|
private static let encoder = PropertyListEncoder()
|
|
|
|
private static let decoder = PropertyListDecoder()
|
|
|
|
|
2020-01-08 02:29:15 +00:00
|
|
|
private static var mastodonController: MastodonController {
|
|
|
|
let scene = UIApplication.shared.activeOrBackgroundScene!
|
|
|
|
return scene.session.mastodonController!
|
|
|
|
}
|
2020-01-05 20:25:07 +00:00
|
|
|
|
2020-10-27 02:13:48 +00:00
|
|
|
private static func getMainViewController() -> TuskerRootViewController {
|
2020-01-07 23:39:19 +00:00
|
|
|
let scene = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.first!
|
|
|
|
let window = scene.windows.first { $0.isKeyWindow }!
|
2020-10-27 02:13:48 +00:00
|
|
|
return window.rootViewController as! TuskerRootViewController
|
2019-09-16 01:00:57 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 01:15:32 +00:00
|
|
|
private static func present(_ vc: UIViewController, animated: Bool = true) {
|
2020-10-27 02:13:48 +00:00
|
|
|
getMainViewController().present(vc, animated: animated)
|
2019-09-16 01:15:32 +00:00
|
|
|
}
|
2020-12-14 03:37:37 +00:00
|
|
|
|
|
|
|
static func getAccount(from activity: NSUserActivity) -> LocalData.UserAccountInfo? {
|
|
|
|
guard let id = activity.userInfo?["accountID"] as? String else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return LocalData.shared.getAccount(id: id)
|
|
|
|
}
|
2021-06-06 18:55:04 +00:00
|
|
|
|
|
|
|
// MARK: - Main Scene
|
|
|
|
static func mainSceneActivity(accountID: String) -> NSUserActivity {
|
|
|
|
let activity = NSUserActivity(type: .mainScene)
|
|
|
|
activity.userInfo = [
|
|
|
|
"accountID": accountID,
|
|
|
|
]
|
|
|
|
return activity
|
|
|
|
}
|
2019-09-16 01:15:32 +00:00
|
|
|
|
2018-10-20 14:54:59 +00:00
|
|
|
// MARK: - New Post
|
2020-12-14 03:37:37 +00:00
|
|
|
static func newPostActivity(mentioning: Account? = nil, accountID: String) -> NSUserActivity {
|
2020-04-12 16:54:27 +00:00
|
|
|
// todo: update to use managed objects
|
2018-10-20 14:54:59 +00:00
|
|
|
let activity = NSUserActivity(type: .newPost)
|
|
|
|
activity.isEligibleForPrediction = true
|
2020-12-14 03:37:37 +00:00
|
|
|
activity.userInfo = [
|
|
|
|
"accountID": accountID,
|
|
|
|
]
|
2018-10-20 14:54:59 +00:00
|
|
|
if let mentioning = mentioning {
|
2020-12-14 03:37:37 +00:00
|
|
|
activity.userInfo!["mentioning"] = mentioning.acct
|
2020-04-12 16:54:27 +00:00
|
|
|
activity.title = "Send a message to \(mentioning.displayName)"
|
|
|
|
activity.suggestedInvocationPhrase = "Send a message to \(mentioning.displayName)"
|
2018-10-20 14:54:59 +00:00
|
|
|
} else {
|
|
|
|
activity.title = "New Post"
|
|
|
|
activity.suggestedInvocationPhrase = "Post in Tusker"
|
|
|
|
}
|
|
|
|
return activity
|
|
|
|
}
|
|
|
|
|
|
|
|
static func handleNewPost(activity: NSUserActivity) {
|
|
|
|
// TODO: check not currently showing compose screen
|
|
|
|
let mentioning = activity.userInfo?["mentioning"] as? String
|
2020-08-31 23:28:50 +00:00
|
|
|
let draft = mastodonController.createDraft(mentioningAcct: mentioning)
|
|
|
|
let composeVC = ComposeHostingController(draft: draft, mastodonController: mastodonController)
|
2020-01-05 20:25:07 +00:00
|
|
|
present(UINavigationController(rootViewController: composeVC))
|
2018-10-20 14:54:59 +00:00
|
|
|
}
|
|
|
|
|
2020-12-14 03:37:37 +00:00
|
|
|
static func editDraftActivity(id: UUID, accountID: String) -> NSUserActivity {
|
|
|
|
let activity = NSUserActivity(type: .newPost)
|
|
|
|
activity.userInfo = [
|
|
|
|
"accountID": accountID,
|
|
|
|
"draftID": id.uuidString,
|
|
|
|
]
|
|
|
|
return activity
|
|
|
|
}
|
|
|
|
|
|
|
|
static func getDraft(from activity: NSUserActivity) -> Draft? {
|
|
|
|
guard activity.activityType == UserActivityType.newPost.rawValue,
|
|
|
|
let str = activity.userInfo?["draftID"] as? String,
|
|
|
|
let uuid = UUID(uuidString: str) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return DraftsManager.shared.getBy(id: uuid)
|
|
|
|
}
|
|
|
|
|
2018-10-20 14:54:59 +00:00
|
|
|
// MARK: - Check Notifications
|
2020-12-14 03:37:37 +00:00
|
|
|
static func checkNotificationsActivity(mode: NotificationsMode = .allNotifications) -> NSUserActivity {
|
2018-10-20 14:54:59 +00:00
|
|
|
let activity = NSUserActivity(type: .checkNotifications)
|
|
|
|
activity.isEligibleForPrediction = true
|
2020-12-14 03:37:37 +00:00
|
|
|
activity.addUserInfoEntries(from: [
|
|
|
|
"notificationsMode": mode.rawValue
|
|
|
|
])
|
|
|
|
switch mode {
|
|
|
|
case .allNotifications:
|
|
|
|
activity.title = NSLocalizedString("Check Notifications", comment: "check notifications shortcut title")
|
|
|
|
activity.suggestedInvocationPhrase = NSLocalizedString("Check my Tusker notifications", comment: "check notifications shortcut invocation phrase")
|
|
|
|
case .mentionsOnly:
|
|
|
|
activity.title = NSLocalizedString("Check Mentions", comment: "check mentions shortcut title")
|
|
|
|
activity.suggestedInvocationPhrase = NSLocalizedString("Check my mentions", comment: "check mentions shortcut invocation phrase")
|
|
|
|
}
|
2018-10-20 14:54:59 +00:00
|
|
|
return activity
|
|
|
|
}
|
|
|
|
|
|
|
|
static func handleCheckNotifications(activity: NSUserActivity) {
|
2020-10-27 02:13:48 +00:00
|
|
|
let mainViewController = getMainViewController()
|
|
|
|
mainViewController.select(tab: .notifications)
|
|
|
|
if let navigationController = mainViewController.getTabController(tab: .notifications) as? UINavigationController,
|
2019-09-16 01:20:50 +00:00
|
|
|
let notificationsPageController = navigationController.viewControllers.first as? NotificationsPageViewController {
|
|
|
|
navigationController.popToRootViewController(animated: false)
|
2020-01-07 23:39:19 +00:00
|
|
|
notificationsPageController.loadViewIfNeeded()
|
2020-12-14 03:37:37 +00:00
|
|
|
notificationsPageController.selectMode(getNotificationsMode(from: activity) ?? Preferences.shared.defaultNotificationsMode)
|
2019-09-16 01:15:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 03:37:37 +00:00
|
|
|
static func getNotificationsMode(from activity: NSUserActivity) -> NotificationsMode? {
|
|
|
|
guard let str = activity.userInfo?["notificationsMode"] as? String else {
|
|
|
|
return nil
|
2019-09-16 01:15:32 +00:00
|
|
|
}
|
2020-12-14 03:37:37 +00:00
|
|
|
return NotificationsMode(rawValue: str)
|
2018-10-20 14:54:59 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 00:43:06 +00:00
|
|
|
// MARK: - Show Timeline
|
2020-12-14 03:37:37 +00:00
|
|
|
static func showTimelineActivity(timeline: Timeline, accountID: String) -> NSUserActivity? {
|
2019-09-16 00:43:06 +00:00
|
|
|
guard let timelineData = try? encoder.encode(timeline) else { return nil }
|
|
|
|
|
|
|
|
let activity = NSUserActivity(type: .showTimeline)
|
|
|
|
activity.isEligibleForPrediction = true
|
2020-12-14 03:37:37 +00:00
|
|
|
activity.userInfo = [
|
|
|
|
"timelineData": timelineData,
|
|
|
|
"accountID": accountID,
|
|
|
|
]
|
2019-09-16 00:43:06 +00:00
|
|
|
switch timeline {
|
|
|
|
case .home:
|
|
|
|
activity.title = NSLocalizedString("Show Home Timeline", comment: "home timeline shortcut title")
|
|
|
|
activity.suggestedInvocationPhrase = NSLocalizedString("Show my home timeline", comment: "home timeline shortcut invocation phrase")
|
|
|
|
case .public(local: true):
|
|
|
|
activity.title = NSLocalizedString("Show Local Timeline", comment: "local timeline shortcut title")
|
|
|
|
activity.suggestedInvocationPhrase = NSLocalizedString("Show my local timeline", comment: "local timeline shortcut invocation phrase")
|
|
|
|
case .public(local: false):
|
|
|
|
activity.title = NSLocalizedString("Show Federated Timeline", comment: "federated timeline shortcut title")
|
|
|
|
activity.suggestedInvocationPhrase = NSLocalizedString("Show my federated timeline", comment: "federated timeline invocation phrase")
|
|
|
|
case let .tag(hashtag):
|
|
|
|
activity.title = String(format: NSLocalizedString("Show #%@", comment: "show hashtag shortcut title"), hashtag)
|
|
|
|
activity.suggestedInvocationPhrase = String(format: NSLocalizedString("Show the %@ hashtag", comment: "hashtag shortcut invocation phrase"), hashtag)
|
|
|
|
case .list:
|
|
|
|
// todo: add title to list
|
|
|
|
activity.title = NSLocalizedString("Show List", comment: "list timeline shortcut title")
|
|
|
|
activity.suggestedInvocationPhrase = NSLocalizedString("Show my list", comment: "list timeline invocation phrase")
|
|
|
|
case .direct:
|
|
|
|
activity.title = NSLocalizedString("Show Direct Messages", comment: "direct message timeline shortcut title")
|
|
|
|
activity.suggestedInvocationPhrase = NSLocalizedString("Show my direct messages", comment: "direct message timeline invocation phrase")
|
|
|
|
}
|
|
|
|
return activity
|
|
|
|
}
|
|
|
|
|
2020-12-14 03:37:37 +00:00
|
|
|
static func getTimeline(from activity: NSUserActivity) -> Timeline? {
|
|
|
|
guard activity.activityType == UserActivityType.showTimeline.rawValue,
|
|
|
|
let data = activity.userInfo?["timelineData"] as? Data else {
|
|
|
|
return nil
|
2019-09-16 00:43:06 +00:00
|
|
|
}
|
2020-12-14 03:37:37 +00:00
|
|
|
return try? decoder.decode(Timeline.self, from: data)
|
|
|
|
}
|
|
|
|
|
|
|
|
static func handleShowTimeline(activity: NSUserActivity) {
|
|
|
|
guard let timeline = getTimeline(from: activity) else { return }
|
2019-09-16 00:43:06 +00:00
|
|
|
|
2020-10-27 02:13:48 +00:00
|
|
|
let mainViewController = getMainViewController()
|
|
|
|
mainViewController.select(tab: .timelines)
|
|
|
|
guard let navigationController = mainViewController.getTabController(tab: .timelines) as? UINavigationController else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-16 00:43:06 +00:00
|
|
|
switch timeline {
|
|
|
|
case .home, .public(true), .public(false):
|
|
|
|
navigationController.popToRootViewController(animated: false)
|
|
|
|
let rootController = navigationController.viewControllers.first! as! SegmentedPageViewController
|
|
|
|
let index: Int
|
|
|
|
switch timeline {
|
|
|
|
case .home:
|
|
|
|
index = 0
|
|
|
|
case .public(false):
|
|
|
|
index = 1
|
|
|
|
case .public(true):
|
|
|
|
index = 2
|
|
|
|
default:
|
|
|
|
fatalError()
|
|
|
|
}
|
|
|
|
rootController.segmentedControl.selectedSegmentIndex = index
|
|
|
|
rootController.selectPage(at: index, animated: false)
|
|
|
|
default:
|
2020-01-05 20:25:07 +00:00
|
|
|
let timeline = TimelineTableViewController(for: timeline, mastodonController: mastodonController)
|
|
|
|
navigationController.pushViewController(timeline, animated: false)
|
2019-09-16 00:43:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 03:37:37 +00:00
|
|
|
// MARK: - Show Conversation
|
|
|
|
static func showConversationActivity(mainStatusID: String, accountID: String, isEligibleForPrediction: Bool = false) -> NSUserActivity {
|
|
|
|
let activity = NSUserActivity(type: .showConversation)
|
|
|
|
activity.userInfo = [
|
|
|
|
"mainStatusID": mainStatusID,
|
|
|
|
"accountID": accountID,
|
|
|
|
]
|
|
|
|
activity.isEligibleForPrediction = isEligibleForPrediction
|
|
|
|
return activity
|
|
|
|
}
|
|
|
|
|
|
|
|
static func getConversationStatus(from activity: NSUserActivity) -> String? {
|
|
|
|
return activity.userInfo?["mainStatusID"] as? String
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:22:25 +00:00
|
|
|
// MARK: - Explore
|
|
|
|
|
2019-09-16 01:20:50 +00:00
|
|
|
static func searchActivity() -> NSUserActivity {
|
|
|
|
let activity = NSUserActivity(type: .search)
|
|
|
|
activity.isEligibleForPrediction = true
|
|
|
|
activity.title = NSLocalizedString("Search", comment: "search shortcut title")
|
|
|
|
activity.suggestedInvocationPhrase = NSLocalizedString("Search the fediverse", comment: "search shortcut invocation phrase")
|
|
|
|
return activity
|
|
|
|
}
|
|
|
|
|
|
|
|
static func handleSearch(activity: NSUserActivity) {
|
2020-10-27 02:13:48 +00:00
|
|
|
let mainViewController = getMainViewController()
|
|
|
|
mainViewController.select(tab: .explore)
|
|
|
|
if let navigationController = mainViewController.getTabController(tab: .explore) as? UINavigationController,
|
2019-12-17 05:22:25 +00:00
|
|
|
let exploreController = navigationController.viewControllers.first as? ExploreViewController {
|
|
|
|
navigationController.popToRootViewController(animated: false)
|
|
|
|
exploreController.searchController.isActive = true
|
|
|
|
exploreController.searchController.searchBar.becomeFirstResponder()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static func bookmarksActivity() -> NSUserActivity {
|
|
|
|
let activity = NSUserActivity(type: .bookmarks)
|
|
|
|
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")
|
|
|
|
return activity
|
|
|
|
}
|
|
|
|
|
|
|
|
static func handleBookmarks(activity: NSUserActivity) {
|
2020-10-27 02:13:48 +00:00
|
|
|
let mainViewController = getMainViewController()
|
|
|
|
mainViewController.select(tab: .explore)
|
|
|
|
if let navigationController = mainViewController.getTabController(tab: .explore) as? UINavigationController {
|
2019-09-16 01:20:50 +00:00
|
|
|
navigationController.popToRootViewController(animated: false)
|
2020-01-05 20:25:07 +00:00
|
|
|
navigationController.pushViewController(BookmarksTableViewController(mastodonController: mastodonController), animated: false)
|
2019-09-16 01:20:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 03:37:37 +00:00
|
|
|
// MARK: - My Profile
|
|
|
|
static func myProfileActivity() -> NSUserActivity {
|
|
|
|
let activity = NSUserActivity(type: .myProfile)
|
|
|
|
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")
|
|
|
|
return activity
|
|
|
|
}
|
|
|
|
|
|
|
|
static func handleMyProfile(activity: NSUserActivity) {
|
|
|
|
let mainViewController = getMainViewController()
|
|
|
|
mainViewController.select(tab: .myProfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Show Profile
|
|
|
|
static func showProfileActivity(id profileID: String, accountID: String) -> NSUserActivity {
|
|
|
|
let activity = NSUserActivity(type: .showProfile)
|
|
|
|
activity.userInfo = [
|
|
|
|
"profileID": profileID,
|
|
|
|
"accountID": accountID,
|
|
|
|
]
|
|
|
|
// todo: should this be eligible for prediction?
|
|
|
|
return activity
|
|
|
|
}
|
|
|
|
|
|
|
|
static func getProfile(from activity: NSUserActivity) -> String? {
|
|
|
|
return activity.userInfo?["profileID"] as? String
|
|
|
|
}
|
|
|
|
|
2018-10-20 14:54:59 +00:00
|
|
|
}
|