Skip to content

Commit

Permalink
Change the prompter so that the state and current field are passed in…
Browse files Browse the repository at this point in the history
…. This is a breaking change in order to provide more flexibility on output. Several people have run into the need for the state and current field.
  • Loading branch information
chrimc62 committed Feb 16, 2017
1 parent 9c6ef87 commit 9ae4978
Show file tree
Hide file tree
Showing 11 changed files with 4,545 additions and 4,438 deletions.
10 changes: 5 additions & 5 deletions CSharp/Library/Microsoft.Bot.Builder/FormFlow/FormBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public virtual IForm<T> Build(Assembly resourceAssembly = null, string resourceN
}
if (this._form._prompter == null)
{
this._form._prompter = async (context, prompt) =>
this._form._prompter = async (context, prompt, state, field) =>
{
var preamble = context.MakeMessage();
var promptMessage = context.MakeMessage();
Expand Down Expand Up @@ -171,7 +171,7 @@ public virtual IFormBuilder<T> OnCompletion(OnCompletionAsyncDelegate<T> callbac
return this;
}

public virtual IFormBuilder<T> Prompter(PromptAsyncDelegate prompter)
public virtual IFormBuilder<T> Prompter(PromptAsyncDelegate<T> prompter)
{
_form._prompter = prompter;
return this;
Expand Down Expand Up @@ -290,7 +290,7 @@ protected internal sealed class Form : IForm<T>
internal readonly Fields<T> _fields = new Fields<T>();
internal readonly List<IStep<T>> _steps = new List<IStep<T>>();
internal OnCompletionAsyncDelegate<T> _completion = null;
internal PromptAsyncDelegate _prompter = null;
internal PromptAsyncDelegate<T> _prompter = null;
internal ILocalizer _resources = new Localizer() { Culture = CultureInfo.CurrentUICulture };

public Form()
Expand Down Expand Up @@ -344,9 +344,9 @@ internal override IReadOnlyList<IStep<T>> Steps
}
}

internal override async Task<FormPrompt> Prompt(IDialogContext context, FormPrompt prompt)
internal override async Task<FormPrompt> Prompt(IDialogContext context, FormPrompt prompt, T state, IField<T> field)
{
return prompt == null ? prompt : await _prompter(context, prompt);
return prompt == null ? prompt : await _prompter(context, prompt, state, field);
}

internal override OnCompletionAsyncDelegate<T> Completion
Expand Down
26 changes: 13 additions & 13 deletions CSharp/Library/Microsoft.Bot.Builder/FormFlow/FormDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,9 @@ public async Task MessageReceived(IDialogContext context, IAwaitable<Connector.I
var next = (_formState.Next == null ? new NextStep() : ActiveSteps(_formState.Next, _state));
bool waitForMessage = false;
FormPrompt lastPrompt = _formState.LastPrompt;
Func<FormPrompt, Task<FormPrompt>> PostAsync = async (prompt) =>
Func<FormPrompt, IStep<T>, Task<FormPrompt>> PostAsync = async (prompt, step) =>
{
return await _form.Prompt(context, prompt);
return await _form.Prompt(context, prompt, _state, step.Field);
};
Func<IStep<T>, IEnumerable<TermMatch>, Task<bool>> DoStepAsync = async (step, matches) =>
{
Expand All @@ -303,12 +303,12 @@ public async Task MessageReceived(IDialogContext context, IAwaitable<Connector.I
next = result.Next;
if (result.Feedback?.Prompt != null)
{
await PostAsync(result.Feedback);
await PostAsync(result.Feedback, step);
if (_formState.Phase() != StepPhase.Completed)
{
if (!_formState.ProcessInputs)
{
await PostAsync(lastPrompt);
await PostAsync(lastPrompt, step);
waitForMessage = true;
}
else if (result.Prompt?.Buttons != null)
Expand All @@ -325,7 +325,7 @@ public async Task MessageReceived(IDialogContext context, IAwaitable<Connector.I
}
if (result.Prompt != null)
{
lastPrompt = await PostAsync(result.Prompt);
lastPrompt = await PostAsync(result.Prompt, step);
waitForMessage = true;
}
return true;
Expand All @@ -342,7 +342,7 @@ public async Task MessageReceived(IDialogContext context, IAwaitable<Connector.I
step = new NavigationStep<T>(_form.Steps[_formState.Step].Name, _form, _state, _formState);
if (start)
{
lastPrompt = await PostAsync(step.Start(context, _state, _formState));
lastPrompt = await PostAsync(step.Start(context, _state, _formState), step);
waitForMessage = true;
}
else
Expand All @@ -361,7 +361,7 @@ public async Task MessageReceived(IDialogContext context, IAwaitable<Connector.I
{
if (step.Type == StepType.Message)
{
await PostAsync(step.Start(context, _state, _formState));
await PostAsync(step.Start(context, _state, _formState), step);
next = new NextStep();
}
else if (_formState.ProcessInputs)
Expand All @@ -371,7 +371,7 @@ public async Task MessageReceived(IDialogContext context, IAwaitable<Connector.I
}
else
{
lastPrompt = await PostAsync(step.Start(context, _state, _formState));
lastPrompt = await PostAsync(step.Start(context, _state, _formState), step);
waitForMessage = true;
}
}
Expand Down Expand Up @@ -410,16 +410,16 @@ public async Task MessageReceived(IDialogContext context, IAwaitable<Connector.I
next = DoCommand(context, _state, _formState, step, commands, out feedback);
if (feedback != null)
{
await PostAsync(feedback);
await PostAsync(lastPrompt);
await PostAsync(feedback, step);
await PostAsync(lastPrompt, step);
waitForMessage = true;
}
}
else
{
if (matches.Count() == 0 && commands.Count() == 0)
{
await PostAsync(step.NotUnderstood(context, _state, _formState, stepInput));
await PostAsync(step.NotUnderstood(context, _state, _formState, stepInput), step);
if (_formState.ProcessInputs && !step.InClarify(_formState))
{
_formState.SetPhase(StepPhase.Ready);
Expand All @@ -443,8 +443,8 @@ public async Task MessageReceived(IDialogContext context, IAwaitable<Connector.I
next = DoCommand(context, _state, _formState, step, commands, out feedback);
if (feedback != null)
{
await PostAsync(feedback);
await PostAsync(lastPrompt);
await PostAsync(feedback, step);
await PostAsync(lastPrompt, step);
waitForMessage = true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion CSharp/Library/Microsoft.Bot.Builder/FormFlow/IForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,6 @@ public abstract class IForm<T>
internal abstract FormConfiguration Configuration { get; }
internal abstract IReadOnlyList<IStep<T>> Steps { get; }
internal abstract OnCompletionAsyncDelegate<T> Completion { get; }
internal abstract Task<FormPrompt> Prompt(IDialogContext context, FormPrompt prompt);
internal abstract Task<FormPrompt> Prompt(IDialogContext context, FormPrompt prompt, T state, IField<T> field);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public interface IFormBuilder<T>
/// </summary>
/// <param name="prompter">Delegate.</param>
/// <returns>Modified IFormBuilder.</returns>
IFormBuilder<T> Prompter(PromptAsyncDelegate prompter);
IFormBuilder<T> Prompter(PromptAsyncDelegate<T> prompter);

/// <summary>
/// Delegate to call when form is completed.
Expand Down
5 changes: 4 additions & 1 deletion CSharp/Library/Microsoft.Bot.Builder/FormFlow/IPrompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,11 @@ public override string ToString()
/// </summary>
/// <param name="context">Message context.</param>
/// <param name="prompt">Prompt to be posted.</param>
/// <param name="state">State of the form.</param>
/// <param name="field">Field being prompted or null if not a field.</param>
/// <returns>Prompt that was posted.</returns>
public delegate Task<FormPrompt> PromptAsyncDelegate(IDialogContext context, FormPrompt prompt);
public delegate Task<FormPrompt> PromptAsyncDelegate<T>(IDialogContext context, FormPrompt prompt, T state, IField<T> field)
where T : class;

public static partial class Extensions
{
Expand Down
2 changes: 1 addition & 1 deletion CSharp/Library/Microsoft.Bot.Builder/FormFlow/Steps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ public IField<T> Field
{
get
{
throw new NotImplementedException();
return null;
}
}

Expand Down
Loading

0 comments on commit 9ae4978

Please sign in to comment.