Refactor busy state and progress reporting

pull/123/head
Oleksii Holub 6 years ago
parent dddb13fcc5
commit bdaeea5512

@ -121,6 +121,9 @@ namespace DiscordChatExporter.Gui.ViewModels
public async void PopulateGuildsAndChannels() public async void PopulateGuildsAndChannels()
{ {
try
{
// Set busy state and indeterminate progress
IsEnabled = false; IsEnabled = false;
Progress = -1; Progress = -1;
@ -135,11 +138,9 @@ namespace DiscordChatExporter.Gui.ViewModels
// Save token // Save token
_settingsService.LastToken = token; _settingsService.LastToken = token;
// Clear existing // Clear guild to channel map
_guildChannelsMap.Clear(); _guildChannelsMap.Clear();
try
{
// Get DM channels // Get DM channels
{ {
var channels = await _dataService.GetDirectMessageChannelsAsync(token); var channels = await _dataService.GetDirectMessageChannelsAsync(token);
@ -158,6 +159,12 @@ namespace DiscordChatExporter.Gui.ViewModels
.ToArray(); .ToArray();
} }
} }
// Update available guilds
AvailableGuilds = _guildChannelsMap.Keys.ToArray();
// Select the first guild
SelectedGuild = AvailableGuilds.FirstOrDefault();
} }
catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Unauthorized) catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Unauthorized)
{ {
@ -167,18 +174,21 @@ namespace DiscordChatExporter.Gui.ViewModels
{ {
Notifications.Enqueue("Forbidden account may be locked by 2FA"); Notifications.Enqueue("Forbidden account may be locked by 2FA");
} }
finally
AvailableGuilds = _guildChannelsMap.Keys.ToArray(); {
SelectedGuild = AvailableGuilds.FirstOrDefault(); // Reset busy state and progress
Progress = 0; Progress = 0;
IsEnabled = true; IsEnabled = true;
} }
}
public bool CanExportChannel => IsEnabled; public bool CanExportChannel => IsEnabled;
public async void ExportChannel(Channel channel) public async void ExportChannel(Channel channel)
{ {
try
{
// Set busy state and indeterminate progress
IsEnabled = false; IsEnabled = false;
Progress = -1; Progress = -1;
@ -190,12 +200,10 @@ namespace DiscordChatExporter.Gui.ViewModels
dialog.Guild = SelectedGuild; dialog.Guild = SelectedGuild;
dialog.Channel = channel; dialog.Channel = channel;
// Show dialog // Show dialog, if canceled - return
if (await _dialogManager.ShowDialogAsync(dialog) == true) if (await _dialogManager.ShowDialogAsync(dialog) != true)
{ return;
// Export
try
{
// Create progress handler // Create progress handler
var progressHandler = new Progress<double>(p => Progress = p); var progressHandler = new Progress<double>(p => Progress = p);
@ -218,10 +226,12 @@ namespace DiscordChatExporter.Gui.ViewModels
{ {
Notifications.Enqueue("This channel doesn't exist"); Notifications.Enqueue("This channel doesn't exist");
} }
} finally
{
// Reset busy state and progress
Progress = 0; Progress = 0;
IsEnabled = true; IsEnabled = true;
} }
} }
} }
}
Loading…
Cancel
Save