Menu

[59aa80]: / FeedbackGeneratorQQ / Models / FeedbackGeneratorModel.cs  Maximize  Restore  History

Download this file

159 lines (140 with data), 5.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
namespace FeedbackGeneratorQQ.Models
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using FeedbackGeneratorQQ.Converters;
using FeedbackGeneratorQQ.Utils;
public class FeedbackGeneratorModel: BindableModel
{
/// <summary>
/// Gets or sets a first name
/// </summary>
public string FirstName
{
get { return this.Get<string>(); }
set { this.Set(value); }
}
/// <summary>
/// Gets or sets a last name
/// </summary>
public string LastName
{
get { return this.Get<string>(); }
set { this.Set(value); }
}
/// <summary>
/// Gets or sets a gender
/// </summary>
public string Gender
{
get { return this.Get<string>(); }
set { this.Set(value); }
}
/// <summary>
/// Gets or sets a result (feedback, error or another user message)
/// </summary>
public string Result
{
get { return this.Get<string>(); }
set { this.Set(value); }
}
/// <summary>
/// Gets or sets list of attributes
/// </summary>
public ObservableCollection<AttributeModel> Attributes
{
get { return this.Get<ObservableCollection<AttributeModel>>(); }
set { this.Set(value); }
}
public FeedbackGeneratorModel()
{
this.Attributes = new ObservableCollection<AttributeModel>();
this.Gender = "Male";
}
/// <summary>
/// Generates attribute sections and adds them to the given container
/// </summary>
/// <param name="binderHelper"> The binder helper </param>
/// <param name="container"> The attributes container </param>
public async void GenerateAttributes(BinderHelper binderHelper, WrapPanel container)
{
try
{
IEnumerable<AttributeModel> attributes = await AttributesHelper.GetAttributesAsync();
this.Attributes = new ObservableCollection<AttributeModel>(attributes);
for (int index = 0; index < this.Attributes.Count; index++)
{
this.GenerateAttributeSection(binderHelper, container, index);
}
}
catch (Exception ex)
{
this.Result = ex.Message;
}
}
/// <summary>
/// Generates feedback based on user selection
/// </summary>
/// <returns> Returns generated feedback (Task) </returns>
public Task<string> GenerateResult()
{
return Task<string>.Factory.StartNew(() =>
{
AttributesHelper.ConfigureReplacers(this.Gender);
List<string> messages = new List<string>();
foreach (AttributeModel attribute in this.Attributes.Where(x => x.IsSelected))
{
List<string> allAttributeMessges = attribute.GetMessages();
int randomIndex = RandomGenerator.GetRandomNumber(0, allAttributeMessges.Count);
string randomMessage = allAttributeMessges[randomIndex];
if (string.IsNullOrEmpty(randomMessage))
{
continue;
}
randomMessage = AttributesHelper.ApplyReplacers(randomMessage, this.FirstName, this.LastName);
if (!char.IsPunctuation(randomMessage.Last()))
{
randomMessage = string.Concat(randomMessage, ".");
}
messages.Add(randomMessage);
}
if (!messages.Any())
{
messages.Add("No phrases found. Please select more attributes or check your Template.xml files.");
}
return string.Join(". ", messages.Shuffle());
});
}
/// <summary>
/// Generates attribute section and adds it to the given container
/// </summary>
/// <param name="binderHelper"> The binding helper </param>
/// <param name="container"> The container </param>
/// <param name="attributeIndex"> The attribute index </param>
private void GenerateAttributeSection(BinderHelper binderHelper, Panel container, int attributeIndex)
{
StackPanel stackPanel = ElementsGenerator.CreateDefaultAttributeContainer();
CheckBox checkbox = ElementsGenerator.CreateDefaultAttributeCheckbox(this.Attributes[attributeIndex].Name);
binderHelper.AddBinding(checkbox, ToggleButton.IsCheckedProperty, $"Attributes[{attributeIndex}].IsSelected");
AttributeModel attribute = this.Attributes[attributeIndex];
ComboBox combobox = ElementsGenerator.CreateDefaultAttributeCombobox(attribute.Types);
binderHelper.AddBinding(combobox, ComboBox.TextProperty, $"Attributes[{attributeIndex}].Type");
combobox.SelectedIndex = 0;
BindingOperations.SetBinding(combobox, UIElement.VisibilityProperty, new Binding("IsChecked")
{
Converter = new CheckboxCheckedVisibilityConverter(),
Source = checkbox
});
stackPanel.Children.Add(checkbox);
stackPanel.Children.Add(combobox);
container.Children.Add(stackPanel);
}
}
}
MongoDB Logo MongoDB