Cleanup user activity and XCB action handling code

This commit is contained in:
Shadowfacts 2019-09-15 21:00:57 -04:00
parent 32d6756762
commit 9f818328ee
4 changed files with 72 additions and 24 deletions

View File

@ -34,13 +34,43 @@ class MainTabBarViewController: UITabBarController, UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool { func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if viewController is ComposeViewController { if viewController is ComposeViewController {
let compose = ComposeViewController() presentCompose()
let navigationController = embedInNavigationController(compose)
navigationController.presentationController?.delegate = compose
present(navigationController, animated: true)
return false return false
} }
return true 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
}
} }

View File

@ -19,6 +19,10 @@ class UserActivityManager {
UIApplication.shared.delegate?.window??.rootViewController?.present(vc, animated: animated) 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 // 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)
@ -51,8 +55,7 @@ class UserActivityManager {
} }
static func handleCheckNotifications(activity: NSUserActivity) { static func handleCheckNotifications(activity: NSUserActivity) {
let tabBarController = UIApplication.shared.keyWindow!.rootViewController! as! UITabBarController getMainTabBarController().select(tab: .notifications)
tabBarController.selectedIndex = 1
} }
// MARK: - Show Timeline // MARK: - Show Timeline
@ -92,8 +95,8 @@ class UserActivityManager {
return return
} }
let tabBarController = UIApplication.shared.keyWindow!.rootViewController! as! UITabBarController let tabBarController = getMainTabBarController()
tabBarController.selectedIndex = 0 tabBarController.select(tab: .timelines)
let navigationController = tabBarController.viewControllers![0] as! UINavigationController let navigationController = tabBarController.viewControllers![0] as! UINavigationController
switch timeline { switch timeline {
case .home, .public(true), .public(false): case .home, .public(true), .public(false):

View File

@ -13,12 +13,20 @@ import SwiftSoup
struct XCBActions { struct XCBActions {
// MARK: - Utils // MARK: - Utils
private static func getMainTabBarController() -> MainTabBarViewController {
return (UIApplication.shared.delegate as! AppDelegate).window!.rootViewController as! MainTabBarViewController
}
private static func show(_ vc: UIViewController) { 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) { 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) { private static func getStatus(from request: XCBRequest, session: XCBSession, completion: @escaping (Status) -> Void) {
@ -238,8 +246,8 @@ struct XCBActions {
// MARK: - Accounts // MARK: - Accounts
static func showAccount(_ request: XCBRequest, _ session: XCBSession, _ silent: Bool?) { static func showAccount(_ request: XCBRequest, _ session: XCBSession, _ silent: Bool?) {
getAccount(from: request, session: session) { (account) in getAccount(from: request, session: session) { (account) in
let vc = ProfileTableViewController(accountID: account.id)
DispatchQueue.main.async { DispatchQueue.main.async {
let vc = ProfileTableViewController(accountID: account.id)
show(vc) show(vc)
} }
} }
@ -320,10 +328,10 @@ struct XCBActions {
static func search(_ request: XCBRequest, _ session: XCBSession, _ silent: Bool?) { static func search(_ request: XCBRequest, _ session: XCBSession, _ silent: Bool?) {
let query = request.arguments["query"]! let query = request.arguments["query"]!
if let tabBarController = UIApplication.shared.keyWindow?.rootViewController as? UITabBarController, let tabBarController = getMainTabBarController()
let navigationController = tabBarController.viewControllers?[3] as? UINavigationController, if let navigationController = tabBarController.getTabController(tab: .search) as? UINavigationController,
let searchController = navigationController.viewControllers.first as? SearchTableViewController { let searchController = navigationController.viewControllers.first as? SearchTableViewController {
tabBarController.selectedIndex = 3 tabBarController.select(tab: .search)
navigationController.popToRootViewController(animated: false) navigationController.popToRootViewController(animated: false)
searchController.searchController.searchBar.text = query searchController.searchController.searchBar.text = query
searchController.performSearch(query: query) searchController.performSearch(query: query)

View File

@ -20,12 +20,23 @@ struct XCBRequest {
init(spec: XCBRequestSpec, components: URLComponents) { init(spec: XCBRequestSpec, components: URLComponents) {
self.path = spec.path self.path = spec.path
let queryItems = components.queryItems! if let queryItems = components.queryItems {
self.arguments = spec.arguments.reduce(into: [String: String](), { (result, el) in self.arguments = spec.arguments.reduce(into: [String: String](), { (result, el) in
if let value = queryItems.first(where: { $0.name == el.key })?.value { if let value = queryItems.first(where: { $0.name == el.key })?.value {
result[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"] { if let arg = arguments["json"] {
json = Bool(arg) ?? false json = Bool(arg) ?? false
} else { } else {
@ -36,9 +47,5 @@ struct XCBRequest {
} else { } else {
silent = false 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) }
} }
} }