<commit_before>//
//  YSDriveModel.swift
//  YSGGP
//
//  Created by <NAME> on 9/23/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation
import SwiftMessages

class YSDriveModel: YSDriveModelProtocol
{
    var isLoggedIn : Bool
    {
        return YSCredentialManager.isLoggedIn
    }
    
    private var currentFolder : YSFolder?
    private let taskUIID = UUID().uuidString
    
    init(folder: YSFolder?)
    {
        currentFolder = folder
    }
    
    deinit
    {
        YSFilesMetadataDownloader.cancelTaskWithIdentifier(taskIdentifier: taskUIID)
    }
    
    func getFiles(pageToken: String, nextPageToken: String?,_ completionHandler: @escaping AllFilesCompletionHandler)
    {
        var url = "\(YSConstants.kDriveAPIEndpoint)files?"
        url.addingPercentEncoding(nextPageToken)
        guard let folder = currentFolder else
        {
            LogDefault(.Service, .Error, "No folder")
            return
        }
        url += "corpus=user&orderBy=folder%2Cname&pageSize=\(YSConstants.kPageSize)&q='\(folder.folderID)'+in+parents+and+(mimeType+contains+'folder'+or+mimeType+contains+'audio')+and+trashed%3Dfalse&spaces=drive&fields=nextPageToken%2C+files(id%2C+name%2C+size%2C+mimeType)&key=\(YSConstants.kDriveAPIKey)"
        YSFilesMetadataDownloader.downloadFilesList(for: url, taskUIID)
        { filesDictionary, error in
            if let yserror = error as? YSError
            {
                if nextPageToken != nil
                {
                    completionHandler([], yserror, nil)
                    return
                }
                YSDatabaseManager.offlineFiles(fileDriveIdentifier: folder.folderID, yserror, completionHandler)
                return
            }
            YSDatabaseManager.save(pageToken: pageToken, remoteFilesDict: filesDictionary!, folder, completionHandler)
        }
    }
    
    func download(for fileDriveIdentifier: String) -> YSDownloadProtocol?
    {
        return YSAppDelegate.appDelegate().fileDownloader.download(for: fileDriveIdentifier)
    }
    
    func download(_ fileDriveIdentifier: String)
    {
        YSAppDelegate.appDelegate().fileDownloader.download(fileDriveIdentifier: fileDriveIdentifier)
    }
    
    func stopDownload(_ fileDriveIdentifier: String)<commit_msg>Fixed UI update from background thread<commit_after>//
//  YSDriveModel.swift
//  YSGGP
//
//  Created by <NAME> on 9/23/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation
import SwiftMessages

class YSDriveModel: YSDriveModelProtocol
{
    var isLoggedIn : Bool
    {
        return YSCredentialManager.isLoggedIn
    }
    
    private var currentFolder : YSFolder?
    private let taskUIID = UUID().uuidString
    
    init(folder: YSFolder?)
    {
        currentFolder = folder
    }
    
    deinit
    {
        YSFilesMetadataDownloader.cancelTaskWithIdentifier(taskIdentifier: taskUIID)
    }
    
    func getFiles(pageToken: String, nextPageToken: String?,_ completionHandler: @escaping AllFilesCompletionHandler)
    {
        var url = "\(YSConstants.kDriveAPIEndpoint)files?"
        url.addingPercentEncoding(nextPageToken)
        guard let folder = currentFolder else
        {
            LogDefault(.Service, .Error, "No folder")
            return
        }
        url += "corpus=user&orderBy=folder%2Cname&pageSize=\(YSConstants.kPageSize)&q='\(folder.folderID)'+in+parents+and+(mimeType+contains+'folder'+or+mimeType+contains+'audio')+and+trashed%3Dfalse&spaces=drive&fields=nextPageToken%2C+files(id%2C+name%2C+size%2C+mimeType)&key=\(YSConstants.kDriveAPIKey)"
        YSFilesMetadataDownloader.downloadFilesList(for: url, taskUIID)
        { filesDictionary, error in
            if let yserror = error as? YSError
            {
                if nextPageToken != nil
                {
                    DispatchQueue.main.async
                    {
                        completionHandler([], yserror, nil)
                    }
                    return
                }
                YSDatabaseManager.offlineFiles(fileDriveIdentifier: folder.folderID, yserror, completionHandler)
                return
            }
            YSDatabaseManager.save(pageToken: pageToken, remoteFilesDict: filesDictionary!, folder, completionHandler)
        }
    }
    
    func download(for fileDriveIdentifier: String) -> YSDownloadProtocol?
    {
        return YSAppDelegate.appDelegate().fileDownloader.download(for: fileDriveIdentifier)
    }
    
    func download(_ fileDriveIdentifier: String)
    {
        YSAppDelegate.appDelegate().fileDownloader.download(fileDriveIdentifier: fileDriveIdentifier)
    }
    
    func stopDownload(_ fileDriveIdentifier: String)
{'fraction_non_alphanumeric': 0.09252217997465145, 'fraction_numerical': 0.0063371356147021544, 'mean_word_length': 2.578987150415722, 'pattern_counts': {'":': 0, '<': 7, '<?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': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2996527', 'n_tokens_mistral': 1400, 'n_tokens_neox': 1316, 'n_words': 233}