<commit_before>//
//  FollowingsGistsTableTableViewController.swift
//  Gistan
//
//  Created by <NAME> on 2017/10/01.
//  Copyright © 2017 miso. All rights reserved.
//

import UIKit
import SafariServices
import Nuke

class FollowingsGistsTableTableViewController: UITableViewController {

    private var followingUsers: [User] = []
    private var gistItems: [GistItem] = []
    private var isFirstAppeared = false

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationController!.navigationBar.prefersLargeTitles = true

        let nib = UINib(nibName: "GistItemCell", bundle: nil)
        self.tableView?.register(nib, forCellReuseIdentifier: "ItemCell")

        self.refreshControl = UIRefreshControl()
        self.refreshControl?.addTarget(self, action: #selector(refresh(sender:)), for: .valueChanged)

        //セルの高さを自動調整にする
        self.tableView.estimatedRowHeight = 30
        self.tableView.rowHeight = UITableViewAutomaticDimension
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        if !isFirstAppeared {
            isFirstAppeared = true
            self.refreshControl!.beginRefreshing()
            refreshData()
        }
    }

    private func refreshData() {
        if let userName = KeychainService.get(forKey: .userName) {
            load(userName: userName) {
                self.tableView.reloadData()
                self.refreshControl!.endRefreshing()
            }
        }
    }

    @objc func refresh(sender: UIRefreshControl) {
        self.gistItems = []
        self.followingUsers = []
        refreshData()
    }

    // UserのFollowsのGistを読み込む
    func load(userName: String,
              loadComplete: @escaping () -> Void) {
        let client = GitHubClient()
        let request = GitHubAPI.GetUsersFollowing(userName: userName)

        client.send(request: request) { result in
            switch result {
            case let .success(response):
                self.followingUsers = response
                self.loadFollowingUsersGists(client: client, loadComplete: loadComplete)
            case .failure(let error):
                logger.error(error)
                DispatchQueue.main.async {
                    loadComplete()
                }
            }
        }
    }
    // FollowsのGistsを読み込む
    func loadFollowingUsersGists(client: GitHubClient,
                                 loadComplete: @escaping () -> Void) {

        var loadedUserCount = 0

        for user in followingUsers {
            let request = GitHubAPI.GetUsersGists(userName: user.login)
            client.send(request: request) { result in
                switch result {
                case let .success(response):<commit_msg>fix bug: no following users case<commit_after>//
//  FollowingsGistsTableTableViewController.swift
//  Gistan
//
//  Created by <NAME> on 2017/10/01.
//  Copyright © 2017 miso. All rights reserved.
//

import UIKit
import SafariServices
import Nuke

class FollowingsGistsTableTableViewController: UITableViewController {

    private var followingUsers: [User] = []
    private var gistItems: [GistItem] = []
    private var isFirstAppeared = false

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationController!.navigationBar.prefersLargeTitles = true

        let nib = UINib(nibName: "GistItemCell", bundle: nil)
        self.tableView?.register(nib, forCellReuseIdentifier: "ItemCell")

        self.refreshControl = UIRefreshControl()
        self.refreshControl?.addTarget(self, action: #selector(refresh(sender:)), for: .valueChanged)

        //セルの高さを自動調整にする
        self.tableView.estimatedRowHeight = 30
        self.tableView.rowHeight = UITableViewAutomaticDimension
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        if !isFirstAppeared {
            isFirstAppeared = true
            self.refreshControl!.beginRefreshing()
            refreshData()
        }
    }

    private func refreshData() {
        if let userName = KeychainService.get(forKey: .userName) {
            load(userName: userName) {
                self.tableView.reloadData()
                self.refreshControl!.endRefreshing()
            }
        }
    }

    @objc func refresh(sender: UIRefreshControl) {
        self.gistItems = []
        self.followingUsers = []
        refreshData()
    }

    // UserのFollowsのGistを読み込む
    func load(userName: String,
              loadComplete: @escaping () -> Void) {
        let client = GitHubClient()
        let request = GitHubAPI.GetUsersFollowing(userName: userName)

        client.send(request: request) { result in
            switch result {
            case let .success(response):
                self.followingUsers = response
                self.loadFollowingUsersGists(client: client, loadComplete: loadComplete)
            case .failure(let error):
                logger.error(error)
                DispatchQueue.main.async {
                    loadComplete()
                }
            }
        }
    }
    // FollowsのGistsを読み込む
    func loadFollowingUsersGists(client: GitHubClient,
                                 loadComplete: @escaping () -> Void) {

        if followingUsers.count == 0 {
            loadComplete()
            return
        }

        var loadedUserCount = 0

        for user in followingUsers {
            let request = GitHubAPI.GetUsersGists(userName: user.login)
            client.send(request: request) { result in
                switch result {
                case let .success(response):
{'fraction_non_alphanumeric': 0.08808382170129639, 'fraction_numerical': 0.005505238856331024, 'mean_word_length': 2.064200217627856, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4739556', 'n_tokens_mistral': 1618, 'n_tokens_neox': 1511, 'n_words': 321}