mirror of
https://github.com/zhigang1992/GitHawk.git
synced 2026-06-03 19:43:10 +08:00
3D Touch shortcuts (#502)
* 🎉 Initial commit for 3dtouch items * 🔥 Added constants for searchVCIndex and use returns instead of breaks * 🔥 Update handle shourtcut to guard instead of optional enum
This commit is contained in:
committed by
Ryan Nystrom
parent
5560678262
commit
b159fd3ff1
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
72
Classes/Systems/ShortcutHandler.swift
Normal file
72
Classes/Systems/ShortcutHandler.swift
Normal file
@@ -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
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 = "<group>"; };
|
||||
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 = "<group>"; };
|
||||
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 = "<group>"; };
|
||||
4920F1A71F72E27200131E9D /* UIViewController+UserActivity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIViewController+UserActivity.swift"; sourceTree = "<group>"; };
|
||||
54AD5E8D1F24D953004A4BD6 /* FeedSelectionProviding.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FeedSelectionProviding.swift; sourceTree = "<group>"; };
|
||||
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 = "<group>"; };
|
||||
@@ -1242,6 +1244,7 @@
|
||||
2939718A1F904D2A002FAC4B /* Toast+GitHawk.swift */,
|
||||
292CD3CF1F0DBB5C00D3D57B /* WebviewCellHeightCache.swift */,
|
||||
2930F2721F8A27750082BA26 /* WidthCache.swift */,
|
||||
3E79A2FE1F8A7DA700E1126B /* ShortcutHandler.swift */,
|
||||
);
|
||||
path = Systems;
|
||||
sourceTree = "<group>";
|
||||
@@ -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 */,
|
||||
|
||||
Reference in New Issue
Block a user