Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.32 KB | None | 0 0
  1. @UIApplicationMain
  2. class AppDelegate: UIResponder, UIApplicationDelegate {
  3.  
  4. var window: UIWindow?
  5.  
  6. var currentUser: SYUser?
  7.  
  8. override init() {
  9. super.init()
  10.  
  11. FIRApp.configure()
  12. }
  13.  
  14. func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
  15.  
  16. if window == nil {
  17. let bounds = UIScreen.mainScreen().bounds
  18. let window = UIWindow(frame: bounds)
  19. self.window = window
  20. }
  21.  
  22. if let userId = FIRAuth.auth()?.currentUser?.uid {
  23. let ref = FIRDatabase.database().reference().child("users").child(userId)
  24. ref.observeSingleEventOfType(.Value, withBlock: { snapshot in
  25. self.currentUser = SYUser(snapshot: snapshot)
  26. self.window?.rootViewController = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()).instantiateInitialViewController()
  27. self.window?.makeKeyAndVisible()
  28. })
  29. } else {
  30. let rootViewController = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()).instantiateViewControllerWithIdentifier("LoginViewController")
  31. self.window?.rootViewController = rootViewController
  32. self.window?.makeKeyAndVisible()
  33. }
  34.  
  35. return true
  36. }
  37. }
  38.  
  39. class SignUpViewController: UIViewController {
  40.  
  41. @IBOutlet weak var firstName: UITextField!
  42. @IBOutlet weak var firstNameLine: UIView!
  43. @IBOutlet weak var lastName: UITextField!
  44. @IBOutlet weak var lastNameLine: UIView!
  45. @IBOutlet weak var email: UITextField!
  46. @IBOutlet weak var emailLine: UIView!
  47. @IBOutlet weak var password: UITextField!
  48. @IBOutlet weak var passwordLine: UIView!
  49. @IBOutlet weak var confirmPassword: UITextField!
  50. @IBOutlet weak var confirmPasswordLine: UIView!
  51. @IBOutlet weak var device: UITextField!
  52. @IBOutlet weak var deviceLine: UIView!
  53. @IBOutlet weak var scrollView: UIScrollView!
  54.  
  55. let appDelegate = UIApplication.sharedApplication().delegate! as! AppDelegate
  56.  
  57. override func viewWillAppear(animated: Bool) {
  58. super.viewWillAppear(animated)
  59.  
  60. registerForKeyboardNotifications()
  61. }
  62.  
  63. override func viewDidLayoutSubviews() {
  64. super.viewDidLayoutSubviews()
  65.  
  66. // Center scrollview for 5/5s screen size
  67. let offsetY = ((scrollView.contentSize.height - scrollView.bounds.height) / 2) + 10
  68. scrollView.contentOffset = CGPointMake(0, offsetY)
  69. }
  70.  
  71. override func viewWillDisappear(animated: Bool) {
  72. super.viewWillDisappear(animated)
  73.  
  74. NSNotificationCenter.defaultCenter().removeObserver(self)
  75. }
  76.  
  77. @IBAction func signUpPressed(sender: AnyObject) {
  78.  
  79. print("Sign up button pressed: (mach_absolute_time())")
  80.  
  81. guard registrationFormIsValid() else { return }
  82.  
  83. var user = [String: String]()
  84. user["firstName"] = firstName.text!
  85. user["lastName"] = lastName.text!
  86. user["email"] = email.text!
  87. user["password"] = password.text!
  88. user["device"] = device.text!
  89.  
  90. print("Start creating Account: (mach_absolute_time())")
  91.  
  92. SYAuthService.createAccount(user) { [unowned self] (error) in
  93. if error == nil {
  94. SYUserService.currentUser({ (user, error) in
  95. if error == nil {
  96. print("Sign up completed successfully: (mach_absolute_time())")
  97. self.appDelegate.currentUser = user
  98. self.appDelegate.window?.rootViewController = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()).instantiateInitialViewController()
  99. } else {
  100. print("Sign up error: (error!.message) (mach_absolute_time())")
  101. self.displayFormError(message: error!.message)
  102. }
  103. })
  104. } else {
  105. print("Sign up error: (error!.message) (mach_absolute_time())")
  106. self.displayFormError(message: error!.message)
  107. }
  108. }
  109.  
  110. print("Waiting for completion: (mach_absolute_time())")
  111.  
  112. }
  113.  
  114. override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  115. view.endEditing(true)
  116. }
  117.  
  118. func registerForKeyboardNotifications() {
  119. let notificationCenter = NSNotificationCenter.defaultCenter()
  120. notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIKeyboardWillHideNotification, object: nil)
  121. notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIKeyboardWillChangeFrameNotification, object: nil)
  122. }
  123.  
  124. func adjustForKeyboard(notification: NSNotification) {
  125. guard let userInfo = notification.userInfo else { return }
  126.  
  127. let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
  128. let keyboardViewEndFrame = view.convertRect(keyboardScreenEndFrame, fromView: view.window)
  129.  
  130. if notification.name == UIKeyboardWillHideNotification {
  131. scrollView.contentInset = UIEdgeInsetsZero
  132. } else {
  133. scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height, right: 0)
  134. }
  135.  
  136. scrollView.scrollIndicatorInsets = scrollView.contentInset
  137. }
  138.  
  139. func registrationFormIsValid() -> Bool {
  140. guard SYFormValidationHelper.fieldIsValid(firstName.text) else { displayFormError(line: firstNameLine); return false }
  141. guard SYFormValidationHelper.fieldIsValid(lastName.text) else { displayFormError(line: lastNameLine); return false }
  142. guard SYFormValidationHelper.emailIsValid(email.text) else { displayFormError(line: emailLine); return false }
  143. guard SYFormValidationHelper.passwordIsValid(password.text) else { displayFormError(line: passwordLine); return false }
  144. guard SYFormValidationHelper.passwordsMatch(password.text, password2: confirmPassword.text) else {
  145. displayFormError(line: passwordLine)
  146. displayFormError(line: confirmPasswordLine)
  147. return false
  148. }
  149. guard SYFormValidationHelper.fieldIsValid(device.text) else { displayFormError(line: deviceLine); return false }
  150. return true
  151. }
  152.  
  153. func displayFormError(message message: String) {
  154. let alert = UIAlertController(title: "Sign up error", message: message, preferredStyle: .Alert)
  155. alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
  156. self.presentViewController(alert, animated: true, completion: nil)
  157. }
  158.  
  159. func displayFormError(line line: UIView) {
  160. UIView.animateWithDuration(0.5) {
  161. line.backgroundColor = UIColor.redColor()
  162. line.alpha = 1.0
  163. }
  164.  
  165. let seconds = 1.0
  166. let delay = seconds * Double(NSEC_PER_SEC) // nanoseconds per seconds
  167. let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
  168.  
  169. dispatch_after(dispatchTime, dispatch_get_main_queue(), {
  170. UIView.animateWithDuration(0.5) {
  171. line.backgroundColor = UIColor.whiteColor()
  172. line.alpha = 0.34
  173. }
  174. })
  175. }
  176.  
  177. }
  178.  
  179. extension SignUpViewController: UITextFieldDelegate {
  180.  
  181. func textFieldDidBeginEditing(textField: UITextField) {
  182. scrollView.scrollRectToVisible(textField.frame, animated: true)
  183. }
  184.  
  185. func textFieldShouldReturn(textField: UITextField) -> Bool {
  186. switch textField {
  187. case firstName:
  188. lastName.becomeFirstResponder()
  189. break
  190. case lastName:
  191. email.becomeFirstResponder()
  192. break
  193. case email:
  194. password.becomeFirstResponder()
  195. break
  196. case password:
  197. confirmPassword.becomeFirstResponder()
  198. break
  199. case confirmPassword:
  200. device.becomeFirstResponder()
  201. break
  202. case device:
  203. device.resignFirstResponder()
  204. break
  205. default:
  206. break
  207. }
  208. return true
  209. }
  210.  
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement