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 {
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
}
}

View File

@ -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):

View File

@ -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)

View File

@ -20,12 +20,23 @@ struct XCBRequest {
init(spec: XCBRequestSpec, components: URLComponents) {
self.path = spec.path
let queryItems = components.queryItems!
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) }
}
}