From bcdb1dd246646dd66c33ccf56a88c457cdefab43 Mon Sep 17 00:00:00 2001 From: Mateusz Skoczek Date: Thu, 3 Mar 2022 00:34:52 +0100 Subject: [PATCH] 1.0-dev11 (Notifications added, removing task after success option added, deleting video temp after error option added) --- VDownload.Core/Services/Config.cs | 7 ++- VDownload.Core/Services/Sources/Twitch/Vod.cs | 14 +++-- VDownload/App.xaml.cs | 3 +- VDownload/Strings/en-US/Resources.resw | 18 ++++++ VDownload/VDownload.csproj | 3 + VDownload/Views/Home/HomeMain.xaml.cs | 2 +- VDownload/Views/Home/HomeTaskPanel.xaml.cs | 57 +++++++++++++++---- 7 files changed, 85 insertions(+), 19 deletions(-) diff --git a/VDownload.Core/Services/Config.cs b/VDownload.Core/Services/Config.cs index 126a032..338449f 100644 --- a/VDownload.Core/Services/Config.cs +++ b/VDownload.Core/Services/Config.cs @@ -17,7 +17,7 @@ namespace VDownload.Core.Services { { "delete_temp_on_start", true }, { "twitch_vod_passive_trim", true }, - { "twitch_vod_downloading_chunk_retry_after_error", true }, + { "twitch_vod_downloading_chunk_retry_after_error", false }, { "twitch_vod_downloading_chunk_max_retries", 10 }, { "twitch_vod_downloading_chunk_retries_delay", 5000 }, { "media_transcoding_use_hardware_acceleration", true }, @@ -32,7 +32,10 @@ namespace VDownload.Core.Services { "custom_temp_location", false }, { "max_active_video_task", 5 }, { "replace_output_file_if_exists", false }, - { "remove_task_when_successfully_ended", false } + { "remove_task_when_successfully_ended", false }, + { "delete_task_temp_when_ended_with_error", false }, + { "show_notification_when_task_ended_successfully", false }, + { "show_notification_when_task_ended_unsuccessfully", false } }; #endregion diff --git a/VDownload.Core/Services/Sources/Twitch/Vod.cs b/VDownload.Core/Services/Sources/Twitch/Vod.cs index 83e0841..a8c30f7 100644 --- a/VDownload.Core/Services/Sources/Twitch/Vod.cs +++ b/VDownload.Core/Services/Sources/Twitch/Vod.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.Diagnostics; using System.Linq; using System.Net; using System.Text.RegularExpressions; @@ -266,7 +267,7 @@ namespace VDownload.Core.Services.Sources.Twitch private static async Task DownloadChunkAsync(Uri chunkUrl, CancellationToken cancellationToken = default) { int retriesCount = 0; - while ((bool)Config.GetValue("twitch_vod_downloading_chunk_retry_after_error") && retriesCount < (int)Config.GetValue("twitch_vod_downloading_chunk_max_retries")) + while (true) { cancellationToken.ThrowIfCancellationRequested(); try @@ -276,13 +277,16 @@ namespace VDownload.Core.Services.Sources.Twitch return await client.DownloadDataTaskAsync(chunkUrl); } } - catch + catch (WebException wex) { - retriesCount++; - await Task.Delay((int)Config.GetValue("twitch_vod_downloading_chunk_retries_delay")); + if ((bool)Config.GetValue("twitch_vod_downloading_chunk_retry_after_error") && retriesCount < (int)Config.GetValue("twitch_vod_downloading_chunk_max_retries")) + { + retriesCount++; + await Task.Delay((int)Config.GetValue("twitch_vod_downloading_chunk_retries_delay")); + } + else throw wex; } } - throw new WebException("An error occurs while downloading a Twitch VOD chunk"); } // WRITE CHUNK TO FILE diff --git a/VDownload/App.xaml.cs b/VDownload/App.xaml.cs index f0c16c6..04d3cdb 100644 --- a/VDownload/App.xaml.cs +++ b/VDownload/App.xaml.cs @@ -35,7 +35,8 @@ namespace VDownload protected override async void OnLaunched(LaunchActivatedEventArgs e) { // Rebuild configuration file - Config.Rebuild(); + //Config.Rebuild(); + Config.SetDefault(); // Delete temp on start if ((bool)Config.GetValue("delete_temp_on_start")) diff --git a/VDownload/Strings/en-US/Resources.resw b/VDownload/Strings/en-US/Resources.resw index 8d95290..62652c4 100644 --- a/VDownload/Strings/en-US/Resources.resw +++ b/VDownload/Strings/en-US/Resources.resw @@ -205,6 +205,9 @@ The number in the numberbox indicades how many videos will be got from playlist. Downloading + + An error occured + Finalizing @@ -259,6 +262,18 @@ The number in the numberbox indicades how many videos will be got from playlist. Only video + + downloading ended successfully + + + Video downloading ended successfully + + + an error occured + + + Video downloading ended unsuccessfully + Sources @@ -295,4 +310,7 @@ The number in the numberbox indicades how many videos will be got from playlist. Not logged in. Twitch authentication is required to download videos. + + Internet connection error + \ No newline at end of file diff --git a/VDownload/VDownload.csproj b/VDownload/VDownload.csproj index 1cff800..745fb9b 100644 --- a/VDownload/VDownload.csproj +++ b/VDownload/VDownload.csproj @@ -322,6 +322,9 @@ 6.2.13 + + 7.1.2 + 7.1.2 diff --git a/VDownload/Views/Home/HomeMain.xaml.cs b/VDownload/Views/Home/HomeMain.xaml.cs index a691f85..ae71e79 100644 --- a/VDownload/Views/Home/HomeMain.xaml.cs +++ b/VDownload/Views/Home/HomeMain.xaml.cs @@ -138,7 +138,7 @@ namespace VDownload.Views.Home catch (WebException wex) { HomeOptionsBarSearchingStatusControl.Content = HomeOptionsBarSearchingStatusErrorImage; - if (wex.Response == null) + if (wex.Response is null) { ContentDialog internetAccessErrorDialog = new ContentDialog { diff --git a/VDownload/Views/Home/HomeTaskPanel.xaml.cs b/VDownload/Views/Home/HomeTaskPanel.xaml.cs index e619e78..d69c4a7 100644 --- a/VDownload/Views/Home/HomeTaskPanel.xaml.cs +++ b/VDownload/Views/Home/HomeTaskPanel.xaml.cs @@ -1,6 +1,8 @@ -using System; +using Microsoft.Toolkit.Uwp.Notifications; +using System; using System.Diagnostics; using System.Linq; +using System.Net; using System.Threading; using System.Threading.Tasks; using VDownload.Core.Enums; @@ -53,7 +55,7 @@ namespace VDownload.Views.Home Location = location; // Set metadata - ThumbnailImage = (BitmapImage)ImagesRes["UnknownThumbnailImage"]; + ThumbnailImage = VideoService.Thumbnail != null ? new BitmapImage { UriSource = VideoService.Thumbnail } : (BitmapImage)ImagesRes["UnknownThumbnailImage"]; SourceImage = new BitmapIcon { UriSource = new Uri($"ms-appx:///Assets/Sources/{VideoService.GetType().Namespace.Split(".").Last()}.png"), ShowAsMonochrome = false }; Title = VideoService.Title; Author = VideoService.Author; @@ -144,6 +146,8 @@ namespace VDownload.Views.Home tempFolder = ApplicationData.Current.TemporaryFolder; tempFolder = await tempFolder.CreateFolderAsync(uniqueID); + bool endedWithError = false; + try { // Throw exception if cancellation requested @@ -212,6 +216,12 @@ namespace VDownload.Views.Home HomeTaskPanelStateText.Text = $"{ResourceLoader.GetForCurrentView().GetString("HomeTaskPanelStateTextDone")} ({(Math.Floor(taskStopwatch.Elapsed.TotalHours) > 0 ? $"{ Math.Floor(taskStopwatch.Elapsed.TotalHours):0}:" : "")}{taskStopwatch.Elapsed.Minutes:00}:{taskStopwatch.Elapsed.Seconds:00})"; HomeTaskPanelStateProgressBar.Visibility = Visibility.Collapsed; + // Show notification + if ((bool)Config.GetValue("show_notification_when_task_ended_successfully")) + new ToastContentBuilder() + .AddText(ResourceLoader.GetForCurrentView().GetString("NotificationTaskEndedSuccessfullyHeader")) + .AddText($"\"{Title}\" - {ResourceLoader.GetForCurrentView().GetString("NotificationTaskEndedSuccessfullyDescription")}") + .Show(); } catch (OperationCanceledException) { @@ -220,19 +230,46 @@ namespace VDownload.Views.Home HomeTaskPanelStateText.Text = ResourceLoader.GetForCurrentView().GetString("HomeTaskPanelStateTextCancelled"); HomeTaskPanelStateProgressBar.Visibility = Visibility.Collapsed; } + catch (WebException ex) + { + string errorInfo; + if (ex.Response is null) errorInfo = ResourceLoader.GetForCurrentView().GetString("TaskErrorInternetConnection"); + else throw ex; + + // Set state controls + HomeTaskPanelStateIcon.Source = (SvgImageSource)IconsRes["StateErrorIcon"]; + HomeTaskPanelStateText.Text = $"{ResourceLoader.GetForCurrentView().GetString("HomeTaskPanelStateTextError")} ({errorInfo})"; + HomeTaskPanelStateProgressBar.Visibility = Visibility.Collapsed; + + // Show notification + if ((bool)Config.GetValue("show_notification_when_task_ended_unsuccessfully")) + new ToastContentBuilder() + .AddText(ResourceLoader.GetForCurrentView().GetString("NotificationTaskEndedUnsuccessfullyHeader")) + .AddText($"\"{Title}\" - {ResourceLoader.GetForCurrentView().GetString("NotificationTaskEndedUnsuccessfullyDescription")} ({errorInfo})") + .Show(); + } finally { - // Change icon - HomeTaskPanelStartStopButton.Icon = new SymbolIcon(Symbol.Download); - // Set video status TaskStatus = Core.Enums.TaskStatus.Idle; - // Delete temporary files - await tempFolder.DeleteAsync(); + // Change icon + HomeTaskPanelStartStopButton.Icon = new SymbolIcon(Symbol.Download); - // Dispose unique id - TaskId.Dispose(uniqueID); + if (!endedWithError || (bool)Config.GetValue("delete_task_temp_when_ended_with_error")) + { + // Delete temporary files + await tempFolder.DeleteAsync(); + + // Dispose unique id + TaskId.Dispose(uniqueID); + } + + if (!endedWithError && !CancellationTokenSource.IsCancellationRequested && (bool)Config.GetValue("remove_task_when_successfully_ended")) + { + // Remove task when successfully ended + TaskRemovingRequested?.Invoke(this, EventArgs.Empty); + } } } else @@ -268,7 +305,7 @@ namespace VDownload.Views.Home private void HomeTaskPanelRemoveButton_Click(object sender, RoutedEventArgs e) { if (TaskStatus == Core.Enums.TaskStatus.InProgress || TaskStatus == Core.Enums.TaskStatus.Waiting) CancellationTokenSource.Cancel(); - TaskRemovingRequested?.Invoke(sender, EventArgs.Empty); + TaskRemovingRequested?.Invoke(this, EventArgs.Empty); } #endregion