2019-05-03 01:17:52 +02:00
|
|
|
//
|
|
|
|
// AccountsAddFeedbinWindowController.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 5/2/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import AppKit
|
|
|
|
import Account
|
2019-05-04 18:48:48 +02:00
|
|
|
import RSWeb
|
2019-05-03 01:17:52 +02:00
|
|
|
|
2019-05-04 23:10:58 +02:00
|
|
|
class AccountsFeedbinWindowController: NSWindowController {
|
2019-05-03 01:17:52 +02:00
|
|
|
|
|
|
|
@IBOutlet weak var progressIndicator: NSProgressIndicator!
|
|
|
|
@IBOutlet weak var usernameTextField: NSTextField!
|
|
|
|
@IBOutlet weak var passwordTextField: NSSecureTextField!
|
|
|
|
@IBOutlet weak var errorMessageLabel: NSTextField!
|
2019-05-04 23:10:58 +02:00
|
|
|
@IBOutlet weak var actionButton: NSButton!
|
|
|
|
|
|
|
|
var account: Account?
|
2019-05-03 01:17:52 +02:00
|
|
|
|
|
|
|
private weak var hostWindow: NSWindow?
|
|
|
|
|
|
|
|
convenience init() {
|
2019-05-04 23:10:58 +02:00
|
|
|
self.init(windowNibName: NSNib.Name("AccountsFeedbin"))
|
|
|
|
}
|
|
|
|
|
|
|
|
override func windowDidLoad() {
|
2019-09-15 17:03:47 +02:00
|
|
|
if let account = account, let credentials = try? account.retrieveCredentials(type: .basic) {
|
|
|
|
usernameTextField.stringValue = credentials.username
|
2019-05-04 23:10:58 +02:00
|
|
|
actionButton.title = NSLocalizedString("Update", comment: "Update")
|
|
|
|
} else {
|
|
|
|
actionButton.title = NSLocalizedString("Create", comment: "Create")
|
|
|
|
}
|
2019-05-03 01:17:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: API
|
|
|
|
|
|
|
|
func runSheetOnWindow(_ hostWindow: NSWindow, completionHandler handler: ((NSApplication.ModalResponse) -> Void)? = nil) {
|
|
|
|
self.hostWindow = hostWindow
|
|
|
|
hostWindow.beginSheet(window!, completionHandler: handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Actions
|
|
|
|
|
|
|
|
@IBAction func cancel(_ sender: Any) {
|
|
|
|
hostWindow!.endSheet(window!, returnCode: NSApplication.ModalResponse.cancel)
|
|
|
|
}
|
|
|
|
|
2019-05-04 23:10:58 +02:00
|
|
|
@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
|
|
|
|
}
|
2019-05-03 01:17:52 +02:00
|
|
|
|
2019-05-04 23:10:58 +02:00
|
|
|
actionButton.isEnabled = false
|
2019-05-03 01:17:52 +02:00
|
|
|
progressIndicator.isHidden = false
|
|
|
|
progressIndicator.startAnimation(self)
|
|
|
|
|
2019-09-15 17:03:47 +02:00
|
|
|
let credentials = Credentials(type: .basic, username: usernameTextField.stringValue, secret: passwordTextField.stringValue)
|
2019-05-04 18:48:48 +02:00
|
|
|
Account.validateCredentials(type: .feedbin, credentials: credentials) { [weak self] result in
|
2019-05-03 01:17:52 +02:00
|
|
|
|
|
|
|
guard let self = self else { return }
|
|
|
|
|
2019-05-04 23:10:58 +02:00
|
|
|
self.actionButton.isEnabled = true
|
2019-05-03 01:17:52 +02:00
|
|
|
self.progressIndicator.isHidden = true
|
|
|
|
self.progressIndicator.stopAnimation(self)
|
|
|
|
|
2019-05-04 15:54:07 +02:00
|
|
|
switch result {
|
2019-05-29 16:54:52 +02:00
|
|
|
case .success(let validatedCredentials):
|
|
|
|
|
|
|
|
guard let validatedCredentials = validatedCredentials else {
|
|
|
|
self.errorMessageLabel.stringValue = NSLocalizedString("Invalid email/password combination.", comment: "Credentials Error")
|
|
|
|
return
|
|
|
|
}
|
2019-09-15 17:03:47 +02:00
|
|
|
|
2019-05-29 16:54:52 +02:00
|
|
|
var newAccount = false
|
|
|
|
if self.account == nil {
|
|
|
|
self.account = AccountManager.shared.createAccount(type: .feedbin)
|
|
|
|
newAccount = true
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2019-09-15 17:03:47 +02:00
|
|
|
try self.account?.removeCredentials(type: .basic)
|
2019-05-29 16:54:52 +02:00
|
|
|
try self.account?.storeCredentials(validatedCredentials)
|
|
|
|
if newAccount {
|
|
|
|
self.account?.refreshAll() { result in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
break
|
|
|
|
case .failure(let error):
|
|
|
|
NSApplication.shared.presentError(error)
|
2019-05-26 18:54:32 +02:00
|
|
|
}
|
2019-05-05 22:41:20 +02:00
|
|
|
}
|
2019-05-04 22:14:49 +02:00
|
|
|
}
|
2019-05-29 16:54:52 +02:00
|
|
|
self.hostWindow?.endSheet(self.window!, returnCode: NSApplication.ModalResponse.OK)
|
|
|
|
} catch {
|
|
|
|
self.errorMessageLabel.stringValue = NSLocalizedString("Keychain error while storing credentials.", comment: "Credentials Error")
|
2019-05-04 15:54:07 +02:00
|
|
|
}
|
2019-05-29 16:54:52 +02:00
|
|
|
|
2019-05-04 15:54:07 +02:00
|
|
|
case .failure:
|
2019-05-03 01:17:52 +02:00
|
|
|
|
2019-05-04 16:18:14 +02:00
|
|
|
self.errorMessageLabel.stringValue = NSLocalizedString("Network error. Try again later.", comment: "Credentials Error")
|
2019-05-03 01:17:52 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|