Add NSUserActivity for checking mentions

This commit is contained in:
Shadowfacts 2019-09-15 21:15:32 -04:00
parent 9f818328ee
commit d08789bfab
6 changed files with 53 additions and 21 deletions

View File

@ -6,6 +6,7 @@
<array> <array>
<string>$(PRODUCT_BUNDLE_IDENTIFIER).activity.show-timeline</string> <string>$(PRODUCT_BUNDLE_IDENTIFIER).activity.show-timeline</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER).activity.check-notifications</string> <string>$(PRODUCT_BUNDLE_IDENTIFIER).activity.check-notifications</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER).activity.check-mentions</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER).activity.new-post</string> <string>$(PRODUCT_BUNDLE_IDENTIFIER).activity.new-post</string>
</array> </array>
<key>CFBundleDevelopmentRegion</key> <key>CFBundleDevelopmentRegion</key>

View File

@ -46,6 +46,16 @@ class MainTabBarViewController: UITabBarController, UITabBarControllerDelegate {
navigationController.presentationController?.delegate = compose navigationController.presentationController?.delegate = compose
present(navigationController, animated: true) present(navigationController, animated: true)
} }
}
extension MainTabBarViewController {
enum Tab: Int {
case timelines
case notifications
case compose
case search
case myProfile
}
func select(tab: Tab) { func select(tab: Tab) {
if tab == .compose { if tab == .compose {
@ -62,15 +72,4 @@ class MainTabBarViewController: UITabBarController, UITabBarControllerDelegate {
return viewControllers![tab.rawValue] return viewControllers![tab.rawValue]
} }
} }
}
extension MainTabBarViewController {
enum Tab: Int {
case timelines
case notifications
case compose
case search
case myProfile
}
} }

View File

@ -17,9 +17,11 @@ class NotificationsPageViewController: SegmentedPageViewController {
init() { init() {
let notifications = NotificationsTableViewController(allowedTypes: Pachyderm.Notification.Kind.allCases) let notifications = NotificationsTableViewController(allowedTypes: Pachyderm.Notification.Kind.allCases)
notifications.title = notificationsTitle notifications.title = notificationsTitle
notifications.userActivity = UserActivityManager.checkNotificationsActivity()
let mentions = NotificationsTableViewController(allowedTypes: [.mention]) let mentions = NotificationsTableViewController(allowedTypes: [.mention])
mentions.title = mentionsTitle mentions.title = mentionsTitle
mentions.userActivity = UserActivityManager.checkMentionsActivity()
super.init(titles: [ super.init(titles: [
notificationsTitle, notificationsTitle,
@ -40,8 +42,12 @@ class NotificationsPageViewController: SegmentedPageViewController {
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
selectMode(Preferences.shared.defaultNotificationsMode)
}
func selectMode(_ mode: NotificationsMode) {
let index: Int let index: Int
switch Preferences.shared.defaultNotificationsMode { switch mode {
case .allNotifications: case .allNotifications:
index = 0 index = 0
case .mentionsOnly: case .mentionsOnly:

View File

@ -69,8 +69,6 @@ class NotificationsTableViewController: EnhancedTableViewController {
self.newer = pagination?.newer self.newer = pagination?.newer
self.older = pagination?.older self.older = pagination?.older
} }
userActivity = UserActivityManager.checkNotificationsActivity()
} }
// MARK: - Table view data source // MARK: - Table view data source

View File

@ -15,14 +15,14 @@ class UserActivityManager {
private static let encoder = PropertyListEncoder() private static let encoder = PropertyListEncoder()
private static let decoder = PropertyListDecoder() 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 { private static func getMainTabBarController() -> MainTabBarViewController {
return (UIApplication.shared.delegate! as! AppDelegate).window!.rootViewController as! MainTabBarViewController return (UIApplication.shared.delegate! as! AppDelegate).window!.rootViewController as! MainTabBarViewController
} }
private static func present(_ vc: UIViewController, animated: Bool = true) {
getMainTabBarController().present(vc, animated: animated)
}
// MARK: - New Post // MARK: - New Post
static func newPostActivity(mentioning: Account? = nil) -> NSUserActivity { static func newPostActivity(mentioning: Account? = nil) -> NSUserActivity {
let activity = NSUserActivity(type: .newPost) let activity = NSUserActivity(type: .newPost)
@ -49,13 +49,38 @@ class UserActivityManager {
static func checkNotificationsActivity() -> NSUserActivity { static func checkNotificationsActivity() -> NSUserActivity {
let activity = NSUserActivity(type: .checkNotifications) let activity = NSUserActivity(type: .checkNotifications)
activity.isEligibleForPrediction = true activity.isEligibleForPrediction = true
activity.title = "Check Notifications" activity.title = NSLocalizedString("Check Notifications", comment: "check notifications shortcut title")
activity.suggestedInvocationPhrase = "Check my Tusker notifications" activity.suggestedInvocationPhrase = NSLocalizedString("Check my Tusker notifications", comment: "check notifications shortcut invocation phrase")
return activity return activity
} }
static func handleCheckNotifications(activity: NSUserActivity) { static func handleCheckNotifications(activity: NSUserActivity) {
getMainTabBarController().select(tab: .notifications) let tabBarController = getMainTabBarController()
tabBarController.select(tab: .notifications)
if let navController = tabBarController.getTabController(tab: .notifications) as? UINavigationController,
let notificationsPageController = navController.viewControllers.first as? NotificationsPageViewController {
navController.popToRootViewController(animated: false)
notificationsPageController.selectMode(.allNotifications)
}
}
// MARK: - Check Mentions
static func checkMentionsActivity() -> NSUserActivity {
let activity = NSUserActivity(type: .checkMentions)
activity.isEligibleForPrediction = true
activity.title = NSLocalizedString("Check Mentions", comment: "check mentions shortcut title")
activity.suggestedInvocationPhrase = NSLocalizedString("Check my mentions", comment: "check mentions shortcut invocation phrase")
return activity
}
static func handleCheckMentions(activity: NSUserActivity) {
let tabBarController = getMainTabBarController()
tabBarController.select(tab: .notifications)
if let navController = tabBarController.getTabController(tab: .notifications) as? UINavigationController,
let notificationsPageController = navController.viewControllers.first as? NotificationsPageViewController {
navController.popToRootViewController(animated: false)
notificationsPageController.selectMode(.mentionsOnly)
}
} }
// MARK: - Show Timeline // MARK: - Show Timeline

View File

@ -11,6 +11,7 @@ import Foundation
enum UserActivityType: String { enum UserActivityType: String {
case newPost = "net.shadowfacts.Tusker.activity.new-post" case newPost = "net.shadowfacts.Tusker.activity.new-post"
case checkNotifications = "net.shadowfacts.Tusker.activity.check-notifications" case checkNotifications = "net.shadowfacts.Tusker.activity.check-notifications"
case checkMentions = "net.shadowfacts.Tusker.activity.check-mentions"
case showTimeline = "net.shadowfacts.Tusker.activity.show-timeline" case showTimeline = "net.shadowfacts.Tusker.activity.show-timeline"
} }
@ -21,6 +22,8 @@ extension UserActivityType {
return UserActivityManager.handleNewPost return UserActivityManager.handleNewPost
case .checkNotifications: case .checkNotifications:
return UserActivityManager.handleCheckNotifications return UserActivityManager.handleCheckNotifications
case .checkMentions:
return UserActivityManager.handleCheckMentions
case .showTimeline: case .showTimeline:
return UserActivityManager.handleShowTimeline return UserActivityManager.handleShowTimeline
} }