Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @UIApplicationMain
- class AppDelegate: UIResponder, UIApplicationDelegate {
- var window: UIWindow?
- var currentUser: SYUser?
- override init() {
- super.init()
- FIRApp.configure()
- }
- func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
- if window == nil {
- let bounds = UIScreen.mainScreen().bounds
- let window = UIWindow(frame: bounds)
- self.window = window
- }
- if let userId = FIRAuth.auth()?.currentUser?.uid {
- let ref = FIRDatabase.database().reference().child("users").child(userId)
- ref.observeSingleEventOfType(.Value, withBlock: { snapshot in
- self.currentUser = SYUser(snapshot: snapshot)
- self.window?.rootViewController = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()).instantiateInitialViewController()
- self.window?.makeKeyAndVisible()
- })
- } else {
- let rootViewController = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()).instantiateViewControllerWithIdentifier("LoginViewController")
- self.window?.rootViewController = rootViewController
- self.window?.makeKeyAndVisible()
- }
- return true
- }
- }
- class SignUpViewController: UIViewController {
- @IBOutlet weak var firstName: UITextField!
- @IBOutlet weak var firstNameLine: UIView!
- @IBOutlet weak var lastName: UITextField!
- @IBOutlet weak var lastNameLine: UIView!
- @IBOutlet weak var email: UITextField!
- @IBOutlet weak var emailLine: UIView!
- @IBOutlet weak var password: UITextField!
- @IBOutlet weak var passwordLine: UIView!
- @IBOutlet weak var confirmPassword: UITextField!
- @IBOutlet weak var confirmPasswordLine: UIView!
- @IBOutlet weak var device: UITextField!
- @IBOutlet weak var deviceLine: UIView!
- @IBOutlet weak var scrollView: UIScrollView!
- let appDelegate = UIApplication.sharedApplication().delegate! as! AppDelegate
- override func viewWillAppear(animated: Bool) {
- super.viewWillAppear(animated)
- registerForKeyboardNotifications()
- }
- override func viewDidLayoutSubviews() {
- super.viewDidLayoutSubviews()
- // Center scrollview for 5/5s screen size
- let offsetY = ((scrollView.contentSize.height - scrollView.bounds.height) / 2) + 10
- scrollView.contentOffset = CGPointMake(0, offsetY)
- }
- override func viewWillDisappear(animated: Bool) {
- super.viewWillDisappear(animated)
- NSNotificationCenter.defaultCenter().removeObserver(self)
- }
- @IBAction func signUpPressed(sender: AnyObject) {
- print("Sign up button pressed: (mach_absolute_time())")
- guard registrationFormIsValid() else { return }
- var user = [String: String]()
- user["firstName"] = firstName.text!
- user["lastName"] = lastName.text!
- user["email"] = email.text!
- user["password"] = password.text!
- user["device"] = device.text!
- print("Start creating Account: (mach_absolute_time())")
- SYAuthService.createAccount(user) { [unowned self] (error) in
- if error == nil {
- SYUserService.currentUser({ (user, error) in
- if error == nil {
- print("Sign up completed successfully: (mach_absolute_time())")
- self.appDelegate.currentUser = user
- self.appDelegate.window?.rootViewController = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()).instantiateInitialViewController()
- } else {
- print("Sign up error: (error!.message) (mach_absolute_time())")
- self.displayFormError(message: error!.message)
- }
- })
- } else {
- print("Sign up error: (error!.message) (mach_absolute_time())")
- self.displayFormError(message: error!.message)
- }
- }
- print("Waiting for completion: (mach_absolute_time())")
- }
- override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
- view.endEditing(true)
- }
- func registerForKeyboardNotifications() {
- let notificationCenter = NSNotificationCenter.defaultCenter()
- notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIKeyboardWillHideNotification, object: nil)
- notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIKeyboardWillChangeFrameNotification, object: nil)
- }
- func adjustForKeyboard(notification: NSNotification) {
- guard let userInfo = notification.userInfo else { return }
- let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
- let keyboardViewEndFrame = view.convertRect(keyboardScreenEndFrame, fromView: view.window)
- if notification.name == UIKeyboardWillHideNotification {
- scrollView.contentInset = UIEdgeInsetsZero
- } else {
- scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height, right: 0)
- }
- scrollView.scrollIndicatorInsets = scrollView.contentInset
- }
- func registrationFormIsValid() -> Bool {
- guard SYFormValidationHelper.fieldIsValid(firstName.text) else { displayFormError(line: firstNameLine); return false }
- guard SYFormValidationHelper.fieldIsValid(lastName.text) else { displayFormError(line: lastNameLine); return false }
- guard SYFormValidationHelper.emailIsValid(email.text) else { displayFormError(line: emailLine); return false }
- guard SYFormValidationHelper.passwordIsValid(password.text) else { displayFormError(line: passwordLine); return false }
- guard SYFormValidationHelper.passwordsMatch(password.text, password2: confirmPassword.text) else {
- displayFormError(line: passwordLine)
- displayFormError(line: confirmPasswordLine)
- return false
- }
- guard SYFormValidationHelper.fieldIsValid(device.text) else { displayFormError(line: deviceLine); return false }
- return true
- }
- func displayFormError(message message: String) {
- let alert = UIAlertController(title: "Sign up error", message: message, preferredStyle: .Alert)
- alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
- self.presentViewController(alert, animated: true, completion: nil)
- }
- func displayFormError(line line: UIView) {
- UIView.animateWithDuration(0.5) {
- line.backgroundColor = UIColor.redColor()
- line.alpha = 1.0
- }
- let seconds = 1.0
- let delay = seconds * Double(NSEC_PER_SEC) // nanoseconds per seconds
- let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
- dispatch_after(dispatchTime, dispatch_get_main_queue(), {
- UIView.animateWithDuration(0.5) {
- line.backgroundColor = UIColor.whiteColor()
- line.alpha = 0.34
- }
- })
- }
- }
- extension SignUpViewController: UITextFieldDelegate {
- func textFieldDidBeginEditing(textField: UITextField) {
- scrollView.scrollRectToVisible(textField.frame, animated: true)
- }
- func textFieldShouldReturn(textField: UITextField) -> Bool {
- switch textField {
- case firstName:
- lastName.becomeFirstResponder()
- break
- case lastName:
- email.becomeFirstResponder()
- break
- case email:
- password.becomeFirstResponder()
- break
- case password:
- confirmPassword.becomeFirstResponder()
- break
- case confirmPassword:
- device.becomeFirstResponder()
- break
- case device:
- device.resignFirstResponder()
- break
- default:
- break
- }
- return true
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement