private void JobStarted(ProgressJob job) { JobTracker tracker = new JobTracker { Job = job }; jobs.Add(job, tracker); Dispatcher?.InvokeOrExecute(delegate { var p = new StackPanel(); tracker.Container = p; // New progress bar ProgressBar bar = new ProgressBar { Minimum = 0, Maximum = job.ExpectedSize, Height = 25, Background = Brushes.LightGray, }; // Bind the Progress value to the Value property bar.SetBinding(ProgressBar.ValueProperty, new Binding("ProgressCompleted") { Source = job, Mode = BindingMode.OneWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, }); var colorConverter = new DownloadProgressColorConverter(); bar.SetBinding(ProgressBar.ForegroundProperty, new Binding("Status") { Source = job, Mode = BindingMode.OneWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, Converter = colorConverter, }); bar.MouseDoubleClick += (s, a) => job.Cancel(); TextBlock t = new TextBlock(); t.SetBinding(TextBlock.TextProperty, new Binding("ProgressSinceLastUpdate") { Source = job, Mode = BindingMode.OneWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, Converter = new DownloadProgressTextConverter(job) }); p.Children.Add(bar); p.Children.Add(t); p.UpdateLayout(); DownloadsPanel.Children.Insert(0, p); DownloadsPanel.ScrollOwner?.ScrollToTop(); DownloadsPanel.UpdateLayout(); }); if (job is FileWriteJob dj) { string kind = dj is DownloadJob ? "download" : "file write"; if (job.ProgressCompleted == 0) { logger.Info($"Starting {kind} of size {Miscellaneous.ToFileSize(job.ExpectedSize)}, File: '{dj.FileName}'."); } else { logger.Info($"Resuming {kind} at {Miscellaneous.ToFileSize(job.ProgressCompleted)}/{Miscellaneous.ToFileSize(job.ExpectedSize)}, File: '{dj.FileName}'."); } } else { logger.Info($"Starting job '{job.JobName}'"); } }
private async Task DownloadAsync() { isDownloading = true; while (_DownloadsPanel.Count > 0) { //get first Queued in list DownloadProgressViewModel item = null; for (int i = 0; i < _DownloadsPanel.Count; i++) { if (_DownloadsPanel[i].DownloadState == DownloadState.Queued) { item = _DownloadsPanel[i]; break; } } //if no items queued if (item == null) { isDownloading = false; return; } //if download state is wait while (PlayPackIcon == PackIconKind.Play) { await Task.Delay(1000); } //get all page paths of chapter List <string> pagePaths = await GetPagePaths(item.url); //set progress bar end item.Maximum = pagePaths.Count; //Change download State item.DownloadState = DownloadState.InProgress; //download all undownloaded files for (int i = 0; i < pagePaths.Count; i++) { //if download state is wait while (PlayPackIcon == PackIconKind.Play) { await Task.Delay(1000); } if (!(item.tempPaths.Count >= i + 1 && item.tempPaths[i] != null)) { //Download data to a byte array byte[] array = DataConversionHelper.DownloadToArray(pagePaths[i]); //Save to a temporary file string path = SaveByteArrayToTempFile(array); //Remember path item.tempPaths.Add(path); } //advance download progress item.Progress += 1; } //Create new ch class ChSave chSave = new ChSave(LoadPaths(item.tempPaths)); //create path for download string savePath = MainView.Settings.mangaPath + item.Header + "/" + item.Description.Replace(" ", "_") + ".ch"; //Save file to path SaveSystem.SaveBinary(chSave, savePath); //dispose of save class chSave = null; //Set download status to complete item.DownloadState = DownloadState.Completed; //notify user according to settings Application.Current.Dispatcher.Invoke(() => { if (MainView.Settings.chapterDownloadNotifications) { MainView.NotificationsViewModel.AddNotification(item.Header + ": " + item.Description + " Download Complete.", NotificationMode.Success); } if (MainView.Settings.autoDeleteCompletedDownloads) { DownloadsPanel.Remove(item); } }); } isDownloading = false; if (MainView.Settings.downloadTaskNotifications) { Application.Current.Dispatcher.Invoke(() => { MainView.NotificationsViewModel.AddNotification("Download Task complete.", NotificationMode.Normal); }); } }