Refactor ExtensionPoints to make them easier to create in the future.

This commit is contained in:
Maurice Parker
2020-04-14 16:47:05 -05:00
parent f5aac9516f
commit e206909237
16 changed files with 381 additions and 246 deletions

View File

@@ -7,21 +7,14 @@
//
import AppKit
import AuthenticationServices
import Secrets
import OAuthSwift
import FeedProvider
class ExtensionPointAddViewController: NSViewController {
@IBOutlet weak var tableView: NSTableView!
private var availableExtensionPointTypes = [ExtensionPointType]()
private var availableExtensionPointTypes = [ExtensionPoint.Type]()
private var extensionPointAddWindowController: NSWindowController?
private let callbackURL = URL(string: "vincodennw://")!
private var oauth: OAuthSwift?
init() {
super.init(nibName: "ExtensionPointAdd", bundle: nil)
}
@@ -68,90 +61,19 @@ extension ExtensionPointAddViewController: NSTableViewDelegate {
}
func tableViewSelectionDidChange(_ notification: Notification) {
let selectedRow = tableView.selectedRow
guard selectedRow != -1 else {
return
}
let extensionPointType = availableExtensionPointTypes[selectedRow]
switch extensionPointType {
case .marsEdit, .microblog:
let windowController = ExtensionPointEnableBasicWindowController()
windowController.extensionPointType = extensionPointType
windowController.runSheetOnWindow(self.view.window!)
extensionPointAddWindowController = windowController
case .twitter:
let oauth = OAuth1Swift(
consumerKey: Secrets.twitterConsumerKey,
consumerSecret: Secrets.twitterConsumerSecret,
requestTokenUrl: "https://api.twitter.com/oauth/request_token",
authorizeUrl: "https://api.twitter.com/oauth/authorize",
accessTokenUrl: "https://api.twitter.com/oauth/access_token"
)
self.oauth = oauth
oauth.authorizeURLHandler = self
oauth.authorize(withCallbackURL: callbackURL) { result in
switch result {
case .success(let tokenSuccess):
// let token = tokenSuccess.credential.oauthToken
// let secret = tokenSuccess.credential.oauthTokenSecret
let screenName = tokenSuccess.parameters["screen_name"] as? String ?? ""
print("******************* \(screenName)")
case .failure(let oauthSwiftError):
NSApplication.shared.presentError(oauthSwiftError)
}
self.oauth?.cancel()
self.oauth = nil
}
}
let windowController = ExtensionPointEnableWindowController()
windowController.extensionPointType = extensionPointType
windowController.runSheetOnWindow(self.view.window!)
extensionPointAddWindowController = windowController
tableView.selectRowIndexes([], byExtendingSelection: false)
}
}
extension ExtensionPointAddViewController: OAuthSwiftURLHandlerType {
public func handle(_ url: URL) {
let session = ASWebAuthenticationSession(url: url, callbackURLScheme: callbackURL.scheme, completionHandler: { (url, error) in
if let callbackedURL = url {
OAuth1Swift.handle(url: callbackedURL)
}
guard let error = error else { return }
self.oauth?.cancel()
self.oauth = nil
if case ASWebAuthenticationSessionError.canceledLogin = error {
print("Login cancelled.")
} else {
NSApplication.shared.presentError(error)
}
})
session.presentationContextProvider = self
if !session.start() {
print("Session failed to start!!!")
}
}
}
extension ExtensionPointAddViewController: ASWebAuthenticationPresentationContextProviding {
public func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
return view.window!
}
}