Tusker/Tusker/Screens/Onboarding/OnboardingViewController.swift

93 lines
3.5 KiB
Swift
Raw Normal View History

2018-08-19 20:14:04 +00:00
//
// OnboardingViewController.swift
// Tusker
//
// Created by Shadowfacts on 8/18/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import UIKit
import AuthenticationServices
protocol OnboardingViewControllerDelegate {
func didFinishOnboarding(account: LocalData.UserAccountInfo)
2018-08-19 20:14:04 +00:00
}
2019-09-15 19:01:35 +00:00
class OnboardingViewController: UINavigationController {
2018-08-19 20:14:04 +00:00
2019-06-19 23:12:42 +00:00
static var blocks: [NSRegularExpression] = {
guard let path = Bundle.main.path(forResource: "DomainBlocks", ofType: "plist"),
let array = NSArray(contentsOfFile: path) as? [String] else { return [] }
return array.compactMap { try? NSRegularExpression(pattern: $0, options: .caseInsensitive) }
}()
2019-09-15 19:01:35 +00:00
var onboardingDelegate: OnboardingViewControllerDelegate?
2018-08-19 20:14:04 +00:00
2019-09-15 19:01:35 +00:00
var instanceSelector = InstanceSelectorTableViewController()
2018-08-19 20:14:04 +00:00
var authenticationSession: ASWebAuthenticationSession?
2018-10-20 16:03:18 +00:00
init() {
2019-09-15 19:01:35 +00:00
super.init(rootViewController: instanceSelector)
2018-10-20 16:03:18 +00:00
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
2018-08-19 20:14:04 +00:00
override func viewDidLoad() {
super.viewDidLoad()
2019-09-15 19:01:35 +00:00
instanceSelector.delegate = self
}
}
extension OnboardingViewController: InstanceSelectorTableViewControllerDelegate {
func didSelectInstance(url instanceURL: URL) {
let mastodonController = MastodonController(instanceURL: instanceURL)
mastodonController.registerApp { (clientID, clientSecret) in
2018-08-19 20:14:04 +00:00
let callbackURL = "tusker://oauth"
var components = URLComponents(url: instanceURL, resolvingAgainstBaseURL: false)!
2018-08-19 20:14:04 +00:00
components.path = "/oauth/authorize"
components.queryItems = [
URLQueryItem(name: "client_id", value: clientID),
URLQueryItem(name: "response_type", value: "code"),
URLQueryItem(name: "scope", value: "read write follow"),
URLQueryItem(name: "redirect_uri", value: callbackURL)
]
2019-09-15 19:01:35 +00:00
let authorizeURL = components.url!
2018-08-19 20:14:04 +00:00
2019-09-15 19:01:35 +00:00
self.authenticationSession = ASWebAuthenticationSession(url: authorizeURL, callbackURLScheme: callbackURL) { url, error in
2018-08-20 21:23:35 +00:00
guard error == nil,
let url = url,
let components = URLComponents(url: url, resolvingAgainstBaseURL: true),
let item = components.queryItems?.first(where: { $0.name == "code" }),
2019-09-24 18:45:29 +00:00
let authCode = item.value else { return }
2018-08-20 21:23:35 +00:00
mastodonController.authorize(authorizationCode: authCode) { (accessToken) in
mastodonController.getOwnAccount { (account) in
DispatchQueue.main.async {
2020-01-19 16:52:06 +00:00
let accountInfo = LocalData.shared.addAccount(instanceURL: instanceURL, clientID: clientID, clientSecret: clientSecret, username: account.username, accessToken: accessToken)
self.onboardingDelegate?.didFinishOnboarding(account: accountInfo)
}
2018-08-20 21:23:35 +00:00
}
}
2018-08-19 20:14:04 +00:00
}
DispatchQueue.main.async {
self.authenticationSession!.presentationContextProvider = self
self.authenticationSession!.start()
}
2018-08-19 20:14:04 +00:00
}
}
}
2019-06-04 17:31:12 +00:00
extension OnboardingViewController: ASWebAuthenticationPresentationContextProviding {
func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
2019-06-11 17:21:22 +00:00
return view.window!
2019-06-04 17:31:12 +00:00
}
}