Refactor busy state and progress reporting

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

@ -121,25 +121,26 @@ namespace DiscordChatExporter.Gui.ViewModels
public async void PopulateGuildsAndChannels() public async void PopulateGuildsAndChannels()
{ {
IsEnabled = false; try
Progress = -1; {
// Set busy state and indeterminate progress
IsEnabled = false;
Progress = -1;
// Sanitize token // Sanitize token
TokenValue = TokenValue.Trim('"'); TokenValue = TokenValue.Trim('"');
// Create token // Create token
var token = new AuthToken( var token = new AuthToken(
IsBotToken ? AuthTokenType.Bot : AuthTokenType.User, IsBotToken ? AuthTokenType.Bot : AuthTokenType.User,
TokenValue); TokenValue);
// 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,61 +174,64 @@ 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)
{ {
IsEnabled = false; try
Progress = -1; {
// Set busy state and indeterminate progress
IsEnabled = false;
Progress = -1;
// Get last used token // Get last used token
var token = _settingsService.LastToken; var token = _settingsService.LastToken;
// Create dialog // Create dialog
var dialog = _viewModelFactory.CreateExportSetupViewModel(); var dialog = _viewModelFactory.CreateExportSetupViewModel();
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
var progressHandler = new Progress<double>(p => Progress = p);
// Get chat log // Create progress handler
var chatLog = await _dataService.GetChatLogAsync(token, dialog.Guild, dialog.Channel, var progressHandler = new Progress<double>(p => Progress = p);
dialog.From, dialog.To, progressHandler);
// Export // Get chat log
_exportService.ExportChatLog(chatLog, dialog.FilePath, dialog.SelectedFormat, var chatLog = await _dataService.GetChatLogAsync(token, dialog.Guild, dialog.Channel,
dialog.PartitionLimit); dialog.From, dialog.To, progressHandler);
// Notify completion // Export
Notifications.Enqueue("Export complete"); _exportService.ExportChatLog(chatLog, dialog.FilePath, dialog.SelectedFormat,
} dialog.PartitionLimit);
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");
}
}
Progress = 0; // Notify completion
IsEnabled = true; 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;
}
} }
} }
} }
Loading…
Cancel
Save