Files
NetNewsWire/Mac/Preferences/Accounts/AccountsReaderAPIWindowController.swift
T

201 lines
7.0 KiB
Swift
Raw Normal View History

//
// AccountsAddFeedbinWindowController.swift
// NetNewsWire
//
// Created by Maurice Parker on 5/2/19.
// Copyright © 2019 Ranchero Software. All rights reserved.
//
import AppKit
import Account
import RSWeb
2020-04-09 21:07:56 -05:00
import Secrets
final class AccountsReaderAPIWindowController: NSWindowController {
2025-11-18 22:22:38 -08:00
@IBOutlet var titleImageView: NSImageView!
@IBOutlet var titleLabel: NSTextField!
2025-11-05 11:22:10 -08:00
2025-11-18 22:22:38 -08:00
@IBOutlet var gridView: NSGridView!
@IBOutlet var progressIndicator: NSProgressIndicator!
@IBOutlet var usernameTextField: NSTextField!
@IBOutlet var apiURLTextField: NSTextField!
@IBOutlet var passwordTextField: NSSecureTextField!
@IBOutlet var createAccountButton: NSButton!
@IBOutlet var errorMessageLabel: NSTextField!
@IBOutlet var actionButton: NSButton!
@IBOutlet var noAccountTextField: NSTextField!
2025-11-05 11:22:10 -08:00
var account: Account?
2019-06-20 07:22:51 -05:00
var accountType: AccountType?
2025-11-05 11:22:10 -08:00
private weak var hostWindow: NSWindow?
2025-11-05 11:22:10 -08:00
convenience init() {
self.init(windowNibName: NSNib.Name("AccountsReaderAPI"))
}
2025-11-05 11:22:10 -08:00
override func windowDidLoad() {
2019-06-20 07:22:51 -05:00
if let accountType = accountType {
switch accountType {
case .freshRSS:
titleImageView.image = Assets.Images.accountFreshRSS
2020-11-06 19:07:28 +08:00
titleLabel.stringValue = NSLocalizedString("Sign in to your FreshRSS account.", comment: "FreshRSS")
noAccountTextField.stringValue = NSLocalizedString("Don’t have a FreshRSS instance?", comment: "No FreshRSS")
2020-11-07 09:31:52 +08:00
createAccountButton.title = NSLocalizedString("Find out more", comment: "No FreshRSS Button")
2024-10-14 20:00:40 -04:00
apiURLTextField.placeholderString = NSLocalizedString("https://fresh.rss.net/api/greader.php", comment: "FreshRSS API Helper")
case .inoreader:
titleImageView.image = Assets.Images.accountInoreader
titleLabel.stringValue = NSLocalizedString("Sign in to your Inoreader account.", comment: "Inoreader")
gridView.row(at: 2).isHidden = true
noAccountTextField.stringValue = NSLocalizedString("Don’t have an Inoreader account?", comment: "No Inoreader")
case .bazQux:
titleImageView.image = Assets.Images.accountBazQux
2020-11-06 19:07:28 +08:00
titleLabel.stringValue = NSLocalizedString("Sign in to your BazQux account.", comment: "BazQux")
gridView.row(at: 2).isHidden = true
noAccountTextField.stringValue = NSLocalizedString("Don’t have a BazQux account?", comment: "No BazQux")
case .theOldReader:
titleImageView.image = Assets.Images.accountTheOldReader
2020-11-06 19:07:28 +08:00
titleLabel.stringValue = NSLocalizedString("Sign in to your The Old Reader account.", comment: "The Old Reader")
gridView.row(at: 2).isHidden = true
noAccountTextField.stringValue = NSLocalizedString("Don’t have a The Old Reader account?", comment: "No OldReader")
2019-06-20 07:22:51 -05:00
default:
break
}
}
2025-11-05 11:22:10 -08:00
if let account = account, let credentials = try? account.retrieveCredentials(type: .readerBasic) {
usernameTextField.stringValue = credentials.username
apiURLTextField.stringValue = account.endpointURL?.absoluteString ?? ""
actionButton.title = NSLocalizedString("Update", comment: "Update")
} else {
actionButton.title = NSLocalizedString("Create", comment: "Create")
}
2025-11-05 11:22:10 -08:00
2021-06-17 12:38:53 +08:00
enableAutofill()
usernameTextField.becomeFirstResponder()
}
2025-11-05 11:22:10 -08:00
// MARK: API
2025-11-05 11:22:10 -08:00
func runSheetOnWindow(_ hostWindow: NSWindow, completion: ((NSApplication.ModalResponse) -> Void)? = nil) {
guard let window else {
return
}
self.hostWindow = hostWindow
hostWindow.beginSheet(window, completionHandler: completion)
}
// MARK: Actions
2025-11-05 11:22:10 -08:00
@IBAction func cancel(_ sender: Any) {
hostWindow!.endSheet(window!, returnCode: NSApplication.ModalResponse.cancel)
}
2025-11-05 11:22:10 -08:00
@IBAction func action(_ sender: Any) {
self.errorMessageLabel.stringValue = ""
2025-11-05 11:22:10 -08:00
guard !usernameTextField.stringValue.isEmpty && !passwordTextField.stringValue.isEmpty else {
2026-09-18 10:06:04 -07:00
self.errorMessageLabel.stringValue = NSLocalizedString("Username, password, and API URL are required.", comment: "Credentials Error")
return
}
2025-11-05 11:22:10 -08:00
guard let accountType = accountType, !(accountType == .freshRSS && apiURLTextField.stringValue.isEmpty) else {
2026-09-18 10:06:04 -07:00
self.errorMessageLabel.stringValue = NSLocalizedString("Username, password, and API URL are required.", comment: "Credentials Error")
return
}
2025-11-05 11:22:10 -08:00
2026-03-08 11:19:54 -07:00
let trimmedUsername = usernameTextField.stringValue.trimmingWhitespace
let apiURL: URL
switch accountType {
case .freshRSS:
2026-03-08 11:19:54 -07:00
guard let inputURL = URL(string: apiURLTextField.stringValue.trimmingWhitespace) else {
self.errorMessageLabel.stringValue = NSLocalizedString("Invalid API URL.", comment: "Invalid API URL")
return
}
apiURL = inputURL
case .inoreader:
apiURL = URL(string: ReaderAPIVariant.inoreader.host)!
case .bazQux:
apiURL = URL(string: ReaderAPIVariant.bazQux.host)!
case .theOldReader:
apiURL = URL(string: ReaderAPIVariant.theOldReader.host)!
default:
self.errorMessageLabel.stringValue = NSLocalizedString("Unrecognized account type.", comment: "Bad account type")
return
}
2025-11-05 09:51:42 -08:00
guard account != nil || !AccountManager.shared.duplicateServiceAccount(type: accountType, username: trimmedUsername, endpoint: apiURL) else {
self.errorMessageLabel.stringValue = NSLocalizedString("There is already an account of this type with that username created.", comment: "Duplicate Error")
return
}
2025-11-05 09:51:42 -08:00
Task { @MainActor in
actionButton.isEnabled = false
progressIndicator.isHidden = false
progressIndicator.startAnimation(self)
@MainActor func stopAnimation() {
actionButton.isEnabled = true
progressIndicator.isHidden = true
progressIndicator.stopAnimation(self)
}
2026-03-08 11:19:54 -07:00
let credentials = Credentials(type: .readerBasic, username: trimmedUsername, secret: passwordTextField.stringValue)
2025-11-05 09:51:42 -08:00
do {
let validatedCredentials = try await Account.validateCredentials(type: accountType, credentials: credentials, endpoint: apiURL)
stopAnimation()
guard let validatedCredentials else {
errorMessageLabel.stringValue = NSLocalizedString("Invalid email/password combination.", comment: "Credentials Error")
2019-05-29 10:54:52 -04:00
return
}
2019-05-29 15:16:09 -04:00
2025-11-05 09:51:42 -08:00
if account == nil {
account = AccountManager.shared.createAccount(type: accountType)
}
do {
account?.endpointURL = apiURL
try account?.storeCredentials(credentials)
try account?.storeCredentials(validatedCredentials)
hostWindow?.endSheet(window!, returnCode: NSApplication.ModalResponse.OK)
account?.triggerRefreshAll()
2019-05-29 10:54:52 -04:00
} catch {
2025-11-05 09:51:42 -08:00
errorMessageLabel.stringValue = NSLocalizedString("Keychain error while storing credentials.", comment: "Credentials Error")
}
2025-11-05 09:51:42 -08:00
} catch {
stopAnimation()
2026-03-08 11:19:54 -07:00
errorMessageLabel.stringValue = error.localizedDescription
}
}
}
2025-11-05 11:22:10 -08:00
2020-11-06 19:07:28 +08:00
@IBAction func createAccountWithProvider(_ sender: Any) {
switch accountType {
case .freshRSS:
NSWorkspace.shared.open(URL(string: "https://freshrss.org")!)
case .inoreader:
NSWorkspace.shared.open(URL(string: "https://www.inoreader.com")!)
case .bazQux:
NSWorkspace.shared.open(URL(string: "https://bazqux.com")!)
case .theOldReader:
NSWorkspace.shared.open(URL(string: "https://theoldreader.com")!)
default:
return
}
}
2025-11-05 11:22:10 -08:00
2021-06-17 12:38:53 +08:00
// MARK: Autofill
func enableAutofill() {
2025-06-20 19:48:15 +08:00
usernameTextField.contentType = .username
passwordTextField.contentType = .password
2021-06-17 12:38:53 +08:00
}
2025-11-05 11:22:10 -08:00
}