// // 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 func presentModally(_ vc: UIViewController, animated: Bool, completion: (() -> Void)? = nil) { UIApplication.shared.keyWindow!.rootViewController!.present(vc, animated: animated, completion: completion) } private static func presentNav(_ vc: UIViewController, animated: Bool) { let tabBarController = UIApplication.shared.keyWindow!.rootViewController! as! UITabBarController let navController = tabBarController.selectedViewController as! UINavigationController navController.pushViewController(vc, animated: animated) } // 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 presentModally(UINavigationController(rootViewController: ComposeViewController(mentioningAcct: mentioning)), animated: true) } // 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) { let index = Preferences.shared.tabs[.notifications] ?? -1 guard index > 0 else { return } let tabBarController = UIApplication.shared.keyWindow!.rootViewController! as! UITabBarController tabBarController.selectedIndex = index } }