public static async Task ImportAsync(IReadOnlyList <IStorageItem> items, IProgress <double> progress = null) { items = GetImportableItems(items); AggregateProgress total = (progress != null) ? new AggregateProgress(progress) : null; ISyncService sync = await App.Services.GetServiceAsync <ISyncService> ().ConfigureAwait(false); DownloadManager downloads = await App.Services.GetServiceAsync <DownloadManager> ().ConfigureAwait(false); List <Task> importTasks = new List <Task> (); foreach (IStorageItem item in items) { var node = total?.CreateProgressNode(); if (item is IStorageFolder folder) { var children = await folder.GetItemsAsync(); importTasks.Add(ImportAsync(children, node)); } else if (item is IStorageFile file) { Task <FileSample> import = null; ManagedDownload download = null; Func <CancellationToken, IProgress <double>, Task <FileSample> > importGetter = async(cancel, progress) => { import = ImportAsync(file, sync, progress, cancel); FileSample sample = null; try { sample = await import; if (download != null) { download.State = DownloadState.Completed; } } catch { if (download != null) { download.State = DownloadState.LocalError; } } return(sample); }; download = downloads.QueueImport(file.Name, importGetter); importTasks.Add(import); } } total?.FinishDiscovery(); await Task.WhenAll(importTasks); }
public void PreallocateNodes() { var progress = new Mock <IProgress <double> > (); var aggregate = new AggregateProgress(progress.Object, holdForDiscovery: true); aggregate.FinishDiscovery(10); IProgress <double> node = aggregate.PopNode(); Assert.That(node, Is.Not.Null); node.Report(1); progress.Verify(p => p.Report(It.IsInRange(0.09, 0.11, Moq.Range.Inclusive))); }
public async Task <IReadOnlyList <FileSample> > EnsureElementPresentAsync(EnvironmentElement element, IProgress <double> progress = null, CancellationToken cancellationToken = default) { if (element is null) { throw new ArgumentNullException(nameof(element)); } await this.loadedServices.ConfigureAwait(false); List <Task> tasks = new List <Task> (); List <FileSample> samples = new List <FileSample> (); AggregateProgress totalProgress = (progress != null) ? new AggregateProgress(progress) : null; if (element.Audio != null) { foreach (string sampleId in element.Audio.Playlist.Descriptors) { FileSample sample = (await this.sync.GetElementByIdAsync(sampleId).ConfigureAwait(false) as FileSample); if (sample == null) { continue; } IProgress <double> nodeProgress = totalProgress?.CreateProgressNode(); samples.Add(sample); tasks.Add(this.downloadManager.EnsurePresentAsync(sample, nodeProgress, cancellationToken)); } } totalProgress?.FinishDiscovery(); int offset = 0; for (int i = 0; i < tasks.Count; i++) { try { await tasks[i].ConfigureAwait(false); } catch { samples.RemoveAt(i - offset++); } } return(samples); }