Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Online and Offline sample visibility fixed #3

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions AIDataForm/AzureBaseService/SemanticKernelService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
{
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
internal class SemanticKernelService
using System.ComponentModel;

internal class SemanticKernelService : INotifyPropertyChanged
{
#region Fields

Expand Down Expand Up @@ -56,6 +58,8 @@ internal class SemanticKernelService
/// </summary>
private Uri? uriResult;

public event PropertyChangedEventHandler? PropertyChanged;

#endregion

public SemanticKernelService()
Expand All @@ -69,7 +73,7 @@ public SemanticKernelService()
/// Gets or Set a value indicating whether an credentials are valid or not.
/// Returns <c>true</c> if the credentials are valid; otherwise, <c>false</c>.
/// </summary>
public static bool IsCredentialValid
public bool IsCredentialValid
{
get
{
Expand All @@ -78,6 +82,7 @@ public static bool IsCredentialValid
set
{
isCredentialValid = value;
RaisePropertyChanged(nameof(IsCredentialValid));
}
}

Expand Down Expand Up @@ -128,6 +133,14 @@ public Kernel? Kernel

#endregion

public void RaisePropertyChanged(string propName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}

/// <summary>
/// Validate Azure Credentials
/// </summary>
Expand Down
28 changes: 22 additions & 6 deletions AIDataForm/Behavior/DataFormAssistViewBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 };
Expand Down Expand Up @@ -251,6 +262,11 @@ protected override void OnDetachingFrom(SfAIAssistView assistView)
{
this.RefreshButton.Clicked -= RefreshButton_Clicked; ;
}

if (semanticKernelService != null)
{
semanticKernelService.PropertyChanged -= SemanticKernelService_PropertyChanged;
}
}

/// <summary>
Expand All @@ -262,7 +278,7 @@ private void OnCreateButtonClicked(object? sender, EventArgs e)
{
UpdateBusyIndicator(true);

if (SemanticKernelService.IsCredentialValid)
if (semanticKernelService.IsCredentialValid)
{
this.GetDataFormFromAI(this.Entry!.Text);
}
Expand All @@ -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);
Expand Down
Loading