51 lines
1.2 KiB
Swift
51 lines
1.2 KiB
Swift
import UIKit
|
|
|
|
class LoginViewController: UIViewController {
|
|
|
|
let viewModel = LoginViewModel()
|
|
|
|
@IBOutlet weak var loginIndicator: UIActivityIndicatorView!
|
|
|
|
@IBAction func loginFieldChanged(_ sender: UITextField) {
|
|
if sender.tag == 0 {
|
|
viewModel.username = sender.text!
|
|
} else {
|
|
viewModel.password = sender.text!
|
|
}
|
|
}
|
|
|
|
@IBAction func loginButtonClicked() {
|
|
viewModel.login()
|
|
}
|
|
|
|
override func viewDidLoad() {
|
|
viewModel.showLoginFailedAlert = { (title:String, message:String) -> Void in
|
|
DispatchQueue.main.async {
|
|
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
|
|
alertController.addAction(UIAlertAction(title: "重試", style: .cancel, handler: nil))
|
|
self.present(alertController, animated: true, completion: nil)
|
|
}
|
|
}
|
|
viewModel.loginStateChanged = { (loggingIn: Bool) -> Void in
|
|
DispatchQueue.main.async {
|
|
self.loginIndicator.setAnimating(to: loggingIn)
|
|
}
|
|
}
|
|
viewModel.loginSuccessfully = {
|
|
DispatchQueue.main.async {
|
|
self.performSegue(withIdentifier: "loginSegue", sender: nil)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
extension UIActivityIndicatorView {
|
|
func setAnimating(to animating: Bool) {
|
|
if animating {
|
|
startAnimating()
|
|
} else {
|
|
stopAnimating()
|
|
}
|
|
}
|
|
}
|