64 lines
1.8 KiB
C#
64 lines
1.8 KiB
C#
using System.Timers;
|
|
using Processor;
|
|
using Timer = System.Timers.Timer;
|
|
|
|
namespace UI_WinForms.Components;
|
|
|
|
public sealed partial class Translation : UserControl
|
|
{
|
|
private readonly string culture = "en-US";
|
|
private readonly Timer saveTimer;
|
|
|
|
private int currentTranslationId = -1;
|
|
private bool isLoading = false;
|
|
|
|
public Translation()
|
|
{
|
|
this.InitializeComponent();
|
|
this.Dock = DockStyle.Top;
|
|
}
|
|
|
|
public Translation(string cultureCode)
|
|
{
|
|
this.InitializeComponent();
|
|
this.culture = cultureCode;
|
|
this.labelHead.Text = $"Culture: {cultureCode}";
|
|
this.Dock = DockStyle.Top;
|
|
this.saveTimer = new Timer
|
|
{
|
|
Enabled = false, // disable timer for now,
|
|
Interval = 1_000, // 1 second interval
|
|
AutoReset = false, // runs only once
|
|
};
|
|
this.saveTimer.Elapsed += this.SaveChanges;
|
|
}
|
|
|
|
private async void SaveChanges(object? sender, ElapsedEventArgs e)
|
|
{
|
|
if (this.currentTranslationId > -1)
|
|
await TranslationProcessor.SaveChangedTranslation(this.currentTranslationId, this.textBox.Text);
|
|
}
|
|
|
|
public void Configure(DataModel.Database.Translation translation)
|
|
{
|
|
this.isLoading = true;
|
|
|
|
this.currentTranslationId = translation.Id;
|
|
this.textBox.Text = translation.Text;
|
|
this.textBox.Multiline = translation.TextElement.IsMultiLine;
|
|
this.Height = translation.TextElement.IsMultiLine ? 280 : 106;
|
|
|
|
this.isLoading = false;
|
|
}
|
|
|
|
private async void textBox_TextChanged(object sender, EventArgs e)
|
|
{
|
|
if(this.isLoading)
|
|
return;
|
|
|
|
if(this.saveTimer.Enabled)
|
|
this.saveTimer.Stop();
|
|
|
|
this.saveTimer.Start();
|
|
}
|
|
} |