From bdaeea55123243c0925bc7a4be5f86a5b27e20f2 Mon Sep 17 00:00:00 2001 From: Oleksii Holub Date: Tue, 11 Dec 2018 18:03:32 +0200 Subject: [PATCH] Refactor busy state and progress reporting --- .../ViewModels/RootViewModel.cs | 122 ++++++++++-------- 1 file changed, 66 insertions(+), 56 deletions(-) diff --git a/DiscordChatExporter.Gui/ViewModels/RootViewModel.cs b/DiscordChatExporter.Gui/ViewModels/RootViewModel.cs index a66688e..2492d1e 100644 --- a/DiscordChatExporter.Gui/ViewModels/RootViewModel.cs +++ b/DiscordChatExporter.Gui/ViewModels/RootViewModel.cs @@ -121,25 +121,26 @@ namespace DiscordChatExporter.Gui.ViewModels public async void PopulateGuildsAndChannels() { - IsEnabled = false; - Progress = -1; + try + { + // Set busy state and indeterminate progress + IsEnabled = false; + Progress = -1; - // Sanitize token - TokenValue = TokenValue.Trim('"'); + // Sanitize token + TokenValue = TokenValue.Trim('"'); - // Create token - var token = new AuthToken( - IsBotToken ? AuthTokenType.Bot : AuthTokenType.User, - TokenValue); + // Create token + var token = new AuthToken( + IsBotToken ? AuthTokenType.Bot : AuthTokenType.User, + TokenValue); - // Save token - _settingsService.LastToken = token; + // Save token + _settingsService.LastToken = token; - // Clear existing - _guildChannelsMap.Clear(); + // Clear guild to channel map + _guildChannelsMap.Clear(); - try - { // Get DM channels { var channels = await _dataService.GetDirectMessageChannelsAsync(token); @@ -158,6 +159,12 @@ namespace DiscordChatExporter.Gui.ViewModels .ToArray(); } } + + // Update available guilds + AvailableGuilds = _guildChannelsMap.Keys.ToArray(); + + // Select the first guild + SelectedGuild = AvailableGuilds.FirstOrDefault(); } catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Unauthorized) { @@ -167,61 +174,64 @@ namespace DiscordChatExporter.Gui.ViewModels { Notifications.Enqueue("Forbidden – account may be locked by 2FA"); } - - AvailableGuilds = _guildChannelsMap.Keys.ToArray(); - SelectedGuild = AvailableGuilds.FirstOrDefault(); - - Progress = 0; - IsEnabled = true; + finally + { + // Reset busy state and progress + Progress = 0; + IsEnabled = true; + } } public bool CanExportChannel => IsEnabled; public async void ExportChannel(Channel channel) { - IsEnabled = false; - Progress = -1; + try + { + // Set busy state and indeterminate progress + IsEnabled = false; + Progress = -1; - // Get last used token - var token = _settingsService.LastToken; + // Get last used token + var token = _settingsService.LastToken; - // Create dialog - var dialog = _viewModelFactory.CreateExportSetupViewModel(); - dialog.Guild = SelectedGuild; - dialog.Channel = channel; + // Create dialog + var dialog = _viewModelFactory.CreateExportSetupViewModel(); + dialog.Guild = SelectedGuild; + dialog.Channel = channel; - // Show dialog - if (await _dialogManager.ShowDialogAsync(dialog) == true) - { - // Export - try - { - // Create progress handler - var progressHandler = new Progress(p => Progress = p); + // Show dialog, if canceled - return + if (await _dialogManager.ShowDialogAsync(dialog) != true) + return; - // Get chat log - var chatLog = await _dataService.GetChatLogAsync(token, dialog.Guild, dialog.Channel, - dialog.From, dialog.To, progressHandler); + // Create progress handler + var progressHandler = new Progress(p => Progress = p); - // Export - _exportService.ExportChatLog(chatLog, dialog.FilePath, dialog.SelectedFormat, - dialog.PartitionLimit); + // Get chat log + var chatLog = await _dataService.GetChatLogAsync(token, dialog.Guild, dialog.Channel, + dialog.From, dialog.To, progressHandler); - // Notify completion - Notifications.Enqueue("Export complete"); - } - catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Forbidden) - { - Notifications.Enqueue("You don't have access to this channel"); - } - catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.NotFound) - { - Notifications.Enqueue("This channel doesn't exist"); - } - } + // Export + _exportService.ExportChatLog(chatLog, dialog.FilePath, dialog.SelectedFormat, + dialog.PartitionLimit); - Progress = 0; - IsEnabled = true; + // Notify completion + Notifications.Enqueue("Export complete"); + } + catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Forbidden) + { + Notifications.Enqueue("You don't have access to this channel"); + } + catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.NotFound) + { + Notifications.Enqueue("This channel doesn't exist"); + } + finally + { + // Reset busy state and progress + Progress = 0; + IsEnabled = true; + } } } } \ No newline at end of file