private void ClearCurrentCachedSongSubscription()
 {
     if (this.currentCachedSong != null)
     {
         this.currentCachedSong.IsDownloading = false;
         this.currentCachedSong = null;
         if (this.currentDownloadStream != null)
         {
             this.currentDownloadStream.DownloadProgressChanged -= this.CurrentDownloadStreamOnDownloadProgressChanged;
         }
     }
 }
        private void SubscribeToSong(Song song, INetworkRandomAccessStream networkRandomAccessStream)
        {
            var queuedTask = this.FindCachedSongBindingModel(song);

            if (queuedTask != null)
            {
                queuedTask.IsDownloading   = true;
                this.currentCachedSong     = queuedTask;
                this.currentDownloadStream = networkRandomAccessStream;
                if (this.currentDownloadStream != null)
                {
                    this.currentDownloadStream.DownloadProgressChanged += this.CurrentDownloadStreamOnDownloadProgressChanged;
                }
            }
        }
        private async void OnCacheUpdated(SongCachingChangeEvent e)
        {
            if (e.EventType == SongCachingChangeEventType.StartDownloading)
            {
                this.ClearCurrentCachedSongSubscription();

                this.SubscribeToSong(e.Song, e.Stream);
            }
            else if (e.EventType == SongCachingChangeEventType.FinishDownloading)
            {
                this.ClearCurrentCachedSongSubscription();

                if (this.currentCachedSong == null)
                {
                    this.currentCachedSong = this.FindCachedSongBindingModel(e.Song);
                }

                if (this.currentCachedSong != null)
                {
                    this.BindingModel.QueuedTasks.Remove(this.currentCachedSong);
                }

                await this.Dispatcher.RunAsync(
                    () =>
                {
                    this.BindingModel.SongsCacheSize += (long)e.Stream.Size;
                });
            }
            else if (e.EventType == SongCachingChangeEventType.FailedToDownload ||
                     e.EventType == SongCachingChangeEventType.DownloadCanceled ||
                     e.EventType == SongCachingChangeEventType.ClearCache)
            {
                this.ClearCurrentCachedSongSubscription();
            }

            await this.Dispatcher.RunAsync(() => this.StartDownloadCommand.RaiseCanExecuteChanged());
        }