async Task<IEnumerable<Track>> ImportDirectory(string directoryName, CancellationToken cancellationToken, IProgress<string> progress) { if (directoryName == null) throw new ArgumentNullException("directoryName"); if (progress == null) throw new ArgumentNullException("progress"); cancellationToken.ThrowIfCancellationRequested(); messenger.SendToUI(new BeganScanningDirectoryEvent()); progress.ReportFormat("Scanning {0}...", Path.GetFileName(directoryName)); var filenames = Directory .GetFiles(directoryName, "*.*", SearchOption.AllDirectories) .Where(loader.IsSupportedFileFormat); return await ImportAsync(filenames, cancellationToken, progress) .ContinueWith<IEnumerable<Track>>(OnImportComplete); }
public async Task<IEnumerable<Track>> ImportAsync(string filename, CancellationToken cancellationToken, IProgress<string> progress) { if (filename == null) throw new ArgumentNullException("filename"); cancellationToken.ThrowIfCancellationRequested(); if (IsDirectory(filename)) return await ImportDirectory(filename, cancellationToken, progress); if (!loader.IsSupportedFileFormat(filename)) return Enumerable.Empty<Track>(); progress.ReportFormat("Loading {0}", Path.GetFileName(filename)); Track track; if (TryGetTrack(filename, out track)) return new[] { track }; track = await loader.LoadAsync(filename); await storage.AddTrackAsync(track); tracks.Add(track.Id, track); messenger.SendToUI(new TrackAddedToLibraryEvent(track)); return new[] { track }; }