<commit_before>//
//  MainTableViewController.swift
//  HackerNews
//
//  Copyright (c) 2014 <NAME>. All rights reserved.
//  See LICENSE for licensing information.
//
//  Abstract:
//      Handles fetching and displaying posts from Hacker News.
//

import UIKit

class MainTableViewController: UITableViewController, UITableViewDataSource {
    
    // MARK: Properties

    let postCellIdentifier = "PostCell"
    let showBrowserIdentifier = "ShowBrowser"
    var posts = HNPost[]()
    
    // MARK: Lifecycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        configureRefreshControl()
        fetchPosts()
    }
    
    // MARK: Functions
    
    func configureRefreshControl() {
        let refreshControl = UIRefreshControl()
        refreshControl.addTarget(self, action: "fetchPosts", forControlEvents: .ValueChanged)
        refreshControl.attributedTitle = NSAttributedString(string: "Pull to Refresh")
        self.refreshControl = refreshControl
    }
    
    func fetchPosts() {
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true;
        
        HNManager.sharedManager().loadPostsWithFilter(.Top, completion: { posts in
            if (posts) {
                self.posts = posts as HNPost[]
                dispatch_async(dispatch_get_main_queue(), {
                    self.tableView.reloadSections(NSIndexSet(index: 0), withRowAnimation: .Fade)
                    self.refreshControl.endRefreshing()
                    UIApplication.sharedApplication().networkActivityIndicatorVisible = false
                })
            } else {
                println("Could not fetch posts!")
            }
        })
    }
    
    func stylePostCellAsRead(cell: UITableViewCell) {
        cell.textLabel.textColor = UIColor(red: 119/255.0, green: 119/255.0, blue: 119/255.0, alpha: 1)
        cell.detailTextLabel.textColor = UIColor(red: 153/255.0, green: 153/255.0, blue: 153/255.0, alpha: 1)
    }
    
    // MARK: UITableViewDataSource<commit_msg>Fix fetch error check<commit_after>//
//  MainTableViewController.swift
//  HackerNews
//
//  Copyright (c) 2014 <NAME>. All rights reserved.
//  See LICENSE for licensing information.
//
//  Abstract:
//      Handles fetching and displaying posts from Hacker News.
//

import UIKit

class MainTableViewController: UITableViewController, UITableViewDataSource {
    
    // MARK: Properties

    let postCellIdentifier = "PostCell"
    let showBrowserIdentifier = "ShowBrowser"
    var posts = HNPost[]()
    
    // MARK: Lifecycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        configureRefreshControl()
        fetchPosts()
    }
    
    // MARK: Functions
    
    func configureRefreshControl() {
        let refreshControl = UIRefreshControl()
        refreshControl.addTarget(self, action: "fetchPosts", forControlEvents: .ValueChanged)
        refreshControl.attributedTitle = NSAttributedString(string: "Pull to Refresh")
        self.refreshControl = refreshControl
    }
    
    func fetchPosts() {
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true;
        
        HNManager.sharedManager().loadPostsWithFilter(.Top, completion: { posts in
            if (posts != nil && posts.count > 0) {
                self.posts = posts as HNPost[]
                dispatch_async(dispatch_get_main_queue(), {
                    self.tableView.reloadSections(NSIndexSet(index: 0), withRowAnimation: .Fade)
                    self.refreshControl.endRefreshing()
                    UIApplication.sharedApplication().networkActivityIndicatorVisible = false
                })
            } else {
                println("Could not fetch posts!")
                self.refreshControl.endRefreshing()
                UIApplication.sharedApplication().networkActivityIndicatorVisible = false;
            }
        })
    }
    
    func stylePostCellAsRead(cell: UITableViewCell) {
        cell.textLabel.textColor = UIColor(red: 119/255.0, green: 119/255.0, blue: 119/255.0, alpha: 1)
        cell.detailTextLabel.textColor = UIColor(red: 153/255.0, green: 153/255.0, blue: 153/255.0, alpha: 1)
    }
    
    // MARK: UITableViewDataSource
{'fraction_non_alphanumeric': 0.09798816568047337, 'fraction_numerical': 0.023431952662721894, 'mean_word_length': 2.361972951471758, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, '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': '4319169', 'n_tokens_mistral': 1258, 'n_tokens_neox': 1119, 'n_words': 244}