<commit_before>//
//  CreateNewPostController.swift
//  EnglishNow
//
//  Created by <NAME> on 6/14/17.
//  Copyright © 2017 IceTeaViet. All rights reserved.
//

import UIKit
import Firebase
import FirebaseDatabase
import FirebaseStorage

class CreateNewPostController: UIViewController , UIImagePickerControllerDelegate , UINavigationControllerDelegate {


    //MARK: -declare
    @IBOutlet var avatar: UIImageView!
    
    @IBOutlet var username: UILabel!
    
    @IBOutlet var txtContent: UITextField!
    
    @IBOutlet var viewAddPhoto: UIView!
    
    @IBOutlet var btnAddPhoto: UIImageView!
    
    
    @IBOutlet var choosenImage: UIImageView!
    
    @IBOutlet var viewInput: UIView!
    
    var selectedImageFromPicker: UIImage?

    var avatarUrl: String?
    
    
    //MARK: -handle post event
    @IBAction func btnPost(_ sender: UIBarButtonItem) {
        
        if txtContent.text != ""{
            let userRef = Database.database().reference().child("users").child(Auth.auth().currentUser!.uid)
            
            var databaseRef = Database.database().reference()
            let storageRef = Storage.storage().reference()
            var currentUser = Auth.auth().currentUser
            
            if selectedImageFromPicker != nil {
                if let imageData:NSData = UIImagePNGRepresentation(selectedImageFromPicker!)! as NSData?{
                    let profilePicStorageRef = storageRef.child("user_profiles/\(currentUser?.uid)/profile_pic")
                    let uploadTask = profilePicStorageRef.putData(imageData as Data, metadata:nil){metadata, error in
                        if (error == nil){
                            let downloadUrl = metadata?.downloadURL()
                            
                            //save to status root
                            let statusRef = databaseRef.child("status").childByAutoId()
                            
                            statusRef.child("photo").setValue(downloadUrl?.absoluteString)
                            statusRef.child("user").setValue(currentUser?.uid)
                            statusRef.child("content").setValue(self.txtContent.text)
                            statusRef.child("time").setValue(Date.timeIntervalBetween1970AndReferenceDate)
                            statusRef.child("like_number").setValue(0)
                            statusRef.child("username").setValue(self.username.text)
                            statusRef.child("avatar").setValue(self.avatarUrl)
                            //save to user_root
                            let userRef = databaseRef.child("user_profile").child((currentUser?.uid)!).child("status").childByAutoId()
                            userRef.child("photo").setValue(downloadUrl?.absoluteString)
                            userRef.child("content").setValue(self.txtContent.text)
                            userRef.child("time").setValue(Date.timeIntervalBetween1970AndReferenceDate)
                            userRef.child("like_number").setValue(0)

                            self.performSegue(withIdentifier: "SegueAfterPost", sender: nil)
                        
                            
                        }else{
                            print(error?.localizedDescription)
                        }
                    }
                }

            } else {
                
                let statusRef = databaseRef.child("status").childByAutoId()
                
                statusRef.child("photo").setValue("")
                statusRef.child("user").setValue(currentUser?.uid)
                statusRef.child("content").setValue(self.txtContent.text)
                statusRef.child("time").setValue(Date.timeIntervalBetween1970AndReferenceDate)
                statusRef.child("like_number").setValue(0)
                statusRef.child("username").setValue(username.text)
                statusRef.child("avatar").setValue(avatarUrl)
                
                let x = statusRef.key
                
                let userRef = databaseRef.child("user_profile").child((currentUser?.uid)!).child("status").child(x)
                userRef.child("photo").setValue("")
                userRef.child("content").setValue(self.txtContent.text)
                userRef.child("time").setValue(Date.timeIntervalBetween1970AndReferenceDate)
                userRef.child("like_number").setValue(0)
                
                self.performSegue(withIdentifier: "SegueAfterPost", sender: nil)

            }
            
        }else {
            let alert = UIAlertController(title: "Error", message: "Content is empty, post failed!", preferredStyle: .actionSheet)
            alert.addAction(UIAlertAction(title: "Ok", style: .default) { action in
                alert.dismiss(animated: true, completion: nil)
            })
            present(alert, animated: true, completion: nil)
            print("post success!")<commit_msg>update avatar in status when user update profile<commit_after>//
//  CreateNewPostController.swift
//  EnglishNow
//
//  Created by <NAME> on 6/14/17.
//  Copyright © 2017 IceTeaViet. All rights reserved.
//

import UIKit
import Firebase
import FirebaseDatabase
import FirebaseStorage

class CreateNewPostController: UIViewController , UIImagePickerControllerDelegate , UINavigationControllerDelegate {


    //MARK: -declare
    @IBOutlet var avatar: UIImageView!
    
    @IBOutlet var username: UILabel!
    
    @IBOutlet var txtContent: UITextField!
    
    @IBOutlet var viewAddPhoto: UIView!
    
    @IBOutlet var btnAddPhoto: UIImageView!
    
    
    @IBOutlet var choosenImage: UIImageView!
    
    @IBOutlet var viewInput: UIView!
    
    var selectedImageFromPicker: UIImage?

    var avatarUrl: String?
    
    
    //MARK: -handle post event
    @IBAction func btnPost(_ sender: UIBarButtonItem) {
        
        if txtContent.text != ""{
            let userRef = Database.database().reference().child("users").child(Auth.auth().currentUser!.uid)
            
            var databaseRef = Database.database().reference()
            let storageRef = Storage.storage().reference()
            var currentUser = Auth.auth().currentUser
            
            if selectedImageFromPicker != nil {
                if let imageData:NSData = UIImagePNGRepresentation(selectedImageFromPicker!)! as NSData?{
                    let profilePicStorageRef = storageRef.child("user_profiles/\(currentUser?.uid)/profile_pic")
                    let uploadTask = profilePicStorageRef.putData(imageData as Data, metadata:nil){metadata, error in
                        if (error == nil){
                            let downloadUrl = metadata?.downloadURL()
                            
                            //save to status root
                            let statusRef = databaseRef.child("status").childByAutoId()
                            
                            statusRef.child("photo").setValue(downloadUrl?.absoluteString)
                            statusRef.child("user").setValue(currentUser?.uid)
                            statusRef.child("content").setValue(self.txtContent.text)
                            statusRef.child("time").setValue(Date.timeIntervalBetween1970AndReferenceDate)
                            statusRef.child("like_number").setValue(0)
                            statusRef.child("username").setValue(self.username.text)
                            //save to user_root
                            let userRef = databaseRef.child("user_profile").child((currentUser?.uid)!).child("status").childByAutoId()
                            userRef.child("photo").setValue(downloadUrl?.absoluteString)
                            userRef.child("content").setValue(self.txtContent.text)
                            userRef.child("time").setValue(Date.timeIntervalBetween1970AndReferenceDate)
                            userRef.child("like_number").setValue(0)

                            self.performSegue(withIdentifier: "SegueAfterPost", sender: nil)
                        
                            
                        }else{
                            print(error?.localizedDescription)
                        }
                    }
                }

            } else {
                
                let statusRef = databaseRef.child("status").childByAutoId()
                
                statusRef.child("photo").setValue("")
                statusRef.child("user").setValue(currentUser?.uid)
                statusRef.child("content").setValue(self.txtContent.text)
                statusRef.child("time").setValue(Date.timeIntervalBetween1970AndReferenceDate)
                statusRef.child("like_number").setValue(0)
                statusRef.child("username").setValue(username.text)
                
                let x = statusRef.key
                
                let userRef = databaseRef.child("user_profile").child((currentUser?.uid)!).child("status").child(x)
                userRef.child("photo").setValue("")
                userRef.child("content").setValue(self.txtContent.text)
                userRef.child("time").setValue(Date.timeIntervalBetween1970AndReferenceDate)
                userRef.child("like_number").setValue(0)
                
                self.performSegue(withIdentifier: "SegueAfterPost", sender: nil)

            }
            
        }else {
            let alert = UIAlertController(title: "Error", message: "Content is empty, post failed!", preferredStyle: .actionSheet)
            alert.addAction(UIAlertAction(title: "Ok", style: .default) { action in
                alert.dismiss(animated: true, completion: nil)
            })
            present(alert, animated: true, completion: nil)
            print("post success!")
{'fraction_non_alphanumeric': 0.10061037639877925, 'fraction_numerical': 0.00590030518819939, 'mean_word_length': 1.7468566638725902, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4440867', 'n_tokens_mistral': 2449, 'n_tokens_neox': 2359, 'n_words': 400}