Exemple #1
0
        public void MultiplePercentNodes()
        {
            var progress  = new Mock <IProgress <double> > ();
            var aggregate = new AggregateProgress(progress.Object, holdForDiscovery: false);

            IProgress <double> node  = aggregate.CreateProgressNode();
            IProgress <double> node2 = aggregate.CreateProgressNode();

            node.Report(5);
            progress.Verify(p => p.Report(It.IsInRange(2.4, 2.51, Moq.Range.Inclusive)));

            node2.Report(5);
            progress.Verify(p => p.Report(It.IsInRange(4.9, 5.1, Moq.Range.Inclusive)));
        }
Exemple #2
0
        public void SinglePercentNode()
        {
            var progress  = new Mock <IProgress <double> > ();
            var aggregate = new AggregateProgress(progress.Object, holdForDiscovery: false);

            IProgress <double> node = aggregate.CreateProgressNode();

            node.Report(5);

            progress.Verify(p => p.Report(5));
        }
Exemple #3
0
        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);
        }
Exemple #4
0
        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);
        }
Exemple #5
0
        public async Task <PlaybackEnvironment> PrepareEncounterStateAsync(EncounterState encounterState, IProgress <double> progress = null, CancellationToken cancellationToken = default)
        {
            if (encounterState is null)
            {
                throw new ArgumentNullException(nameof(encounterState));
            }

            await this.loadedServices.ConfigureAwait(false);

            AggregateProgress totalProgress = (progress != null) ? new AggregateProgress(progress) : null;

            var elements     = new List <(EncounterStateElement, EnvironmentElement)> ();
            var presentTasks = new List <Task <IReadOnlyList <FileSample> > > ();

            foreach (EncounterStateElement stateElement in encounterState.EnvironmentElements)
            {
                EnvironmentElement element = await this.sync.GetElementByIdAsync <EnvironmentElement> (stateElement.ElementId).ConfigureAwait(false);

                elements.Add((stateElement, element));
                presentTasks.Add(EnsureElementPresentAsync(element, totalProgress?.CreateProgressNode(), cancellationToken));
            }

            // Lower level errors should be handled by EnsurePresent.
            // If EnsurePresent fails we should bubble up because we can't be sure we can prepare the encounter.
            await Task.WhenAll(presentTasks).ConfigureAwait(false);

            var playbackElements = new List <PlaybackEnvironmentElement> (elements.Count);

            for (int i = 0; i < elements.Count; i++)
            {
                (var state, var element) = elements[i];
                playbackElements.Add(new PlaybackEnvironmentElement(state, element));                  //, presentTasks[i].Result));
            }

            return(new PlaybackEnvironment(playbackElements));
        }