Confirmation.cs
1 //
2 // Copyright (c) Microsoft. All rights reserved.
3 // Licensed under the MIT license.
4 //
5 // Microsoft Bot Framework: http://botframework.com
6 //
7 // Bot Builder SDK GitHub:
8 // https://github.com/Microsoft/BotBuilder
9 //
10 // Copyright (c) Microsoft Corporation
11 // All rights reserved.
12 //
13 // MIT License:
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33 
34 using System;
35 using System.Collections.Generic;
36 using System.Linq;
37 using System.Threading.Tasks;
38 
39 namespace Microsoft.Bot.Builder.FormFlow.Advanced
40 {
45  public class Confirmation<T> : Field<T>
46  where T : class
47  {
55  public Confirmation(PromptAttribute prompt, ActiveDelegate<T> condition, IEnumerable<string> dependencies, IForm<T> form)
56  : base("confirmation" + form.Steps.Count, FieldRole.Confirm)
57  {
58  SetPrompt(prompt);
59  SetType(typeof(bool));
60  SetDependencies(dependencies.ToArray());
61  SetActive(condition);
62  SetFieldDescription(new DescribeAttribute(form.Configuration.Confirmation) { IsLocalizable = false });
63  var noStep = (dependencies.Any() ? new NextStep(dependencies) : new NextStep());
64  _next = (value, state) => (bool)value ? new NextStep() : noStep;
65  }
66 
74  public Confirmation(MessageDelegate<T> generateMessage, ActiveDelegate<T> condition, IEnumerable<string> dependencies, IForm<T> form)
75  : base("confirmation" + form.Steps.Count, FieldRole.Confirm)
76  {
77  SetDefine(async (state, field) => { field.SetPrompt(await generateMessage(state)); return true; });
78  SetType(typeof(bool));
79  SetDependencies(dependencies.ToArray());
80  SetActive(condition);
81  SetFieldDescription(new DescribeAttribute(form.Configuration.Confirmation) { IsLocalizable = false });
82  var noStep = (dependencies.Any() ? new NextStep(dependencies) : new NextStep());
83  SetNext((value, state) => (bool)value ? new NextStep() : noStep);
84  }
85 
86  public override object GetValue(T state)
87  {
88  return null;
89  }
90 
91  public override IEnumerable<string> Dependencies
92  {
93  get
94  {
95  return _dependencies;
96  }
97  }
98 
99  #region IFieldPrompt
100  public override bool Active(T state)
101  {
102  return _condition(state);
103  }
104 
105  public override NextStep Next(object value, T state)
106  {
107  return _next((bool)value, state);
108  }
109 
110  public override void SetValue(T state, object value)
111  {
112  throw new NotImplementedException();
113  }
114 
115  public override bool IsUnknown(T state)
116  {
117  return true;
118  }
119 
120  public override void SetUnknown(T state)
121  {
122  throw new NotImplementedException();
123  }
124  #endregion
125  }
126 }
FieldRole
The role the field plays in a form.
Definition: IField.cs:129
override bool Active(T state)
Test to see if field is currently active based on the current state.
Define the prompt used when asking about a field.
Definition: Attributes.cs:294
Confirmation(MessageDelegate< T > generateMessage, ActiveDelegate< T > condition, IEnumerable< string > dependencies, IForm< T > form)
Construct a confirmation dynamically.
Definition: Confirmation.cs:74
Confirmation(PromptAttribute prompt, ActiveDelegate< T > condition, IEnumerable< string > dependencies, IForm< T > form)
Construct a confirmation.
Definition: Confirmation.cs:55
Form definition interface.
Definition: IForm.cs:47
override NextStep Next(object value, T state)
Next step to execute.
Base class with declarative implementation of IField.
Definition: Field.cs:67
override object GetValue(T state)
Get this field value from form state.
Definition: Confirmation.cs:86
override bool IsUnknown(T state)
Test to see if the field value form state has a value.
override void SetUnknown(T state)
Set this field value in form state to unknown.
Field is used to confirm some settings during the dialog.
override void SetValue(T state, object value)
Set this field value in form state.
Attribute to override the default description of a field, property or enum value. ...
Definition: Attributes.cs:62