2022-07-12 18:50:42 +00:00
|
|
|
|
using DataModel.Database;
|
2022-07-12 19:05:50 +00:00
|
|
|
|
using DataModel.Database.Common;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Processor;
|
2022-07-12 18:50:42 +00:00
|
|
|
|
|
|
|
|
|
namespace UI_WinForms.Components;
|
2022-07-11 17:52:15 +00:00
|
|
|
|
|
|
|
|
|
public partial class TextElements : UserControl
|
|
|
|
|
{
|
2022-07-12 19:05:50 +00:00
|
|
|
|
private readonly DataContext db;
|
|
|
|
|
|
2022-07-12 18:50:42 +00:00
|
|
|
|
private Section? currentSection;
|
|
|
|
|
|
2022-07-11 17:52:15 +00:00
|
|
|
|
public TextElements()
|
|
|
|
|
{
|
|
|
|
|
this.InitializeComponent();
|
2022-07-12 18:50:42 +00:00
|
|
|
|
|
2022-07-12 19:05:50 +00:00
|
|
|
|
// Check if we are in the designer:
|
|
|
|
|
if(Program.SERVICE_PROVIDER is null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Get the DI context from the main form:
|
|
|
|
|
this.db = Program.SERVICE_PROVIDER.GetService<DataContext>()!;
|
|
|
|
|
|
|
|
|
|
// Dispose of the context when the control is disposed:
|
|
|
|
|
this.Disposed += (_, _) => this.db.Dispose();
|
|
|
|
|
|
2022-07-12 18:50:42 +00:00
|
|
|
|
// When the section is changed, update this component:
|
2022-07-13 18:07:06 +00:00
|
|
|
|
AppEvents.WhenSectionChanged += async (sender, section) =>
|
2022-07-12 18:50:42 +00:00
|
|
|
|
{
|
|
|
|
|
this.currentSection = section;
|
|
|
|
|
this.buttonAdd.Enabled = this.currentSection is not null;
|
2022-07-12 19:05:50 +00:00
|
|
|
|
|
|
|
|
|
// Update the path:
|
2022-07-16 20:40:06 +00:00
|
|
|
|
if (this.currentSection is null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
this.labelSectionPath.Text = await SectionProcessor.GetSectionPath(this.db, this.currentSection.DataKey);
|
|
|
|
|
this.LoadTextElements();
|
2022-07-12 18:50:42 +00:00
|
|
|
|
};
|
2022-07-11 17:52:15 +00:00
|
|
|
|
}
|
2022-07-16 20:40:06 +00:00
|
|
|
|
|
|
|
|
|
// Loads all the text elements for the current section.
|
|
|
|
|
private async void LoadTextElements()
|
|
|
|
|
{
|
|
|
|
|
if (this.currentSection is null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Load the text elements:
|
|
|
|
|
var textElements = await TextElementProcessor.GetTextElements(this.db, this.currentSection);
|
|
|
|
|
|
|
|
|
|
// Update the list:
|
|
|
|
|
this.listTextElements.Items.Clear();
|
|
|
|
|
await foreach (var textElement in textElements)
|
|
|
|
|
{
|
|
|
|
|
var item = new ListViewItem(textElement.Name)
|
|
|
|
|
{
|
|
|
|
|
Tag = textElement
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.listTextElements.Items.Add(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-11 17:52:15 +00:00
|
|
|
|
}
|