2019-10-21 11:51:33 -05:00
//
2019-10-21 13:44:47 -05:00
// FeedbinAccountViewController.swift
2019-10-21 11:51:33 -05:00
// NetNewsWire-iOS
//
// Created by Maurice Parker on 5/19/19.
// Copyright © 2019 Ranchero Software. All rights reserved.
//
import UIKit
2025-11-19 19:04:35 -08:00
import SafariServices
import RSCore
import RSWeb
2019-10-21 11:51:33 -05:00
import Account
2020-04-16 08:25:40 -05:00
import Secrets
2019-10-21 11:51:33 -05:00
2025-09-16 10:21:29 -07:00
final class FeedbinAccountViewController : UITableViewController {
2019-10-21 11:51:33 -05:00
2025-11-19 19:43:09 -08:00
@IBOutlet var activityIndicator : UIActivityIndicatorView !
@IBOutlet var cancelBarButtonItem : UIBarButtonItem !
@IBOutlet var emailTextField : UITextField !
@IBOutlet var passwordTextField : UITextField !
@IBOutlet var showHideButton : UIButton !
@IBOutlet var actionButton : UIButton !
@IBOutlet var footerLabel : UILabel !
2024-12-04 17:14:00 -08:00
2019-10-21 11:51:33 -05:00
weak var account : Account ?
weak var delegate : AddAccountDismissDelegate ?
override func viewDidLoad () {
super . viewDidLoad ()
2020-11-09 21:41:05 +08:00
setupFooter ()
2019-10-21 11:51:33 -05:00
activityIndicator . isHidden = true
emailTextField . delegate = self
passwordTextField . delegate = self
2025-11-05 11:17:50 -08:00
2019-10-21 11:51:33 -05:00
if let account = account , let credentials = try ? account . retrieveCredentials ( type : . basic ) {
actionButton . setTitle ( NSLocalizedString ( "Update Credentials" , comment : "Update Credentials" ), for : . normal )
2020-01-16 12:04:40 -07:00
actionButton . isEnabled = true
2019-10-21 11:51:33 -05:00
emailTextField . text = credentials . username
passwordTextField . text = credentials . secret
} else {
2020-01-16 12:04:40 -07:00
actionButton . setTitle ( NSLocalizedString ( "Add Account" , comment : "Add Account" ), for : . normal )
2019-10-21 11:51:33 -05:00
}
2019-10-22 09:06:35 -05:00
NotificationCenter . default . addObserver ( self , selector : #selector ( textDidChange ( _ :)), name : UITextField . textDidChangeNotification , object : emailTextField )
NotificationCenter . default . addObserver ( self , selector : #selector ( textDidChange ( _ :)), name : UITextField . textDidChangeNotification , object : passwordTextField )
2019-11-16 15:47:12 -06:00
tableView . register ( ImageHeaderView . self , forHeaderFooterViewReuseIdentifier : "SectionHeader" )
}
2025-11-05 11:17:50 -08:00
2020-11-09 21:41:05 +08:00
private func setupFooter () {
2021-05-31 16:01:01 -07:00
footerLabel . text = NSLocalizedString ( "Sign in to your Feedbin account and sync your feeds across your devices. Your username and password will be encrypted and stored in Keychain. \n\n Don’ t have a Feedbin account?" , comment : "Feedbin" )
2020-11-09 21:41:05 +08:00
}
2019-11-16 15:47:12 -06:00
override func tableView ( _ tableView : UITableView , heightForHeaderInSection section : Int ) -> CGFloat {
2019-11-22 15:11:15 -06:00
return section == 0 ? ImageHeaderView . rowHeight : super . tableView ( tableView , heightForHeaderInSection : section )
2019-10-21 11:51:33 -05:00
}
2025-11-05 11:17:50 -08:00
2019-11-16 15:47:12 -06:00
override func tableView ( _ tableView : UITableView , viewForHeaderInSection section : Int ) -> UIView ? {
if section == 0 {
let headerView = tableView . dequeueReusableHeaderFooterView ( withIdentifier : "SectionHeader" ) as ! ImageHeaderView
2025-11-18 22:08:33 -08:00
headerView . imageView . image = Assets . accountImage (. feedbin )
2019-11-16 15:47:12 -06:00
return headerView
} else {
return super . tableView ( tableView , viewForHeaderInSection : section )
}
}
2019-10-21 11:51:33 -05:00
@IBAction func cancel ( _ sender : Any ) {
dismiss ( animated : true , completion : nil )
}
2025-11-05 11:17:50 -08:00
2019-10-21 13:44:47 -05:00
@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:17:50 -08:00
2019-10-21 11:51:33 -05:00
@IBAction func action ( _ sender : Any ) {
2019-10-21 13:44:47 -05:00
guard let email = emailTextField . text , let password = passwordTextField . text else {
2019-10-21 20:28:50 -05:00
showError ( NSLocalizedString ( "Username & password required." , comment : "Credentials Error" ))
2019-10-21 11:51:33 -05:00
return
}
2025-11-05 11:17:50 -08:00
2019-10-21 11:51:33 -05:00
// When you fill in the email address via auto-complete it adds extra whitespace
2019-10-21 13:44:47 -05:00
let trimmedEmail = email . trimmingCharacters ( in : . whitespaces )
2025-11-05 11:17:50 -08:00
2020-10-29 12:01:27 -05:00
guard account != nil || ! AccountManager . shared . duplicateServiceAccount ( type : . feedbin , username : trimmedEmail ) else {
2020-09-24 18:02:12 -05:00
showError ( NSLocalizedString ( "There is already a Feedbin account with that username created." , comment : "Duplicate Error" ))
return
}
2025-11-05 11:17:50 -08:00
Task { @ MainActor in
resignFirstResponder ()
toggleActivityIndicatorAnimation ( visible : true )
setNavigationEnabled ( to : false )
@ MainActor func stopAnimation () {
toggleActivityIndicatorAnimation ( visible : false )
setNavigationEnabled ( to : true )
}
let credentials = Credentials ( type : . basic , username : trimmedEmail , secret : password )
do {
let validatedCredentials = try await Account . validateCredentials ( type : . feedbin , credentials : credentials )
stopAnimation ()
if let validatedCredentials {
if account == nil {
account = AccountManager . shared . createAccount ( type : . feedbin )
2019-10-21 11:51:33 -05:00
}
2025-11-05 11:17:50 -08:00
2019-10-21 11:51:33 -05:00
do {
2025-11-05 11:17:50 -08:00
try account ?. storeCredentials ( validatedCredentials )
2026-01-19 14:46:24 -08:00
account ?. triggerRefreshAll ()
2026-01-19 17:00:47 -08:00
2025-11-05 11:17:50 -08:00
dismiss ( animated : true , completion : nil )
delegate ?. dismiss ()
2019-10-21 11:51:33 -05:00
} catch {
2025-11-05 11:17:50 -08:00
showError ( NSLocalizedString ( "Keychain error while storing credentials." , comment : "Credentials Error" ))
2019-10-21 11:51:33 -05:00
}
} else {
2025-11-05 11:17:50 -08:00
showError ( NSLocalizedString ( "Invalid email/password combination." , comment : "Credentials Error" ))
2019-10-21 11:51:33 -05:00
}
2025-11-05 11:17:50 -08:00
} catch {
stopAnimation ()
showError ( NSLocalizedString ( "Network error. Try again later." , comment : "Credentials Error" ))
2019-10-21 11:51:33 -05:00
}
}
}
2025-11-05 11:17:50 -08:00
2020-11-09 21:31:29 +08:00
@IBAction func signUpWithProvider ( _ sender : Any ) {
let url = URL ( string : "https://feedbin.com/signup" ) !
let safari = SFSafariViewController ( url : url )
safari . modalPresentationStyle = . currentContext
self . present ( safari , animated : true , completion : nil )
}
2025-11-05 11:17:50 -08:00
2019-10-22 09:06:35 -05:00
@objc func textDidChange ( _ note : Notification ) {
2026-01-02 15:05:23 -08:00
actionButton . isEnabled = ! ( emailTextField . text ?. isEmpty ?? false ) && ! ( passwordTextField . text ?. isEmpty ?? false )
2019-10-22 09:06:35 -05:00
}
2025-11-05 11:17:50 -08:00
2019-10-21 20:28:50 -05:00
private func showError ( _ message : String ) {
2026-06-10 10:48:40 -07:00
presentError ( title : NSLocalizedString ( "Error" , comment : "Error" ), message : message )
2019-10-21 20:28:50 -05:00
}
2025-11-05 11:17:50 -08:00
2026-01-01 18:07:54 -08:00
private func setNavigationEnabled ( to value : Bool ) {
2020-03-11 13:42:25 +01:00
cancelBarButtonItem . isEnabled = value
actionButton . isEnabled = value
2019-10-21 11:51:33 -05:00
}
2025-11-05 11:17:50 -08:00
2026-01-02 17:50:35 -08:00
private func toggleActivityIndicatorAnimation ( visible value : Bool ) {
2020-03-11 13:42:25 +01:00
activityIndicator . isHidden = ! value
if value {
activityIndicator . startAnimating ()
} else {
activityIndicator . stopAnimating ()
}
2019-10-21 11:51:33 -05:00
}
}
extension FeedbinAccountViewController : UITextFieldDelegate {
2025-11-05 11:17:50 -08:00
2019-10-21 11:51:33 -05:00
func textFieldShouldReturn ( _ textField : UITextField ) -> Bool {
2020-03-09 20:15:04 +01:00
if textField == emailTextField {
passwordTextField . becomeFirstResponder ()
} else {
textField . resignFirstResponder ()
2020-03-11 14:02:06 +01:00
action ( self )
2020-03-09 20:15:04 +01:00
}
2019-10-21 11:51:33 -05:00
return true
}
}