From 9ad19efc46028020b748405ff003a453b2af6055 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Thu, 21 Jul 2022 20:35:22 +0200 Subject: [PATCH] Implemented the text element rename function --- .../Processor/TextElementProcessor.cs | 30 +++++++++++++++++++ .../UI WinForms/Components/TextElements.cs | 29 +++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/I18N Commander/Processor/TextElementProcessor.cs b/I18N Commander/Processor/TextElementProcessor.cs index a2ea7e0..48e7562 100644 --- a/I18N Commander/Processor/TextElementProcessor.cs +++ b/I18N Commander/Processor/TextElementProcessor.cs @@ -57,4 +57,34 @@ public static class TextElementProcessor return updateException.ToProcessorResult(); } } + + // Renames a text element: + public static async Task> RenameTextElement(int id, string newName) + { + await using var db = ProcessorMeta.ServiceProvider.GetRequiredService(); + + var textElement = await db.TextElements.FirstAsync(n => n.Id == id); + if (textElement is null) + throw new ArgumentOutOfRangeException(nameof(id)); + + // Get the corresponding section: + var section = (await db.TextElements.FirstAsync(n => n.Id == id)).Section; + + // Generate a code: + var code = await Utils.GenerateCode(newName, db.TextElements, (n, code) => n.Section == section && n.Code == code); + + textElement.Name = newName; + textElement.Code = code; + + // Save the changes: + try + { + await db.SaveChangesAsync(); + return new ProcessorResult(textElement); + } + catch (DbUpdateException updateException) + { + return updateException.ToProcessorResult(); + } + } } \ No newline at end of file diff --git a/I18N Commander/UI WinForms/Components/TextElements.cs b/I18N Commander/UI WinForms/Components/TextElements.cs index aec4ea3..f1e5e2a 100644 --- a/I18N Commander/UI WinForms/Components/TextElements.cs +++ b/I18N Commander/UI WinForms/Components/TextElements.cs @@ -107,9 +107,36 @@ public partial class TextElements : UserControl } - private void buttonRename_Click(object sender, EventArgs e) + private async void buttonRename_Click(object sender, EventArgs e) { + if(this.DesignMode) + return; + + // Ask the user if he really wants to rename the text element: + if(MessageBox.Show("Are you sure, you want to rename the selected text element? If you are already using this element in your code, you will need to manually refactor your code after renaming it.", "Rename text element", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.No) + return; + // Ask the user for the new name: + var result = InputDialog.Show(new InputDialog.Options( + Message: "Please edit the text element's name.", + PreloadedText: this.currentTextElement!.Name, + ShowQuestionCheckbox: false, + Title: "Rename text element" + )); + + // If the user canceled, return: + if(result.DialogResult == DialogResult.Cancel) + return; + + // Rename the text element: + var alteredTextElement = await TextElementProcessor.RenameTextElement(this.currentTextElement.Id, result.Text); + + alteredTextElement.ProcessError(); + if(!alteredTextElement.Successful) + return; + + // Reload the text elements: + this.LoadTextElements(); } private async void listTextElements_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)