<commit_before>//
//  HomeTimeLineVC.swift
//  EnglishNow
//
//  Created by <NAME> on 5/21/17.
//  Copyright © 2017 IceTeaViet. All rights reserved.
//

import UIKit
import Firebase
import FirebaseDatabase


class HomeTimeLineVC: UIViewController, UITableViewDataSource, UITableViewDelegate{

    //MARK: -declare
    @IBOutlet var btnMenu: UIBarButtonItem!
        
    @IBOutlet weak var table: UITableView!
    
    var statusList: [Status] = [Status]()
    
    override func viewDidLoad() {
        super.viewDidLoad()

    
        initShow()
        loadData()
        
        // Do any additional setup after loading the view.
    }

    //MARK: -hamburger menu
    override func viewDidAppear(_ animated: Bool) {
        
        if  revealViewController() != nil {
            btnMenu.target = self.revealViewController()
            btnMenu.action = "revealToggle:"
            
            self.view.addGestureRecognizer(revealViewController().panGestureRecognizer())
        }
        
        
    }
    
  //MARK: -init to show
    func initShow(){
        table.delegate = self
        table.dataSource = self
    }
    
    //MARK: -load data
    func loadData(){
            
        let queryRef = Database.database().reference().child("status").observe(.value, with: { (snapshot) -> Void in
                
            self.statusList.removeAll()
            
            //go to each status
            for item in snapshot.children {
            let status = (item as! DataSnapshot).value as! [String:AnyObject]
                
                
            // if status have enough child (it means no error appear)
            if status.count >= FirebaseUtils.numberChildStatus - 1{
                let user = status["user"] as! String
                let content = status["content"] as! String
                let time = status["time"] as! TimeInterval
                let likeNumber = status["like_number"] as! Int
                let username = status["username"] as! String
                
                var avatar: UIImage?
                var photo:UIImage?
                        
                if let url = status["photo"] {
                    if let imgUrl =  URL(string: url as! String){
                        let data = try? Data(contentsOf: imgUrl)
                                
                        if let imageData = data {
                            let profilePic = UIImage(data: data!)
                            photo = profilePic
                        }
                                
                    }
                }
                        
                if let url = status["avatar"] {
                    if let imgUrl =  URL(string: url as! String){
                        let data = try? Data(contentsOf: imgUrl)
                                
                        if let imageData = data {
                            let profilePic = UIImage(data: data!)
                            avatar = profilePic
                        }
                                
                    }
                }
                
                //if no avatar -> set default avatar
                if avatar == nil{
                    avatar = UIImage(named: "sample.jpg")
                }
                
                var isUserLiked: Bool = false
                
                if (status.index(forKey: "like") != nil) {
                    let likes = ((item as! DataSnapshot).childSnapshot(forPath: "like") as! DataSnapshot).value as! [String:AnyObject]
                    
                    if let user = Auth.auth().currentUser{
                        if (likes.index(forKey: user.uid) != nil) {
                            let isLiked = likes[user.uid]as! Bool
                            if isLiked == true {
                                isUserLiked = true
                            }
                        }
                    }

                }
                
                if photo == nil{
                    
                    //if photo is nil -> can't constructure with a nil param -> call constructure without photo
                    self.statusList.append(Status(statusId: (item as! DataSnapshot).key , user: user ,username: username, avatar: avatar!, content: content, time: time, likeNumber: likeNumber , isUserLiked: isUserLiked))
                } else {
                    
                    //call constructure with photo
                    self.statusList.append(Status(statusId: (item as! DataSnapshot).key , user: user , username: username, avatar: avatar! , content: content, time: time, photo: photo!, likeNumber: likeNumber , isUserLiked: isUserLiked))
                }
                        
                self.table.reloadData()
                        
            }<commit_msg>update avatar in status when user update profile<commit_after>//
//  HomeTimeLineVC.swift
//  EnglishNow
//
//  Created by <NAME> on 5/21/17.
//  Copyright © 2017 IceTeaViet. All rights reserved.
//

import UIKit
import Firebase
import FirebaseDatabase


class HomeTimeLineVC: UIViewController, UITableViewDataSource, UITableViewDelegate{

    //MARK: -declare
    @IBOutlet var btnMenu: UIBarButtonItem!
        
    @IBOutlet weak var table: UITableView!
    
    var statusList: [Status] = [Status]()
    
    override func viewDidLoad() {
        super.viewDidLoad()

    
        initShow()
        loadData()
        
        // Do any additional setup after loading the view.
    }

    //MARK: -hamburger menu
    override func viewDidAppear(_ animated: Bool) {
        
        if  revealViewController() != nil {
            btnMenu.target = self.revealViewController()
            btnMenu.action = "revealToggle:"
            
            self.view.addGestureRecognizer(revealViewController().panGestureRecognizer())
        }
        
        
    }
    
  //MARK: -init to show
    func initShow(){
        table.delegate = self
        table.dataSource = self
    }
    
    //MARK: -load data
    func loadData(){
            
        let queryRef = Database.database().reference().child("status").observe(.value, with: { (snapshot) -> Void in
                
            self.statusList.removeAll()
            
            //go to each status
            for item in snapshot.children {
            let status = (item as! DataSnapshot).value as! [String:AnyObject]
                
                
            // if status have enough child (it means no error appear)
            if status.count >= FirebaseUtils.numberChildStatus - 1{
                let user = status["user"] as! String
                let content = status["content"] as! String
                let time = status["time"] as! TimeInterval
                let likeNumber = status["like_number"] as! Int
                let username = status["username"] as! String
                
                var avatar: UIImage?
                var photo:UIImage?
                        
                if let url = status["photo"] {
                    if let imgUrl =  URL(string: url as! String){
                        let data = try? Data(contentsOf: imgUrl)
                                
                        if let imageData = data {
                            let profilePic = UIImage(data: data!)
                            photo = profilePic
                        }
                    }
                }
                
                let queryRef = Database.database().reference().child("user_profile/\(user)").observe(.value, with: { (snapshot) -> Void in
                    
                    if let dictionary = snapshot.value as? [String:Any] {
                        
                        if let url = dictionary["profile_pic"] {
                            if let imgUrl =  URL(string: url as! String){
                                let data = try? Data(contentsOf: imgUrl)
                                
                                if let imageData = data {
                                    let profilePic = UIImage(data: data!)
                                    avatar = profilePic
                                }
                                
                            }
                        }
                    }
                    
                    //if no avatar -> set default avatar
                    if avatar == nil{
                        avatar = UIImage(named: "sample.jpg")
                    }
                    
                    var isUserLiked: Bool = false
                    
                    if (status.index(forKey: "like") != nil) {
                        let likes = ((item as! DataSnapshot).childSnapshot(forPath: "like") as! DataSnapshot).value as! [String:AnyObject]
                        
                        if let user = Auth.auth().currentUser{
                            if (likes.index(forKey: user.uid) != nil) {
                                let isLiked = likes[user.uid]as! Bool
                                if isLiked == true {
                                    isUserLiked = true
                                }
                            }
                        }
                    }
                    
                    if photo == nil{
                        //if photo is nil -> can't constructure with a nil param -> call constructure without photo

                        var count = 0
                        var check = 0
                        for status in self.statusList{
                            if status.statusId == (item as! DataSnapshot).key {
                                check = 1
                                self.statusList.remove(at: count)
                                self.statusList.insert(Status(statusId: (item as! DataSnapshot).key , user: user ,username: username, avatar: avatar!, content: content, time: time, likeNumber: likeNumber , isUserLiked: isUserLiked), at: count)
                            }
                            
                            count += 1
                        }
                        
                        if (check == 0){
                            self.statusList.append(Status(statusId: (item as! DataSnapshot).key , user: user ,username: username, avatar: avatar!, content: content, time: time, likeNumber: likeNumber , isUserLiked: isUserLiked))
                        }
                        
                        
                    } else {
                        
                        //call constructure with photo
                        var count = 0
                        var check = 0
                        for status in self.statusList{
                            if status.statusId == (item as! DataSnapshot).key {
                                check = 1
                                self.statusList.remove(at: count)
                                self.statusList.insert(Status(statusId: (item as! DataSnapshot).key , user: user , username: username, avatar: avatar! , content: content, time: time, photo: photo!, likeNumber: likeNumber , isUserLiked: isUserLiked), at: count)
                            }
                            
                            count += 1
                        }
                        
                        if check == 0{
                            self.statusList.append(Status(statusId: (item as! DataSnapshot).key , user: user , username: username, avatar: avatar! , content: content, time: time, photo: photo!, likeNumber: likeNumber , isUserLiked: isUserLiked))
                        }
            
                    }
                    
                    self.table.reloadData()
                })
                
                
                
                
                        
            }
{'fraction_non_alphanumeric': 0.08085501858736059, 'fraction_numerical': 0.0025346400811084827, 'mean_word_length': 1.0158378746594006, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '673297', 'n_tokens_mistral': 2872, 'n_tokens_neox': 2603, 'n_words': 736}