Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // Miza Martelinho
- //
- // Created by Bruno Santos on 4/27/16.
- // Copyright © 2016 Bruno Santos. All rights reserved.
- //
- import UIKit
- import Alamofire
- import SwiftyJSON
- import SVProgressHUD
- class LoginViewController: UIViewController {
- @IBOutlet var tfUsername: UITextField!
- @IBOutlet var tfPassword: UITextField!
- @IBOutlet var bottomConstraint: NSLayoutConstraint!
- @IBOutlet var inputBottomConstraint: NSLayoutConstraint!
- @IBOutlet var scrollView: UIScrollView!
- var origBottomConstraint = CGFloat(0)
- var keyboardShown = false
- override func viewDidLoad() {
- super.viewDidLoad()
- origBottomConstraint = bottomConstraint.constant
- if AppSingleton.sharedInstance.preferences.objectForKey("username") == nil || AppSingleton.sharedInstance.preferences.objectForKey("password") == nil {
- // Doesn't exist
- } else {
- let username = AppSingleton.sharedInstance.preferences.stringForKey("username") as String!
- let password = AppSingleton.sharedInstance.preferences.stringForKey("password") as String!
- tfUsername.text = username
- tfPassword.text = password
- executeLogin(username, password: password)
- }
- NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillShow), name: UIKeyboardWillShowNotification, object: nil)
- NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillHide), name: UIKeyboardWillHideNotification, object: nil)
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- }
- func executeLogin (username:String, password:String) -> Void {
- SVProgressHUD.showWithStatus("Carregando, aguarde...")
- let inputLogin = ["username": username, "password": password]
- Alamofire.request(.POST, NetworkConstants.userLoginPath, parameters: inputLogin).responseJSON { (responseData) -> Void in
- SVProgressHUD.dismiss()
- if((responseData.result.value) != nil) {
- let swiftyJsonVar = JSON(responseData.result.value!)
- print(swiftyJsonVar)
- if(swiftyJsonVar["success"]) {
- AppSingleton.sharedInstance.token = swiftyJsonVar["token"].stringValue
- AppSingleton.sharedInstance.employee = swiftyJsonVar["employee"]
- AppSingleton.sharedInstance.preferences.setValuesForKeysWithDictionary(inputLogin)
- let carsVC:CarsViewController = self.storyboard?.instantiateViewControllerWithIdentifier("CarsViewController") as! CarsViewController
- self.presentViewController(carsVC, animated: true, completion: nil)
- } else {
- let errorMessage = swiftyJsonVar["message"]
- showOkAlert("Erro ao efetuar login", message: "\(errorMessage)", view: self)
- }
- }
- }
- }
- @IBAction func onLoginClick(btnLogin: UIButton) {
- let username = tfUsername.text!
- let password = tfPassword.text!
- executeLogin(username, password: password)
- }
- func adjustingHeight(show:Bool, notification:NSNotification) {
- var userInfo = notification.userInfo!
- let keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
- let animationDurarion = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSTimeInterval
- let changeInHeight = (CGRectGetHeight(keyboardFrame) + 20) * (show ? 1 : 0)
- UIView.animateWithDuration(animationDurarion, animations: { () -> Void in
- self.bottomConstraint.constant = self.origBottomConstraint + changeInHeight
- self.inputBottomConstraint.constant = self.origBottomConstraint - 2 * changeInHeight
- })
- }
- func keyboardWillShow(notification:NSNotification) {
- adjustingHeight(true, notification: notification)
- keyboardShown = true
- }
- func keyboardWillHide(notification:NSNotification) {
- adjustingHeight(false, notification: notification)
- }
- override func viewWillDisappear(animated: Bool) {
- NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
- NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
- }
- override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
- self.view.endEditing(true)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement