NetNewsWire/iOS/Account/NewsBlurAccountViewControll...

176 lines
5.3 KiB
Swift
Raw Normal View History

2020-03-09 23:39:58 +01:00
//
// NewsBlurAccountViewController.swift
// NetNewsWire
//
// Created by Anh-Quang Do on 3/9/20.
// Copyright (c) 2020 Ranchero Software. All rights reserved.
//
import UIKit
import Account
import Secrets
2020-03-09 23:39:58 +01:00
import RSWeb
class NewsBlurAccountViewController: UITableViewController {
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
@IBOutlet weak var cancelBarButtonItem: UIBarButtonItem!
2020-03-10 01:19:24 +01:00
@IBOutlet weak var usernameTextField: UITextField!
2020-03-09 23:39:58 +01:00
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var showHideButton: UIButton!
@IBOutlet weak var actionButton: UIButton!
weak var account: Account?
weak var delegate: AddAccountDismissDelegate?
override func viewDidLoad() {
super.viewDidLoad()
activityIndicator.isHidden = true
2020-03-10 01:19:24 +01:00
usernameTextField.delegate = self
2020-03-09 23:39:58 +01:00
passwordTextField.delegate = self
2020-03-23 00:52:48 +01:00
if let account = account, let credentials = try? account.retrieveCredentials(type: .newsBlurBasic) {
2020-03-09 23:39:58 +01:00
actionButton.setTitle(NSLocalizedString("Update Credentials", comment: "Update Credentials"), for: .normal)
actionButton.isEnabled = true
2020-03-10 01:19:24 +01:00
usernameTextField.text = credentials.username
2020-03-09 23:39:58 +01:00
passwordTextField.text = credentials.secret
} else {
actionButton.setTitle(NSLocalizedString("Add Account", comment: "Add Account"), for: .normal)
}
2020-03-10 01:19:24 +01:00
NotificationCenter.default.addObserver(self, selector: #selector(textDidChange(_:)), name: UITextField.textDidChangeNotification, object: usernameTextField)
2020-03-09 23:39:58 +01:00
NotificationCenter.default.addObserver(self, selector: #selector(textDidChange(_:)), name: UITextField.textDidChangeNotification, object: passwordTextField)
tableView.register(ImageHeaderView.self, forHeaderFooterViewReuseIdentifier: "SectionHeader")
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return section == 0 ? ImageHeaderView.rowHeight : super.tableView(tableView, heightForHeaderInSection: section)
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "SectionHeader") as! ImageHeaderView
headerView.imageView.image = AppAssets.image(for: .newsBlur)
return headerView
} else {
return super.tableView(tableView, viewForHeaderInSection: section)
}
}
@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)
}
}
@IBAction func action(_ sender: Any) {
2020-03-10 01:19:24 +01:00
guard let username = usernameTextField.text else {
showError(NSLocalizedString("Username required.", comment: "Credentials Error"))
2020-03-09 23:39:58 +01:00
return
}
2020-03-10 01:19:24 +01:00
let password = passwordTextField.text ?? ""
2020-03-09 23:39:58 +01:00
startAnimatingActivityIndicator()
disableNavigation()
// When you fill in the email address via auto-complete it adds extra whitespace
2020-03-10 01:19:24 +01:00
let trimmedUsername = username.trimmingCharacters(in: .whitespaces)
2020-03-10 04:26:11 +01:00
let credentials = Credentials(type: .newsBlurBasic, username: trimmedUsername, secret: password)
2020-03-09 23:39:58 +01:00
Account.validateCredentials(type: .newsBlur, credentials: credentials) { result in
2020-03-23 00:52:48 +01:00
self.stopAnimatingActivityIndicator()
2020-03-09 23:39:58 +01:00
self.enableNavigation()
switch result {
case .success(let credentials):
if let credentials = credentials {
var newAccount = false
if self.account == nil {
self.account = AccountManager.shared.createAccount(type: .newsBlur)
newAccount = true
}
do {
do {
2020-03-23 00:52:48 +01:00
try self.account?.removeCredentials(type: .newsBlurBasic)
2020-03-09 23:39:58 +01:00
} catch {}
try self.account?.storeCredentials(credentials)
if newAccount {
self.account?.refreshAll() { result in
switch result {
case .success:
break
case .failure(let error):
self.presentError(error)
}
}
}
self.dismiss(animated: true, completion: nil)
self.delegate?.dismiss()
} catch {
self.showError(NSLocalizedString("Keychain error while storing credentials.", comment: "Credentials Error"))
}
} else {
2020-03-10 01:19:24 +01:00
self.showError(NSLocalizedString("Invalid username/password combination.", comment: "Credentials Error"))
2020-03-09 23:39:58 +01:00
}
2020-03-10 01:19:24 +01:00
case .failure(let error):
self.showError(error.localizedDescription)
2020-03-09 23:39:58 +01:00
}
}
}
@objc func textDidChange(_ note: Notification) {
2020-03-10 01:19:24 +01:00
actionButton.isEnabled = !(usernameTextField.text?.isEmpty ?? false)
2020-03-09 23:39:58 +01:00
}
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()
}
2020-03-23 00:52:48 +01:00
private func stopAnimatingActivityIndicator() {
2020-03-09 23:39:58 +01:00
self.activityIndicator.isHidden = true
self.activityIndicator.stopAnimating()
}
}
extension NewsBlurAccountViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}