<commit_before>//
//  UploadPhotoViewController.swift
//  chopchop
//
//  Created by <NAME> on 14/02/2017.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit
import Firebase
import os.log

class UploadPhotoViewController: UIViewController , UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    //MARK: Properties
    
    @IBOutlet weak var captionTextField: UITextField!
    @IBOutlet weak var photoImageView: UIImageView!
    @IBOutlet weak var doneButton: UIBarButtonItem!
    
    private var captionText: String!
    
    var feed: FeedItem?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        captionTextField.delegate = self
        doneButton.isEnabled = false
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        super.prepare(for: segue, sender: sender)
        
        guard let button = sender as? UIBarButtonItem, button === doneButton else {
            os_log("The done button was not pressed", log: .default, type: .debug)
            return
        }
        
        let caption = captionTextField.text ?? ""
        
        guard let uid = FIRAuth.auth()?.currentUser?.uid else {
            return
        }
        
        let currentDateTime = Date()
        
        // initialize the date formatter and set the style
        let formatter = DateFormatter()
        formatter.timeStyle = .medium
        formatter.dateStyle = .long
        
        // get the date time String from the date object
        formatter.string(from: currentDateTime)
        
        if photoImageView.image != nil {
            
            Model.instance.saveImage(image: photoImageView.image!, name:self.captionText){(url) in
                
                let feed = FeedItem(id: "", userId: uid, author: (FIRAuth.auth()?.currentUser?.displayName)!, date: currentDateTime, imageUrl: url!, location: caption, likesCount: 0, isLikeClicked: false)
                
                Model.instance.addFeedItem(fi: feed)
                
                self.navigationController!.popViewController(animated: true)
            }
        }
    }
    
    @IBAction func cancel(_ sender: UIBarButtonItem) {
        dismiss(animated: true, completion: nil)
    }

    
    //MARK: UITextFieldDelegate
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        //Hide the keyboard
        textField.resignFirstResponder()
        return true
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        captionText = textField.text
    }
    
    //MARK: Actions
    
    @IBAction func selectImageFromLibrary(_ sender: UITapGestureRecognizer) {
        captionTextField.resignFirstResponder()
        let imagePickerController = UIImagePickerController()
        imagePickerController.sourceType = .photoLibrary
        imagePickerController.delegate = self
        present(imagePickerController, animated: true, completion: nil)
    }
    
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true, completion: nil)
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
            fatalError("Error with photo selected")
        }
        
        self.photoImageView.image = selectedImage
        doneButton.isEnabled = true
        dismiss(animated: true, completion: nil)
    }

}<commit_msg>ME: attempt to fix UIImage format error<commit_after>//
//  UploadPhotoViewController.swift
//  chopchop
//
//  Created by <NAME> on 14/02/2017.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit
import Firebase
import os.log

class UploadPhotoViewController: UIViewController , UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    //MARK: Properties
    
    @IBOutlet weak var captionTextField: UITextField!
    @IBOutlet weak var photoImageView: UIImageView!
    @IBOutlet weak var doneButton: UIBarButtonItem!
    
    private var captionText: String!
    
    var feed: FeedItem?
    var image : UIImage?
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        captionTextField.delegate = self
        doneButton.isEnabled = false
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func done(_ sender: UIBarButtonItem) {
        
        if image != nil {
            Model.instance.saveImage(image: image!) {(url) in
                
                let caption = self.captionTextField.text ?? ""
                let currentDateTime = Date()
                
                // initialize the date formatter and set the style
                let formatter = DateFormatter()
                formatter.timeStyle = .medium
                formatter.dateStyle = .long
                
                // get the date time String from the date object
                formatter.string(from: currentDateTime)
                
                let feedItem = FeedItem(id: "",userId: (Model.instance.currentUser?.id)!, author: (Model.instance.currentUser?.name)!, date: currentDateTime, imageUrl: url!, location: caption, likesCount: 0, isLikeClicked: false)
                
                Model.instance.addFeedItem(fi: feedItem)
                
                self.navigationController!.popViewController(animated: true)
            }
        }else{
            self.navigationController!.popViewController(animated: true)
        }
    }

    
    @IBAction func cancel(_ sender: UIBarButtonItem) {
        dismiss(animated: true, completion: nil)
    }
    
    
    //MARK: UITextFieldDelegate
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        //Hide the keyboard
        textField.resignFirstResponder()
        return true
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        captionText = textField.text
    }
    
    //MARK: Actions
    
    @IBAction func selectImageFromLibrary(_ sender: UITapGestureRecognizer) {
        captionTextField.resignFirstResponder()
        let imagePickerController = UIImagePickerController()
        imagePickerController.sourceType = .photoLibrary
        imagePickerController.delegate = self
        present(imagePickerController, animated: true, completion: nil)
    }
    
    
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true, completion: nil)
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
            fatalError("Error with photo selected")
        }
        
        self.photoImageView.image = selectedImage
        image = selectedImage
        
        doneButton.isEnabled = true
        dismiss(animated: true, completion: nil)
    }
    
    
    
}
{'fraction_non_alphanumeric': 0.07825023773943758, 'fraction_numerical': 0.0035321287868496126, 'mean_word_length': 2.2575221238938052, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6195541', 'n_tokens_mistral': 1995, 'n_tokens_neox': 1875, 'n_words': 484}