<commit_before>//
//  IssuesManager.swift
//  FiveCalls
//
//  Created by <NAME> on 2/2/17.
//  Copyright © 2017 5calls. All rights reserved.
//

import Foundation

enum IssuesLoadResult {
    case success
    case serverError(Error)
    case offline
}

class IssuesManager {
    
    var issuesList: IssuesList?
    
    var userLocation: UserLocation?
    
    var issues: [Issue] {
        return issuesList?.issues ?? []
    }
    
    var isSplitDistrict: Bool { return self.issuesList?.splitDistrict == true }
    
    func issue(withId id: String) -> Issue? {
        return issuesList?.issues.filter { $0.id == id }.first
    }
    
    func fetchIssues(completion: @escaping (IssuesLoadResult) -> Void) {
        
        let operation = FetchIssuesOperation(location: userLocation)
        
        operation.completionBlock = { [weak self, unowned operation] in
            if let issuesList = operation.issuesList {
                self?.issuesList = issuesList
                // notification!
                DispatchQueue.main.async {
                    completion(.success)
                }
            } else {
                print("Could not load issues..")
                
                DispatchQueue.main.async {
                    if let e = operation.error {
                        print(e.localizedDescription)
                        
                        if self?.isOfflineError(error: e) == true {
                            completion(.offline)
                        } else {
                            completion(.serverError(e))
                        }
                        
                    } else {
                        // souldn't happen, but let's just assume connection error
                        completion(.offline)
                    }
                }
                
            }
        }
        OperationQueue.main.addOperation(operation)
    }
    
    private func isOfflineError(error: Error) -> Bool {
        let e = error as NSError
        guard e.domain == NSURLErrorDomain else { return false }
        
        return e.code == NSURLErrorNetworkConnectionLost ||
            e.code == NSURLErrorNotConnectedToInternet ||<commit_msg>fixed potential issue accessing an operation after it’s been completed<commit_after>//
//  IssuesManager.swift
//  FiveCalls
//
//  Created by <NAME> on 2/2/17.
//  Copyright © 2017 5calls. All rights reserved.
//

import Foundation

enum IssuesLoadResult {
    case success
    case serverError(Error)
    case offline
}

class IssuesManager {
    
    var issuesList: IssuesList?
    
    var userLocation: UserLocation?
    
    var issues: [Issue] {
        return issuesList?.issues ?? []
    }
    
    var isSplitDistrict: Bool { return self.issuesList?.splitDistrict == true }
    
    func issue(withId id: String) -> Issue? {
        return issuesList?.issues.filter { $0.id == id }.first
    }
    
    func fetchIssues(completion: @escaping (IssuesLoadResult) -> Void) {
        
        let operation = FetchIssuesOperation(location: userLocation)
        
        operation.completionBlock = { [weak self, unowned operation] in
            if let issuesList = operation.issuesList {
                self?.issuesList = issuesList
                // notification!
                DispatchQueue.main.async {
                    completion(.success)
                }
            } else {
                let error = operation.error
                print("Could not load issues..")
                
                DispatchQueue.main.async {
                    if let e = error {
                        print(e.localizedDescription)
                        
                        if self?.isOfflineError(error: e) == true {
                            completion(.offline)
                        } else {
                            completion(.serverError(e))
                        }
                        
                    } else {
                        // souldn't happen, but let's just assume connection error
                        completion(.offline)
                    }
                }
                
            }
        }
        OperationQueue.main.addOperation(operation)
    }
    
    private func isOfflineError(error: Error) -> Bool {
        let e = error as NSError
        guard e.domain == NSURLErrorDomain else { return false }
        
        return e.code == NSURLErrorNetworkConnectionLost ||
            e.code == NSURLErrorNotConnectedToInternet ||
{'fraction_non_alphanumeric': 0.08176795580110498, 'fraction_numerical': 0.004419889502762431, 'mean_word_length': 1.3234086242299794, '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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4611818', 'n_tokens_mistral': 1186, 'n_tokens_neox': 1077, 'n_words': 268}