<commit_before>//
//  RandomDeckLoadingController.swift
//  StudyBox_iOS
//
//  Created by <NAME> on 15.05.2016.
//  Copyright © 2016 BLStream. All rights reserved.
//

import UIKit
import Reachability

class RandomDeckLoadingController: StudyBoxViewController {
    
    @IBOutlet weak var loadingLabelOutlet: UILabel!
    @IBOutlet weak var retryButton: UIButton!
    var flashcards = [Flashcard]()

    var dataManager = UIApplication.appDelegate().newDataManager
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        loadingLabelOutlet.text = "Ładowanie..."
        retryButton.hidden = true
        
        if !Reachability.isConnected() {
            updateUI(message: "Nie jesteś połączony z Internetem.")
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        recieveFlashcardsAndPerformSegue()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        loadingLabelOutlet.text = "Ładowanie..."
        retryButton.hidden = true
    }
    
    @IBAction func retryAction(sender: AnyObject) {
        if !Reachability.isConnected() {
            updateUI(message: "Nie jesteś połączony z Internetem.")
        } else {
            recieveFlashcardsAndPerformSegue()
        }
    }
    
    func recieveFlashcardsAndPerformSegue() {
        
        dataManager.randomDeck({ deckResponse in
            switch deckResponse {
            case .Success(let recievedDeck):
                self.dataManager.flashcards(recievedDeck.serverID, completion: { flashcardsResponse in
                    switch flashcardsResponse {
                    case .Success(let flashcards):
                        if flashcards.isEmpty {
                            self.updateUI(message: "Otrzymano pustą talię.")
                        }
                        self.performSegueWithIdentifier("StartTest", sender: Test(deck: flashcards, testType: .Test(uint(flashcards.count))))
                        
                    case .Error(let err):
                        print(err)
                        self.updateUI(message: "Błąd pobierania fiszek.")
                    }
                })
                
            case .Error(let err):
                print(err)
                self.updateUI(message: "Błąd pobierania talii.")
            }
        })
    }
    
    func updateUI(message message: String) {
            loadingLabelOutlet.text = message
            retryButton.hidden = false
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "StartTest", let testViewController = segue.destinationViewController as? TestViewController, let testLogic = sender as? Test {
            testViewController.testLogicSource = testLogic
        }
    }
    
}<commit_msg>Fix starting test with empty deck as noticed by @osjup<commit_after>//
//  RandomDeckLoadingController.swift
//  StudyBox_iOS
//
//  Created by <NAME> on 15.05.2016.
//  Copyright © 2016 BLStream. All rights reserved.
//

import UIKit
import Reachability

class RandomDeckLoadingController: StudyBoxViewController {
    
    @IBOutlet weak var loadingLabelOutlet: UILabel!
    @IBOutlet weak var retryButton: UIButton!
    var flashcards = [Flashcard]()

    var dataManager = UIApplication.appDelegate().newDataManager
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        loadingLabelOutlet.text = "Ładowanie..."
        retryButton.hidden = true
        
        if !Reachability.isConnected() {
            updateUI(message: "Nie jesteś połączony z Internetem.")
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        recieveFlashcardsAndPerformSegue()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        loadingLabelOutlet.text = "Ładowanie..."
        retryButton.hidden = true
    }
    
    @IBAction func retryAction(sender: AnyObject) {
        if !Reachability.isConnected() {
            updateUI(message: "Nie jesteś połączony z Internetem.")
        } else {
            recieveFlashcardsAndPerformSegue()
        }
    }
    
    func recieveFlashcardsAndPerformSegue() {
        
        dataManager.randomDeck({ deckResponse in
            switch deckResponse {
            case .Success(let recievedDeck):
                self.dataManager.flashcards(recievedDeck.serverID, completion: { flashcardsResponse in
                    switch flashcardsResponse {
                    case .Success(let flashcards):
                        if flashcards.isEmpty {
                            self.updateUI(message: "Otrzymano pustą talię.")
                        } else {
                            self.performSegueWithIdentifier("StartTest", sender: Test(deck: flashcards, testType: .Test(uint(flashcards.count))))
                        }
                    case .Error(let err):
                        print(err)
                        self.updateUI(message: "Błąd pobierania fiszek.")
                    }
                })
                
            case .Error(let err):
                print(err)
                self.updateUI(message: "Błąd pobierania talii.")
            }
        })
    }
    
    func updateUI(message message: String) {
            loadingLabelOutlet.text = message
            retryButton.hidden = false
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "StartTest", let testViewController = segue.destinationViewController as? TestViewController, testLogic = sender as? Test {
            testViewController.testLogicSource = testLogic
        }
    }
    
}
{'fraction_non_alphanumeric': 0.08263185019756056, 'fraction_numerical': 0.004123002920460402, 'mean_word_length': 1.855321235899951, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6711141', 'n_tokens_mistral': 1621, 'n_tokens_neox': 1564, 'n_words': 327}