Remove redundant code to lessen LOC

- Less code - less bugs.
- Also resign the first responder when the user presses the action button
This commit is contained in:
Flowinho 2020-03-11 13:42:25 +01:00
parent 386efd632b
commit 66fae15f64
2 changed files with 32 additions and 45 deletions

View File

@ -78,17 +78,17 @@ class FeedWranglerAccountViewController: UITableViewController {
showError(NSLocalizedString("Username & password required.", comment: "Credentials Error")) showError(NSLocalizedString("Username & password required.", comment: "Credentials Error"))
return return
} }
resignFirstResponder()
startAnimatingActivityIndicator() toggleActivityIndicatorAnimation(visible: true)
disableNavigation() setNavigationEnabled(to: false)
// When you fill in the email address via auto-complete it adds extra whitespace // When you fill in the email address via auto-complete it adds extra whitespace
let trimmedEmail = email.trimmingCharacters(in: .whitespaces) let trimmedEmail = email.trimmingCharacters(in: .whitespaces)
let credentials = Credentials(type: .feedWranglerBasic, username: trimmedEmail, secret: password) let credentials = Credentials(type: .feedWranglerBasic, username: trimmedEmail, secret: password)
Account.validateCredentials(type: .feedWrangler, credentials: credentials) { result in Account.validateCredentials(type: .feedWrangler, credentials: credentials) { result in
self.stopAnimtatingActivityIndicator() self.toggleActivityIndicatorAnimation(visible: false)
self.enableNavigation() self.setNavigationEnabled(to: true)
switch result { switch result {
case .success(let validatedCredentials): case .success(let validatedCredentials):
@ -138,27 +138,21 @@ class FeedWranglerAccountViewController: UITableViewController {
} }
private func showError(_ message: String) { private func showError(_ message: String) {
presentError(title: "Error", message: message) presentError(title: NSLocalizedString("Error", comment: "Credentials Error"), message: message)
} }
private func enableNavigation() { private func setNavigationEnabled(to value:Bool){
self.cancelBarButtonItem.isEnabled = true cancelBarButtonItem.isEnabled = value
self.actionButton.isEnabled = true actionButton.isEnabled = value
} }
private func disableNavigation() { private func toggleActivityIndicatorAnimation(visible value: Bool){
cancelBarButtonItem.isEnabled = false activityIndicator.isHidden = !value
actionButton.isEnabled = false if value {
} activityIndicator.startAnimating()
} else {
private func startAnimatingActivityIndicator() { activityIndicator.stopAnimating()
activityIndicator.isHidden = false }
activityIndicator.startAnimating()
}
private func stopAnimtatingActivityIndicator() {
self.activityIndicator.isHidden = true
self.activityIndicator.stopAnimating()
} }
} }

View File

@ -79,17 +79,16 @@ class FeedbinAccountViewController: UITableViewController {
showError(NSLocalizedString("Username & password required.", comment: "Credentials Error")) showError(NSLocalizedString("Username & password required.", comment: "Credentials Error"))
return return
} }
resignFirstResponder()
startAnimatingActivityIndicator() toggleActivityIndicatorAnimation(visible: true)
disableNavigation() setNavigationEnabled(to: false)
// When you fill in the email address via auto-complete it adds extra whitespace // When you fill in the email address via auto-complete it adds extra whitespace
let trimmedEmail = email.trimmingCharacters(in: .whitespaces) let trimmedEmail = email.trimmingCharacters(in: .whitespaces)
let credentials = Credentials(type: .basic, username: trimmedEmail, secret: password) let credentials = Credentials(type: .basic, username: trimmedEmail, secret: password)
Account.validateCredentials(type: .feedbin, credentials: credentials) { result in Account.validateCredentials(type: .feedbin, credentials: credentials) { result in
self.toggleActivityIndicatorAnimation(visible: false)
self.stopAnimtatingActivityIndicator() self.setNavigationEnabled(to: true)
self.enableNavigation()
switch result { switch result {
case .success(let credentials): case .success(let credentials):
@ -138,27 +137,21 @@ class FeedbinAccountViewController: UITableViewController {
} }
private func showError(_ message: String) { private func showError(_ message: String) {
presentError(title: "Error", message: message) presentError(title: NSLocalizedString("Error", comment: "Credentials Error"), message: message)
} }
private func enableNavigation() { private func setNavigationEnabled(to value:Bool){
self.cancelBarButtonItem.isEnabled = true cancelBarButtonItem.isEnabled = value
self.actionButton.isEnabled = true actionButton.isEnabled = value
} }
private func disableNavigation() { private func toggleActivityIndicatorAnimation(visible value: Bool){
cancelBarButtonItem.isEnabled = false activityIndicator.isHidden = !value
actionButton.isEnabled = false if value {
} activityIndicator.startAnimating()
} else {
private func startAnimatingActivityIndicator() { activityIndicator.stopAnimating()
activityIndicator.isHidden = false }
activityIndicator.startAnimating()
}
private func stopAnimtatingActivityIndicator() {
self.activityIndicator.isHidden = true
self.activityIndicator.stopAnimating()
} }
} }