 
     struct ProgressReporter {
 
-        let name: String
+        let downloadTask: NSURLSessionDownloadTask
 
         typealias ReportAction = Double -> Void
 
-        let reportAction: ReportAction
+        let reportProgressAction: ReportAction?
+
+        let finishedAction: NSData -> Void
     }
+
+    var progressReporters = [ProgressReporter]()
 
     class func downloadAttachmentsOfMessage(message: Message, reportProgress: ProgressReporter.ReportAction?) {
 


        if downloadState == MessageDownloadState.Downloaded.rawValue {
            reportProgress?(1.0)
        }
        
        if !attachmentURLString.isEmpty {

            if let URL = NSURL(string: attachmentURLString) {

                let downloadTask = sharedDownloader.session.downloadTaskWithURL(URL, completionHandler: { location, response, error in

                    if let data = NSData(contentsOfURL: location) {

                        reportProgress?(1.0)

                        let fileName = NSUUID().UUIDString


                            let realm = Realm()

                            if let message = messageWithMessageID(messageID, inRealm: realm) {
                                if let fileURL = NSFileManager.saveMessageImageData(data, withName: fileName) {
                                    self.updateThumbnailOfMessage(message, withThumbnailFileName: fileName, inRealm: realm)
                                }
                            }
                        }
                    }
                })
                
                downloadTask.resume()
            }
        }
    }
}

extension YepDownloader: NSURLSessionDelegate {

}

extension YepDownloader: NSURLSessionDownloadDelegate {

    private func reportProgress(progress: Double, ofDownloadTask downloadTask: NSURLSessionDownloadTask) {
        progressReporters.filter({ $0.downloadTask == downloadTask }).map({ $0.reportProgressAction?(progress) })
    }

    private func handleData(data: NSData, ofDownloadTask downloadTask: NSURLSessionDownloadTask) {
        progressReporters.filter({ $0.downloadTask == downloadTask }).map({ $0.finishedAction(data) })
    }

    func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {

        if let data = NSData(contentsOfURL: location) {
            handleData(data, ofDownloadTask: downloadTask)
        }

        reportProgress(1.0, ofDownloadTask: downloadTask)

        println("didFinishDownloadingToURL")
    }

    func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {

        let progress: Double = Double(totalBytesWritten) / Double(totalBytesExpectedToWrite)

        reportProgress(progress, ofDownloadTask: downloadTask)

        println("downloadTask progress \(progress)")
    }
}

