-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f05784d
commit 4f52431
Showing
13 changed files
with
269 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
namespace SillyChat | ||
{ | ||
/// <summary> | ||
/// Translation records. | ||
/// </summary> | ||
public class Translation | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Translation"/> class. | ||
/// </summary> | ||
/// <param name="input">Input text to be translated.</param> | ||
/// <param name="output">Output text from translation.</param> | ||
public Translation(string input, string output) | ||
{ | ||
this.Input = input; | ||
this.Output = output; | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets input text to be translated. | ||
/// </summary> | ||
public string Input { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets output text from translation. | ||
/// </summary> | ||
public string Output { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/SillyChat/SillyChat/Service/HistoryService/HistoryService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Timers; | ||
|
||
using DalamudPluginCommon; | ||
|
||
namespace SillyChat | ||
{ | ||
/// <summary> | ||
/// Manage history of translations. | ||
/// </summary> | ||
public class HistoryService | ||
{ | ||
private readonly ISillyChatPlugin plugin; | ||
private readonly Timer processTranslationsTimer; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="HistoryService"/> class. | ||
/// </summary> | ||
/// <param name="plugin">SillyChat plugin.</param> | ||
public HistoryService(ISillyChatPlugin plugin) | ||
{ | ||
this.plugin = plugin; | ||
this.Translations = new ConcurrentQueue<Translation>(); | ||
this.TranslationsDisplay = new List<Translation>(); | ||
this.processTranslationsTimer = new Timer | ||
{ | ||
Interval = plugin.Configuration.ProcessTranslationInterval, | ||
Enabled = true, | ||
}; | ||
this.processTranslationsTimer.Elapsed += this.ProcessTranslationsTimerOnElapsed; | ||
this.processTranslationsTimer.Start(); | ||
} | ||
|
||
/// <summary> | ||
/// Gets a value indicating whether gets indicator if history is being processed. | ||
/// </summary> | ||
public bool IsProcessing { get; private set; } | ||
|
||
/// <summary> | ||
/// Gets list of current historical records for display. | ||
/// </summary> | ||
public List<Translation> TranslationsDisplay { get; private set; } | ||
|
||
/// <summary> | ||
/// Gets queue for historical translations. | ||
/// </summary> | ||
public ConcurrentQueue<Translation> Translations { get; } | ||
|
||
/// <summary> | ||
/// Add translation to queue. | ||
/// </summary> | ||
/// <param name="translation">translation.</param> | ||
public void AddTranslation(Translation translation) | ||
{ | ||
this.Translations.Enqueue(translation); | ||
if (!this.IsProcessing) | ||
{ | ||
this.TranslationsDisplay.Add(translation); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Dispose history service. | ||
/// </summary> | ||
public void Dispose() | ||
{ | ||
this.processTranslationsTimer.Stop(); | ||
this.processTranslationsTimer.Elapsed -= this.ProcessTranslationsTimerOnElapsed; | ||
} | ||
|
||
private void ProcessTranslationsTimerOnElapsed(object sender, ElapsedEventArgs e) | ||
{ | ||
try | ||
{ | ||
this.IsProcessing = true; | ||
while (this.Translations.Count > this.plugin.Configuration.TranslationHistoryMax) | ||
{ | ||
this.Translations.TryDequeue(out _); | ||
} | ||
|
||
this.TranslationsDisplay = this.Translations.ToList(); | ||
this.IsProcessing = false; | ||
} | ||
catch (Exception ex) | ||
{ | ||
Logger.LogError(ex, "Failed to process translations."); | ||
this.IsProcessing = false; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System.Linq; | ||
|
||
using CheapLoc; | ||
using Dalamud.Interface.Colors; | ||
using ImGuiNET; | ||
|
||
namespace SillyChat | ||
{ | ||
/// <summary> | ||
/// History window for the plugin. | ||
/// </summary> | ||
public class HistoryWindow : PluginWindow | ||
{ | ||
private readonly ISillyChatPlugin plugin; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="HistoryWindow"/> class. | ||
/// </summary> | ||
/// <param name="plugin">SillyChat plugin.</param> | ||
public HistoryWindow(ISillyChatPlugin plugin) | ||
: base(plugin, Loc.Localize("HistoryTitle", "SillyChat History")) | ||
{ | ||
this.plugin = plugin; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override void Draw() | ||
{ | ||
try | ||
{ | ||
if (this.plugin.HistoryService.IsProcessing) return; | ||
var translations = this.plugin.HistoryService.Translations.ToList(); | ||
if (translations.Count > 0) | ||
{ | ||
ImGui.Columns(2); | ||
ImGui.TextColored(ImGuiColors.HealerGreen, Loc.Localize("SourceMessage", "Source")); | ||
ImGui.NextColumn(); | ||
ImGui.TextColored(ImGuiColors.DPSRed, Loc.Localize("TranslationMessage", "Translation")); | ||
ImGui.NextColumn(); | ||
ImGui.Separator(); | ||
foreach (var translation in translations) | ||
{ | ||
ImGui.TextWrapped(translation.Input); | ||
ImGui.NextColumn(); | ||
ImGui.TextWrapped(translation.Output); | ||
ImGui.NextColumn(); | ||
ImGui.Separator(); | ||
} | ||
} | ||
else | ||
{ | ||
ImGui.Text(Loc.Localize("NoTranslations", "Nothing has been translated yet.")); | ||
} | ||
} | ||
catch | ||
{ | ||
// ignored | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.