Advertisement
Guest User

Untitled

a guest
May 20th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // Miza Martelinho
  4. //
  5. // Created by Bruno Santos on 4/27/16.
  6. // Copyright © 2016 Bruno Santos. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import Alamofire
  11. import SwiftyJSON
  12. import SVProgressHUD
  13.  
  14. class LoginViewController: UIViewController {
  15.  
  16. @IBOutlet var tfUsername: UITextField!
  17. @IBOutlet var tfPassword: UITextField!
  18. @IBOutlet var bottomConstraint: NSLayoutConstraint!
  19. @IBOutlet var inputBottomConstraint: NSLayoutConstraint!
  20. @IBOutlet var scrollView: UIScrollView!
  21.  
  22. var origBottomConstraint = CGFloat(0)
  23. var keyboardShown = false
  24.  
  25. override func viewDidLoad() {
  26. super.viewDidLoad()
  27. origBottomConstraint = bottomConstraint.constant
  28.  
  29. if AppSingleton.sharedInstance.preferences.objectForKey("username") == nil || AppSingleton.sharedInstance.preferences.objectForKey("password") == nil {
  30. // Doesn't exist
  31. } else {
  32. let username = AppSingleton.sharedInstance.preferences.stringForKey("username") as String!
  33. let password = AppSingleton.sharedInstance.preferences.stringForKey("password") as String!
  34. tfUsername.text = username
  35. tfPassword.text = password
  36. executeLogin(username, password: password)
  37. }
  38.  
  39. NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillShow), name: UIKeyboardWillShowNotification, object: nil)
  40. NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillHide), name: UIKeyboardWillHideNotification, object: nil)
  41. }
  42.  
  43. override func didReceiveMemoryWarning() {
  44. super.didReceiveMemoryWarning()
  45. }
  46.  
  47. func executeLogin (username:String, password:String) -> Void {
  48.  
  49. SVProgressHUD.showWithStatus("Carregando, aguarde...")
  50.  
  51. let inputLogin = ["username": username, "password": password]
  52.  
  53. Alamofire.request(.POST, NetworkConstants.userLoginPath, parameters: inputLogin).responseJSON { (responseData) -> Void in
  54. SVProgressHUD.dismiss()
  55. if((responseData.result.value) != nil) {
  56. let swiftyJsonVar = JSON(responseData.result.value!)
  57. print(swiftyJsonVar)
  58.  
  59. if(swiftyJsonVar["success"]) {
  60. AppSingleton.sharedInstance.token = swiftyJsonVar["token"].stringValue
  61. AppSingleton.sharedInstance.employee = swiftyJsonVar["employee"]
  62. AppSingleton.sharedInstance.preferences.setValuesForKeysWithDictionary(inputLogin)
  63.  
  64. let carsVC:CarsViewController = self.storyboard?.instantiateViewControllerWithIdentifier("CarsViewController") as! CarsViewController
  65. self.presentViewController(carsVC, animated: true, completion: nil)
  66.  
  67. } else {
  68. let errorMessage = swiftyJsonVar["message"]
  69. showOkAlert("Erro ao efetuar login", message: "\(errorMessage)", view: self)
  70. }
  71.  
  72. }
  73. }
  74. }
  75.  
  76. @IBAction func onLoginClick(btnLogin: UIButton) {
  77. let username = tfUsername.text!
  78. let password = tfPassword.text!
  79. executeLogin(username, password: password)
  80. }
  81.  
  82. func adjustingHeight(show:Bool, notification:NSNotification) {
  83. var userInfo = notification.userInfo!
  84. let keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
  85. let animationDurarion = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSTimeInterval
  86. let changeInHeight = (CGRectGetHeight(keyboardFrame) + 20) * (show ? 1 : 0)
  87. UIView.animateWithDuration(animationDurarion, animations: { () -> Void in
  88. self.bottomConstraint.constant = self.origBottomConstraint + changeInHeight
  89. self.inputBottomConstraint.constant = self.origBottomConstraint - 2 * changeInHeight
  90. })
  91.  
  92. }
  93.  
  94. func keyboardWillShow(notification:NSNotification) {
  95. adjustingHeight(true, notification: notification)
  96. keyboardShown = true
  97. }
  98.  
  99. func keyboardWillHide(notification:NSNotification) {
  100. adjustingHeight(false, notification: notification)
  101. }
  102.  
  103. override func viewWillDisappear(animated: Bool) {
  104. NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
  105. NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
  106. }
  107.  
  108. override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
  109. self.view.endEditing(true)
  110. }
  111.  
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement