<commit_before>//
//  ProfileViewController.swift
//  TwitterClientDemo
//
//  Created by <NAME> on 5/21/15.
//  Copyright (c) 2015 kitasuke. All rights reserved.
//

import UIKit

class ProfileViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet weak var profileDetailView: ProfileDetailView?
    @IBOutlet weak var tableView: UITableView?
    private var tweets = [Tweet]()
    private var loading = false

    // MARK: - Lifecycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.setupNavigationBar()
        profileDetailView?.setup()
        self.setupTableView()
        
        self.fetchTimeline(readMore: false)
    }
    
    // MARK: - UITableView
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return count(tweets)
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let tweet = tweets[indexPath.row]
        let cell = tableView.dequeueReusableCellWithIdentifier(CellName.Tweet.rawValue, forIndexPath: indexPath) as! TweetCell
        cell.setup(tweet)
        return cell
    }
    
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        // read more if needed
        if !loading && count(tweets) - 10 < indexPath.row { // TODO
//            self.fetchTimeline(readMore: true) // TODO
        }
    }
    
    // MARK: - Setup
    
    private func setupNavigationBar() {
        self.navigationItem.title = UserStore.sharedStore.currentUser?.name
    }
    
    private func setupTableView() {
        tableView?.registerNib(UINib(nibName: CellName.Tweet.rawValue, bundle: nil), forCellReuseIdentifier: CellName.Tweet.rawValue)
    }
    
    // MARK: - Fetcher
    
    private func fetchTimeline(#readMore: Bool) {
        loading = true
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), { [unowned self] () -> Void in
            let sinceID: String?
            if readMore && count(self.tweets) > 0 {
                sinceID = self.tweets.last?.id
            } else {
                sinceID = nil
            }
            TwitterStore.sharedStore.fetchTimeline ({ (result: Result<[Tweet]>) -> Void in
                UIApplication.sharedApplication().networkActivityIndicatorVisible = false
                self.loading = false
                if let error = result.error {
                    return
                }
                
                if let tweets = result.value {
                    self.tweets += tweets
                    dispatch_async(dispatch_get_main_queue(), { () -> Void in
                        self.tableView?.reloadData()
                    })
                }
            }, sinceID:sinceID)
        })
    }
}<commit_msg>Add one more error handling<commit_after>//
//  ProfileViewController.swift
//  TwitterClientDemo
//
//  Created by <NAME> on 5/21/15.
//  Copyright (c) 2015 kitasuke. All rights reserved.
//

import UIKit

class ProfileViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet weak var profileDetailView: ProfileDetailView?
    @IBOutlet weak var tableView: UITableView?
    private var tweets = [Tweet]()
    private var loading = false

    // MARK: - Lifecycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.setupNavigationBar()
        profileDetailView?.setup()
        self.setupTableView()
        
        self.fetchTimeline(readMore: false)
    }
    
    // MARK: - UITableView
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return count(tweets)
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let tweet = tweets[indexPath.row]
        let cell = tableView.dequeueReusableCellWithIdentifier(CellName.Tweet.rawValue, forIndexPath: indexPath) as! TweetCell
        cell.setup(tweet)
        return cell
    }
    
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        // read more if needed
        if !loading && count(tweets) - 10 < indexPath.row { // TODO
//            self.fetchTimeline(readMore: true) // TODO
        }
    }
    
    // MARK: - Setup
    
    private func setupNavigationBar() {
        self.navigationItem.title = UserStore.sharedStore.currentUser?.name
    }
    
    private func setupTableView() {
        tableView?.registerNib(UINib(nibName: CellName.Tweet.rawValue, bundle: nil), forCellReuseIdentifier: CellName.Tweet.rawValue)
    }
    
    // MARK: - Fetcher
    
    private func fetchTimeline(#readMore: Bool) {
        loading = true
        UIApplication.sharedApplication().networkActivityIndicatorVisible = true
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), { [unowned self] () -> Void in
            let sinceID: String?
            if readMore && count(self.tweets) > 0 {
                sinceID = self.tweets.last?.id
            } else {
                sinceID = nil
            }
            TwitterStore.sharedStore.fetchTimeline ({ (result: Result<[Tweet]>) -> Void in
                UIApplication.sharedApplication().networkActivityIndicatorVisible = false
                self.loading = false
                if let error = result.error {
                    let alertController = ConnectionAlert.Error(message: error.description).alertController
                    self.presentViewController(alertController, animated: true, completion: nil)
                    return
                }
                
                if let tweets = result.value {
                    self.tweets += tweets
                    dispatch_async(dispatch_get_main_queue(), { () -> Void in
                        self.tableView?.reloadData()
                    })
                }
            }, sinceID:sinceID)
        })
    }
}
{'fraction_non_alphanumeric': 0.08981380065717415, 'fraction_numerical': 0.004381161007667032, 'mean_word_length': 2.0745550745550747, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, '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': '3437072', 'n_tokens_mistral': 1847, 'n_tokens_neox': 1708, 'n_words': 371}