Tusker/Tusker/Screens/Onboarding/OnboardingViewController.swift

91 lines
3.2 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()
}
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: URL) {
2018-09-11 14:52:21 +00:00
LocalData.shared.instanceURL = url
2018-10-02 23:31:00 +00:00
MastodonController.createClient()
MastodonController.registerApp {
2018-08-19 20:14:04 +00:00
let clientID = LocalData.shared.clientID!
let callbackURL = "tusker://oauth"
2019-09-15 19:01:35 +00:00
var components = URLComponents(url: url, 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
2018-10-02 23:31:00 +00:00
MastodonController.authorize(authorizationCode: authCode) {
2018-08-20 21:23:35 +00:00
DispatchQueue.main.async {
2019-09-15 19:01:35 +00:00
self.onboardingDelegate?.didFinishOnboarding()
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
}
}