<commit_before>//
//  PINRemoteImageIntegration.swift
//  AXPhotoViewer
//
//  Created by <NAME> on 6/1/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

#if canImport(PINRemoteImage)
    
import PINRemoteImage

class PINRemoteImageIntegration: NSObject, AXNetworkIntegrationProtocol {

    weak var delegate: AXNetworkIntegrationDelegate?
    
    fileprivate var downloadUUIDs = NSMapTable<AXPhotoProtocol, NSUUID>(keyOptions: .strongMemory, valueOptions: .strongMemory)
    
    func loadPhoto(_ photo: AXPhotoProtocol) {
        if photo.imageData != nil || photo.image != nil {
            self.delegate?.networkIntegration(self, loadDidFinishWith: photo)
        }
        
        guard let url = photo.url else {
            return
        }
        
        let progress: PINRemoteImageManagerProgressDownload = { [weak self] (completedBytes, totalBytes) in
            guard let `self` = self else {
                return
            }
            
            self.delegate?.networkIntegration?(self, didUpdateLoadingProgress: CGFloat(completedBytes) / CGFloat(totalBytes), for: photo)
        }
        
        let completion: PINRemoteImageManagerImageCompletion = { [weak self] (result) in
            guard let `self` = self else {
                return
            }
            
            self.downloadUUIDs.removeObject(forKey: photo)
            
            if let animatedImage = result.alternativeRepresentation as? FLAnimatedImage {
                // this is not great, as `PINRemoteImage` already creates the `FLAnimatedImage` that we need.
                // something to fix in the future.
                photo.imageData = animatedImage.data
                self.delegate?.networkIntegration(self, loadDidFinishWith: photo)
            } else if let image = result.image {
                photo.image = image
                self.delegate?.networkIntegration(self, loadDidFinishWith: photo)
            } else {
                let error = NSError(
                    domain: AXNetworkIntegrationErrorDomain,
                    code: AXNetworkIntegrationFailedToLoadErrorCode,
                    userInfo: nil
                )
                self.delegate?.networkIntegration(self, loadDidFailWith: error, for: photo)
            }
        }
        
        guard let uuid = PINRemoteImageManager.shared().downloadImage(with: url, options: [], progressDownload: progress, completion: completion) else {
            return
        }
        
        self.downloadUUIDs.setObject(uuid as NSUUID, forKey: photo)
    }
    
    func cancelLoad(for photo: AXPhotoProtocol) {
        guard let uuid = self.downloadUUIDs.object(forKey: photo) else {
            return
        }
        
        PINRemoteImageManager.shared().cancelTask(with: uuid as UUID, storeResumeData: true)
    }
    
    func cancelAllLoads() {
        let enumerator = self.downloadUUIDs.objectEnumerator()
        
        while let uuid  = enumerator?.nextObject() as? NSUUID {
            PINRemoteImageManager.shared().cancelTask(with: uuid as UUID, storeResumeData: true)
        }
        
        self.downloadUUIDs.removeAllObjects()
    }
    
}

#endif<commit_msg>PINRemoteImageIntegration -- updated to use PINRemoteImageManagerAlternateRepresentationProvider for providing image data<commit_after>//
//  PINRemoteImageIntegration.swift
//  AXPhotoViewer
//
//  Created by <NAME> on 6/1/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

#if canImport(PINRemoteImage)
    
import PINRemoteImage

class PINRemoteImageIntegration: NSObject, AXNetworkIntegrationProtocol, PINRemoteImageManagerAlternateRepresentationProvider {

    weak var delegate: AXNetworkIntegrationDelegate?
    
    fileprivate var downloadUUIDs = NSMapTable<AXPhotoProtocol, NSUUID>(keyOptions: .strongMemory, valueOptions: .strongMemory)
    fileprivate(set) var imageManager: PINRemoteImageManager!
    
    override init() {
        super.init()
        self.imageManager = PINRemoteImageManager(
            sessionConfiguration: .default,
            alternativeRepresentationProvider: self,
            imageCache: PINRemoteImageManager.shared().cache // PINRemoteImageManager cache is thread safe, so we should be able to use the same cache instance as our shared counterpart
        )
    }
    
    func loadPhoto(_ photo: AXPhotoProtocol) {
        if photo.imageData != nil || photo.image != nil {
            self.delegate?.networkIntegration(self, loadDidFinishWith: photo)
        }
        
        guard let url = photo.url else {
            return
        }
        
        let progress: PINRemoteImageManagerProgressDownload = { [weak self] (completedBytes, totalBytes) in
            guard let `self` = self else {
                return
            }
            
            self.delegate?.networkIntegration?(self, didUpdateLoadingProgress: CGFloat(completedBytes) / CGFloat(totalBytes), for: photo)
        }
        
        let completion: PINRemoteImageManagerImageCompletion = { [weak self] (result) in
            guard let `self` = self else {
                return
            }
            
            self.downloadUUIDs.removeObject(forKey: photo)
            
            if let data = result.alternativeRepresentation as? Data {
                photo.imageData = data
                self.delegate?.networkIntegration(self, loadDidFinishWith: photo)
            } else if let image = result.image {
                photo.image = image
                self.delegate?.networkIntegration(self, loadDidFinishWith: photo)
            } else {
                let error = NSError(
                    domain: AXNetworkIntegrationErrorDomain,
                    code: AXNetworkIntegrationFailedToLoadErrorCode,
                    userInfo: nil
                )
                self.delegate?.networkIntegration(self, loadDidFailWith: error, for: photo)
            }
        }
        
        guard let uuid = self.imageManager.downloadImage(with: url, options: [], progressDownload: progress, completion: completion) else {
            return
        }
        
        self.downloadUUIDs.setObject(uuid as NSUUID, forKey: photo)
    }
    
    func cancelLoad(for photo: AXPhotoProtocol) {
        guard let uuid = self.downloadUUIDs.object(forKey: photo) else {
            return
        }
        
        self.imageManager.cancelTask(with: uuid as UUID, storeResumeData: true)
    }
    
    func cancelAllLoads() {
        let enumerator = self.downloadUUIDs.objectEnumerator()
        
        while let uuid  = enumerator?.nextObject() as? NSUUID {
            self.imageManager.cancelTask(with: uuid as UUID, storeResumeData: true)
        }
        
        self.downloadUUIDs.removeAllObjects()
    }
    
    // MARK: - PINRemoteImageManagerAlternateRepresentationProvider
    func alternateRepresentation(with data: Data!, options: PINRemoteImageManagerDownloadOptions = []) -> Any! {
        guard let `data` = data else {
            return nil
        }
        
        return data.containsGIF() ? data : nil
    }
    
}

#endif
{'fraction_non_alphanumeric': 0.07801918735891648, 'fraction_numerical': 0.002257336343115124, 'mean_word_length': 2.082173913043478, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 19, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4734335', 'n_tokens_mistral': 1872, 'n_tokens_neox': 1805, 'n_words': 436}