// // SignInController.swift // GetHip // // Created by Okechi on 2/14/16. // Copyright (c) 2016 Kroleo. All rights reserved. // import UIKit class SignInController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate { @IBOutlet var nameField: UITextField! @IBOutlet var userField: UITextField! @IBOutlet var emailField: UITextField! @IBOutlet var passField: UITextField! @IBOutlet var saveBtn: UIButton! @IBOutlet var chngPhotoBtn: UIButton! @IBOutlet var profilePic: UIImageView! private var picker = UIImagePickerController() @IBAction func saveNewProfile(sender: UIButton){ if(self.nameField.hasText() == false || self.userField.hasText() == false || self.emailField.hasText() == false || self.passField.hasText() == false || self.profilePic.image == nil){ let alert = UIAlertController(title: "Invalid Registration", message: "We're missing some information from you, before we can start the party!", preferredStyle: .Alert) alert.addAction(UIAlertAction(title: "OK", style: .Default, handler:{(action: UIAlertAction!) in alert.dismissViewControllerAnimated(true, completion: nil)})) self.presentViewController(alert, animated: true, completion: nil) }else{ if(!(contains(self.userField.text!, "@") || contains(self.nameField.text!, "@"))){ let predicate: NSPredicate = NSPredicate(format: "((username = %@) OR (email = %@))", argumentArray: [self.nameField.text!,self.emailField.text!]) var userQuery: PFQuery = PFQuery(className: "_User", predicate: predicate) dispatch_async(dispatch_get_main_queue(), { userQuery.getFirstObjectInBackgroundWithBlock({ (object, error) -> Void in if(object != nil && error == nil){ let alert = UIAlertController(title: "User Info Taken", message: "Sorry this information is already registered to another user. Please try again.", preferredStyle: .Alert) alert.addAction(UIAlertAction(title: "OK", style: .Default, handler:{(action: UIAlertAction!) in alert.dismissViewControllerAnimated(true, completion: nil)})) self.presentViewController(alert, animated: true, completion: nil) }else{ var user = PFUser() var img:PFFile = PFFile(data: UIImagePNGRepresentation(self.profilePic.image))! user.username = self.userField.text! user.password = self.passField.text! user.email = self.emailField.text! user.setObject(self.nameField.text!, forKey: "displayName") user.setObject(img, forKey: "profilePicture") //sign up new user user.signUpInBackgroundWithBlock({ (succeeded: Bool, error: NSError?) -> Void in if error == nil { self.performSegueWithIdentifier("SignedUpSegue", sender: self) } else{ } }) } }) }) }else{ let alert = UIAlertController(title: "Illegal Characters", message: "The username or email you entered contains illegal characters such as: '@'", preferredStyle: .Alert) alert.addAction(UIAlertAction(title: "OK", style: .Default, handler:{(action: UIAlertAction!) in alert.dismissViewControllerAnimated(true, completion: nil)})) self.presentViewController(alert, animated: true, completion: nil) } } } @IBAction func cancelSignUp(){ self.parentViewController?.dismissViewControllerAnimated(true, completion: nil) } @IBAction func chngePhoto(sender: UIButton){ let captureMenu = UIAlertController(title: nil, message:nil, preferredStyle: .ActionSheet) let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: { (alert: UIAlertAction!) -> Void in }) let cameraAction = UIAlertAction(title: "Take a New Pic", style: .Default, handler: { (alert: UIAlertAction!) -> Void in self.picker.allowsEditing = false self.picker.sourceType = UIImagePickerControllerSourceType.Camera self.picker.cameraCaptureMode = .Photo self.presentViewController(self.picker, animated: true, completion: nil) }) let galleryAction = UIAlertAction(title: "Select a Profile Pic", style: .Default, handler: { (alert: UIAlertAction!) -> Void in self.picker.allowsEditing = false self.picker.sourceType = .PhotoLibrary self.presentViewController(self.picker, animated: true, completion: nil) }) captureMenu.addAction(galleryAction) captureMenu.addAction(cameraAction) captureMenu.addAction(cancelAction) self.presentViewController(captureMenu, animated: true, completion: nil) } override func viewDidLoad() { super.viewDidLoad() self.navigationController?.title = "Create An Account" self.chngPhotoBtn.layer.borderWidth = 1 self.chngPhotoBtn.layer.cornerRadius = 5 self.chngPhotoBtn.layer.borderColor = UIColor.blackColor().CGColor self.picker.delegate = self self.profilePic.layer.cornerRadius = self.profilePic.frame.size.width/2 self.profilePic.backgroundColor = UIColor.grayColor() //add cancel bar to navigation bar self.navigationController?.navigationBarHidden = false self.navigationController?.navigationItem.leftBarButtonItem?.title = "Cancel" // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ } extension SignInController: UIImagePickerControllerDelegate{ //MARK: -Image Picker Delegate func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) { self.profilePic.image = image } func imagePickerControllerDidCancel(picker: UIImagePickerController) { dismissViewControllerAnimated(true, completion: nil) } }