/// <summary> /// Discovers Current and Completed Downloads. /// </summary> public async Task DiscoverDownloadsAsync() { CurrentDownloads.Clear(); CompletedDownloads.Clear(); var currentDownloads = await _downloader.DiscoverBackgroundDownloadsAsync(); foreach (var download in currentDownloads) { var status = download.Progress.Status; if (status == BackgroundTransferStatus.Canceled) { continue; } var appDownloadItem = GetAppDownloadItem(download); appDownloadItem.Tag = download; if (status == BackgroundTransferStatus.Completed) { CompletedDownloads.Add(appDownloadItem); } else { CurrentDownloads.Add(appDownloadItem); } } }
private void LoadCompletedDownloadsFromDownloadsHistory() { _downloadsHistory.ForEach(appDownloadItem => { if (appDownloadItem.GetStatus() == BackgroundTransferStatus.Completed && appDownloadItem.TotalBytesToReceive > 0) { CompletedDownloads.Add(appDownloadItem); } }); }
private void downloader_DownloadProgress(DownloadOperation downloadOperation) { var appDownloadItem = CurrentDownloads.SingleOrDefault(item => (item.Tag as DownloadOperation).Guid == downloadOperation.Guid); if (appDownloadItem == null) { return; } appDownloadItem.BytesReceived = downloadOperation.Progress.BytesReceived; appDownloadItem.SetStatus(downloadOperation.Progress.Status); // When this event handler is occured for first time (for a specefic DownloadOperation), // appDownloadItem.TotalBytesToReceive is zero if (appDownloadItem.TotalBytesToReceive == 0) { appDownloadItem.TotalBytesToReceive = downloadOperation.Progress.TotalBytesToReceive; } // OS might not report a download completion event, we should check this. if ((downloadOperation.Progress.BytesReceived == downloadOperation.Progress.TotalBytesToReceive) && (downloadOperation.Progress.BytesReceived > 0)) { appDownloadItem.SetStatus(BackgroundTransferStatus.Completed); } if (appDownloadItem.GetStatus() == BackgroundTransferStatus.Completed) { appDownloadItem.EndDate = DateTime.Now; CurrentDownloads.Remove(appDownloadItem); CompletedDownloads.Add(appDownloadItem); var task = SaveDownloadsHistoryToFileAsync(); if (this.DownloadCompleted != null) { DownloadCompleted(appDownloadItem); } } if (this.DownloadProgress != null) { DownloadProgress(appDownloadItem); } }