From d8db4fca903ec8165dd586cb290967141e2ade62 Mon Sep 17 00:00:00 2001 From: Alexey Golub Date: Thu, 28 Sep 2017 22:48:24 +0300 Subject: [PATCH] ExportService is pseudo async now --- DiscordChatExporter/Services/ExportService.cs | 168 +++++++++--------- .../Services/IExportService.cs | 5 +- .../ViewModels/MainViewModel.cs | 2 +- 3 files changed, 91 insertions(+), 84 deletions(-) diff --git a/DiscordChatExporter/Services/ExportService.cs b/DiscordChatExporter/Services/ExportService.cs index 20e22e04a..331b7fe64 100644 --- a/DiscordChatExporter/Services/ExportService.cs +++ b/DiscordChatExporter/Services/ExportService.cs @@ -3,6 +3,7 @@ using System.Reflection; using System.Resources; using System.Text.RegularExpressions; +using System.Threading.Tasks; using DiscordChatExporter.Models; using HtmlAgilityPack; using Tyrrrz.Extensions; @@ -18,104 +19,109 @@ public ExportService(ISettingsService settingsService) _settingsService = settingsService; } - public void Export(string filePath, ChannelChatLog log, Theme theme) + public Task ExportAsync(string filePath, ChannelChatLog log, Theme theme) { - var doc = GetTemplate(); - var style = GetStyle(theme); - var dateFormat = _settingsService.DateFormat; - - // Set theme - var themeHtml = doc.GetElementbyId("theme"); - themeHtml.InnerHtml = style; - - // Title - var titleHtml = doc.DocumentNode.Element("html").Element("head").Element("title"); - titleHtml.InnerHtml = $"{log.Guild.Name} - {log.Channel.Name}"; - - // Info - var infoHtml = doc.GetElementbyId("info"); - var infoLeftHtml = infoHtml.AppendChild(HtmlNode.CreateNode("
")); - infoLeftHtml.AppendChild(HtmlNode.CreateNode( - $"")); - var infoRightHtml = infoHtml.AppendChild(HtmlNode.CreateNode("
")); - infoRightHtml.AppendChild(HtmlNode.CreateNode( - $"
{log.Guild.Name}
")); - infoRightHtml.AppendChild(HtmlNode.CreateNode( - $"
{log.Channel.Name}
")); - infoRightHtml.AppendChild(HtmlNode.CreateNode( - $"
{log.MessageGroups.SelectMany(g => g.Messages).Count():N0} messages
")); - - // Log - var logHtml = doc.GetElementbyId("log"); - foreach (var messageGroup in log.MessageGroups) + return Task.Run(() => { - // Container - var messageHtml = logHtml.AppendChild(HtmlNode.CreateNode("
")); + var doc = GetTemplate(); + var style = GetStyle(theme); + var dateFormat = _settingsService.DateFormat; + + // Set theme + var themeHtml = doc.GetElementbyId("theme"); + themeHtml.InnerHtml = style; + + // Title + var titleHtml = doc.DocumentNode.Element("html").Element("head").Element("title"); + titleHtml.InnerHtml = $"{log.Guild.Name} - {log.Channel.Name}"; + + // Info + var infoHtml = doc.GetElementbyId("info"); + var infoLeftHtml = infoHtml.AppendChild(HtmlNode.CreateNode("
")); + infoLeftHtml.AppendChild(HtmlNode.CreateNode( + $"")); + var infoRightHtml = infoHtml.AppendChild(HtmlNode.CreateNode("
")); + infoRightHtml.AppendChild(HtmlNode.CreateNode( + $"
{log.Guild.Name}
")); + infoRightHtml.AppendChild(HtmlNode.CreateNode( + $"
{log.Channel.Name}
")); + infoRightHtml.AppendChild(HtmlNode.CreateNode( + $"
{log.MessageGroups.SelectMany(g => g.Messages).Count():N0} messages
")); + + // Log + var logHtml = doc.GetElementbyId("log"); + foreach (var messageGroup in log.MessageGroups) + { + // Container + var messageHtml = logHtml.AppendChild(HtmlNode.CreateNode("
")); - // Left - var messageLeftHtml = messageHtml.AppendChild(HtmlNode.CreateNode("
")); + // Left + var messageLeftHtml = + messageHtml.AppendChild(HtmlNode.CreateNode("
")); - // Avatar - messageLeftHtml.AppendChild( - HtmlNode.CreateNode($"")); + // Avatar + messageLeftHtml.AppendChild( + HtmlNode.CreateNode($"")); - // Right - var messageRightHtml = messageHtml.AppendChild(HtmlNode.CreateNode("
")); + // Right + var messageRightHtml = + messageHtml.AppendChild(HtmlNode.CreateNode("
")); - // Author - var authorName = HtmlDocument.HtmlEncode(messageGroup.Author.Name); - messageRightHtml.AppendChild(HtmlNode.CreateNode($"{authorName}")); + // Author + var authorName = HtmlDocument.HtmlEncode(messageGroup.Author.Name); + messageRightHtml.AppendChild(HtmlNode.CreateNode($"{authorName}")); - // Date - var timeStamp = HtmlDocument.HtmlEncode(messageGroup.TimeStamp.ToString(dateFormat)); - messageRightHtml.AppendChild(HtmlNode.CreateNode($"{timeStamp}")); + // Date + var timeStamp = HtmlDocument.HtmlEncode(messageGroup.TimeStamp.ToString(dateFormat)); + messageRightHtml.AppendChild(HtmlNode.CreateNode($"{timeStamp}")); - // Individual messages - foreach (var message in messageGroup.Messages) - { - // Content - if (message.Content.IsNotBlank()) + // Individual messages + foreach (var message in messageGroup.Messages) { - var content = FormatMessageContent(message.Content); - var contentHtml = - messageRightHtml.AppendChild( - HtmlNode.CreateNode($"
{content}
")); - - // Edited timestamp - if (message.EditedTimeStamp != null) + // Content + if (message.Content.IsNotBlank()) { - contentHtml.AppendChild( - HtmlNode.CreateNode( - $"(edited)")); + var content = FormatMessageContent(message.Content); + var contentHtml = + messageRightHtml.AppendChild( + HtmlNode.CreateNode($"
{content}
")); + + // Edited timestamp + if (message.EditedTimeStamp != null) + { + contentHtml.AppendChild( + HtmlNode.CreateNode( + $"(edited)")); + } } - } - // Attachments - foreach (var attachment in message.Attachments) - { - if (attachment.Type == AttachmentType.Image) - { - messageRightHtml.AppendChild( - HtmlNode.CreateNode("
" + - $"" + - $"" + - "" + - "
")); - } - else + // Attachments + foreach (var attachment in message.Attachments) { - messageRightHtml.AppendChild( - HtmlNode.CreateNode("
" + - $"" + - $"Attachment: {attachment.FileName} ({NormalizeFileSize(attachment.FileSize)})" + - "" + - "
")); + if (attachment.Type == AttachmentType.Image) + { + messageRightHtml.AppendChild( + HtmlNode.CreateNode("
" + + $"" + + $"" + + "" + + "
")); + } + else + { + messageRightHtml.AppendChild( + HtmlNode.CreateNode("
" + + $"" + + $"Attachment: {attachment.FileName} ({NormalizeFileSize(attachment.FileSize)})" + + "" + + "
")); + } } } } - } - doc.Save(filePath); + doc.Save(filePath); + }); } } diff --git a/DiscordChatExporter/Services/IExportService.cs b/DiscordChatExporter/Services/IExportService.cs index a0cc54b63..c92ee65d0 100644 --- a/DiscordChatExporter/Services/IExportService.cs +++ b/DiscordChatExporter/Services/IExportService.cs @@ -1,9 +1,10 @@ -using DiscordChatExporter.Models; +using System.Threading.Tasks; +using DiscordChatExporter.Models; namespace DiscordChatExporter.Services { public interface IExportService { - void Export(string filePath, ChannelChatLog log, Theme theme); + Task ExportAsync(string filePath, ChannelChatLog log, Theme theme); } } \ No newline at end of file diff --git a/DiscordChatExporter/ViewModels/MainViewModel.cs b/DiscordChatExporter/ViewModels/MainViewModel.cs index 178b90985..57dd254c3 100644 --- a/DiscordChatExporter/ViewModels/MainViewModel.cs +++ b/DiscordChatExporter/ViewModels/MainViewModel.cs @@ -176,7 +176,7 @@ private async void ExportChannel(Channel channel) var chatLog = new ChannelChatLog(SelectedGuild, channel, messageGroups); // Export - _exportService.Export(sfd.FileName, chatLog, _settingsService.Theme); + await _exportService.ExportAsync(sfd.FileName, chatLog, _settingsService.Theme); // Show dialog MessengerInstance.Send(new ShowExportDoneMessage(sfd.FileName));