add a little progress dialog while downloading the database

This commit is contained in:
Florian Stinglmayr 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);
}
}

View File

@ -236,8 +236,9 @@ namespace NonaBGS {
return;
}
// Force download
api.Download(true);
ProgressDialog dialog = new ProgressDialog(api);
dialog.StartDownload();
dialog.ShowDialog();
}
}
}

21
ProgressDialog.xaml Normal file
View File

@ -0,0 +1,21 @@
<Window x:Class="NonaBGS.ProgressDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:NonaBGS"
mc:Ignorable="d"
Title="Progress" Height="100" Width="450">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Content="Downloading EDDB databases might take a while, please be patient." Grid.Row="0" Grid.Column="0" HorizontalAlignment="Center" Margin="0,0,0,0" VerticalAlignment="Top"/>
<ProgressBar x:Name="progress" Height="25" Margin="10,10" Grid.Row="1" Grid.Column="0" VerticalAlignment="Top" Width="Auto" IsIndeterminate="True"/>
</Grid>
</Window>

30
ProgressDialog.xaml.cs Normal file
View File

@ -0,0 +1,30 @@
using System.Windows;
using NonaBGS.EDDB;
namespace NonaBGS {
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
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);
}
}
}

View File

@ -86,6 +86,9 @@
<Compile Include="Util\AppConfig.cs" />
<Compile Include="Util\Config.cs" />
<Compile Include="EDDB\API.cs" />
<Compile Include="ProgressDialog.xaml.cs">
<DependentUpon>ProgressDialog.xaml</DependentUpon>
</Compile>
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
@ -113,6 +116,10 @@
<DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Page Include="ProgressDialog.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs">