Example #1
0
        public async Task CheckDownloadStatusAsync(IReadOnlyList <DownloadOperation> operations)
        {
            ImageItem.Init();
            var task = ImageItem.DownloadBitmapForListAsync();

            var folder = await AppSettings.Instance.GetSavingFolderAsync();

            var item = await folder.TryGetItemAsync(ImageItem.GetFileNameForDownloading());

            if (item != null)
            {
                if (item is StorageFile file)
                {
                    _resultFile = file;
                    var prop = await _resultFile.GetBasicPropertiesAsync();

                    if (prop.Size > 0)
                    {
                        Progress = 100;
                    }
                }
            }
            if (Progress != 100)
            {
                var downloadOperation = operations.Where(s => s.Guid == DownloadOperationGUID).FirstOrDefault();
                if (downloadOperation != null)
                {
                    var progress = new Progress <DownloadOperation>();
                    progress.ProgressChanged += Progress_ProgressChanged;
                    _cts = new CancellationTokenSource();
                    await downloadOperation.StartAsync().AsTask(_cts.Token, progress);
                }
                else
                {
                    DisplayIndex = (int)DisplayMenu.Retry;
                }
            }
        }
Example #2
0
        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);
            }
        }
Example #3
0
        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);
            }
        }