Repository navigation
Expand file tree
/
Copy pathMainWindowViewModel.cs
More file actions
286 lines (277 loc) · 11 KB
/
Copy pathMainWindowViewModel.cs
File metadata and controls
286 lines (277 loc) · 11 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
using Avalonia.Controls;
using Avalonia.Controls.Models.TreeDataGrid;
using Avalonia.Controls.Selection;
using Avalonia.Controls.Templates;
using Avalonia.Interactivity;
using Avalonia.Media;
//using DynamicData;
using ReactiveUI;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using voicio.Models;
using voicio.Services;
namespace voicio.ViewModels
{
public class MainWindowViewModel : ViewModelBase
{
//private TaskCompletionSource<bool> voiceSearchTask;
private ObservableCollection<SearchItem> _LastSearches;
public ObservableCollection<SearchItem> LastSearches
{
get => _LastSearches;
set => this.RaiseAndSetIfChanged(ref _LastSearches, value);
}
private SearchService _searchService;
//private List<Tag>? _TagsForChoice;
//public List<Tag>? TagsForChoice
//{
// get => _TagsForChoice;
// set => this.RaiseAndSetIfChanged(ref _TagsForChoice, value);
//}
private ObservableCollection<Hint>? _HintsRows;
public ObservableCollection<Hint>? HintsRows
{
get => _HintsRows;
set => this.RaiseAndSetIfChanged(ref _HintsRows, value);
}
private FlatTreeDataGridSource<Hint>? _HintsGridData;
public FlatTreeDataGridSource<Hint>? HintsGridData
{
get => _HintsGridData;
set => this.RaiseAndSetIfChanged(ref _HintsGridData, value);
}
private string _query = "";
public string Query
{
get => _query;
set => this.RaiseAndSetIfChanged(ref _query, value);
}
private string? _StatusText;
public string? StatusText
{
get => _StatusText;
set => this.RaiseAndSetIfChanged(ref _StatusText, value);
}
private bool _IsPinnedWindow = false;
public bool IsPinnedWindow
{
get => _IsPinnedWindow;
set => this.RaiseAndSetIfChanged(ref _IsPinnedWindow, value);
}
private bool _IsTextSearch = true;
private bool _IsCommentSearch = true;
//private bool _IsTagSearch = true;
private bool _IsFuzzy = true;
private bool _IsGridEditable = false;
private bool _IsAddButtonVisible = false;
private bool _IsHighlighting = false; //to do when i will understand the way to assign some struct to every table field in TreeDataGrid
private bool _IsVoiceSearching = false;
public bool IsVoiceSearching
{
get => _IsVoiceSearching;
set => this.RaiseAndSetIfChanged(ref _IsVoiceSearching, value);
}
public bool IsTextSearch
{
get => _IsTextSearch;
set => this.RaiseAndSetIfChanged(ref _IsTextSearch, value);
}
public bool IsCommentSearch
{
get => _IsCommentSearch;
set => this.RaiseAndSetIfChanged(ref _IsCommentSearch, value);
}
//public bool IsTagSearch
//{
// get => _IsTagSearch;
// set => this.RaiseAndSetIfChanged(ref _IsTagSearch, value);
//}
public bool IsHighlighting
{
get => _IsHighlighting;
set => this.RaiseAndSetIfChanged(ref _IsHighlighting, value);
}
public bool IsFuzzy
{
get => _IsFuzzy;
set => this.RaiseAndSetIfChanged(ref _IsFuzzy, value);
}
public bool IsAddButtonVisible
{
get => _IsAddButtonVisible;
set => this.RaiseAndSetIfChanged(ref _IsAddButtonVisible, value);
}
public bool IsGridEditable
{
get => _IsGridEditable;
set
{
this.RaiseAndSetIfChanged(ref _IsGridEditable, value);
TreeDataGridInit();
}
}
public ReactiveCommand<Unit, Unit> StartSearchCommand { get; }
private void RemoveHint(object sender, RoutedEventArgs e)
{
Button removeButton = (Button)sender;
Hint removedHint = (Hint)removeButton.DataContext;
HintsRows.Remove(removedHint);
if (removedHint.IsSaved)
{
using (var DataSource = new HelpContext())
{
DataSource.HintTable.Attach(removedHint);
DataSource.HintTable.Remove(removedHint);
DataSource.SaveChanges();
}
}
}
private void UpdateHint(object sender, RoutedEventArgs e)
{
Button updateButton = (Button)sender;
Hint updateHint = (Hint)updateButton.DataContext;
List<Tag> assosiatedTags = new List<Tag>();
if (updateHint.IsSaved)
{
using (var DataSource = new HelpContext())
{
DataSource.HintTable.Attach(updateHint);
DataSource.HintTable.Update(updateHint);
DataSource.SaveChanges();
}
}
else
{
using (var DataSource = new HelpContext())
{
DataSource.HintTable.Attach(updateHint);
DataSource.HintTable.Add(updateHint);
DataSource.SaveChanges();
}
updateHint.IsSaved = true;
}
}
public void AddTempHint()
{
Hint NewHint = new Hint(false);
HintsRows.Add(NewHint);
}
private Button UpdateButtonInit()
{
var b = new Button();
b.Background = new SolidColorBrush() { Color = new Color(255, 34, 139, 34) };
b.Content = "Add";
b.Click += UpdateHint;
return b;
}
private Button RemoveButtonInit()
{
var b = new Button();
b.Background = new SolidColorBrush() { Color = new Color(255, 80, 00, 20) };
b.Content = "Remove";
b.Click += RemoveHint;
return b;
}
private DockPanel ButtonsPanelInit()
{
var panel = new DockPanel();
panel.Children.Add(UpdateButtonInit());
panel.Children.Add(RemoveButtonInit());
panel.HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Stretch;
return panel;
}
//private ComboBox TagControlInit()
//{
// var addTag = new ComboBox();
// //addTag.SelectedItem = new string[] { "all", "not all" };
// addTag.ItemsSource = TagsForChoice;
// addTag.SelectedItem = TagsForChoice[0];
// addTag.SelectedValueBinding = new Binding("TagText") { Converter = TagGocntrolConverter };
// addTag.ItemTemplate = new FuncDataTemplate<Tag>((value, namescope) => new Label
// {
// [!Label.ContentProperty] = new Binding("TagText") {}
// });
// return addTag;
//}
public void TreeDataGridInit()
{
var TextColumnLength = new GridLength(1, GridUnitType.Star);
var TemplateColumnLength = new GridLength(125, GridUnitType.Pixel);
if (IsGridEditable)
{
var EditOptions = new TextColumnOptions<Hint>
{
BeginEditGestures = BeginEditGestures.Tap,
MinWidth = new GridLength(80, GridUnitType.Pixel),
IsTextSearchEnabled = true,
};
TextColumn<Hint, string> HintTextColumn = new TextColumn<Hint, string>("Text", x => x.HintText, (r, v) => r.HintText = v, options: EditOptions, width: TextColumnLength);
TextColumn<Hint, string> HintCommentColumn = new TextColumn<Hint, string>("Comment", x => x.Comment, (r, v) => r.Comment = v, options: EditOptions, width: TextColumnLength);
HintsGridData = new FlatTreeDataGridSource<Hint>(HintsRows)
{
Columns =
{
HintTextColumn,
HintCommentColumn,
//new TemplateColumn<Hint>("", new FuncDataTemplate<Hint>((a, e) => TagControlInit(), supportsRecycling: true), width: TemplateColumnLength),
new TemplateColumn<Hint>("", new FuncDataTemplate<Hint>((a, e) => ButtonsPanelInit(), supportsRecycling: true), width: TemplateColumnLength),
},
};
IsAddButtonVisible = true;
}
else
{
var ReadOptions = new TextColumnOptions<Hint>
{
MinWidth = new GridLength(80, GridUnitType.Pixel)
};
TextColumn<Hint, string> HintTextColumn = new TextColumn<Hint, string>("Text", x => x.HintText, options: ReadOptions, width: TextColumnLength);
TextColumn<Hint, string> HintCommentColumn = new TextColumn<Hint, string>("Comment", x => x.Comment, options: ReadOptions, width: TextColumnLength);
HintsGridData = new FlatTreeDataGridSource<Hint>(HintsRows)
{
Columns =
{
HintTextColumn,
HintCommentColumn,
//new TemplateColumn<Hint>("", new FuncDataTemplate<Hint>((a, e) => TagControlInit(), supportsRecycling: true), width: TemplateColumnLength)
},
};
IsAddButtonVisible = false;
}
HintsGridData.Selection = new TreeDataGridCellSelectionModel<Hint>(HintsGridData);
}
public void StartSearch()
{
var hints = _searchService.SearchHint(Query, false, IsFuzzy, IsTextSearch, IsCommentSearch);
LastSearches = _searchService.LastSearches;
HintsRows = new ObservableCollection<Hint>(hints.Distinct());
if (HintsRows.Count > 0)
{
StatusText = "Search results: " + HintsRows.Count.ToString() + " rows";
}
else
{
StatusText = "No results found";
}
TreeDataGridInit();
}
public MainWindowViewModel(SearchService searchService, string query = "")
{
StartSearchCommand = ReactiveCommand.Create(StartSearch);
//StartVoiceSearchCommand = ReactiveCommand.CreateFromTask(StartVoiceSearch);
//StartVoiceSearchCommand = ReactiveCommand.Create(StartVoiceSearch);
_searchService = searchService;
LastSearches = searchService.LastSearches;
List<Hint> hints = new List<Hint>();
using (var DataSource = new HelpContext())
{
hints.AddRange(DataSource.HintTable.ToList());
HintsRows = new ObservableCollection<Hint>(hints.Distinct());
}
TreeDataGridInit();
}
}
}