Refactor busy state and progress reporting

This commit is contained in:
Oleksii Holub 2018-12-11 18:03:32 +02:00
parent dddb13fcc5
commit bdaeea5512

View file

@ -121,25 +121,26 @@ namespace DiscordChatExporter.Gui.ViewModels
public async void PopulateGuildsAndChannels() public async void PopulateGuildsAndChannels()
{ {
IsEnabled = false;
Progress = -1;
// Sanitize token
TokenValue = TokenValue.Trim('"');
// Create token
var token = new AuthToken(
IsBotToken ? AuthTokenType.Bot : AuthTokenType.User,
TokenValue);
// Save token
_settingsService.LastToken = token;
// Clear existing
_guildChannelsMap.Clear();
try try
{ {
// Set busy state and indeterminate progress
IsEnabled = false;
Progress = -1;
// Sanitize token
TokenValue = TokenValue.Trim('"');
// Create token
var token = new AuthToken(
IsBotToken ? AuthTokenType.Bot : AuthTokenType.User,
TokenValue);
// Save token
_settingsService.LastToken = token;
// Clear guild to channel map
_guildChannelsMap.Clear();
// 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;
// Get last used token
var token = _settingsService.LastToken;
// Create dialog
var dialog = _viewModelFactory.CreateExportSetupViewModel();
dialog.Guild = SelectedGuild;
dialog.Channel = channel;
// Show dialog
if (await _dialogManager.ShowDialogAsync(dialog) == true)
{ {
// Set busy state and indeterminate progress
IsEnabled = false;
Progress = -1;
// Get last used token
var token = _settingsService.LastToken;
// Create dialog
var dialog = _viewModelFactory.CreateExportSetupViewModel();
dialog.Guild = SelectedGuild;
dialog.Channel = channel;
// Show dialog, if canceled - return
if (await _dialogManager.ShowDialogAsync(dialog) != true)
return;
// Create progress handler
var progressHandler = new Progress<double>(p => Progress = p);
// Get chat log
var chatLog = await _dataService.GetChatLogAsync(token, dialog.Guild, dialog.Channel,
dialog.From, dialog.To, progressHandler);
// Export // Export
try _exportService.ExportChatLog(chatLog, dialog.FilePath, dialog.SelectedFormat,
{ dialog.PartitionLimit);
// Create progress handler
var progressHandler = new Progress<double>(p => Progress = p);
// Get chat log // Notify completion
var chatLog = await _dataService.GetChatLogAsync(token, dialog.Guild, dialog.Channel, Notifications.Enqueue("Export complete");
dialog.From, dialog.To, progressHandler); }
catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Forbidden)
// Export {
_exportService.ExportChatLog(chatLog, dialog.FilePath, dialog.SelectedFormat, Notifications.Enqueue("You don't have access to this channel");
dialog.PartitionLimit); }
catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
// Notify completion {
Notifications.Enqueue("Export complete"); Notifications.Enqueue("This channel doesn't exist");
} }
catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Forbidden) finally
{ {
Notifications.Enqueue("You don't have access to this channel"); // Reset busy state and progress
} Progress = 0;
catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.NotFound) IsEnabled = true;
{
Notifications.Enqueue("This channel doesn't exist");
}
} }
Progress = 0;
IsEnabled = true;
} }
} }
} }