Files
NetNewsWire/iOS/Account/ReaderAPIAccountViewController.swift
T

308 lines
9.9 KiB
Swift
Raw Normal View History

2020-10-26 09:03:32 +08:00
//
// ReaderAPIAccountViewController.swift
// NetNewsWire-iOS
//
// Created by Stuart Breckenridge on 25/10/20.
// Copyright © 2020 Ranchero Software. All rights reserved.
//
import UIKit
2025-11-19 19:04:35 -08:00
import SafariServices
import RSCore
import RSWeb
2020-10-26 09:03:32 +08:00
import Account
import Secrets
final class ReaderAPIAccountViewController: UITableViewController {
2025-11-19 19:43:09 -08:00
@IBOutlet var activityIndicator: UIActivityIndicatorView!
@IBOutlet var cancelBarButtonItem: UIBarButtonItem!
@IBOutlet var usernameTextField: UITextField!
@IBOutlet var passwordTextField: UITextField!
@IBOutlet var apiURLTextField: UITextField!
@IBOutlet var showHideButton: UIButton!
@IBOutlet var actionButton: UIButton!
@IBOutlet var footerLabel: UILabel!
@IBOutlet var signUpButton: UIButton!
2024-12-04 17:14:00 -08:00
2020-10-26 09:03:32 +08:00
weak var account: Account?
var accountType: AccountType?
weak var delegate: AddAccountDismissDelegate?
2025-11-05 11:22:10 -08:00
2020-10-26 09:03:32 +08:00
override func viewDidLoad() {
super.viewDidLoad()
2020-11-09 21:41:05 +08:00
setupFooter()
2020-10-26 09:03:32 +08:00
activityIndicator.isHidden = true
usernameTextField.delegate = self
passwordTextField.delegate = self
2025-11-05 11:22:10 -08:00
2025-04-28 21:45:23 -07:00
if let unwrappedAccount = account,
let credentials = try? retrieveCredentialsForAccount(for: unwrappedAccount) {
2020-10-26 09:03:32 +08:00
actionButton.setTitle(NSLocalizedString("Update Credentials", comment: "Update Credentials"), for: .normal)
actionButton.isEnabled = true
usernameTextField.text = credentials.username
passwordTextField.text = credentials.secret
} else {
actionButton.setTitle(NSLocalizedString("Add Account", comment: "Add Account"), for: .normal)
}
2025-11-05 11:22:10 -08:00
2020-10-26 09:03:32 +08:00
if let unwrappedAccountType = accountType {
switch unwrappedAccountType {
case .freshRSS:
title = "FreshRSS"
2024-10-14 20:00:40 -04:00
apiURLTextField.placeholder = NSLocalizedString("API URL: https://fresh.rss.net/api/greader.php", comment: "FreshRSS API Helper")
2020-10-26 09:03:32 +08:00
case .inoreader:
title = "Inoreader"
2020-10-26 09:03:32 +08:00
case .bazQux:
title = "BazQux"
2020-10-26 09:03:32 +08:00
case .theOldReader:
title = "The Old Reader"
2020-10-26 09:03:32 +08:00
default:
title = ""
}
}
2025-11-05 11:22:10 -08:00
2020-10-26 09:03:32 +08:00
NotificationCenter.default.addObserver(self, selector: #selector(textDidChange(_:)), name: UITextField.textDidChangeNotification, object: usernameTextField)
NotificationCenter.default.addObserver(self, selector: #selector(textDidChange(_:)), name: UITextField.textDidChangeNotification, object: passwordTextField)
tableView.register(ImageHeaderView.self, forHeaderFooterViewReuseIdentifier: "SectionHeader")
2025-11-05 11:22:10 -08:00
2020-10-26 09:03:32 +08:00
}
2025-11-05 11:22:10 -08:00
2020-11-09 21:31:29 +08:00
private func setupFooter() {
switch accountType {
2026-01-02 11:42:12 -08:00
case .bazQux:
footerLabel.text = NSLocalizedString("Sign in to your BazQux account and sync your feeds across your devices. Your username and password will be encrypted and stored in Keychain.\n\nDon’t have a BazQux account?", comment: "BazQux")
signUpButton.setTitle(NSLocalizedString("Sign Up Here", comment: "Sign Up"), for: .normal)
2026-01-02 11:42:12 -08:00
case .inoreader:
footerLabel.text = NSLocalizedString("Sign in to your Inoreader account and sync your feeds across your devices. Your username and password will be encrypted and stored in Keychain.\n\nDon’t have an Inoreader account?", comment: "Inoreader")
signUpButton.setTitle(NSLocalizedString("Sign Up Here", comment: "Sign Up"), for: .normal)
2026-01-02 11:42:12 -08:00
case .theOldReader:
footerLabel.text = NSLocalizedString("Sign in to your The Old Reader account and sync your feeds across your devices. Your username and password will be encrypted and stored in Keychain.\n\nDon’t have a The Old Reader account?", comment: "TOR")
signUpButton.setTitle(NSLocalizedString("Sign Up Here", comment: "Sign Up"), for: .normal)
2026-01-02 11:42:12 -08:00
case .freshRSS:
footerLabel.text = NSLocalizedString("Sign in to your FreshRSS instance and sync your feeds across your devices. Your username and password will be encrypted and stored in Keychain.\n\nDon’t have an FreshRSS instance?", comment: "FreshRSS")
signUpButton.setTitle(NSLocalizedString("Find Out More", comment: "Find Out More"), for: .normal)
2026-01-02 11:42:12 -08:00
default:
return
2020-11-09 21:31:29 +08:00
}
}
2025-11-05 11:22:10 -08:00
2020-10-26 09:03:32 +08:00
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return section == 0 ? ImageHeaderView.rowHeight : super.tableView(tableView, heightForHeaderInSection: section)
}
2025-11-05 11:22:10 -08:00
2020-10-26 09:03:32 +08:00
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "SectionHeader") as! ImageHeaderView
headerView.imageView.image = headerViewImage()
return headerView
} else {
return super.tableView(tableView, viewForHeaderInSection: section)
}
}
2025-11-05 11:22:10 -08:00
2020-10-26 09:03:32 +08:00
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
switch accountType {
case .freshRSS:
return 3
default:
return 2
}
default:
return 1
}
}
2025-11-05 11:22:10 -08:00
2020-10-26 09:03:32 +08:00
@IBAction func cancel(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
@IBAction func showHidePassword(_ sender: Any) {
if passwordTextField.isSecureTextEntry {
passwordTextField.isSecureTextEntry = false
showHideButton.setTitle("Hide", for: .normal)
} else {
passwordTextField.isSecureTextEntry = true
showHideButton.setTitle("Show", for: .normal)
}
}
2025-11-05 11:22:10 -08:00
2020-10-26 09:03:32 +08:00
@IBAction func action(_ sender: Any) {
guard validateDataEntry(), let type = accountType else {
return
}
2025-11-05 11:22:10 -08:00
2020-10-26 09:03:32 +08:00
let username = usernameTextField.text!
let password = passwordTextField.text!
let url = apiURL()!
2025-11-05 11:22:10 -08:00
// When you fill in the email address via auto-complete it adds extra whitespace
2026-03-08 11:20:25 -07:00
let trimmedUsername = username.trimmingWhitespace
2025-11-05 11:22:10 -08:00
guard account != nil || !AccountManager.shared.duplicateServiceAccount(type: type, username: trimmedUsername, endpoint: url) else {
showError(NSLocalizedString("There is already an account of that type with that username created.", comment: "Duplicate Error"))
return
}
2025-11-05 11:17:50 -08:00
Task { @MainActor in
startAnimatingActivityIndicator()
disableNavigation()
2020-10-26 09:03:32 +08:00
2025-11-05 11:17:50 -08:00
@MainActor func stopAnimation() {
stopAnimatingActivityIndicator()
enableNavigation()
}
2020-10-26 09:03:32 +08:00
2025-11-05 11:17:50 -08:00
let credentials = Credentials(type: .readerBasic, username: trimmedUsername, secret: password)
do {
let validatedCredentials = try await Account.validateCredentials(type: type, credentials: credentials, endpoint: url)
stopAnimation()
2020-10-26 09:03:32 +08:00
2025-11-05 11:17:50 -08:00
if let validatedCredentials {
if account == nil {
account = AccountManager.shared.createAccount(type: type)
2020-10-26 09:03:32 +08:00
}
do {
2025-11-05 11:17:50 -08:00
account?.endpointURL = url
2020-10-26 09:03:32 +08:00
2025-11-05 11:17:50 -08:00
try account?.storeCredentials(credentials)
try account?.storeCredentials(validatedCredentials)
account?.triggerRefreshAll()
2025-11-05 11:17:50 -08:00
dismiss(animated: true, completion: nil)
delegate?.dismiss()
2020-10-26 09:03:32 +08:00
} catch {
2025-11-05 11:17:50 -08:00
showError(NSLocalizedString("Keychain error while storing credentials.", comment: "Credentials Error"))
2020-10-26 09:03:32 +08:00
}
} else {
2025-11-05 11:17:50 -08:00
showError(NSLocalizedString("Invalid username/password combination.", comment: "Credentials Error"))
2020-10-26 09:03:32 +08:00
}
2025-11-05 11:17:50 -08:00
} catch {
stopAnimation()
showError(error.localizedDescription)
2020-10-26 09:03:32 +08:00
}
}
}
2025-11-05 11:22:10 -08:00
2020-10-26 09:03:32 +08:00
private func retrieveCredentialsForAccount(for account: Account) throws -> Credentials? {
switch accountType {
case .bazQux, .inoreader, .theOldReader, .freshRSS:
return try account.retrieveCredentials(type: .readerBasic)
default:
return nil
}
}
2025-11-05 11:22:10 -08:00
2020-10-26 09:03:32 +08:00
private func headerViewImage() -> UIImage? {
if let accountType {
2020-10-26 09:03:32 +08:00
switch accountType {
case .bazQux:
return Assets.Images.accountBazQux
case .inoreader:
return Assets.Images.accountInoreader
case .theOldReader:
return Assets.Images.accountTheOldReader
case .freshRSS:
return Assets.Images.accountFreshRSS
default:
return nil
2020-10-26 09:03:32 +08:00
}
}
return nil
}
2025-11-05 11:22:10 -08:00
private func validateDataEntry() -> Bool {
2020-10-26 09:03:32 +08:00
switch accountType {
case .freshRSS:
if !usernameTextField.hasText || !passwordTextField.hasText || !apiURLTextField.hasText {
showError(NSLocalizedString("Username, password, and API URL are required.", comment: "Credentials Error"))
return false
2020-10-26 09:03:32 +08:00
}
2026-01-02 17:50:35 -08:00
guard URL(string: apiURLTextField.text!) != nil else {
2020-10-26 09:03:32 +08:00
showError(NSLocalizedString("Invalid API URL.", comment: "Invalid API URL"))
return false
2020-10-26 09:03:32 +08:00
}
default:
if !usernameTextField.hasText || !passwordTextField.hasText {
showError(NSLocalizedString("Username and password are required.", comment: "Credentials Error"))
return false
2020-10-26 09:03:32 +08:00
}
}
return true
2020-10-26 09:03:32 +08:00
}
2025-11-05 11:22:10 -08:00
2020-11-09 21:31:29 +08:00
@IBAction func signUpWithProvider(_ sender: Any) {
var url: URL!
switch accountType {
2026-01-02 11:42:12 -08:00
case .bazQux:
url = URL(string: "https://bazqux.com")!
case .inoreader:
url = URL(string: "https://www.inoreader.com")!
case .theOldReader:
url = URL(string: "https://theoldreader.com")!
case .freshRSS:
url = URL(string: "https://freshrss.org")!
default:
return
2020-11-09 21:31:29 +08:00
}
let safari = SFSafariViewController(url: url)
safari.modalPresentationStyle = .currentContext
self.present(safari, animated: true, completion: nil)
}
2025-11-05 11:22:10 -08:00
2020-10-26 09:03:32 +08:00
private func apiURL() -> URL? {
switch accountType {
case .freshRSS:
2026-03-08 11:20:25 -07:00
return URL(string: apiURLTextField.text!.trimmingWhitespace)!
2020-10-26 09:03:32 +08:00
case .inoreader:
return URL(string: ReaderAPIVariant.inoreader.host)!
case .bazQux:
return URL(string: ReaderAPIVariant.bazQux.host)!
case .theOldReader:
return URL(string: ReaderAPIVariant.theOldReader.host)!
default:
return nil
}
}
2025-11-05 11:22:10 -08:00
2020-10-26 09:03:32 +08:00
@objc func textDidChange(_ note: Notification) {
actionButton.isEnabled = !(usernameTextField.text?.isEmpty ?? false)
}
private func showError(_ message: String) {
presentError(title: "Error", message: message)
}
private func enableNavigation() {
self.cancelBarButtonItem.isEnabled = true
self.actionButton.isEnabled = true
}
private func disableNavigation() {
cancelBarButtonItem.isEnabled = false
actionButton.isEnabled = false
}
private func startAnimatingActivityIndicator() {
activityIndicator.isHidden = false
activityIndicator.startAnimating()
}
private func stopAnimatingActivityIndicator() {
self.activityIndicator.isHidden = true
self.activityIndicator.stopAnimating()
}
}
extension ReaderAPIAccountViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}