diff --git a/AIDataForm/AzureBaseService/SemanticKernelService.cs b/AIDataForm/AzureBaseService/SemanticKernelService.cs index 3005f8a..ef5f946 100644 --- a/AIDataForm/AzureBaseService/SemanticKernelService.cs +++ b/AIDataForm/AzureBaseService/SemanticKernelService.cs @@ -2,7 +2,9 @@ { using Microsoft.SemanticKernel; using Microsoft.SemanticKernel.ChatCompletion; - internal class SemanticKernelService + using System.ComponentModel; + + internal class SemanticKernelService : INotifyPropertyChanged { #region Fields @@ -56,6 +58,8 @@ internal class SemanticKernelService /// private Uri? uriResult; + public event PropertyChangedEventHandler? PropertyChanged; + #endregion public SemanticKernelService() @@ -69,7 +73,7 @@ public SemanticKernelService() /// Gets or Set a value indicating whether an credentials are valid or not. /// Returns true if the credentials are valid; otherwise, false. /// - public static bool IsCredentialValid + public bool IsCredentialValid { get { @@ -78,6 +82,7 @@ public static bool IsCredentialValid set { isCredentialValid = value; + RaisePropertyChanged(nameof(IsCredentialValid)); } } @@ -128,6 +133,14 @@ public Kernel? Kernel #endregion + public void RaisePropertyChanged(string propName) + { + if (this.PropertyChanged != null) + { + this.PropertyChanged(this, new PropertyChangedEventArgs(propName)); + } + } + /// /// Validate Azure Credentials /// diff --git a/AIDataForm/Behavior/DataFormAssistViewBehavior.cs b/AIDataForm/Behavior/DataFormAssistViewBehavior.cs index f5b5da9..92c1830 100644 --- a/AIDataForm/Behavior/DataFormAssistViewBehavior.cs +++ b/AIDataForm/Behavior/DataFormAssistViewBehavior.cs @@ -122,8 +122,6 @@ protected override void OnAttachedTo(SfAIAssistView assistView) this.assistView = assistView; animation = new Animation(); - UpdateVisibility(); - if (this.assistView != null) { this.assistView.Request += this.OnAssistViewRequest; @@ -149,13 +147,26 @@ protected override void OnAttachedTo(SfAIAssistView assistView) this.AIActionButton.Clicked += this.OnAIActionButtonClicked; this.StartAnimation(); } + + if(semanticKernelService != null) + { + semanticKernelService.PropertyChanged += SemanticKernelService_PropertyChanged; + } + } + + private void SemanticKernelService_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e) + { + if(e.PropertyName == nameof(semanticKernelService.IsCredentialValid)) + { + UpdateVisibility(); + } } private void UpdateVisibility() { if (this.DataFormGeneratorModel != null) { - if (SemanticKernelService.IsCredentialValid) + if (semanticKernelService.IsCredentialValid) { this.DataFormGeneratorModel.ShowInputView = true; this.DataFormGeneratorModel.ShowDataForm = false; @@ -194,7 +205,7 @@ private void RefreshButton_Clicked(object? sender, EventArgs e) { this.DataFormGeneratorModel.Messages.Clear(); - if (!SemanticKernelService.IsCredentialValid) + if (!semanticKernelService.IsCredentialValid) { AssistItemSuggestion assistItemSuggestion = this.GetSubjectSuggestion(); AssistItem assistItem = new AssistItem() { Text = "You are in offline mode. Please select one of the forms below.", Suggestion = assistItemSuggestion, ShowAssistItemFooter = false }; @@ -251,6 +262,11 @@ protected override void OnDetachingFrom(SfAIAssistView assistView) { this.RefreshButton.Clicked -= RefreshButton_Clicked; ; } + + if (semanticKernelService != null) + { + semanticKernelService.PropertyChanged -= SemanticKernelService_PropertyChanged; + } } /// @@ -262,7 +278,7 @@ private void OnCreateButtonClicked(object? sender, EventArgs e) { UpdateBusyIndicator(true); - if (SemanticKernelService.IsCredentialValid) + if (semanticKernelService.IsCredentialValid) { this.GetDataFormFromAI(this.Entry!.Text); } @@ -276,7 +292,7 @@ private void OnCreateButtonClicked(object? sender, EventArgs e) private async void OnAssistViewRequest(object? sender, RequestEventArgs e) { string requestText = e.RequestItem.Text; - if (SemanticKernelService.IsCredentialValid && this.DataFormGeneratorModel != null) + if (semanticKernelService.IsCredentialValid && this.DataFormGeneratorModel != null) { this.DataFormGeneratorModel.ShowOfflineLabel = false; this.GetDataFormFromAI(requestText);