Archived
1
0

add a little progress dialog while downloading the database

This commit is contained in:
2021-08-13 09:30:06 +02:00
parent e1b1ff316e
commit b992894f79
5 changed files with 80 additions and 3 deletions

View File

@@ -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<JObject>()) {
int system_id = child.Value<int>("system_id");
@@ -79,6 +95,8 @@ namespace NonaBGS.EDDB {
systems.Add(system_id, new List<string>());
}
DatabaseUpdateProgress?.Invoke();
systems[system_id].Add(name);
}
}