<commit_before>//
//  NetworkVC.swift
//  TapglueSample
//
//  Created by <NAME> on 21/12/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import UIKit
import Tapglue
import Contacts
import TwitterKit
import FBSDKLoginKit
import FBSDKCoreKit

class NetworkVC: UIViewController, UITableViewDelegate {
    
    @IBOutlet weak var friendsTableView: UITableView!
    
    let defaults = NSUserDefaults.standardUserDefaults()
    
    var users: [TGUser] = []
    var fromUsers: [TGUser] = []
    var toUsers: [TGUser] = []
    
    var resultSearchController: UISearchController!
    
    let contactStore = CNContactStore()
    var contactEmails: [String] = []
    var contacts: [[String:String]] = []
    
    var searchingForUser = false
    
    var sections = ["Find Friends", "Pending Requests"]
    var networks = ["Contacts", "Facebook", "Twitter"]
    let networkImages = ["AddressBookFilled", "FacebookFilled", "TwitterFilled"]

    override func viewDidLoad() {
        super.viewDidLoad()


        self.resultSearchController = ({
            let controller = UISearchController(searchResultsController: nil)
            controller.searchResultsUpdater = self
            controller.dimsBackgroundDuringPresentation = false
            controller.searchBar.sizeToFit()
            
            self.friendsTableView.tableHeaderView = controller.searchBar
            
            return controller
        })()
        
        // Setup searchBar
        resultSearchController.searchResultsUpdater = self
        resultSearchController.hidesNavigationBarDuringPresentation = false
        resultSearchController.dimsBackgroundDuringPresentation = false
        resultSearchController.searchBar.sizeToFit()
        resultSearchController.searchBar.searchBarStyle = .Minimal
        resultSearchController.searchBar.placeholder = "search"
        self.friendsTableView.tableHeaderView = resultSearchController.searchBar
    }
    
    override func viewWillAppear(animated: Bool) {
        clearUsersArrayAndReloadTableView()
        checkForPendingConnections()
        
        // Reload the TableView
        reloadTableViewWithAnimation()
        
        self.resultSearchController.searchBar.hidden = false

    }
    
    override func viewDidDisappear(animated: Bool) {
        self.resultSearchController.searchBar.hidden = true
        self.resultSearchController.active = false
    }

    
    func clearUsersArrayAndReloadTableView(){
        self.fromUsers.removeAll(keepCapacity: false)
        self.toUsers.removeAll(keepCapacity: false)
        self.users.removeAll(keepCapacity: false)
        
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            self.friendsTableView.reloadData()
        })
    }
    
    func checkForPendingConnections(){
        
        Tapglue.retrievePendingConncetionsForCurrentUserWithCompletionBlock { (incoming: [AnyObject]!, outgoing: [AnyObject]!, error: NSError!) -> Void in
            if error != nil {
                print("\nError retrievePendingConncetionsForCurrentUser: \(error)")
            } else {
                for inc in incoming {
                    self.fromUsers.append((inc as! TGConnection).fromUser)
                }
                for out in outgoing {
                    self.toUsers.append((out as! TGConnection).toUser)
                }
                print("\nFrom: \(self.fromUsers)")
                print("\nTo: \(self.toUsers)")<commit_msg>New Network buttons<commit_after>//
//  NetworkVC.swift
//  TapglueSample
//
//  Created by <NAME> on 21/12/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import UIKit
import Tapglue
import Contacts
import TwitterKit
import FBSDKLoginKit
import FBSDKCoreKit

class NetworkVC: UIViewController, UITableViewDelegate {
    
    @IBOutlet weak var friendsTableView: UITableView!
    
    let defaults = NSUserDefaults.standardUserDefaults()
    
    var users: [TGUser] = []
    var fromUsers: [TGUser] = []
    var toUsers: [TGUser] = []
    
    var resultSearchController: UISearchController!
    
    let contactStore = CNContactStore()
    var contactEmails: [String] = []
    var contacts: [[String:String]] = []
    
    var searchingForUser = false
    
    var sections = ["Find Friends", "Pending Requests"]
    var networks = ["Contacts", "Facebook", "Twitter"]
    let networkImages = ["AddressBookFilled", "FacebookFilled", "TwitterFilled"]

    override func viewDidLoad() {
        super.viewDidLoad()


        self.resultSearchController = ({
            let controller = UISearchController(searchResultsController: nil)
            controller.searchResultsUpdater = self
            controller.dimsBackgroundDuringPresentation = false
            controller.searchBar.sizeToFit()
            
            self.friendsTableView.tableHeaderView = controller.searchBar
            
            return controller
        })()
        
        // Setup searchBar
        resultSearchController.searchResultsUpdater = self
        resultSearchController.hidesNavigationBarDuringPresentation = false
        resultSearchController.dimsBackgroundDuringPresentation = false
        resultSearchController.searchBar.sizeToFit()
        resultSearchController.searchBar.searchBarStyle = .Minimal
        resultSearchController.searchBar.placeholder = "search"
        self.friendsTableView.tableHeaderView = resultSearchController.searchBar
    }
    
    override func viewWillAppear(animated: Bool) {
        clearUsersArrayAndReloadTableView()
        checkForPendingConnections()
        
        // Reload the TableView
        reloadTableViewWithAnimation()
        
        self.resultSearchController.searchBar.hidden = false

    }
    
    override func viewDidDisappear(animated: Bool) {
        self.resultSearchController.searchBar.hidden = true
        self.resultSearchController.active = false
    }
    
    @IBAction func conctactsBtnPressed(sender: UIButton) {
        let storyboard = UIStoryboard(name: "FindUsers", bundle: nil)
        let fuVC = storyboard.instantiateViewControllerWithIdentifier("FindUsersViewController")
                as! FindUsersVC
        //TO-DO
        fuVC.currentSelectedNetwork = "Contacts"
        self.navigationController!.pushViewController(fuVC, animated: true)
    }
    
    @IBAction func facebookBtnPressed(sender: UIButton) {
        
    }
    
    @IBAction func twitterBtnPressed(sender: UIButton) {
    }

    
    func clearUsersArrayAndReloadTableView(){
        self.fromUsers.removeAll(keepCapacity: false)
        self.toUsers.removeAll(keepCapacity: false)
        self.users.removeAll(keepCapacity: false)
        
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            self.friendsTableView.reloadData()
        })
    }
    
    func checkForPendingConnections(){
        
        Tapglue.retrievePendingConncetionsForCurrentUserWithCompletionBlock { (incoming: [AnyObject]!, outgoing: [AnyObject]!, error: NSError!) -> Void in
            if error != nil {
                print("\nError retrievePendingConncetionsForCurrentUser: \(error)")
            } else {
                for inc in incoming {
                    self.fromUsers.append((inc as! TGConnection).fromUser)
                }
                for out in outgoing {
                    self.toUsers.append((out as! TGConnection).toUser)
                }
                print("\nFrom: \(self.fromUsers)")
                print("\nTo: \(self.toUsers)")
{'fraction_non_alphanumeric': 0.08653333333333334, 'fraction_numerical': 0.0026666666666666666, 'mean_word_length': 2.4920856610800746, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6317777', 'n_tokens_mistral': 2060, 'n_tokens_neox': 1942, 'n_words': 390}