Пример #1
0
        public async Task RestoreCacheAsync()
        {
            StorageFolder storageFolder = await this.GetCacheFolderAsync();

            var folders = await storageFolder.GetFoldersAsync();

            foreach (var folder in folders)
            {
                IReadOnlyList <StorageFile> files = await folder.GetFilesAsync();

                foreach (var file in files)
                {
                    var song = await this.songsRepository.FindSongAsync(file.Name);

                    if (song != null)
                    {
                        CachedSong cachedSong = new CachedSong()
                        {
                            FileName      = file.Name,
                            SongId        = song.SongId,
                            IsAddedByUser = true,
                            TaskAdded     = DateTime.Now
                        };

                        await this.songsCacheRepository.AddAsync(cachedSong);
                    }
                    else
                    {
                        await file.DeleteAsync(StorageDeleteOption.PermanentDelete);
                    }
                }
            }
        }
Пример #2
0
 public Task AddAsync(CachedSong item)
 {
     return(this.Connection.RunInTransactionAsync(
                c =>
     {
         var cachedSong = c.Find <CachedSong>(item.SongId);
         if (cachedSong == null)
         {
             c.Insert(item);
         }
     }));
 }
Пример #3
0
        private async Task DownloadAsync(CancellationToken cancellationToken)
        {
            try
            {
                await this.InitializeCacheFolderAsync();

                CachedSong nextTask;
                while ((nextTask = await this.songsCacheRepository.GetNextAsync()) != null)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        break;
                    }

                    INetworkRandomAccessStream stream = null;

                    await this.mutex.WaitAsync(cancellationToken).ConfigureAwait(continueOnCapturedContext: false);

                    try
                    {
                        if (this.currentDownloadSong != null && this.currentDownloadSong.SongId == nextTask.SongId)
                        {
                            stream = this.currentDownloadStream;
                        }

                        if (this.predownloadedSong != null && this.predownloadedSong.SongId == nextTask.SongId)
                        {
                            stream = this.predownloadedStream;
                        }
                    }
                    finally
                    {
                        this.mutex.Release(1);
                    }

                    if (stream == null)
                    {
                        stream = (await this.GetNetworkStreamAsync(nextTask.Song)).Item1;
                    }

                    this.eventAggregator.Publish(new SongCachingChangeEvent(SongCachingChangeEventType.StartDownloading, stream, nextTask.Song));

                    if (stream == null)
                    {
                        await this.ClearDownloadTask(cancellationToken);

                        this.eventAggregator.Publish(new SongCachingChangeEvent(SongCachingChangeEventType.FailedToDownload, null, nextTask.Song));
                        break;
                    }
                    else
                    {
                        await this.SetCurrentStreamAsync(nextTask.Song, stream);

                        await this.InitializeCacheFolderAsync();

                        await stream.DownloadAsync();

                        if (nextTask.Song.AlbumArtUrl != null)
                        {
                            await this.albumArtCacheService.GetCachedImageAsync(nextTask.Song.AlbumArtUrl, size : 116);
                        }

                        if (cancellationToken.IsCancellationRequested)
                        {
                            break;
                        }

                        await this.mutex.WaitAsync(cancellationToken).ConfigureAwait(continueOnCapturedContext: false);

                        try
                        {
                            var cache = await this.songsCacheRepository.FindAsync(nextTask.Song);

                            if (cache == null || string.IsNullOrEmpty(cache.FileName))
                            {
                                var songFolder = await this.cacheFolder.CreateFolderAsync(nextTask.Song.SongId.Substring(0, 1), CreationCollisionOption.OpenIfExists);

                                var file = await songFolder.CreateFileAsync(nextTask.Song.SongId, CreationCollisionOption.ReplaceExisting);

                                await stream.SaveToFileAsync(file);

                                if (cache == null)
                                {
                                    cache = new CachedSong()
                                    {
                                        FileName = file.Name, SongId = nextTask.Song.SongId, TaskAdded = DateTime.Now
                                    };
                                    await this.songsCacheRepository.AddAsync(cache);
                                }
                                else
                                {
                                    cache.FileName = file.Name;
                                    await this.songsCacheRepository.UpdateAsync(cache);
                                }
                            }

                            this.currentDownloadStream = null;
                            this.currentDownloadSong   = null;
                        }
                        finally
                        {
                            this.mutex.Release(1);
                        }

                        var song = await this.songsRepository.GetSongAsync(nextTask.Song.SongId);

                        this.eventAggregator.Publish(new SongCachingChangeEvent(SongCachingChangeEventType.FinishDownloading, stream, song));
                    }
                }
            }
            catch (OperationCanceledException e)
            {
                this.logger.Debug(e, "DownloadAsync was canceled.");
            }
            catch (Exception exception)
            {
                this.logger.Error(exception, "Exception while tried to DownloadAsync.");
            }

            await this.ClearDownloadTask(cancellationToken);
        }
Пример #4
0
        public async Task CancelTaskAsync(CachedSong cachedSong)
        {
            if (cachedSong == null)
            {
                throw new ArgumentNullException("cachedSong");
            }

            Song currentSong = null;
            INetworkRandomAccessStream currentStream = null;

            await this.mutex.WaitAsync().ConfigureAwait(continueOnCapturedContext: false);

            try
            {
                if (this.downloadTask != null && this.currentDownloadSong != null && this.currentDownloadSong.SongId == cachedSong.SongId)
                {
                    if (this.downloadTaskCancellationToken != null)
                    {
                        this.downloadTaskCancellationToken.Cancel();
                        this.downloadTaskCancellationToken = null;
                    }

                    if (this.downloadTask != null && this.currentDownloadSong != null && this.currentDownloadStream != null)
                    {
                        currentSong   = this.currentDownloadSong;
                        currentStream = this.currentDownloadStream;
                    }

                    if (this.currentDownloadStream != null)
                    {
                        this.currentDownloadStream.Dispose();
                    }

                    this.downloadTask          = null;
                    this.currentDownloadSong   = null;
                    this.currentDownloadStream = null;
                }

                var refreshedCache = await this.songsCacheRepository.FindAsync(cachedSong.Song);

                if (refreshedCache != null)
                {
                    if (!string.IsNullOrEmpty(refreshedCache.FileName))
                    {
                        var storageFile = await StorageFile.GetFileFromPathAsync(this.GetFullPath(refreshedCache.FileName));

                        await storageFile.DeleteAsync(StorageDeleteOption.PermanentDelete);
                    }

                    await this.songsCacheRepository.RemoveAsync(refreshedCache);
                }
            }
            finally
            {
                this.mutex.Release(1);
            }

            if (currentSong != null && currentStream != null)
            {
                this.eventAggregator.Publish(
                    new SongCachingChangeEvent(SongCachingChangeEventType.DownloadCanceled, currentStream, currentSong));
            }

            await this.StartDownloadTaskAsync();
        }
Пример #5
0
 public CachedSongBindingModel(CachedSong cachedSong)
 {
     this.CachedSong = cachedSong;
 }
Пример #6
0
 public Task UpdateAsync(CachedSong item)
 {
     return(this.Connection.UpdateAsync(item));
 }
Пример #7
0
 public Task RemoveAsync(CachedSong item)
 {
     return(this.Connection.DeleteAsync(item));
 }