SHARE
TWEET

Untitled

a guest Dec 29th, 2016 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class RegistrationViewController: UITableViewController {
  2.     @IBOutlet var photoLibrary: UIButton!
  3.     @IBOutlet var registration: UIButton!
  4.     @IBOutlet var avatarImageView: UIImageView!
  5.     @IBOutlet var emailTextField: UITextField!
  6.     @IBOutlet var passTextField: UITextField!
  7.     @IBOutlet var nameTextField: UITextField!
  8.     @IBOutlet var rightSwipe: UISwipeGestureRecognizer!
  9.    
  10.     fileprivate let imagePicker = UIImagePickerController()
  11.     let imageStorage = FIRStorage.storage().reference(withPath: "avatarImages")
  12.     var assetPhotoURL: URL? = nil
  13.    
  14.     // MARK: - View life cycle
  15.     override func viewDidLoad() {
  16.         super.viewDidLoad()
  17.         self.configureImagePicker()
  18.     }
  19.    
  20.     // MARK: - Actions
  21.     @IBAction func onPhotoLibraty(_ sender: UIButton) {
  22.         self.present(imagePicker, animated: true, completion: nil)
  23.     }
  24.    
  25.     @IBAction func onRegistration(_ sender: UIButton) {
  26.         guard let email = emailTextField.text, let pass = passTextField.text else {
  27.             return
  28.         }
  29.        
  30.         FIRAuth.auth()?.createUser(withEmail: email, password: pass) { (user, error) in
  31.             self.displayError(error)
  32.             user.map { AppState.instance.user = User(userData: $0) }
  33.             user.map { self.configurateUser($0) }
  34.         }
  35.     }
  36.    
  37.     @IBAction func onRightSwipe(_ sender: UISwipeGestureRecognizer) {
  38.       _ = self.navigationController?.popViewController(animated: true)
  39.     }
  40.    
  41.     // MARK: - Private methods
  42.     private func configureImagePicker() {
  43.         let picker = self.imagePicker
  44.         picker.delegate = self
  45.         picker.sourceType = .photoLibrary
  46.     }
  47.    
  48.     func configurateUser(_ user: FIRUser) {
  49.         let imageRef = self.avatarReference(fromUser: user)
  50.         let changeRequest = user.profileChangeRequest()
  51.         changeRequest.displayName = self.nameTextField.text ?? (user.email?.components(separatedBy: "@")[0])
  52.         changeRequest.photoURL = (imageRef?.fullPath).flatMap { return URL(string: $0) }
  53.         imageRef.map { self.uploadToReference($0) }
  54.        
  55.         changeRequest.commitChanges() { (error) in
  56.             self.displayError(error)
  57.             let sharedUser =  AppState.instance.user
  58.             sharedUser?.name = user.displayName
  59.             sharedUser?.photoURL = user.photoURL
  60.             //transition to anothe controller
  61.         }
  62.     }
  63.    
  64.     private func avatarReference(fromUser user: FIRUser) -> FIRStorageReference? {
  65.         guard let localULR = self.assetPhotoURL else { return nil }
  66.         return imageStorage.child(user.uid + "." + localULR.pathExtension)
  67.     }
  68.    
  69.     private func uploadToReference(_ reference : FIRStorageReference) {
  70.         guard let assetURL = self.assetPhotoURL else { return }
  71.         guard let asset = PHAsset.fetchAssets(withALAssetURLs: [assetURL], options: nil).firstObject else { return }
  72.         PHImageManager.default().requestImage(for: asset,
  73.                                         targetSize: CGSize(width: 200 , height: 200),
  74.                                        contentMode: .aspectFill,
  75.                                            options: nil)
  76.         { (photo, info) in
  77.             if let imageData = (photo.flatMap { return UIImagePNGRepresentation($0) }) {
  78.                 reference.put(imageData)
  79.             }
  80.         }
  81.     }
  82.    
  83.     private func displayError(_ error: Error?) {
  84.         if let error = error {
  85.             let controller = UIAlertController.errorController(massage: error.localizedDescription, alletAction: nil)
  86.             self.present(controller, animated: true, completion: nil)
  87.             return
  88.         }
  89.     }
  90. }
  91.  
  92. extension RegistrationViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
  93.     func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
  94.         self.avatarImageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
  95.         self.assetPhotoURL = info[UIImagePickerControllerReferenceURL] as? URL
  96.         self.dismiss(animated: true, completion: nil)
  97.     }
  98.    
  99.     func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
  100.         self.dismiss(animated: true, completion: nil)
  101.     }
  102. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top