<commit_before><commit_msg>added CachedTileOverlay<commit_after>//
//  CachedTileOverlay.swift
//  uni-hd
//
//  Created by <NAME> on 25.11.14.
//  Copyright (c) 2014 Universität Heidelberg. All rights reserved.
//

import Foundation
import MapKit
import VILogKit

class CachedTileOverlay: MKTileOverlay {
   
    let cache = NSCache()
    let operationQueue = NSOperationQueue()
    
    override func loadTileAtPath(path: MKTileOverlayPath, result: ((NSData!, NSError!) -> ())!) {
        // FIXME: caching does not work
        if let result = result {
            if let cachedData = self.cache.objectForKey(self.URLForTilePath(path).absoluteString!) as? NSData {
                logger.log("Loaded cached tile for path (\(path.z),\(path.x),\(path.y))", forLevel: .Verbose)
                result(cachedData, nil)
            } else {
                let url = self.URLForTilePath(path)
                let request = NSURLRequest(URL: url)
                logger.log("Requesting tile for path (\(path.z),\(path.x),\(path.y)) from URL \(url)...", forLevel: .Verbose)
                NSURLConnection.sendAsynchronousRequest(request, queue: operationQueue, completionHandler: { response, data, error in
                    var imageData: NSData! = nil
                    if UIImage(data: data) == nil { // TODO: more efficiently check for error in response
                        self.logger.log("Could not load tile for path (\(path.z),\(path.x),\(path.y)) from URL \(url).", forLevel: .Verbose)
                    } else {
                        imageData = data
                        self.cache.setObject(imageData, forKey: url.absoluteString!)
                        self.logger.log("Loaded and cached tile for path (\(path.z),\(path.x),\(path.y)).", forLevel: .Debug)
                    }
                    result(imageData, error)
                })
            }
        }
    }

}


// MARK: Logging

extension CachedTileOverlay {
    var logger: Logger {
        // TOOD: dont do this
        let logger = Logger.loggerForKeyPath("VIOSFramework.CachedTileOverlay")
        logger.logLevel = .Debug
        return logger
    }
}

{'fraction_non_alphanumeric': 0.12057728119180633, 'fraction_numerical': 0.004655493482309125, 'mean_word_length': 1.968232044198895, 'pattern_counts': {'":': 0, '<': 4, '<?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': '3623412', 'n_tokens_mistral': 626, 'n_tokens_neox': 600, 'n_words': 153}