From 9f818328eeaa75cfef09fdddbcbfceae7a4507f6 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 15 Sep 2019 21:00:57 -0400 Subject: [PATCH] Cleanup user activity and XCB action handling code --- .../Main/MainTabBarViewController.swift | 38 +++++++++++++++++-- Tusker/Shortcuts/UserActivityManager.swift | 11 ++++-- Tusker/XCallbackURL/XCBActions.swift | 20 +++++++--- Tusker/XCallbackURL/XCBRequest.swift | 27 ++++++++----- 4 files changed, 72 insertions(+), 24 deletions(-) diff --git a/Tusker/Screens/Main/MainTabBarViewController.swift b/Tusker/Screens/Main/MainTabBarViewController.swift index abccffc9..7b181fb3 100644 --- a/Tusker/Screens/Main/MainTabBarViewController.swift +++ b/Tusker/Screens/Main/MainTabBarViewController.swift @@ -34,13 +34,43 @@ class MainTabBarViewController: UITabBarController, UITabBarControllerDelegate { func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool { if viewController is ComposeViewController { - let compose = ComposeViewController() - let navigationController = embedInNavigationController(compose) - navigationController.presentationController?.delegate = compose - present(navigationController, animated: true) + presentCompose() return false } return true } + func presentCompose() { + let compose = ComposeViewController() + let navigationController = embedInNavigationController(compose) + navigationController.presentationController?.delegate = compose + present(navigationController, animated: true) + } + + func select(tab: Tab) { + if tab == .compose { + presentCompose() + } else { + selectedIndex = tab.rawValue + } + } + + func getTabController(tab: Tab) -> UIViewController? { + if tab == .compose { + return nil + } else { + return viewControllers![tab.rawValue] + } + } + +} + +extension MainTabBarViewController { + enum Tab: Int { + case timelines + case notifications + case compose + case search + case myProfile + } } diff --git a/Tusker/Shortcuts/UserActivityManager.swift b/Tusker/Shortcuts/UserActivityManager.swift index d1949408..079d3e73 100644 --- a/Tusker/Shortcuts/UserActivityManager.swift +++ b/Tusker/Shortcuts/UserActivityManager.swift @@ -19,6 +19,10 @@ class UserActivityManager { 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) @@ -51,8 +55,7 @@ class UserActivityManager { } static func handleCheckNotifications(activity: NSUserActivity) { - let tabBarController = UIApplication.shared.keyWindow!.rootViewController! as! UITabBarController - tabBarController.selectedIndex = 1 + getMainTabBarController().select(tab: .notifications) } // MARK: - Show Timeline @@ -92,8 +95,8 @@ class UserActivityManager { return } - let tabBarController = UIApplication.shared.keyWindow!.rootViewController! as! UITabBarController - tabBarController.selectedIndex = 0 + let tabBarController = getMainTabBarController() + tabBarController.select(tab: .timelines) let navigationController = tabBarController.viewControllers![0] as! UINavigationController switch timeline { case .home, .public(true), .public(false): diff --git a/Tusker/XCallbackURL/XCBActions.swift b/Tusker/XCallbackURL/XCBActions.swift index 2430a3c7..4494f288 100644 --- a/Tusker/XCallbackURL/XCBActions.swift +++ b/Tusker/XCallbackURL/XCBActions.swift @@ -13,12 +13,20 @@ import SwiftSoup struct XCBActions { // MARK: - Utils + private static func getMainTabBarController() -> MainTabBarViewController { + return (UIApplication.shared.delegate as! AppDelegate).window!.rootViewController as! MainTabBarViewController + } + private static func show(_ vc: UIViewController) { - UIApplication.shared.delegate!.window!!.rootViewController!.show(vc, sender: nil) + let tabBarController = getMainTabBarController() + if tabBarController.presentedViewController != nil { + tabBarController.presentedViewController?.dismiss(animated: false) + } + tabBarController.selectedViewController!.show(vc, sender: nil) } private static func present(_ vc: UIViewController, animated: Bool = true) { - UIApplication.shared.delegate!.window!!.rootViewController!.present(vc, animated: animated) + getMainTabBarController().present(vc, animated: animated) } private static func getStatus(from request: XCBRequest, session: XCBSession, completion: @escaping (Status) -> Void) { @@ -238,8 +246,8 @@ struct XCBActions { // MARK: - Accounts static func showAccount(_ request: XCBRequest, _ session: XCBSession, _ silent: Bool?) { getAccount(from: request, session: session) { (account) in - let vc = ProfileTableViewController(accountID: account.id) DispatchQueue.main.async { + let vc = ProfileTableViewController(accountID: account.id) show(vc) } } @@ -320,10 +328,10 @@ struct XCBActions { static func search(_ request: XCBRequest, _ session: XCBSession, _ silent: Bool?) { let query = request.arguments["query"]! - if let tabBarController = UIApplication.shared.keyWindow?.rootViewController as? UITabBarController, - let navigationController = tabBarController.viewControllers?[3] as? UINavigationController, + let tabBarController = getMainTabBarController() + if let navigationController = tabBarController.getTabController(tab: .search) as? UINavigationController, let searchController = navigationController.viewControllers.first as? SearchTableViewController { - tabBarController.selectedIndex = 3 + tabBarController.select(tab: .search) navigationController.popToRootViewController(animated: false) searchController.searchController.searchBar.text = query searchController.performSearch(query: query) diff --git a/Tusker/XCallbackURL/XCBRequest.swift b/Tusker/XCallbackURL/XCBRequest.swift index 5b32f223..69d8418b 100644 --- a/Tusker/XCallbackURL/XCBRequest.swift +++ b/Tusker/XCallbackURL/XCBRequest.swift @@ -20,12 +20,23 @@ struct XCBRequest { init(spec: XCBRequestSpec, components: URLComponents) { self.path = spec.path - let queryItems = components.queryItems! - self.arguments = spec.arguments.reduce(into: [String: String](), { (result, el) in - if let value = queryItems.first(where: { $0.name == el.key })?.value { - result[el.key] = value - } - }) + if let queryItems = components.queryItems { + self.arguments = spec.arguments.reduce(into: [String: String](), { (result, el) in + if let value = queryItems.first(where: { $0.name == el.key })?.value { + result[el.key] = value + } + }) + source = queryItems.first(where: { $0.name == "x-source" }).flatMap { $0.value } + success = queryItems.first(where: { $0.name == "x-success" }).flatMap { $0.value }.flatMap { URL(string: $0) } + error = queryItems.first(where: { $0.name == "x-error" }).flatMap { $0.value }.flatMap { URL(string: $0) } + cancel = queryItems.first(where: { $0.name == "x-cancel" }).flatMap { $0.value }.flatMap { URL(string: $0) } + } else { + self.arguments = [:] + source = nil + success = nil + error = nil + cancel = nil + } if let arg = arguments["json"] { json = Bool(arg) ?? false } else { @@ -36,9 +47,5 @@ struct XCBRequest { } else { silent = false } - source = queryItems.first(where: { $0.name == "x-source" }).flatMap { $0.value } - success = queryItems.first(where: { $0.name == "x-success" }).flatMap { $0.value }.flatMap { URL(string: $0) } - error = queryItems.first(where: { $0.name == "x-error" }).flatMap { $0.value }.flatMap { URL(string: $0) } - cancel = queryItems.first(where: { $0.name == "x-cancel" }).flatMap { $0.value }.flatMap { URL(string: $0) } } }