From 2b4d61334738948e81a81b456ff8fb0f3c29c924 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sun, 17 Jul 2022 12:56:06 +0200 Subject: [PATCH] Implemented the add text element function --- .../Processor/TextElementProcessor.cs | 48 ++++++++++++++++++- .../Components/TextElements.Designer.cs | 21 ++++---- .../UI WinForms/Components/TextElements.cs | 44 ++++++++++++++++- .../UI WinForms/Components/TextElements.resx | 6 +++ 4 files changed, 107 insertions(+), 12 deletions(-) diff --git a/I18N Commander/Processor/TextElementProcessor.cs b/I18N Commander/Processor/TextElementProcessor.cs index e1578dc..1d31532 100644 --- a/I18N Commander/Processor/TextElementProcessor.cs +++ b/I18N Commander/Processor/TextElementProcessor.cs @@ -1,5 +1,4 @@ -using System.Collections; -using DataModel.Database; +using DataModel.Database; using DataModel.Database.Common; using Microsoft.EntityFrameworkCore; @@ -11,4 +10,49 @@ public static class TextElementProcessor { return Task.FromResult(db.TextElements.Where(n => n.Section == section).AsAsyncEnumerable()); } + + public static async Task> AddTextElement(DataContext db, string? currentSectionKey, string elementName) + { + if(string.IsNullOrWhiteSpace(currentSectionKey)) + throw new ArgumentOutOfRangeException(nameof(currentSectionKey)); + + var currentSection = await db.Sections.FirstOrDefaultAsync(n => n.DataKey == currentSectionKey); + if (currentSection is null) + throw new ArgumentOutOfRangeException(nameof(currentSectionKey)); + + // Remove any whitespaces from the element name, regardless of how many e.g. spaces the user typed: + var code = string.Join('_', elementName.Split(' ', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)).ToUpperInvariant(); + + // Check, if this key already exists: + if (await db.TextElements.AnyAsync(n => n.Section == currentSection && n.Code == code)) + { + var rng = new Random(); + while (await db.TextElements.AnyAsync(n => n.Section == currentSection && n.Code == code)) + { + // Add a random number to the end of the key: + code += $"_{rng.Next(1, 10_000)}"; + } + } + + var textElement = new TextElement + { + Name = elementName, + Code = code, + Section = currentSection, + }; + + // Add the new element to the database: + await db.TextElements.AddAsync(textElement); + + try + { + // Save the changes: + 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.Designer.cs b/I18N Commander/UI WinForms/Components/TextElements.Designer.cs index 2369d9d..a51a63c 100644 --- a/I18N Commander/UI WinForms/Components/TextElements.Designer.cs +++ b/I18N Commander/UI WinForms/Components/TextElements.Designer.cs @@ -37,8 +37,8 @@ this.listTextElements = new System.Windows.Forms.ListBox(); this.textBoxFilter = new System.Windows.Forms.TextBox(); this.labelFilter = new System.Windows.Forms.Label(); - this.toolTip = new System.Windows.Forms.ToolTip(this.components); this.labelSectionPath = new System.Windows.Forms.Label(); + this.toolTip = new System.Windows.Forms.ToolTip(this.components); this.tableLayout.SuspendLayout(); this.flowLayoutToolbar.SuspendLayout(); this.SuspendLayout(); @@ -90,6 +90,7 @@ this.buttonAdd.TabIndex = 0; this.toolTip.SetToolTip(this.buttonAdd, "Add text element to selected section"); this.buttonAdd.UseVisualStyleBackColor = true; + this.buttonAdd.Click += new System.EventHandler(this.buttonAdd_Click); // // buttonRemove // @@ -103,6 +104,7 @@ this.buttonRemove.TabIndex = 2; this.toolTip.SetToolTip(this.buttonRemove, "Delete this text element"); this.buttonRemove.UseVisualStyleBackColor = true; + this.buttonRemove.Click += new System.EventHandler(this.buttonRemove_Click); // // buttonRename // @@ -116,6 +118,7 @@ this.buttonRename.TabIndex = 1; this.toolTip.SetToolTip(this.buttonRename, "Rename this text element"); this.buttonRename.UseVisualStyleBackColor = true; + this.buttonRename.Click += new System.EventHandler(this.buttonRename_Click); // // listTextElements // @@ -150,14 +153,6 @@ this.labelFilter.Text = "Filter:"; this.labelFilter.TextAlign = System.Drawing.ContentAlignment.MiddleRight; // - // toolTip - // - this.toolTip.AutoPopDelay = 30000; - this.toolTip.InitialDelay = 500; - this.toolTip.ReshowDelay = 100; - this.toolTip.ToolTipIcon = System.Windows.Forms.ToolTipIcon.Info; - this.toolTip.ToolTipTitle = "Help"; - // // labelSectionPath // this.labelSectionPath.AutoSize = true; @@ -171,6 +166,14 @@ this.labelSectionPath.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; this.toolTip.SetToolTip(this.labelSectionPath, "The path of the currently selected section"); // + // toolTip + // + this.toolTip.AutoPopDelay = 30000; + this.toolTip.InitialDelay = 500; + this.toolTip.ReshowDelay = 100; + this.toolTip.ToolTipIcon = System.Windows.Forms.ToolTipIcon.Info; + this.toolTip.ToolTipTitle = "Help"; + // // TextElements // this.AutoScaleDimensions = new System.Drawing.SizeF(120F, 120F); diff --git a/I18N Commander/UI WinForms/Components/TextElements.cs b/I18N Commander/UI WinForms/Components/TextElements.cs index a371e2c..24ad44f 100644 --- a/I18N Commander/UI WinForms/Components/TextElements.cs +++ b/I18N Commander/UI WinForms/Components/TextElements.cs @@ -2,6 +2,7 @@ using DataModel.Database.Common; using Microsoft.Extensions.DependencyInjection; using Processor; +using UI_WinForms.Dialogs; namespace UI_WinForms.Components; @@ -55,10 +56,51 @@ public partial class TextElements : UserControl { var item = new ListViewItem(textElement.Name) { - Tag = textElement + Tag = textElement.Code, }; this.listTextElements.Items.Add(item); } } + + private async void buttonAdd_Click(object sender, EventArgs e) + { + if(this.DesignMode) + return; + + var result = InputDialog.Show(new InputDialog.Options( + Message: "Please type the desired text element's name.", + Title: "Add a text element", + Placeholder: "My text element", + ShowQuestionCheckbox: false + )); + + if(result.DialogResult == DialogResult.Cancel) + return; + + // Add the text element to the database into the current section: + var newTextElement = await TextElementProcessor.AddTextElement(this.db, this.currentSection?.DataKey, result.Text); + newTextElement.ProcessError(); + + if(!newTextElement.Successful) + return; + + // Add the text element to the list: + var item = new ListViewItem(newTextElement.Result!.Name) + { + Tag = newTextElement.Result.Code, + }; + + this.listTextElements.Items.Add(item); + } + + private void buttonRemove_Click(object sender, EventArgs e) + { + + } + + private void buttonRename_Click(object sender, EventArgs e) + { + + } } \ No newline at end of file diff --git a/I18N Commander/UI WinForms/Components/TextElements.resx b/I18N Commander/UI WinForms/Components/TextElements.resx index 99de901..01006c8 100644 --- a/I18N Commander/UI WinForms/Components/TextElements.resx +++ b/I18N Commander/UI WinForms/Components/TextElements.resx @@ -60,4 +60,10 @@ 17, 17 + + 17, 17 + + + 17, 17 + \ No newline at end of file