Implemented a form change event

This commit is contained in:
Thorsten Sommer 2024-12-19 11:47:38 +01:00
parent 7ed32f44f2
commit bbc906f4ca
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
2 changed files with 31 additions and 2 deletions

View File

@ -6,7 +6,7 @@
<InnerScrolling HeaderHeight="6em"> <InnerScrolling HeaderHeight="6em">
<ChildContent> <ChildContent>
<MudForm @ref="@(this.form)" @bind-IsValid="@(this.inputIsValid)" @bind-Errors="@(this.inputIssues)" Class="pr-2"> <MudForm @ref="@(this.form)" @bind-IsValid="@(this.inputIsValid)" @bind-Errors="@(this.inputIssues)" FieldChanged="@this.TriggerFormChange" Class="pr-2">
<MudText Typo="Typo.body1" Align="Align.Justify" Class="mb-6"> <MudText Typo="Typo.body1" Align="Align.Justify" Class="mb-6">
@this.Description @this.Description
</MudText> </MudText>

View File

@ -4,7 +4,10 @@ using AIStudio.Settings;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using MudBlazor.Utilities;
using RustService = AIStudio.Tools.RustService; using RustService = AIStudio.Tools.RustService;
using Timer = System.Timers.Timer;
namespace AIStudio.Assistants; namespace AIStudio.Assistants;
@ -91,8 +94,10 @@ public abstract partial class AssistantBase : ComponentBase, IMessageBusReceiver
protected MudForm? form; protected MudForm? form;
protected bool inputIsValid; protected bool inputIsValid;
protected Profile currentProfile = Profile.NO_PROFILE; protected Profile currentProfile = Profile.NO_PROFILE;
protected ChatThread? chatThread; protected ChatThread? chatThread;
private readonly Timer formChangeTimer = new(TimeSpan.FromSeconds(1.6));
private ContentBlock? resultingContentBlock; private ContentBlock? resultingContentBlock;
private string[] inputIssues = []; private string[] inputIssues = [];
private bool isProcessing; private bool isProcessing;
@ -101,6 +106,13 @@ public abstract partial class AssistantBase : ComponentBase, IMessageBusReceiver
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
this.formChangeTimer.AutoReset = false;
this.formChangeTimer.Elapsed += async (_, _) =>
{
this.formChangeTimer.Stop();
await this.OnFormChange();
};
this.MightPreselectValues(); this.MightPreselectValues();
this.providerSettings = this.SettingsManager.GetPreselectedProvider(this.Component); this.providerSettings = this.SettingsManager.GetPreselectedProvider(this.Component);
this.currentProfile = this.SettingsManager.GetPreselectedProfile(this.Component); this.currentProfile = this.SettingsManager.GetPreselectedProfile(this.Component);
@ -162,6 +174,22 @@ public abstract partial class AssistantBase : ComponentBase, IMessageBusReceiver
return null; return null;
} }
private void TriggerFormChange(FormFieldChangedEventArgs _)
{
this.formChangeTimer.Stop();
this.formChangeTimer.Start();
}
/// <summary>
/// This method is called after any form field has changed.
/// </summary>
/// <remarks>
/// This method is called after a delay of 1.6 seconds. This is to prevent
/// the method from being called too often. This method is called after
/// the user has stopped typing or selecting options.
/// </remarks>
protected virtual Task OnFormChange() => Task.CompletedTask;
protected void CreateChatThread() protected void CreateChatThread()
{ {
this.chatThread = new() this.chatThread = new()
@ -341,6 +369,7 @@ public abstract partial class AssistantBase : ComponentBase, IMessageBusReceiver
public void Dispose() public void Dispose()
{ {
this.MessageBus.Unregister(this); this.MessageBus.Unregister(this);
this.formChangeTimer.Dispose();
} }
#endregion #endregion