Browse Source

Added download progress bar

Krzysztof Krysiński 2 months ago
parent
commit
58c5f424a2

+ 14 - 11
src/PixiEditor.UpdateModule/UpdateDownloader.cs

@@ -11,6 +11,8 @@ public static class UpdateDownloader
 {
     public static string DownloadLocation { get; } = Path.Join(Path.GetTempPath(), "PixiEditor");
 
+    public static event Action<double> ProgressChanged;
+
     public static async Task DownloadReleaseZip(ReleaseInfo release, string contentType, string extension)
     {
         Asset? matchingAsset = GetMatchingAsset(release, contentType);
@@ -20,16 +22,17 @@ public static class UpdateDownloader
             throw new FileNotFoundException("No matching update for your system found.");
         }
 
-        using HttpClient client = new HttpClient();
-        client.DefaultRequestHeaders.Add("User-Agent", "PixiEditor");
-        client.DefaultRequestHeaders.Add("Accept", "application/octet-stream");
-        var response = await client.GetAsync(matchingAsset.Url);
-        if (response.StatusCode == HttpStatusCode.OK)
+        using WebClient client = new WebClient();
+        client.Headers.Add("User-Agent", "PixiEditor");
+        client.Headers.Add("Accept", "application/octet-stream");
+        client.DownloadProgressChanged += (sender, args) =>
         {
-            byte[] bytes = await response.Content.ReadAsByteArrayAsync();
-            CreateTempDirectory();
-            await File.WriteAllBytesAsync(Path.Join(DownloadLocation, $"update-{release.TagName}.{extension}"), bytes);
-        }
+            ProgressChanged?.Invoke(args.ProgressPercentage);
+        };
+
+        var bytes = await client.DownloadDataTaskAsync(matchingAsset.Url);
+        CreateTempDirectory();
+        await File.WriteAllBytesAsync(Path.Join(DownloadLocation, $"update-{release.TagName}.{extension}"), bytes);
     }
 
     public static async Task DownloadInstaller(ReleaseInfo info)
@@ -70,8 +73,8 @@ public static class UpdateDownloader
                                                       && x.Name.Contains(archOld));
         }
 
-        string arch = OperatingSystem.IsWindows() ? "x64" : 
-                      OperatingSystem.IsLinux() ? "amd64" : "universal";
+        string arch = OperatingSystem.IsWindows() ? "x64" :
+            OperatingSystem.IsLinux() ? "amd64" : "universal";
         string os = OperatingSystem.IsWindows() ? "win" : OperatingSystem.IsLinux() ? "linux" : "macos";
         return release.Assets.FirstOrDefault(x => x.ContentType.Contains(assetType)
                                                   && x.Name.Contains(arch) && x.Name.Contains(os));

+ 19 - 0
src/PixiEditor/ViewModels/SubViewModels/UpdateViewModel.cs

@@ -26,6 +26,7 @@ namespace PixiEditor.ViewModels.SubViewModels;
 
 internal class UpdateViewModel : SubViewModel<ViewModelMain>
 {
+    private double currentProgress;
     public UpdateChecker UpdateChecker { get; set; }
 
     public List<UpdateChannel> UpdateChannels { get; } = new List<UpdateChannel>();
@@ -107,6 +108,16 @@ internal class UpdateViewModel : SubViewModel<ViewModelMain>
     {
         get => _updateState == UpdateState.UpToDate;
     }
+    
+    public double CurrentProgress 
+    {
+        get => currentProgress;
+        set
+        {
+            currentProgress = value;
+            OnPropertyChanged(nameof(CurrentProgress));
+        }
+    }
 
     public string ZipExtension => IOperatingSystem.Current.IsLinux ? "tar.gz" : "zip";
     public string ZipContentType => IOperatingSystem.Current.IsLinux ? "octet-stream" : "zip";
@@ -138,6 +149,13 @@ internal class UpdateViewModel : SubViewModel<ViewModelMain>
 
         Owner.OnStartupEvent += Owner_OnStartupEvent;
         Owner.OnClose += Owner_OnClose;
+        UpdateDownloader.ProgressChanged += d =>
+        {
+            Dispatcher.UIThread.InvokeAsync(() =>
+            {
+                CurrentProgress = d;
+            });
+        };
         PixiEditorSettings.Update.UpdateChannel.ValueChanged += (_, value) =>
         {
             string prevChannel = UpdateChecker.Channel.ApiUrl;
@@ -208,6 +226,7 @@ internal class UpdateViewModel : SubViewModel<ViewModelMain>
             try
             {
                 UpdateState = UpdateState.Downloading;
+                CurrentProgress = 0;
                 if (updateCompatible || !IOperatingSystem.Current.IsWindows)
                 {
                     await UpdateDownloader.DownloadReleaseZip(UpdateChecker.LatestReleaseInfo, ZipContentType,

+ 3 - 0
src/PixiEditor/Views/Main/MainTitleBar.axaml

@@ -66,6 +66,9 @@
                                         Background="{DynamicResource ThemeAccentBrush}"
                                         Command="{Binding UpdateViewModel.DownloadCommand}"
                                         IsVisible="{Binding UpdateViewModel.IsUpdateAvailable}" />
+                                <ProgressBar IsVisible="{Binding UpdateViewModel.IsDownloading}" 
+                                             ShowProgressText="True" Value="{Binding UpdateViewModel.CurrentProgress}"
+                                              Maximum="100" Minimum="0"/>
                                 <Button ui:Translator.Key="SWITCH_TO_NEW_VERSION"
                                         Background="{DynamicResource ThemeAccentBrush}"
                                         Command="{Binding UpdateViewModel.InstallCommand}"