150 lines
4.4 KiB
Swift
150 lines
4.4 KiB
Swift
//
|
|
// AccountsAddFeedbinWindowController.swift
|
|
// NetNewsWire
|
|
//
|
|
// Created by Maurice Parker on 5/2/19.
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
//
|
|
|
|
import AppKit
|
|
import Account
|
|
import Web
|
|
import Secrets
|
|
|
|
final class AccountsFeedbinWindowController: NSWindowController {
|
|
|
|
@IBOutlet weak var signInTextField: NSTextField!
|
|
@IBOutlet weak var noAccountTextField: NSTextField!
|
|
@IBOutlet weak var createNewAccountButton: NSButton!
|
|
@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("AccountsFeedbin"))
|
|
}
|
|
|
|
override func windowDidLoad() {
|
|
if let account = account, let credentials = try? account.retrieveCredentials(type: .basic) {
|
|
usernameTextField.stringValue = credentials.username
|
|
actionButton.title = NSLocalizedString("Update", comment: "Update")
|
|
signInTextField.stringValue = NSLocalizedString("Update your Feedbin account credentials.", comment: "SignIn")
|
|
noAccountTextField.isHidden = true
|
|
createNewAccountButton.isHidden = true
|
|
} else {
|
|
actionButton.title = NSLocalizedString("Create", comment: "Add Account")
|
|
signInTextField.stringValue = NSLocalizedString("Sign in to your Feedbin account.", comment: "SignIn")
|
|
}
|
|
|
|
enableAutofill()
|
|
|
|
usernameTextField.becomeFirstResponder()
|
|
}
|
|
|
|
// MARK: API
|
|
|
|
func runSheetOnWindow(_ hostWindow: NSWindow) {
|
|
self.hostWindow = hostWindow
|
|
Task { @MainActor in
|
|
await hostWindow.beginSheet(window!)
|
|
}
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
guard account != nil || !AccountManager.shared.duplicateServiceAccount(type: .feedbin, username: usernameTextField.stringValue) else {
|
|
self.errorMessageLabel.stringValue = NSLocalizedString("There is already a Feedbin account with that username created.", comment: "Duplicate Error")
|
|
return
|
|
}
|
|
|
|
actionButton.isEnabled = false
|
|
progressIndicator.isHidden = false
|
|
progressIndicator.startAnimation(self)
|
|
|
|
let credentials = Credentials(type: .basic, username: usernameTextField.stringValue, secret: passwordTextField.stringValue)
|
|
|
|
Task { @MainActor in
|
|
|
|
var validationDidThrow = false
|
|
var validatedCredentials: Credentials?
|
|
|
|
do {
|
|
validatedCredentials = try await Account.validateCredentials(type: .feedbin, credentials: credentials, secretsProvider: Secrets())
|
|
} catch {
|
|
self.errorMessageLabel.stringValue = NSLocalizedString("Network error. Try again later.", comment: "Credentials Error")
|
|
validationDidThrow = true
|
|
}
|
|
|
|
self.actionButton.isEnabled = true
|
|
self.progressIndicator.isHidden = true
|
|
self.progressIndicator.stopAnimation(self)
|
|
|
|
if validationDidThrow {
|
|
return
|
|
}
|
|
|
|
guard let validatedCredentials else {
|
|
self.errorMessageLabel.stringValue = NSLocalizedString("Invalid email/password combination.", comment: "Credentials Error")
|
|
return
|
|
}
|
|
|
|
if self.account == nil {
|
|
self.account = AccountManager.shared.createAccount(type: .feedbin)
|
|
}
|
|
|
|
do {
|
|
try self.account?.removeCredentials(type: .basic)
|
|
try self.account?.storeCredentials(validatedCredentials)
|
|
|
|
self.refreshAll()
|
|
|
|
self.hostWindow?.endSheet(self.window!, returnCode: NSApplication.ModalResponse.OK)
|
|
} catch {
|
|
self.errorMessageLabel.stringValue = NSLocalizedString("Keychain error while storing credentials.", comment: "Credentials Error")
|
|
}
|
|
}
|
|
}
|
|
|
|
private func refreshAll() {
|
|
|
|
Task { @MainActor in
|
|
do {
|
|
try await self.account?.refreshAll()
|
|
} catch {
|
|
NSApplication.shared.presentError(error)
|
|
}
|
|
}
|
|
}
|
|
|
|
@IBAction func createAccountWithProvider(_ sender: Any) {
|
|
NSWorkspace.shared.open(URL(string: "https://feedbin.com/signup")!)
|
|
}
|
|
|
|
// MARK: Autofill
|
|
|
|
func enableAutofill() {
|
|
|
|
usernameTextField.contentType = .username
|
|
passwordTextField.contentType = .password
|
|
}
|
|
}
|