2019-05-20 01:04:32 +02:00
|
|
|
//
|
|
|
|
// AddFeedbinAccountViewController.swift
|
|
|
|
// NetNewsWire-iOS
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 5/19/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Account
|
|
|
|
import RSWeb
|
|
|
|
|
|
|
|
class FeedbinAccountViewController: UIViewController {
|
|
|
|
|
|
|
|
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
|
2019-05-24 17:07:17 +02:00
|
|
|
@IBOutlet weak var cancelBarButtonItem: UIBarButtonItem!
|
2019-05-20 01:04:32 +02:00
|
|
|
@IBOutlet weak var emailTextField: UITextField!
|
|
|
|
@IBOutlet weak var passwordTextField: UITextField!
|
2019-06-03 12:04:03 +02:00
|
|
|
@IBOutlet weak var actionButton: UIButton!
|
2019-05-23 04:17:50 +02:00
|
|
|
|
2019-05-20 01:04:32 +02:00
|
|
|
@IBOutlet weak var errorMessageLabel: UILabel!
|
|
|
|
|
|
|
|
weak var account: Account?
|
|
|
|
weak var delegate: AddAccountDismissDelegate?
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
|
|
|
activityIndicator.isHidden = true
|
2019-05-23 04:17:50 +02:00
|
|
|
emailTextField.delegate = self
|
|
|
|
passwordTextField.delegate = self
|
2019-05-20 01:04:32 +02:00
|
|
|
|
|
|
|
if let account = account, let credentials = try? account.retrieveBasicCredentials() {
|
2019-06-03 12:04:03 +02:00
|
|
|
actionButton.setTitle(NSLocalizedString("Update Credentials", comment: "Update Credentials"), for: .normal)
|
2019-05-20 01:04:32 +02:00
|
|
|
if case .basic(let username, let password) = credentials {
|
|
|
|
emailTextField.text = username
|
|
|
|
passwordTextField.text = password
|
|
|
|
}
|
2019-06-03 12:04:03 +02:00
|
|
|
} else {
|
|
|
|
actionButton.setTitle(NSLocalizedString("Add Account", comment: "Update Credentials"), for: .normal)
|
2019-05-20 01:04:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-24 17:07:17 +02:00
|
|
|
@IBAction func cancel(_ sender: Any) {
|
2019-06-03 12:04:03 +02:00
|
|
|
dismiss(animated: true, completion: nil)
|
|
|
|
delegate?.dismiss()
|
2019-05-24 17:07:17 +02:00
|
|
|
}
|
|
|
|
|
2019-06-03 12:04:03 +02:00
|
|
|
@IBAction func action(_ sender: Any) {
|
2019-05-20 01:04:32 +02:00
|
|
|
self.errorMessageLabel.text = nil
|
|
|
|
|
|
|
|
guard emailTextField.text != nil && passwordTextField.text != nil else {
|
|
|
|
self.errorMessageLabel.text = NSLocalizedString("Username & password required.", comment: "Credentials Error")
|
|
|
|
return
|
|
|
|
}
|
2019-05-23 04:17:50 +02:00
|
|
|
|
|
|
|
startAnimatingActivityIndicator()
|
|
|
|
disableNavigation()
|
2019-05-20 01:04:32 +02:00
|
|
|
|
2019-05-23 04:17:50 +02:00
|
|
|
// When you fill in the email address via auto-complete it adds extra whitespace
|
|
|
|
let emailAddress = emailTextField.text?.trimmingCharacters(in: .whitespaces)
|
|
|
|
let credentials = Credentials.basic(username: emailAddress ?? "", password: passwordTextField.text ?? "")
|
2019-06-03 12:04:03 +02:00
|
|
|
Account.validateCredentials(type: .feedbin, credentials: credentials) { result in
|
2019-05-20 01:04:32 +02:00
|
|
|
|
2019-05-23 04:17:50 +02:00
|
|
|
self.stopAnimtatingActivityIndicator()
|
|
|
|
self.enableNavigation()
|
2019-05-20 01:04:32 +02:00
|
|
|
|
|
|
|
switch result {
|
|
|
|
case .success(let authenticated):
|
|
|
|
if authenticated {
|
|
|
|
var newAccount = false
|
|
|
|
if self.account == nil {
|
|
|
|
self.account = AccountManager.shared.createAccount(type: .feedbin)
|
|
|
|
newAccount = true
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2019-05-26 18:54:32 +02:00
|
|
|
|
2019-06-03 12:04:03 +02:00
|
|
|
do {
|
|
|
|
try self.account?.removeBasicCredentials()
|
|
|
|
} catch {}
|
2019-05-20 01:04:32 +02:00
|
|
|
try self.account?.storeCredentials(credentials)
|
2019-05-26 18:54:32 +02:00
|
|
|
|
2019-05-20 01:04:32 +02:00
|
|
|
if newAccount {
|
2019-05-26 18:54:32 +02:00
|
|
|
self.account?.refreshAll() { result in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
break
|
|
|
|
case .failure(let error):
|
|
|
|
UIApplication.shared.presentError(error)
|
|
|
|
}
|
|
|
|
}
|
2019-05-20 01:04:32 +02:00
|
|
|
}
|
2019-05-23 04:17:50 +02:00
|
|
|
|
2019-06-03 12:04:03 +02:00
|
|
|
self.dismiss(animated: true, completion: nil)
|
|
|
|
self.delegate?.dismiss()
|
2019-05-20 01:04:32 +02:00
|
|
|
} catch {
|
|
|
|
self.errorMessageLabel.text = NSLocalizedString("Keychain error while storing credentials.", comment: "Credentials Error")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.errorMessageLabel.text = NSLocalizedString("Invalid email/password combination.", comment: "Credentials Error")
|
|
|
|
}
|
|
|
|
case .failure:
|
2019-05-23 04:17:50 +02:00
|
|
|
self.errorMessageLabel.text = NSLocalizedString("Network error. Try again later.", comment: "Credentials Error")
|
2019-05-20 01:04:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2019-05-23 04:17:50 +02:00
|
|
|
|
|
|
|
private func enableNavigation() {
|
2019-05-24 17:07:17 +02:00
|
|
|
self.cancelBarButtonItem.isEnabled = true
|
2019-06-03 12:04:03 +02:00
|
|
|
self.actionButton.isEnabled = true
|
2019-05-23 04:17:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private func disableNavigation() {
|
2019-05-24 17:07:17 +02:00
|
|
|
cancelBarButtonItem.isEnabled = false
|
2019-06-03 12:04:03 +02:00
|
|
|
actionButton.isEnabled = false
|
2019-05-23 04:17:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private func startAnimatingActivityIndicator() {
|
|
|
|
activityIndicator.isHidden = false
|
|
|
|
activityIndicator.startAnimating()
|
|
|
|
}
|
|
|
|
|
|
|
|
private func stopAnimtatingActivityIndicator() {
|
|
|
|
self.activityIndicator.isHidden = true
|
|
|
|
self.activityIndicator.stopAnimating()
|
|
|
|
}
|
2019-05-24 17:07:17 +02:00
|
|
|
|
2019-05-23 04:17:50 +02:00
|
|
|
}
|
2019-05-20 01:04:32 +02:00
|
|
|
|
2019-05-23 04:17:50 +02:00
|
|
|
extension FeedbinAccountViewController: UITextFieldDelegate {
|
|
|
|
|
|
|
|
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
|
|
|
|
textField.resignFirstResponder()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-05-20 01:04:32 +02:00
|
|
|
}
|