Ensured, that last minute changes are written to disk

This commit is contained in:
Thorsten Sommer 2022-09-21 17:33:03 +02:00
parent 97f3226d17
commit 24a7093674
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
3 changed files with 36 additions and 6 deletions

View File

@ -50,12 +50,6 @@ public sealed partial class Translation : UserControl
} }
} }
private async void SaveChanges(object? sender, ElapsedEventArgs e)
{
if (this.currentTranslationId > -1)
await TranslationProcessor.SaveChangedTranslation(this.currentTranslationId, this.textBox.Text);
}
public async Task Configure(DataModel.Database.Translation translation) public async Task Configure(DataModel.Database.Translation translation)
{ {
this.isLoading = true; this.isLoading = true;
@ -78,5 +72,16 @@ public sealed partial class Translation : UserControl
this.saveTimer.Stop(); this.saveTimer.Stop();
this.saveTimer.Start(); this.saveTimer.Start();
if(this.ParentForm is UI_WinForms.Main main)
main.AddDeferredSaveOperation(this.currentTranslationId, () => this.SaveChanges(null, null));
}
private async void SaveChanges(object? sender, ElapsedEventArgs e)
{
if (this.currentTranslationId > -1)
await TranslationProcessor.SaveChangedTranslation(this.currentTranslationId, this.textBox.Text);
if(this.ParentForm is UI_WinForms.Main main)
main.RemoveDeferredSaveOperation(this.currentTranslationId);
} }
} }

View File

@ -52,6 +52,7 @@
this.Name = "Main"; this.Name = "Main";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "I18N Commander"; this.Text = "I18N Commander";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Main_FormClosing);
this.ResumeLayout(false); this.ResumeLayout(false);
} }

View File

@ -2,8 +2,32 @@ namespace UI_WinForms;
public partial class Main : Form public partial class Main : Form
{ {
private readonly Dictionary<int, Action> deferredSaveOperations = new();
public Main() public Main()
{ {
this.InitializeComponent(); this.InitializeComponent();
} }
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
foreach (var (_, action) in this.deferredSaveOperations)
action();
this.deferredSaveOperations.Clear();
}
internal void AddDeferredSaveOperation(int id, Action action)
{
if (this.deferredSaveOperations.ContainsKey(id))
this.deferredSaveOperations.Remove(id);
this.deferredSaveOperations.Add(id, action);
}
internal void RemoveDeferredSaveOperation(int id)
{
if (this.deferredSaveOperations.ContainsKey(id))
this.deferredSaveOperations.Remove(id);
}
} }