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

Huge refactor of AppDelegate and authentication routing #2238

Merged
merged 3 commits into from
Oct 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 32 additions & 35 deletions Classes/Login/LoginSplashViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,23 @@ import GitHubSession
private let loginURL = URL(string: "http://github.com/login/oauth/authorize?client_id=\(Secrets.GitHub.clientId)&scope=user+repo+notifications")!
private let callbackURLScheme = "freetime://"

final class LoginSplashViewController: UIViewController, GitHubSessionListener {
protocol LoginSplashViewControllerDelegate: class {
func finishLogin(token: String, authMethod: GitHubUserSession.AuthMethod, username: String)
}

final class LoginSplashViewController: UIViewController {

enum State {
case idle
case fetchingToken
}

private var client: Client!
private var sessionManager: GitHubSessionManager!

@IBOutlet weak var splashView: SplashView!
@IBOutlet weak var signInButton: UIButton!
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
private weak var safariController: SFSafariViewController?
private weak var delegate: LoginSplashViewControllerDelegate?

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

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

// MARK: Public API

func config(client: Client, sessionManager: GitHubSessionManager) {
self.client = client
self.sessionManager = sessionManager
static func make(client: Client, delegate: LoginSplashViewControllerDelegate) -> LoginSplashViewController? {
let controller = UIStoryboard(
name: "OauthLogin",
bundle: Bundle(for: AppDelegate.self))
.instantiateInitialViewController() as? LoginSplashViewController
controller?.client = client
controller?.delegate = delegate
controller?.modalPresentationStyle = .formSheet
return controller
}

// MARK: Private API
Expand All @@ -88,7 +96,22 @@ final class LoginSplashViewController: UIViewController, GitHubSessionListener {
}
return
}
self?.sessionManager.receivedCodeRedirect(url: callbackUrl)

guard let items = URLComponents(url: callbackUrl, resolvingAgainstBaseURL: false)?.queryItems,
let index = items.index(where: { $0.name == "code" }),
let code = items[index].value
else { return }

self?.state = .fetchingToken

self?.client.requestAccessToken(code: code) { [weak self] result in
switch result {
case .error:
self?.handleError()
case .success(let user):
self?.delegate?.finishLogin(token: user.token, authMethod: .oauth, username: user.username)
}
}
})
self.authSession?.start()
}
Expand Down Expand Up @@ -117,7 +140,7 @@ final class LoginSplashViewController: UIViewController, GitHubSessionListener {
case .failure:
self?.handleError()
case .success(let user):
self?.finishLogin(token: token, authMethod: .pat, username: user.data.login)
self?.delegate?.finishLogin(token: token, authMethod: .pat, username: user.data.login)
}
}
})
Expand All @@ -138,34 +161,8 @@ final class LoginSplashViewController: UIViewController, GitHubSessionListener {
present(alert, animated: trueUnlessReduceMotionEnabled)
}

private func finishLogin(token: String, authMethod: GitHubUserSession.AuthMethod, username: String) {
sessionManager.focus(
GitHubUserSession(token: token, authMethod: authMethod, username: username),
dismiss: true
)
}

private func setupSplashView() {
splashView.configureView()
}

// MARK: GitHubSessionListener

func didReceiveRedirect(manager: GitHubSessionManager, code: String) {
safariController?.dismiss(animated: trueUnlessReduceMotionEnabled)
state = .fetchingToken

client.requestAccessToken(code: code) { [weak self] result in
switch result {
case .error:
self?.handleError()
case .success(let user):
self?.finishLogin(token: user.token, authMethod: .oauth, username: user.username)
}
}
}

func didFocus(manager: GitHubSessionManager, userSession: GitHubUserSession, dismiss: Bool) {}
func didLogout(manager: GitHubSessionManager) {}

}
1 change: 0 additions & 1 deletion Classes/Settings/SettingsAccountsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ final class SettingsAccountsViewController: UITableViewController, GitHubSession
tableView.reloadData()
}

func didReceiveRedirect(manager: GitHubSessionManager, code: String) {}
func didLogout(manager: GitHubSessionManager) {}

}
4 changes: 1 addition & 3 deletions Classes/Settings/SettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ NewIssueTableViewControllerDelegate {

// must be injected
var sessionManager: GitHubSessionManager!
weak var rootNavigationManager: RootNavigationManager?

var client: GithubClient!

@IBOutlet weak var versionLabel: UILabel!
Expand Down Expand Up @@ -158,7 +156,7 @@ NewIssueTableViewControllerDelegate {

func onReportBug() {
guard let viewController = NewIssueTableViewController.create(
client: newGithubClient(userSession: sessionManager.focusedUserSession),
client: GithubClient(userSession: sessionManager.focusedUserSession),
owner: "GitHawkApp",
repo: "GitHawk",
signature: .bugReport
Expand Down
28 changes: 0 additions & 28 deletions Classes/Systems/Alamofire+GithubAPI.swift

This file was deleted.

57 changes: 5 additions & 52 deletions Classes/Systems/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,12 @@ import GitHubSession
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
private var showingLogin = false
private let flexController = FlexController()
private let sessionManager = GitHubSessionManager()
private var watchAppSync: WatchAppUserSessionSync?

private lazy var rootNavigationManager: RootNavigationManager = {
return RootNavigationManager(
sessionManager: self.sessionManager,
rootViewController: self.window?.rootViewController as! UISplitViewController
)
}()
private let appController = AppController()

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

sessionManager.addListener(listener: self)

let focusedSession = sessionManager.focusedUserSession
watchAppSync = WatchAppUserSessionSync(userSession: focusedSession)
watchAppSync?.start()

// initialize a webview at the start so webview startup later on isn't so slow
_ = UIWebView()
appController.appDidFinishLaunching(with: window)

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

// setup root VCs
window?.backgroundColor = Styles.Colors.background
rootNavigationManager.resetRootViewController(userSession: focusedSession)

// use Alamofire status bar network activity helper
NetworkActivityIndicatorManager.shared.isEnabled = true

Expand All @@ -73,42 +53,15 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
completionHandler(false)
return
}
completionHandler(ShortcutHandler.handle(
route: route,
sessionManager: sessionManager,
navigationManager: rootNavigationManager))
completionHandler(appController.handle(route: route))
}

func applicationDidBecomeActive(_ application: UIApplication) {
if showingLogin == false && sessionManager.focusedUserSession == nil {
showingLogin = true
rootNavigationManager.showLogin(animated: false)
}
}

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
if let sourceApp = options[.sourceApplication],
String(describing: sourceApp) == "com.apple.SafariViewService" {
sessionManager.receivedCodeRedirect(url: url)
return true
}
return false
appController.appDidBecomeActive()
}

func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
rootNavigationManager.client?.badge.fetch(application: application, handler: completionHandler)
}

}

extension AppDelegate: GitHubSessionListener {

// configure 3d touch shortcut handling
func didFocus(manager: GitHubSessionManager, userSession: GitHubUserSession, dismiss: Bool) {
ShortcutHandler.configure(application: UIApplication.shared, sessionManager: sessionManager)
watchAppSync?.sync(userSession: userSession)
appController.performFetch(application: application, with: completionHandler)
}

func didReceiveRedirect(manager: GitHubSessionManager, code: String) {}
func didLogout(manager: GitHubSessionManager) {}
}
Loading