From 2ab6773c17e8919a6f5022e21463712967cb7dbb Mon Sep 17 00:00:00 2001 From: Tyrrrz Date: Sun, 18 Jul 2021 00:02:28 +0300 Subject: [PATCH] [GUI] Show error in a dialog when pull or export fails, instead of crashing the whole app --- .../ViewModels/Dialogs/MessageBoxViewModel.cs | 27 +++++ .../ViewModels/Framework/IViewModelFactory.cs | 2 + .../ViewModels/RootViewModel.cs | 109 +++++++++++------- .../Views/Dialogs/MessageBoxView.xaml | 49 ++++++++ .../Views/Dialogs/MessageBoxView.xaml.cs | 10 ++ 5 files changed, 153 insertions(+), 44 deletions(-) create mode 100644 DiscordChatExporter.Gui/ViewModels/Dialogs/MessageBoxViewModel.cs create mode 100644 DiscordChatExporter.Gui/Views/Dialogs/MessageBoxView.xaml create mode 100644 DiscordChatExporter.Gui/Views/Dialogs/MessageBoxView.xaml.cs diff --git a/DiscordChatExporter.Gui/ViewModels/Dialogs/MessageBoxViewModel.cs b/DiscordChatExporter.Gui/ViewModels/Dialogs/MessageBoxViewModel.cs new file mode 100644 index 0000000..426fd63 --- /dev/null +++ b/DiscordChatExporter.Gui/ViewModels/Dialogs/MessageBoxViewModel.cs @@ -0,0 +1,27 @@ +using DiscordChatExporter.Gui.ViewModels.Framework; + +namespace DiscordChatExporter.Gui.ViewModels.Dialogs +{ + public class MessageBoxViewModel : DialogScreen + { + public string? Title { get; set; } + + public string? Message { get; set; } + } + + public static class MessageBoxViewModelExtensions + { + public static MessageBoxViewModel CreateMessageBoxViewModel( + this IViewModelFactory factory, + string title, + string message) + { + var viewModel = factory.CreateMessageBoxViewModel(); + + viewModel.Title = title; + viewModel.Message = message; + + return viewModel; + } + } +} \ No newline at end of file diff --git a/DiscordChatExporter.Gui/ViewModels/Framework/IViewModelFactory.cs b/DiscordChatExporter.Gui/ViewModels/Framework/IViewModelFactory.cs index e741b6d..b543038 100644 --- a/DiscordChatExporter.Gui/ViewModels/Framework/IViewModelFactory.cs +++ b/DiscordChatExporter.Gui/ViewModels/Framework/IViewModelFactory.cs @@ -7,6 +7,8 @@ namespace DiscordChatExporter.Gui.ViewModels.Framework { ExportSetupViewModel CreateExportSetupViewModel(); + MessageBoxViewModel CreateMessageBoxViewModel(); + SettingsViewModel CreateSettingsViewModel(); } } \ No newline at end of file diff --git a/DiscordChatExporter.Gui/ViewModels/RootViewModel.cs b/DiscordChatExporter.Gui/ViewModels/RootViewModel.cs index a822b29..1f898e9 100644 --- a/DiscordChatExporter.Gui/ViewModels/RootViewModel.cs +++ b/DiscordChatExporter.Gui/ViewModels/RootViewModel.cs @@ -178,6 +178,15 @@ namespace DiscordChatExporter.Gui.ViewModels { Notifications.Enqueue(ex.Message.TrimEnd('.')); } + catch (Exception ex) + { + var dialog = _viewModelFactory.CreateMessageBoxViewModel( + "Error pulling guilds and channels", + ex.ToString() + ); + + await _dialogManager.ShowDialogAsync(dialog); + } } public bool CanExportChannels => @@ -185,56 +194,68 @@ namespace DiscordChatExporter.Gui.ViewModels public async void ExportChannels() { - var token = _settingsService.LastToken; - if (token is null || SelectedGuild is null || SelectedChannels is null || !SelectedChannels.Any()) - return; - - var dialog = _viewModelFactory.CreateExportSetupViewModel(SelectedGuild, SelectedChannels); - if (await _dialogManager.ShowDialogAsync(dialog) != true) - return; + try + { + var token = _settingsService.LastToken; + if (token is null || SelectedGuild is null || SelectedChannels is null || !SelectedChannels.Any()) + return; - var exporter = new ChannelExporter(token); + var dialog = _viewModelFactory.CreateExportSetupViewModel(SelectedGuild, SelectedChannels); + if (await _dialogManager.ShowDialogAsync(dialog) != true) + return; - var operations = ProgressManager.CreateOperations(dialog.Channels!.Count); - var successfulExportCount = 0; + var exporter = new ChannelExporter(token); - await dialog.Channels.Zip(operations).ParallelForEachAsync(async tuple => - { - var (channel, operation) = tuple; + var operations = ProgressManager.CreateOperations(dialog.Channels!.Count); + var successfulExportCount = 0; - try - { - var request = new ExportRequest( - dialog.Guild!, - channel!, - dialog.OutputPath!, - dialog.SelectedFormat, - dialog.After?.Pipe(Snowflake.FromDate), - dialog.Before?.Pipe(Snowflake.FromDate), - dialog.PartitionLimit, - dialog.MessageFilter, - dialog.ShouldDownloadMedia, - _settingsService.ShouldReuseMedia, - _settingsService.DateFormat - ); - - await exporter.ExportChannelAsync(request, operation); - - Interlocked.Increment(ref successfulExportCount); - } - catch (DiscordChatExporterException ex) when (!ex.IsCritical) + await dialog.Channels.Zip(operations).ParallelForEachAsync(async tuple => { - Notifications.Enqueue(ex.Message.TrimEnd('.')); - } - finally - { - operation.Dispose(); - } - }, _settingsService.ParallelLimit.ClampMin(1)); + var (channel, operation) = tuple; + + try + { + var request = new ExportRequest( + dialog.Guild!, + channel!, + dialog.OutputPath!, + dialog.SelectedFormat, + dialog.After?.Pipe(Snowflake.FromDate), + dialog.Before?.Pipe(Snowflake.FromDate), + dialog.PartitionLimit, + dialog.MessageFilter, + dialog.ShouldDownloadMedia, + _settingsService.ShouldReuseMedia, + _settingsService.DateFormat + ); + + await exporter.ExportChannelAsync(request, operation); + + Interlocked.Increment(ref successfulExportCount); + } + catch (DiscordChatExporterException ex) when (!ex.IsCritical) + { + Notifications.Enqueue(ex.Message.TrimEnd('.')); + } + finally + { + operation.Dispose(); + } + }, _settingsService.ParallelLimit.ClampMin(1)); + + // Notify of overall completion + if (successfulExportCount > 0) + Notifications.Enqueue($"Successfully exported {successfulExportCount} channel(s)"); + } + catch (Exception ex) + { + var dialog = _viewModelFactory.CreateMessageBoxViewModel( + "Error exporting channel(s)", + ex.ToString() + ); - // Notify of overall completion - if (successfulExportCount > 0) - Notifications.Enqueue($"Successfully exported {successfulExportCount} channel(s)"); + await _dialogManager.ShowDialogAsync(dialog); + } } } } \ No newline at end of file diff --git a/DiscordChatExporter.Gui/Views/Dialogs/MessageBoxView.xaml b/DiscordChatExporter.Gui/Views/Dialogs/MessageBoxView.xaml new file mode 100644 index 0000000..a17421d --- /dev/null +++ b/DiscordChatExporter.Gui/Views/Dialogs/MessageBoxView.xaml @@ -0,0 +1,49 @@ + + + + + + + + + + + + + + + +