From 5528231cc2717c0461ce47a590052cba4f244fd3 Mon Sep 17 00:00:00 2001 From: Bishbash777 <50243964+Bishbash777@users.noreply.github.com> Date: Wed, 28 Jul 2021 09:04:08 +0100 Subject: [PATCH] Create PluginDownloader.xaml.cs --- Torch.Server/Views/PluginDownloader.xaml.cs | 78 +++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 Torch.Server/Views/PluginDownloader.xaml.cs diff --git a/Torch.Server/Views/PluginDownloader.xaml.cs b/Torch.Server/Views/PluginDownloader.xaml.cs new file mode 100644 index 0000000..e8e4495 --- /dev/null +++ b/Torch.Server/Views/PluginDownloader.xaml.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using Torch.API.WebAPI; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Shapes; +using System.ComponentModel; + +namespace Torch.Server.Views +{ + + /// + /// Interaction logic for PluginDownloadProgressBar.xaml + /// + public partial class PluginDownloader : Window + { + + private bool downloadNoFailures = true; + private int successfulDownloads = 0; + private int failedDownloads = 0; + private IList PluginsToDownload; + + public PluginDownloader(IList SelectedItems) { + InitializeComponent(); + PluginsToDownload = SelectedItems; + } + + + private void DownloadProgress_ContentRendered(object sender, EventArgs e) { + BackgroundWorker worker = new BackgroundWorker(); + worker.WorkerReportsProgress = true; + worker.DoWork += DownloadPlugins; + worker.ProgressChanged += PluginDownloaded; + worker.RunWorkerCompleted += DownloadCompleted; + + worker.RunWorkerAsync(); + } + + void DownloadPlugins (object sender, DoWorkEventArgs e) { + var DownloadProgress = 0; + var PercentChangeOnDownload = 100 / PluginsToDownload.Count; + + foreach (PluginItem PluginItem in PluginsToDownload) { + if (!Task.Run(async () => await PluginQuery.Instance.DownloadPlugin(PluginItem.ID)).Result) { + failedDownloads++; + DownloadProgress += PercentChangeOnDownload; + (sender as BackgroundWorker).ReportProgress(DownloadProgress); + continue; + } + DownloadProgress += PercentChangeOnDownload; + (sender as BackgroundWorker).ReportProgress(DownloadProgress); + successfulDownloads++; + } + (sender as BackgroundWorker).ReportProgress(100); + } + + void PluginDownloaded(object sender, ProgressChangedEventArgs e) { + downloadProgress.Value = e.ProgressPercentage; + } + + void DownloadCompleted(object sender, RunWorkerCompletedEventArgs e) { + MessageBox.Show(downloadNoFailures ? $"{successfulDownloads} out of {PluginsToDownload.Count} Plugin(s) downloaded successfully! Please restart the server to load changes." + : $"{failedDownloads} out of {PluginsToDownload.Count} Plugin(s) failed to download! See log for details.", + "Plugin Downloader", + MessageBoxButton.OK, downloadNoFailures ? MessageBoxImage.Information : MessageBoxImage.Warning); + Close(); + } + } +}