<commit_before>//
//  CampaignsCollectionViewController.swift
//  euajudo
//
//  Created by <NAME> on 4/11/15.
//  Copyright (c) 2015 euajudo. All rights reserved.
//

import UIKit


let kCellCampaignIdentifier = "CellCampaign"


class CampaignsCollectionViewController: UICollectionViewController {

    var campaigns = [Campaign]()
    
    @IBOutlet weak var buttonProfile: UIBarButtonItem!
    @IBOutlet weak var buttonSettings: UIBarButtonItem!
    
    lazy var refreshControl: UIRefreshControl = {
        let refresh = UIRefreshControl()
        refresh.addTarget(self, action: "refreshControlUpdateStatus:", forControlEvents: .ValueChanged)
        return refresh
    }()
    
    
    // MARK: View Life Cycle
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Register cell in collectionView
        collectionView?.registerNib(UINib(nibName: "CampaignCell", bundle: NSBundle.mainBundle()), forCellWithReuseIdentifier: kCellCampaignIdentifier)
        
        // Refresh data
        collectionView?.addSubview(refreshControl)
        reloadCampaigns()
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "Campaign" {
            if let campaign = sender as? Campaign, controller = segue.destinationViewController as? CampaignDetailViewController {
                controller.campaign = campaign
            }
        }
    }
    
    
    // MARK: Data
    
    func reloadCampaigns() {
        campaigns = [Campaign]()
        refreshControl.beginRefreshing()
        
        unowned let weakSelf = self
        API.sharedInstance.allCampaings { (campaignList, error) -> Void in
            weakSelf.campaigns += campaignList as! [Campaign]
            weakSelf.collectionView!.reloadData()
            weakSelf.refreshControl.endRefreshing()
        }
    }
    
    
    // MARK: RefreshControl
    
    func refreshControlUpdateStatus(refreshControl: UIRefreshControl) {
        if refreshControl.refreshing {
            reloadCampaigns()
        }
    }
    
    // MARK: - Navigation
    
    func showAuth() {
        let auth = self.storyboard!.instantiateViewControllerWithIdentifier("Auth") as! UINavigationController
        self.presentViewController(auth, animated: true, completion: nil)
    }
    
    
    // MARK: - IBAction
    
    @IBAction func buttonProfilePressed(sender: AnyObject) {
        showAuth()
    }
    
    @IBAction func buttonSettingsPressed(sender: AnyObject) {
        
    }
    
}


// MARK: UICollectionViewDataSource

extension CampaignsCollectionViewController: UICollectionViewDataSource {
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return campaigns.count
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(kCellCampaignIdentifier, forIndexPath: indexPath) as! CampaignCell
        
        let campaign = campaigns[indexPath.row]
        cell.campaign = campaign
        cell.updateInformations()
        
        return cell<commit_msg>Do not open auth controller if user is logged in<commit_after>//
//  CampaignsCollectionViewController.swift
//  euajudo
//
//  Created by <NAME> on 4/11/15.
//  Copyright (c) 2015 euajudo. All rights reserved.
//

import UIKit


let kCellCampaignIdentifier = "CellCampaign"


class CampaignsCollectionViewController: UICollectionViewController {

    var campaigns = [Campaign]()
    
    @IBOutlet weak var buttonProfile: UIBarButtonItem!
    @IBOutlet weak var buttonSettings: UIBarButtonItem!
    
    lazy var refreshControl: UIRefreshControl = {
        let refresh = UIRefreshControl()
        refresh.addTarget(self, action: "refreshControlUpdateStatus:", forControlEvents: .ValueChanged)
        return refresh
    }()
    
    
    // MARK: View Life Cycle
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Register cell in collectionView
        collectionView?.registerNib(UINib(nibName: "CampaignCell", bundle: NSBundle.mainBundle()), forCellWithReuseIdentifier: kCellCampaignIdentifier)
        
        // Refresh data
        collectionView?.addSubview(refreshControl)
        reloadCampaigns()
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "Campaign" {
            if let campaign = sender as? Campaign, controller = segue.destinationViewController as? CampaignDetailViewController {
                controller.campaign = campaign
            }
        }
    }
    
    
    // MARK: Data
    
    func reloadCampaigns() {
        campaigns = [Campaign]()
        refreshControl.beginRefreshing()
        
        unowned let weakSelf = self
        API.sharedInstance.allCampaings { (campaignList, error) -> Void in
            weakSelf.campaigns += campaignList as! [Campaign]
            weakSelf.collectionView!.reloadData()
            weakSelf.refreshControl.endRefreshing()
        }
    }
    
    
    // MARK: RefreshControl
    
    func refreshControlUpdateStatus(refreshControl: UIRefreshControl) {
        if refreshControl.refreshing {
            reloadCampaigns()
        }
    }
    
    // MARK: - Navigation
    
    func showAuth() {
        let auth = self.storyboard!.instantiateViewControllerWithIdentifier("Auth") as! UINavigationController
        self.presentViewController(auth, animated: true, completion: nil)
    }
    
    
    // MARK: - IBAction
    
    @IBAction func buttonProfilePressed(sender: AnyObject) {
        if !API.sharedInstance.isLogged() {
            showAuth()
        }
    }
    
    @IBAction func buttonSettingsPressed(sender: AnyObject) {
        
    }
    
}


// MARK: UICollectionViewDataSource

extension CampaignsCollectionViewController: UICollectionViewDataSource {
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return campaigns.count
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(kCellCampaignIdentifier, forIndexPath: indexPath) as! CampaignCell
        
        let campaign = campaigns[indexPath.row]
        cell.campaign = campaign
        cell.updateInformations()
        
        return cell
{'fraction_non_alphanumeric': 0.07953340402969247, 'fraction_numerical': 0.0027268595667323133, 'mean_word_length': 2.6274725274725275, 'pattern_counts': {'":': 0, '<': 5, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3433052', 'n_tokens_mistral': 1809, 'n_tokens_neox': 1764, 'n_words': 382}