-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSkillManagementWindow.xaml.cs
More file actions
194 lines (172 loc) · 6.72 KB
/
SkillManagementWindow.xaml.cs
File metadata and controls
194 lines (172 loc) · 6.72 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Threading;
using System.IO;
using System.Text;
using System.Text.Json;
namespace TimeTask
{
public partial class SkillManagementWindow : Window
{
private List<SkillViewItem> _items = new List<SkillViewItem>();
public SkillManagementWindow()
{
InitializeComponent();
LoadSkills();
ShowFirstTimeHintIfNeeded();
}
private void LoadSkills()
{
var defs = SkillManagementService.GetSkillDefinitions();
var performance = new UserProfileManager()
.GetActionPerformance(30)
.ToDictionary(x => x.ActionId, StringComparer.OrdinalIgnoreCase);
_items = defs.Select(d =>
{
performance.TryGetValue(d.SkillId, out var stat);
return new SkillViewItem
{
SkillId = d.SkillId,
Title = d.Title,
Description = d.Description,
Scenario = d.Scenario,
Enabled = d.Enabled,
Shown = stat?.Shown ?? 0,
Accepted = stat?.Accepted ?? 0,
Deferred = stat?.Deferred ?? 0,
Rejected = stat?.Rejected ?? 0
};
}).ToList();
SkillsDataGrid.ItemsSource = _items;
}
private void EnableAll_Click(object sender, RoutedEventArgs e)
{
foreach (var item in _items)
{
item.Enabled = true;
}
SkillsDataGrid.Items.Refresh();
}
private void DisableAll_Click(object sender, RoutedEventArgs e)
{
foreach (var item in _items)
{
item.Enabled = false;
}
SkillsDataGrid.Items.Refresh();
}
private void Import_Click(object sender, RoutedEventArgs e)
{
try
{
var openDialog = new Microsoft.Win32.OpenFileDialog
{
Filter = I18n.T("Import_Filter"),
DefaultExt = "json"
};
if (openDialog.ShowDialog() != true)
{
return;
}
string json = File.ReadAllText(openDialog.FileName, Encoding.UTF8);
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var package = JsonSerializer.Deserialize<SkillExportPackage>(json, options);
if (package == null || package.EnabledSkillIds == null)
{
MessageBox.Show(I18n.T("Skill_Message_ImportParseFailed"), I18n.T("Title_Error"), MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
SkillManagementService.SaveEnabledSkillIds(package.EnabledSkillIds);
LoadSkills();
MessageBox.Show(I18n.T("Skill_Message_Imported"), I18n.T("Title_Prompt"), MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ex)
{
MessageBox.Show(I18n.Tf("Skill_Message_ImportFailedFormat", ex.Message), I18n.T("Title_Error"), MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void Export_Click(object sender, RoutedEventArgs e)
{
try
{
var saveDialog = new Microsoft.Win32.SaveFileDialog
{
Filter = I18n.T("Export_Filter"),
DefaultExt = "json",
FileName = $"TimeTask_Skills_{DateTime.Now:yyyyMMdd_HHmmss}.json"
};
if (saveDialog.ShowDialog() != true)
{
return;
}
var package = new SkillExportPackage
{
ExportedAt = DateTime.Now,
EnabledSkillIds = SkillManagementService.LoadEnabledSkillIds().ToList()
};
var options = new JsonSerializerOptions { WriteIndented = true };
string json = JsonSerializer.Serialize(package, options);
File.WriteAllText(saveDialog.FileName, json, Encoding.UTF8);
MessageBox.Show(I18n.T("Skill_Message_Exported"), I18n.T("Title_Prompt"), MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ex)
{
MessageBox.Show(I18n.Tf("Skill_Message_ExportFailedFormat", ex.Message), I18n.T("Title_Error"), MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void Save_Click(object sender, RoutedEventArgs e)
{
try
{
var enabled = _items.Where(x => x.Enabled).Select(x => x.SkillId).ToList();
SkillManagementService.SaveEnabledSkillIds(enabled);
MessageBox.Show(I18n.T("Skill_Message_Saved"), I18n.T("Title_Prompt"), MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ex)
{
MessageBox.Show(I18n.Tf("Skill_Message_SaveFailedFormat", ex.Message), I18n.T("Title_Error"), MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void Close_Click(object sender, RoutedEventArgs e)
{
Close();
}
private void ShowFirstTimeHintIfNeeded()
{
if (Properties.Settings.Default.SkillHintShown)
{
return;
}
FirstTimeHintBanner.Visibility = Visibility.Visible;
Properties.Settings.Default.SkillHintShown = true;
Properties.Settings.Default.Save();
var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(6) };
timer.Tick += (s, e) =>
{
timer.Stop();
FirstTimeHintBanner.Visibility = Visibility.Collapsed;
};
timer.Start();
}
}
public class SkillViewItem
{
public string SkillId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Scenario { get; set; }
public bool Enabled { get; set; }
public int Shown { get; set; }
public int Accepted { get; set; }
public int Deferred { get; set; }
public int Rejected { get; set; }
}
public class SkillExportPackage
{
public int Version { get; set; } = 1;
public DateTime ExportedAt { get; set; } = DateTime.Now;
public List<string> EnabledSkillIds { get; set; } = new List<string>();
}
}