Tusker/Tusker/Shortcuts/UserActivityManager.swift

124 lines
5.7 KiB
Swift

//
// UserActivityManager.swift
// Tusker
//
// Created by Shadowfacts on 10/19/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import UIKit
import Pachyderm
class UserActivityManager {
// MARK: - Utils
private static let encoder = PropertyListEncoder()
private static let decoder = PropertyListDecoder()
private static func present(_ vc: UIViewController, animated: Bool = true) {
UIApplication.shared.delegate?.window??.rootViewController?.present(vc, animated: animated)
}
private static func getMainTabBarController() -> MainTabBarViewController {
return (UIApplication.shared.delegate! as! AppDelegate).window!.rootViewController as! MainTabBarViewController
}
// MARK: - New Post
static func newPostActivity(mentioning: Account? = nil) -> NSUserActivity {
let activity = NSUserActivity(type: .newPost)
activity.isEligibleForPrediction = true
if let mentioning = mentioning {
activity.userInfo = ["mentioning": mentioning.acct]
activity.title = "Send a message to \(mentioning.realDisplayName)"
activity.suggestedInvocationPhrase = "Send a message to \(mentioning.realDisplayName)"
} else {
activity.userInfo = [:]
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
present(UINavigationController(rootViewController: ComposeViewController(mentioningAcct: mentioning)))
}
// MARK: - Check Notifications
static func checkNotificationsActivity() -> NSUserActivity {
let activity = NSUserActivity(type: .checkNotifications)
activity.isEligibleForPrediction = true
activity.title = "Check Notifications"
activity.suggestedInvocationPhrase = "Check my Tusker notifications"
return activity
}
static func handleCheckNotifications(activity: NSUserActivity) {
getMainTabBarController().select(tab: .notifications)
}
// MARK: - Show Timeline
static func showTimelineActivity(timeline: Timeline) -> NSUserActivity? {
guard let timelineData = try? encoder.encode(timeline) else { return nil }
let activity = NSUserActivity(type: .showTimeline)
activity.isEligibleForPrediction = true
activity.userInfo = ["timelineData": timelineData]
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
}
static func handleShowTimeline(activity: NSUserActivity) {
guard let timelineData = activity.userInfo?["timelineData"] as? Data,
let timeline = try? decoder.decode(Timeline.self, from: timelineData) else {
return
}
let tabBarController = getMainTabBarController()
tabBarController.select(tab: .timelines)
let navigationController = tabBarController.viewControllers![0] as! UINavigationController
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:
navigationController.pushViewController(TimelineTableViewController(for: timeline), animated: false)
}
}
}