From b992894f7966723bd016d063ba82aeb23a3fb3cf Mon Sep 17 00:00:00 2001 From: Florian Stinglmayr Date: Fri, 13 Aug 2021 09:30:06 +0200 Subject: [PATCH] add a little progress dialog while downloading the database --- EDDB/API.cs | 20 +++++++++++++++++++- MainWindow.xaml.cs | 5 +++-- ProgressDialog.xaml | 21 +++++++++++++++++++++ ProgressDialog.xaml.cs | 30 ++++++++++++++++++++++++++++++ nonabgs.csproj | 7 +++++++ 5 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 ProgressDialog.xaml create mode 100644 ProgressDialog.xaml.cs diff --git a/EDDB/API.cs b/EDDB/API.cs index 0209546..8acfd29 100644 --- a/EDDB/API.cs +++ b/EDDB/API.cs @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using System.IO; using System.Net; using System.Collections.Generic; @@ -17,10 +18,14 @@ namespace NonaBGS.EDDB { private string stations_file_short = null; public delegate void DatabaseAvailableDelegate(); + public delegate void DatabaseUpdateProgressDelegate(); public event DatabaseAvailableDelegate SystemsAvailable; public event DatabaseAvailableDelegate StationsAvailable; + public event DatabaseUpdateProgressDelegate DatabaseUpdateProgress; + public event DatabaseUpdateProgressDelegate DatabaseUpdateFinished; + public string SystemsFile => systems_file; public string StationsFile => stations_file; public string StationsFileShort => stations_file_short; @@ -39,14 +44,26 @@ namespace NonaBGS.EDDB { systems_file = Path.Combine(this.cache_folder, "systems_populated.json"); stations_file = Path.Combine(this.cache_folder, "stations.json"); stations_file_short = Path.Combine(this.cache_folder, "stations_short.json"); + + this.StationsAvailable += API_StationsAvailable; + } + + private void API_StationsAvailable() { + TranslateStations(); + DatabaseUpdateFinished?.Invoke(); } private void DownloadFile(string url, string file, DatabaseAvailableDelegate notifier) { WebClient client = new WebClient(); client.DownloadFileCompleted += Client_DownloadFileCompleted; + client.DownloadProgressChanged += Client_DownloadProgressChanged; client.DownloadFileAsync(new Uri(url), file, notifier); } + private void Client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { + DatabaseUpdateProgress?.Invoke(); + } + private void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { DatabaseAvailableDelegate notifier = e.UserState as DatabaseAvailableDelegate; notifier?.Invoke(); @@ -69,7 +86,6 @@ namespace NonaBGS.EDDB { using (var str = new StreamReader(StationsFile)) { using (var reader = new JsonTextReader(str)) { JArray obj = (JArray)JToken.ReadFrom(reader); - foreach (JObject child in obj.Children()) { int system_id = child.Value("system_id"); @@ -79,6 +95,8 @@ namespace NonaBGS.EDDB { systems.Add(system_id, new List()); } + DatabaseUpdateProgress?.Invoke(); + systems[system_id].Add(name); } } diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 2f3c666..1127140 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -236,8 +236,9 @@ namespace NonaBGS { return; } - // Force download - api.Download(true); + ProgressDialog dialog = new ProgressDialog(api); + dialog.StartDownload(); + dialog.ShowDialog(); } } } diff --git a/ProgressDialog.xaml b/ProgressDialog.xaml new file mode 100644 index 0000000..6bcd30b --- /dev/null +++ b/ProgressDialog.xaml @@ -0,0 +1,21 @@ + + + + + + + + + + + + diff --git a/ProgressDialog.xaml.cs b/ProgressDialog.xaml.cs new file mode 100644 index 0000000..af63f55 --- /dev/null +++ b/ProgressDialog.xaml.cs @@ -0,0 +1,30 @@ +using System.Windows; +using NonaBGS.EDDB; + +namespace NonaBGS { + /// + /// Interaction logic for Window1.xaml + /// + public partial class ProgressDialog : Window { + private readonly API api = null; + + public ProgressDialog(API api) { + InitializeComponent(); + this.api = api; + this.api.DatabaseUpdateFinished += Api_DatabaseUpdateFinished; + this.api.DatabaseUpdateProgress += Api_DatabaseUpdateProgress; + } + + private void Api_DatabaseUpdateProgress() { + progress.Value = (progress.Value + 1) % 100; + } + + private void Api_DatabaseUpdateFinished() { + Close(); + } + + public void StartDownload() { + api.Download(true); + } + } +} diff --git a/nonabgs.csproj b/nonabgs.csproj index 123953f..94e91a8 100644 --- a/nonabgs.csproj +++ b/nonabgs.csproj @@ -86,6 +86,9 @@ + + ProgressDialog.xaml + MSBuild:Compile Designer @@ -113,6 +116,10 @@ MainWindow.xaml Code + + Designer + MSBuild:Compile +