public async Task <bool> DownloadFullImageAsync(CancellationTokenSource cts) { _cts = cts; _tcs = new TaskCompletionSource <int>(); var url = ImageItem.GetSaveImageUrlFromSettings(); if (string.IsNullOrEmpty(url)) { return(false); } DisplayIndex = (int)DisplayMenu.Downloading; ImageItem.DownloadStatus = Common.DownloadStatus.Downloading; StorageFolder savedFolder = null; StorageFile savedFile = null; try { savedFolder = await AppSettings.Instance.GetSavingFolderAsync(); savedFile = await savedFolder.CreateFileAsync(ImageItem.GetFileNameForDownloading(), CreationCollisionOption.OpenIfExists); } catch (Exception e) { await Logger.LogAsync(e); ToastService.SendToast("No right to create file for writing. Please check your security settings. \n If necessary, please contact me via about page.", 5000); return(false); } var backgroundDownloader = new BackgroundDownloader() { SuccessToastNotification = ToastHelper.CreateToastNotification("Saved:D", $"Find it in {savedFolder.Path}.", savedFile.Path) }; var downloadOperation = backgroundDownloader.CreateDownload(new Uri(url), savedFile); downloadOperation.Priority = BackgroundTransferPriority.High; DownloadOperationGUID = downloadOperation.Guid; _tcs.TrySetResult(0); ToastService.SendToast("Downloading in background...", 2000); try { DownloadStatus = "DOWNLOADING"; Progress = 0; var progress = new Progress <DownloadOperation>(); progress.ProgressChanged += Progress_ProgressChanged; await downloadOperation.StartAsync().AsTask(_cts.Token, progress); return(true); } catch (TaskCanceledException) { await downloadOperation.ResultFile.DeleteAsync(); ToastService.SendToast("Download has been cancelled."); DownloadStatus = ""; DisplayIndex = (int)DisplayMenu.Retry; ImageItem.DownloadStatus = Common.DownloadStatus.Pending; throw; } catch (Exception e) { ImageItem.DownloadStatus = Common.DownloadStatus.Pending; await Logger.LogAsync(e); ToastService.SendToast("ERROR" + e.Message, 2000); return(false); } }
public async Task <bool> DownloadFullImageAsync(CancellationTokenSource cts) { _cts = cts; _tcs = new TaskCompletionSource <int>(); var url = ImageItem.GetSaveImageUrlFromSettings(); if (string.IsNullOrEmpty(url)) { return(false); } DisplayIndex = (int)DisplayMenu.Downloading; ImageItem.DownloadStatus = Common.DownloadStatus.Downloading; StorageFolder savedFolder = null; StorageFile savedFile = null; try { savedFolder = await AppSettings.GetSavingFolderAsync(); savedFile = await savedFolder.CreateFileAsync(ImageItem.GetFileNameForDownloading(), CreationCollisionOption.OpenIfExists); } catch (Exception e) { await Logger.LogAsync(e); ToastService.SendToast(ResourceLoader.GetForCurrentView().GetString("PermissonError"), 5000); return(false); } var locationUrl = ImageItem.GetDownloadLocationUrl(); if (locationUrl != null) { var reportTask = ReportDownloadAsync(locationUrl); } var backgroundDownloader = new BackgroundDownloader() { SuccessToastNotification = ToastHelper.CreateToastNotification( ResourceLoader.GetForCurrentView().GetString("SavedTitle"), savedFolder.Path, savedFile.Path) }; var downloadOperation = backgroundDownloader.CreateDownload(new Uri(url), savedFile); downloadOperation.Priority = BackgroundTransferPriority.High; DownloadOperationGUID = downloadOperation.Guid; _tcs.TrySetResult(0); ToastService.SendToast(ResourceLoader.GetForCurrentView().GetString("DownloadingHint"), 2000); try { DownloadStatus = ResourceLoader.GetForCurrentView().GetString("DownloadingStatus"); Progress = 0; var progress = new Progress <DownloadOperation>(); progress.ProgressChanged += Progress_ProgressChanged; await downloadOperation.StartAsync().AsTask(_cts.Token, progress); return(true); } catch (TaskCanceledException) { await downloadOperation.ResultFile.DeleteAsync(); ToastService.SendToast(ResourceLoader.GetForCurrentView().GetString("CancelStatus")); throw; } catch (Exception e) { ReportFailure(); await Logger.LogAsync(e); ToastService.SendToast(ResourceLoader.GetForCurrentView().GetString("ErrorStatus") + e.Message, 3000); return(false); } }