//// //// LookupOperation.swift //// TagTunes //// //// Created by Kim Wittenburg on 08.04.16. //// Copyright © 2016 Kim Wittenburg. All rights reserved. //// // //import AppKitPlus // //class LookupOperation: Operation { // // /// The `NSURLSession` used to perform lookup tasks. // private let urlSession = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration(), delegate: nil, delegateQueue: NSOperationQueue.mainQueue()) // // /// The task of the current lookup. If there is currently no lookup being // /// processed this is `nil`. // /// // /// If you need to know whether there is currently a active lookup you should // /// use the `lookupActive` property instead. // private var lookupTask: NSURLSessionTask? // // var lookupActive = false // // /// The tracks that are currently being looked up. These tracks do not // /// contain the tracks enqueued for lookup. // private var lookupTracks = [SearchAPIID: TagTunesTrack]() // // private var queue = [TagTunesTrack]() { // didSet { // lookupActivity.progress.localizedAdditionalDescription = String(format: NSLocalizedString("%d Tracks Pending", comment: "Additional description format fpr the iTunes Match lookup."), queue.count) // } // } // // func enqueueTracksForLookup(tracks: S) { // queue.appendContentsOf(tracks) // if !lookupActive { // beginLookup() // } // } // // @IBAction func cancelLookup(sender: AnyObject) { // cancelLookup() // } // // func cancelLookup() { // lookupTask?.cancel() // lookupTask = nil // var tracks = queue // tracks.appendContentsOf(lookupTracks.values) // for track in tracks { // track.lookupState = .Error(NSCocoaError.UserCancelledError) // } // lookupDelegate?.lookupController(self, completedLookupForTracks: tracks) // queue = [] // lookupTracks = [:] // lookupDelegate?.lookupControllerDidFinishLookup(self) // // lookupActive = false // } // // private func beginLookup() { // lookupActive = true // lookupDelegate?.lookupController(self, willBeginLookupForTracks: queue) // // let tracks = queue // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { self.performLookupForTracks(tracks) } // queue = [] // } // // /// Actually starts the lookup for the specified tracks. // /// // /// This method does not execute the network request (but it starts it). // /// Still it is recommended not to invoke this method on the main thread // /// since it can block the current thread for quite a while (depending on the // /// location of the tracks). // private func performLookupForTracks(tracks: [TagTunesTrack]) { // dispatch_sync(dispatch_get_main_queue()) { // self.lookupActivity.progress.totalUnitCount = -1 // self.lookupActivity.progress.localizedDescription = String(format: NSLocalizedString("Preparing Lookup for %d Tracks…", comment: "Description format for the iTunes Match lookup preparation."), tracks.count) // } // var invalidTracks = [TagTunesTrack]() // lookupTracks = [:] // for track in tracks { // track.lookupState = .Preparing // if let id = track.id { // lookupTracks[id] = track // track.lookupState = .Searching // } else { // invalidTracks.append(track) // track.lookupState = .Unqualified // } // } // dispatch_sync(dispatch_get_main_queue()) { // if !invalidTracks.isEmpty { // self.lookupDelegate?.lookupController(self, completedLookupForTracks: invalidTracks) // } // self.lookupActivity.progress.localizedDescription = String(format: NSLocalizedString("Looking up %d tracks…", comment: "Description format for the iTunes Match lookup."), self.lookupTracks.count) // } // var request = SearchAPIRequest(lookupRequestWithIDs: lookupTracks.keys) // request.entity = .Album // request.country = Preferences.sharedPreferences.iTunesStore // if Preferences.sharedPreferences.useEnglishTags { // request.language = .English // } // NSThread.sleepForTimeInterval(2) // lookupTask = urlSession.dataTaskWithURL(request.URL, completionHandler: finishedTrackLookupWithData) // lookupTask?.resume() // } // // /// Invoked after the lookup network request has completed. This method // /// processes the raw lookup data and send delegate messages accordingly. // /// This method may send multiple delegate messages depending on the // /// characteristics of the `data`. // /// // /// This method must be called on the main thread. // private func finishedTrackLookupWithData(data: NSData?, response: NSURLResponse?, error: NSError?) { // assert(NSThread.isMainThread()) // defer { // lookupTask = nil // } // let result = (try? data.map(SearchAPIResult.init)) ?? nil // for (id, track) in lookupTracks { // if let error = error { // track.lookupState = .Error(error) // } else if let resultTrack = result?.entityForID(id) as? Track { // track.lookupState = .Found(resultTrack) // } else { // track.lookupState = .NotFound // } // } // lookupDelegate?.lookupController(self, completedLookupForTracks: Array(lookupTracks.values)) // if queue.isEmpty { // lookupDelegate?.lookupControllerDidFinishLookup(self) // lookupActive = false // } else { // beginLookup() // } // } // //}