Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit e2b2bc9

Browse files
authored
Huge refactor of AppDelegate and authentication routing (#2238)
* refactor with new app controller, replace root nav mgr * finish refactoring out root nav mgr * remove root mgr
1 parent a5ed67a commit e2b2bc9

File tree

17 files changed

+327
-367
lines changed

17 files changed

+327
-367
lines changed

Classes/Login/LoginSplashViewController.swift

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,23 @@ import GitHubSession
1414
private let loginURL = URL(string: "http://github.com/login/oauth/authorize?client_id=\(Secrets.GitHub.clientId)&scope=user+repo+notifications")!
1515
private let callbackURLScheme = "freetime://"
1616

17-
final class LoginSplashViewController: UIViewController, GitHubSessionListener {
17+
protocol LoginSplashViewControllerDelegate: class {
18+
func finishLogin(token: String, authMethod: GitHubUserSession.AuthMethod, username: String)
19+
}
20+
21+
final class LoginSplashViewController: UIViewController {
1822

1923
enum State {
2024
case idle
2125
case fetchingToken
2226
}
2327

2428
private var client: Client!
25-
private var sessionManager: GitHubSessionManager!
2629

2730
@IBOutlet weak var splashView: SplashView!
2831
@IBOutlet weak var signInButton: UIButton!
2932
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
30-
private weak var safariController: SFSafariViewController?
33+
private weak var delegate: LoginSplashViewControllerDelegate?
3134

3235
@available(iOS 11.0, *)
3336
private var authSession: SFAuthenticationSession? {
@@ -60,7 +63,6 @@ final class LoginSplashViewController: UIViewController, GitHubSessionListener {
6063
override func viewDidLoad() {
6164
super.viewDidLoad()
6265
state = .idle
63-
sessionManager.addListener(listener: self)
6466
signInButton.layer.cornerRadius = Styles.Sizes.cardCornerRadius
6567
}
6668

@@ -72,9 +74,15 @@ final class LoginSplashViewController: UIViewController, GitHubSessionListener {
7274

7375
// MARK: Public API
7476

75-
func config(client: Client, sessionManager: GitHubSessionManager) {
76-
self.client = client
77-
self.sessionManager = sessionManager
77+
static func make(client: Client, delegate: LoginSplashViewControllerDelegate) -> LoginSplashViewController? {
78+
let controller = UIStoryboard(
79+
name: "OauthLogin",
80+
bundle: Bundle(for: AppDelegate.self))
81+
.instantiateInitialViewController() as? LoginSplashViewController
82+
controller?.client = client
83+
controller?.delegate = delegate
84+
controller?.modalPresentationStyle = .formSheet
85+
return controller
7886
}
7987

8088
// MARK: Private API
@@ -88,7 +96,22 @@ final class LoginSplashViewController: UIViewController, GitHubSessionListener {
8896
}
8997
return
9098
}
91-
self?.sessionManager.receivedCodeRedirect(url: callbackUrl)
99+
100+
guard let items = URLComponents(url: callbackUrl, resolvingAgainstBaseURL: false)?.queryItems,
101+
let index = items.index(where: { $0.name == "code" }),
102+
let code = items[index].value
103+
else { return }
104+
105+
self?.state = .fetchingToken
106+
107+
self?.client.requestAccessToken(code: code) { [weak self] result in
108+
switch result {
109+
case .error:
110+
self?.handleError()
111+
case .success(let user):
112+
self?.delegate?.finishLogin(token: user.token, authMethod: .oauth, username: user.username)
113+
}
114+
}
92115
})
93116
self.authSession?.start()
94117
}
@@ -117,7 +140,7 @@ final class LoginSplashViewController: UIViewController, GitHubSessionListener {
117140
case .failure:
118141
self?.handleError()
119142
case .success(let user):
120-
self?.finishLogin(token: token, authMethod: .pat, username: user.data.login)
143+
self?.delegate?.finishLogin(token: token, authMethod: .pat, username: user.data.login)
121144
}
122145
}
123146
})
@@ -138,34 +161,8 @@ final class LoginSplashViewController: UIViewController, GitHubSessionListener {
138161
present(alert, animated: trueUnlessReduceMotionEnabled)
139162
}
140163

141-
private func finishLogin(token: String, authMethod: GitHubUserSession.AuthMethod, username: String) {
142-
sessionManager.focus(
143-
GitHubUserSession(token: token, authMethod: authMethod, username: username),
144-
dismiss: true
145-
)
146-
}
147-
148164
private func setupSplashView() {
149165
splashView.configureView()
150166
}
151167

152-
// MARK: GitHubSessionListener
153-
154-
func didReceiveRedirect(manager: GitHubSessionManager, code: String) {
155-
safariController?.dismiss(animated: trueUnlessReduceMotionEnabled)
156-
state = .fetchingToken
157-
158-
client.requestAccessToken(code: code) { [weak self] result in
159-
switch result {
160-
case .error:
161-
self?.handleError()
162-
case .success(let user):
163-
self?.finishLogin(token: user.token, authMethod: .oauth, username: user.username)
164-
}
165-
}
166-
}
167-
168-
func didFocus(manager: GitHubSessionManager, userSession: GitHubUserSession, dismiss: Bool) {}
169-
func didLogout(manager: GitHubSessionManager) {}
170-
171168
}

Classes/Settings/SettingsAccountsViewController.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ final class SettingsAccountsViewController: UITableViewController, GitHubSession
116116
tableView.reloadData()
117117
}
118118

119-
func didReceiveRedirect(manager: GitHubSessionManager, code: String) {}
120119
func didLogout(manager: GitHubSessionManager) {}
121120

122121
}

Classes/Settings/SettingsViewController.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ NewIssueTableViewControllerDelegate {
1717

1818
// must be injected
1919
var sessionManager: GitHubSessionManager!
20-
weak var rootNavigationManager: RootNavigationManager?
21-
2220
var client: GithubClient!
2321

2422
@IBOutlet weak var versionLabel: UILabel!
@@ -158,7 +156,7 @@ NewIssueTableViewControllerDelegate {
158156

159157
func onReportBug() {
160158
guard let viewController = NewIssueTableViewController.create(
161-
client: newGithubClient(userSession: sessionManager.focusedUserSession),
159+
client: GithubClient(userSession: sessionManager.focusedUserSession),
162160
owner: "GitHawkApp",
163161
repo: "GitHawk",
164162
signature: .bugReport

Classes/Systems/Alamofire+GithubAPI.swift

Lines changed: 0 additions & 28 deletions
This file was deleted.

Classes/Systems/AppDelegate.swift

Lines changed: 5 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,12 @@ import GitHubSession
1717
class AppDelegate: UIResponder, UIApplicationDelegate {
1818

1919
var window: UIWindow?
20-
private var showingLogin = false
2120
private let flexController = FlexController()
22-
private let sessionManager = GitHubSessionManager()
23-
private var watchAppSync: WatchAppUserSessionSync?
24-
25-
private lazy var rootNavigationManager: RootNavigationManager = {
26-
return RootNavigationManager(
27-
sessionManager: self.sessionManager,
28-
rootViewController: self.window?.rootViewController as! UISplitViewController
29-
)
30-
}()
21+
private let appController = AppController()
3122

3223
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
3324

34-
sessionManager.addListener(listener: self)
35-
36-
let focusedSession = sessionManager.focusedUserSession
37-
watchAppSync = WatchAppUserSessionSync(userSession: focusedSession)
38-
watchAppSync?.start()
39-
40-
// initialize a webview at the start so webview startup later on isn't so slow
41-
_ = UIWebView()
25+
appController.appDidFinishLaunching(with: window)
4226

4327
// setup fabric
4428
Fabric.with([Crashlytics.self])
@@ -49,10 +33,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
4933
// setup FLEX
5034
flexController.configureWindow(window)
5135

52-
// setup root VCs
53-
window?.backgroundColor = Styles.Colors.background
54-
rootNavigationManager.resetRootViewController(userSession: focusedSession)
55-
5636
// use Alamofire status bar network activity helper
5737
NetworkActivityIndicatorManager.shared.isEnabled = true
5838

@@ -73,42 +53,15 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
7353
completionHandler(false)
7454
return
7555
}
76-
completionHandler(ShortcutHandler.handle(
77-
route: route,
78-
sessionManager: sessionManager,
79-
navigationManager: rootNavigationManager))
56+
completionHandler(appController.handle(route: route))
8057
}
8158

8259
func applicationDidBecomeActive(_ application: UIApplication) {
83-
if showingLogin == false && sessionManager.focusedUserSession == nil {
84-
showingLogin = true
85-
rootNavigationManager.showLogin(animated: false)
86-
}
87-
}
88-
89-
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
90-
if let sourceApp = options[.sourceApplication],
91-
String(describing: sourceApp) == "com.apple.SafariViewService" {
92-
sessionManager.receivedCodeRedirect(url: url)
93-
return true
94-
}
95-
return false
60+
appController.appDidBecomeActive()
9661
}
9762

9863
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
99-
rootNavigationManager.client?.badge.fetch(application: application, handler: completionHandler)
100-
}
101-
102-
}
103-
104-
extension AppDelegate: GitHubSessionListener {
105-
106-
// configure 3d touch shortcut handling
107-
func didFocus(manager: GitHubSessionManager, userSession: GitHubUserSession, dismiss: Bool) {
108-
ShortcutHandler.configure(application: UIApplication.shared, sessionManager: sessionManager)
109-
watchAppSync?.sync(userSession: userSession)
64+
appController.performFetch(application: application, with: completionHandler)
11065
}
11166

112-
func didReceiveRedirect(manager: GitHubSessionManager, code: String) {}
113-
func didLogout(manager: GitHubSessionManager) {}
11467
}

0 commit comments

Comments
 (0)