diff --git a/Classes/Systems/AppDelegate.swift b/Classes/Systems/AppDelegate.swift index caad0db3..6a6a057b 100644 --- a/Classes/Systems/AppDelegate.swift +++ b/Classes/Systems/AppDelegate.swift @@ -35,9 +35,17 @@ class AppDelegate: UIResponder, UIApplicationDelegate { NetworkActivityIndicatorManager.shared.isEnabled = true Styles.setupAppearance() BadgeNotifications.configure(application: application) + ShortcutHandler.configure(application: application, sessionManager: sessionManager) + return true } + func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) { + completionHandler(ShortcutHandler.handle(shortcutItem: shortcutItem, + sessionManager: sessionManager, + navigationManager: rootNavigationManager)) + } + func applicationDidBecomeActive(_ application: UIApplication) { if showingLogin == false && sessionManager.focusedUserSession == nil { showingLogin = true diff --git a/Classes/Systems/RootNavigationManager.swift b/Classes/Systems/RootNavigationManager.swift index 34018a26..430fb88e 100644 --- a/Classes/Systems/RootNavigationManager.swift +++ b/Classes/Systems/RootNavigationManager.swift @@ -82,6 +82,12 @@ final class RootNavigationManager: GithubSessionListener { nav.pushViewController(login, animated: true) } + @discardableResult + public func selectViewController(atIndex index: Int) -> UIViewController? { + tabBarController?.selectedIndex = index + return tabBarController?.selectedViewController + } + // MARK: GithubSessionListener func didFocus(manager: GithubSessionManager, userSession: GithubUserSession, dismiss: Bool) { diff --git a/Classes/Systems/ShortcutHandler.swift b/Classes/Systems/ShortcutHandler.swift new file mode 100644 index 00000000..1e657010 --- /dev/null +++ b/Classes/Systems/ShortcutHandler.swift @@ -0,0 +1,72 @@ +// +// ShortcutHandler.swift +// Freetime +// +// Created by Viktor Gardart on 2017-10-08. +// Copyright © 2017 Ryan Nystrom. All rights reserved. +// + +import Foundation + +struct ShortcutHandler { + + private struct Constants { + static let searchViewControllerIndex = 1 + } + + private enum Items: String { + case search + case switchAccount + } + + static func configure(application: UIApplication, sessionManager: GithubSessionManager) { + application.shortcutItems = generateItems(sessionManager: sessionManager) + } + + static func handle(shortcutItem item: UIApplicationShortcutItem, + sessionManager: GithubSessionManager, + navigationManager: RootNavigationManager) -> Bool { + guard let itemType = Items(rawValue: item.type) else { return false } + switch itemType { + case .search: + navigationManager.selectViewController(atIndex: Constants.searchViewControllerIndex) + return true + case .switchAccount: + if let index = item.userInfo?["sessionIndex"] as? Int { + let session = sessionManager.userSessions[index] + sessionManager.focus(session, dismiss: false) + } + return true + } + } + + private static func generateItems(sessionManager: GithubSessionManager) -> [UIApplicationShortcutItem] { + var items: [UIApplicationShortcutItem] = [] + + // Search + let searchIcon = UIApplicationShortcutIcon(templateImageName: "search") + let searchItem = UIApplicationShortcutItem(type: Items.search.rawValue, + localizedTitle: NSLocalizedString("Search", comment: ""), + localizedSubtitle: nil, + icon: searchIcon, + userInfo: nil) + items.append(searchItem) + + // Switchuser + if sessionManager.userSessions.count >= 2 { + let userSession = sessionManager.userSessions[1] + if let username = userSession.username { + let userIcon = UIApplicationShortcutIcon(templateImageName: "organization") + let userItem = UIApplicationShortcutItem(type: Items.switchAccount.rawValue, + localizedTitle: NSLocalizedString("Switch Account", comment: ""), + localizedSubtitle: username, + icon: userIcon, + userInfo: ["sessionIndex": 1]) + items.append(userItem) + } + } + + return items + } + +} diff --git a/Freetime.xcodeproj/project.pbxproj b/Freetime.xcodeproj/project.pbxproj index ccec9103..6590a8bd 100644 --- a/Freetime.xcodeproj/project.pbxproj +++ b/Freetime.xcodeproj/project.pbxproj @@ -327,6 +327,7 @@ 29FB942E1EE751F70016E6D4 /* IssueLabeledSectionController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29FB942D1EE751F70016E6D4 /* IssueLabeledSectionController.swift */; }; 29FB94301EE752280016E6D4 /* IssueLabeledCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29FB942F1EE752280016E6D4 /* IssueLabeledCell.swift */; }; 29FF85A51EE1EA7A007B8762 /* ReactionContent+ReactionType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29FF85A41EE1EA7A007B8762 /* ReactionContent+ReactionType.swift */; }; + 3E79A2FF1F8A7DA700E1126B /* ShortcutHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E79A2FE1F8A7DA700E1126B /* ShortcutHandler.swift */; }; 4920F1A81F72E27200131E9D /* UIViewController+UserActivity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4920F1A71F72E27200131E9D /* UIViewController+UserActivity.swift */; }; 525C147297BCEEF388B5FDBC /* Pods_Freetime.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3DB749FB402B660C8A9F516C /* Pods_Freetime.framework */; }; 54AD5E8E1F24D953004A4BD6 /* FeedSelectionProviding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54AD5E8D1F24D953004A4BD6 /* FeedSelectionProviding.swift */; }; @@ -664,6 +665,7 @@ 29FF85A41EE1EA7A007B8762 /* ReactionContent+ReactionType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "ReactionContent+ReactionType.swift"; sourceTree = ""; }; 35E1CE59E98A2C8E7F451CA6 /* Pods-FreetimeTests.testflight.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-FreetimeTests.testflight.xcconfig"; path = "Pods/Target Support Files/Pods-FreetimeTests/Pods-FreetimeTests.testflight.xcconfig"; sourceTree = ""; }; 3DB749FB402B660C8A9F516C /* Pods_Freetime.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Freetime.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + 3E79A2FE1F8A7DA700E1126B /* ShortcutHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShortcutHandler.swift; sourceTree = ""; }; 4920F1A71F72E27200131E9D /* UIViewController+UserActivity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIViewController+UserActivity.swift"; sourceTree = ""; }; 54AD5E8D1F24D953004A4BD6 /* FeedSelectionProviding.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FeedSelectionProviding.swift; sourceTree = ""; }; 6079633B7825448DA6CAED21 /* Pods-FreetimeTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-FreetimeTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-FreetimeTests/Pods-FreetimeTests.debug.xcconfig"; sourceTree = ""; }; @@ -1242,6 +1244,7 @@ 2939718A1F904D2A002FAC4B /* Toast+GitHawk.swift */, 292CD3CF1F0DBB5C00D3D57B /* WebviewCellHeightCache.swift */, 2930F2721F8A27750082BA26 /* WidthCache.swift */, + 3E79A2FE1F8A7DA700E1126B /* ShortcutHandler.swift */, ); path = Systems; sourceTree = ""; @@ -2005,6 +2008,7 @@ 295C31CF1F0AA67600521CED /* IssueStatus+ButtonState.swift in Sources */, 290D2A421F04D3470082E6CC /* IssueStatus.swift in Sources */, 294563EA1EE4EEF000DBCD35 /* IssueStatusCell.swift in Sources */, + 3E79A2FF1F8A7DA700E1126B /* ShortcutHandler.swift in Sources */, 295C31D11F0AA72000521CED /* IssueStatusEvent+ButtonState.swift in Sources */, 295C31CD1F0AA55400521CED /* IssueStatusEvent.swift in Sources */, 295840671EE89FE4007723C6 /* IssueStatusEventCell.swift in Sources */,