Skip to content

Commit

Permalink
add login error alert popup
Browse files Browse the repository at this point in the history
  • Loading branch information
Brent Mifsud committed Jun 2, 2019
1 parent 42fc3a3 commit 91b0fbe
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
12 changes: 9 additions & 3 deletions TheMovieManager/Controller/LoginViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ class LoginViewController: UIViewController {
loginButton.isEnabled = !loggingIn
loginViaWebsiteButton.isEnabled = !loggingIn
}

func showLoginFailure(message: String) {
let alertVC = UIAlertController(title: "Login Failed", message: message, preferredStyle: .alert)
alertVC.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
show(alertVC, sender: nil)
}

}

Expand All @@ -59,7 +65,7 @@ extension LoginViewController {
TMDBClient.login(username: self.emailTextField.text ?? "", password: self.passwordTextField.text ?? "", completion: self.handleLoginResponse(success:error:))
} else {
setLoggingIn(false)
print(error ?? "Get Request Token Failed")
showLoginFailure(message: error?.localizedDescription ?? "")
}
}

Expand All @@ -68,7 +74,7 @@ extension LoginViewController {
TMDBClient.getSessionId(completion: self.handleSessionIDResponse(success:error:))
} else {
setLoggingIn(false)
print(error ?? "Get Request Token Failed")
showLoginFailure(message: error?.localizedDescription ?? "")
}
}

Expand All @@ -77,7 +83,7 @@ extension LoginViewController {
if success {
self.performSegue(withIdentifier: "completeLogin", sender: nil)
} else {
print(error ?? "Get Session ID Failed")
showLoginFailure(message: error?.localizedDescription ?? "")
}
}
}
2 changes: 1 addition & 1 deletion TheMovieManager/Controller/WatchlistViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class WatchlistViewController: UIViewController {
super.viewDidLoad()

_ = TMDBClient.getWatchlist() { movies, error in
MovieModel.watchlist = movies
MovieModel.watchlist = movies
DispatchQueue.main.async {
self.tableView.reloadData()
}
Expand Down
25 changes: 19 additions & 6 deletions TheMovieManager/Model/TMDB Client/TMDBClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class TMDBClient {
Auth.requestToken = response.requestToken
completion(true, nil)
} else {
completion(false, nil)
completion(false, error)
}
}
}
Expand Down Expand Up @@ -196,10 +196,16 @@ extension TMDBClient {
completion(responseObject, nil)
}
} catch {
DispatchQueue.main.async {
completion(nil, error)
do {
let errorResponse = try decoder.decode(TMDBResponse.self, from: data) as Error
DispatchQueue.main.async {
completion(nil, errorResponse)
}
} catch {
DispatchQueue.main.async {
completion(nil, error)
}
}

}
}
task.resume()
Expand All @@ -226,8 +232,15 @@ extension TMDBClient {
completion(responseObject, nil)
}
} catch {
DispatchQueue.main.async {
completion(nil, error)
do {
let errorResponse = try decoder.decode(TMDBResponse.self, from: data) as Error
DispatchQueue.main.async {
completion(nil, errorResponse)
}
} catch {
DispatchQueue.main.async {
completion(nil, error)
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions TheMovieManager/Model/TMDB Responses/TMDBResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ struct TMDBResponse: Codable {
}

}

extension TMDBResponse: LocalizedError {
var errorDescription: String? {
return self.statusMessage
}
}

0 comments on commit 91b0fbe

Please sign in to comment.