2019-09-28 06:44:58 +02:00
|
|
|
//
|
|
|
|
// AccountsFeedWranglerWindowController.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Jonathan Bennett on 2019-08-29.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import AppKit
|
|
|
|
import Account
|
|
|
|
import RSWeb
|
2020-04-10 04:07:56 +02:00
|
|
|
import Secrets
|
2019-09-28 06:44:58 +02:00
|
|
|
|
|
|
|
class AccountsFeedWranglerWindowController: NSWindowController {
|
2020-11-06 12:07:28 +01:00
|
|
|
|
|
|
|
@IBOutlet weak var signInTextField: NSTextField!
|
|
|
|
@IBOutlet weak var noAccountTextField: NSTextField!
|
|
|
|
@IBOutlet weak var createNewAccountButton: NSButton!
|
2019-09-28 06:44:58 +02:00
|
|
|
@IBOutlet weak var progressIndicator: NSProgressIndicator!
|
|
|
|
@IBOutlet weak var usernameTextField: NSTextField!
|
|
|
|
@IBOutlet weak var passwordTextField: NSSecureTextField!
|
|
|
|
@IBOutlet weak var errorMessageLabel: NSTextField!
|
|
|
|
@IBOutlet weak var actionButton: NSButton!
|
|
|
|
|
|
|
|
var account: Account?
|
|
|
|
|
|
|
|
private weak var hostWindow: NSWindow?
|
|
|
|
|
|
|
|
convenience init() {
|
|
|
|
self.init(windowNibName: NSNib.Name("AccountsFeedWrangler"))
|
|
|
|
}
|
|
|
|
|
|
|
|
override func windowDidLoad() {
|
|
|
|
if let account = account, let credentials = try? account.retrieveCredentials(type: .basic) {
|
|
|
|
usernameTextField.stringValue = credentials.username
|
|
|
|
actionButton.title = NSLocalizedString("Update", comment: "Update")
|
2020-11-06 12:07:28 +01:00
|
|
|
signInTextField.stringValue = NSLocalizedString("Update your Feed Wrangler account credentials.", comment: "SignIn")
|
|
|
|
noAccountTextField.isHidden = true
|
|
|
|
createNewAccountButton.isHidden = true
|
2019-09-28 06:44:58 +02:00
|
|
|
} else {
|
|
|
|
actionButton.title = NSLocalizedString("Create", comment: "Create")
|
2020-11-06 12:07:28 +01:00
|
|
|
signInTextField.stringValue = NSLocalizedString("Sign in to your Feed Wrangler account.", comment: "SignIn")
|
2019-09-28 06:44:58 +02:00
|
|
|
}
|
2020-11-07 02:42:20 +01:00
|
|
|
|
|
|
|
usernameTextField.becomeFirstResponder()
|
2019-09-28 06:44:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: API
|
|
|
|
|
2019-12-15 01:14:55 +01:00
|
|
|
func runSheetOnWindow(_ hostWindow: NSWindow, completion: ((NSApplication.ModalResponse) -> Void)? = nil) {
|
2019-09-28 06:44:58 +02:00
|
|
|
self.hostWindow = hostWindow
|
2019-12-15 01:14:55 +01:00
|
|
|
hostWindow.beginSheet(window!, completionHandler: completion)
|
2019-09-28 06:44:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Actions
|
|
|
|
|
|
|
|
@IBAction func cancel(_ sender: Any) {
|
|
|
|
hostWindow!.endSheet(window!, returnCode: NSApplication.ModalResponse.cancel)
|
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func action(_ sender: Any) {
|
|
|
|
self.errorMessageLabel.stringValue = ""
|
|
|
|
|
|
|
|
guard !usernameTextField.stringValue.isEmpty && !passwordTextField.stringValue.isEmpty else {
|
|
|
|
self.errorMessageLabel.stringValue = NSLocalizedString("Username & password required.", comment: "Credentials Error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-29 20:05:55 +01:00
|
|
|
guard account != nil || !AccountManager.shared.duplicateServiceAccount(type: .feedWrangler, username: usernameTextField.stringValue) else {
|
2020-09-25 03:29:56 +02:00
|
|
|
self.errorMessageLabel.stringValue = NSLocalizedString("There is already a FeedWrangler account with that username created.", comment: "Duplicate Error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-28 06:44:58 +02:00
|
|
|
actionButton.isEnabled = false
|
|
|
|
progressIndicator.isHidden = false
|
|
|
|
progressIndicator.startAnimation(self)
|
|
|
|
|
|
|
|
let credentials = Credentials(type: .feedWranglerBasic, username: usernameTextField.stringValue, secret: passwordTextField.stringValue)
|
|
|
|
Account.validateCredentials(type: .feedWrangler, credentials: credentials) { [weak self] result in
|
|
|
|
|
|
|
|
guard let self = self else { return }
|
|
|
|
|
|
|
|
self.actionButton.isEnabled = true
|
|
|
|
self.progressIndicator.isHidden = true
|
|
|
|
self.progressIndicator.stopAnimation(self)
|
|
|
|
|
|
|
|
switch result {
|
|
|
|
case .success(let validatedCredentials):
|
|
|
|
guard let validatedCredentials = validatedCredentials else {
|
|
|
|
self.errorMessageLabel.stringValue = NSLocalizedString("Invalid email/password combination.", comment: "Credentials Error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if self.account == nil {
|
|
|
|
self.account = AccountManager.shared.createAccount(type: .feedWrangler)
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
try self.account?.removeCredentials(type: .feedWranglerBasic)
|
|
|
|
try self.account?.removeCredentials(type: .feedWranglerToken)
|
|
|
|
try self.account?.storeCredentials(credentials)
|
|
|
|
try self.account?.storeCredentials(validatedCredentials)
|
2021-01-09 22:15:14 +01:00
|
|
|
|
|
|
|
self.account?.refreshAll() { result in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
break
|
|
|
|
case .failure(let error):
|
|
|
|
NSApplication.shared.presentError(error)
|
2019-09-28 06:44:58 +02:00
|
|
|
}
|
|
|
|
}
|
2021-01-09 22:15:14 +01:00
|
|
|
|
2019-09-28 06:44:58 +02:00
|
|
|
self.hostWindow?.endSheet(self.window!, returnCode: NSApplication.ModalResponse.OK)
|
|
|
|
} catch {
|
|
|
|
self.errorMessageLabel.stringValue = NSLocalizedString("Keychain error while storing credentials.", comment: "Credentials Error")
|
|
|
|
}
|
|
|
|
|
|
|
|
case .failure:
|
|
|
|
|
2019-12-08 03:10:32 +01:00
|
|
|
self.errorMessageLabel.stringValue = NSLocalizedString("Network error. Try again later.", comment: "Credentials Error")
|
2019-09-28 06:44:58 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-06 12:07:28 +01:00
|
|
|
|
|
|
|
@IBAction func createAccountWithProvider(_ sender: Any) {
|
|
|
|
NSWorkspace.shared.open(URL(string: "https://feedwrangler.net/users/new")!)
|
|
|
|
}
|
|
|
|
|
2019-09-28 06:44:58 +02:00
|
|
|
}
|