-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
268 lines (216 loc) · 11.1 KB
/
MainWindow.xaml.cs
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
using PCConfig.View;
using PCConfig.View.Tabs;
using PCConfig.View.Tabs.Products;
using PCConfig.ViewModel;
using PCConfig.ViewModel.SideMenu;
using PCConfig.ViewModel.SideMenu.Tabs;
using PCConfig.ViewModel.SideMenu.Tabs.Realizations.Products;
using PCConfig.ViewModel.SideMenu.Tabs.Strategies;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace PCConfig
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private Dictionary<ISideMenuItem, ITabStrategy?> _tabStrategies;
private Dictionary<ISideMenuItem, ITabStrategy?> _parts;
private readonly int _userId;
public MainWindow(int userId)
{
InitializeComponent();
_userId = userId;
InitializeTabStrategies();
MainWindowViewModel model = new(_tabStrategies.Keys);
model.ExitButtonClicked += Model_ExitButtonClicked;
DataContext = model;
}
private void InitializeTabStrategies()
{
CPUTabStrategy cpuStrategy = new CPUTabStrategy();
cpuStrategy.ProductItemClicked += ProductItemControl_Clicked;
CPUCoolerTabStrategy cpuCoolerStrategy = new CPUCoolerTabStrategy();
cpuCoolerStrategy.ProductItemClicked += ProductItemControl_Clicked;
GPUTabStrategy gpuStrategy = new GPUTabStrategy();
gpuStrategy.ProductItemClicked += ProductItemControl_Clicked;
RAMTabStrategy ramStrategy = new RAMTabStrategy();
ramStrategy.ProductItemClicked += ProductItemControl_Clicked;
MotherboardTabStrategy motherboardStrategy = new MotherboardTabStrategy();
motherboardStrategy.ProductItemClicked += ProductItemControl_Clicked;
SSDTabStrategy ssdStrategy = new SSDTabStrategy();
ssdStrategy.ProductItemClicked += ProductItemControl_Clicked;
HDDTabStrategy hddStrategy = new HDDTabStrategy();
hddStrategy.ProductItemClicked += ProductItemControl_Clicked;
HybridStorageTabStrategy hybridStrategy = new HybridStorageTabStrategy();
hybridStrategy.ProductItemClicked += ProductItemControl_Clicked;
PowerSupplyTabStrategy powerSupplyStrategy = new PowerSupplyTabStrategy();
powerSupplyStrategy.ProductItemClicked += ProductItemControl_Clicked;
CaseFanTabStrategy caseFanStrategy = new CaseFanTabStrategy();
caseFanStrategy.ProductItemClicked += ProductItemControl_Clicked;
CaseTabStrategy caseStrategy = new CaseTabStrategy();
caseStrategy.ProductItemClicked += ProductItemControl_Clicked;
Dictionary<ISideMenuItem, ITabStrategy?> parts = new() {
{ new SideMenuItemViewModel("Процессоры", "Memory", SideMenuItemLocationType.Top), cpuStrategy },
{ new SideMenuItemViewModel("Кулеры для процессоров", "CeilingFan",SideMenuItemLocationType.Top), cpuCoolerStrategy },
{ new SideMenuItemViewModel("Видеокарты", "ExpansionCard", SideMenuItemLocationType.Top), gpuStrategy },
{ new SideMenuItemViewModel("Оперативная память", "Ruler", SideMenuItemLocationType.Top), ramStrategy },
{ new SideMenuItemViewModel("Материнские платы", "Chip", SideMenuItemLocationType.Top), motherboardStrategy },
{ new SideMenuItemViewModel("Твердотельные накопители (SSD)", "Nas", SideMenuItemLocationType.Top), ssdStrategy },
{ new SideMenuItemViewModel("Жесткие диски (HDD)", "Harddisk", SideMenuItemLocationType.Top), hddStrategy },
{ new SideMenuItemViewModel("Гибридные накопители", "Server", SideMenuItemLocationType.Top), hybridStrategy },
{ new SideMenuItemViewModel("Блоки питания", "PowerSocket", SideMenuItemLocationType.Top), powerSupplyStrategy },
{ new SideMenuItemViewModel("Охлаждение компьютера", "Fan", SideMenuItemLocationType.Top), caseFanStrategy },
{ new SideMenuItemViewModel("Корпуса", "DesktopTower", SideMenuItemLocationType.Top), caseStrategy }
};
_parts = parts;
AccountSettingsTabStrategy accountStrategy = new(_userId);
accountStrategy.ChangePassword += AccountStrategy_ChangePassword;
GameDataTabStrategy gameDataStrategy = new();
gameDataStrategy.Backed += GameDataStrategy_Backed;
gameDataStrategy.Clicked += GameDataStrategy_Clicked;
_tabStrategies = new Dictionary<ISideMenuItem, ITabStrategy?>
{
{ new DropOutSideMenuItemViewModel("Комплектующие", "Grid", parts.Keys, SideMenuItemLocationType.Top), null },
{ new SideMenuItemViewModel("Игровые данные", "ControllerClassic", SideMenuItemLocationType.Top), gameDataStrategy },
{ new SideMenuItemViewModel("Генерация ПК", "Cogs", SideMenuItemLocationType.Top), new GenerationAssemblyTabStrategy() },
{ new SideMenuItemViewModel("Настройки аккаунта", "AccountBox", SideMenuItemLocationType.Bottom, true), accountStrategy }
};
Item_IsSelectedPropertyChanged(_tabStrategies.Keys.Last(), null);
foreach (ISideMenuItem item in _tabStrategies.Keys)
{
Type type = item.GetType();
if (type == typeof(SideMenuItemViewModel))
{
SideMenuItemViewModel viewModel = (SideMenuItemViewModel)item;
viewModel.IsSelectedPropertyChanged += Item_IsSelectedPropertyChanged;
}
if (type == typeof(DropOutSideMenuItemViewModel))
{
DropOutSideMenuItemViewModel viewModel = (DropOutSideMenuItemViewModel)item;
foreach (SideMenuItemViewModel sideMenuItem in viewModel.SideMenuItems)
{
sideMenuItem.IsSelectedPropertyChanged += Item_IsSelectedPropertyChanged;
}
}
}
}
private void ProductItemControl_Clicked(ProductsListItemViewModel model, Type tabType)
{
ProductItemControl control = new(tabType, model.Part.Id);
control.Backed += ProductItemControl_Backed;
MainSectionGrid.Children.Clear();
MainSectionGrid.Children.Add(control);
}
private void ProductItemControl_Backed(Type tabType)
{
ISideMenuItem foundKey = FindKeyByValueType(_tabStrategies, _parts, tabType);
Item_IsSelectedPropertyChanged(foundKey, null);
}
private void GameDataStrategy_Clicked(string gameName, int samples, string imageUrl)
{
GameDataControl control = new(gameName, samples, imageUrl);
control.Backed += GameDataStrategy_Backed;
MainSectionGrid.Children.Clear();
MainSectionGrid.Children.Add(control);
}
private static ISideMenuItem FindKeyByValueType(Dictionary<ISideMenuItem, ITabStrategy?> dictionary, Dictionary<ISideMenuItem, ITabStrategy?> parts, Type targetType)
{
KeyValuePair<ISideMenuItem, ITabStrategy?> foundItem = dictionary.FirstOrDefault(pair => pair.Value?.GetType() == targetType);
if (foundItem.Key is null)
{
foundItem = parts.FirstOrDefault(pair => pair.Value?.GetType() == targetType);
}
return foundItem.Key;
}
private void GameDataStrategy_Backed()
{
ISideMenuItem foundKey = FindKeyByValueType(_tabStrategies, _parts, typeof(GameDataTabStrategy));
Item_IsSelectedPropertyChanged(foundKey, null);
}
private void AccountStrategy_ChangePassword(string email)
{
ChangePasswordTabControl control = new(_userId, email);
control.BackButtonClicked += Control_BackButtonClicked;
control.PasswordChangeProccedureExecute += Control_PasswordChangeProccedureExecute;
MainSectionGrid.Children.Clear();
MainSectionGrid.Children.Add(control);
}
private void Control_PasswordChangeProccedureExecute()
{
Item_IsSelectedPropertyChanged(_tabStrategies.Keys.Last(), null);
}
private void Control_BackButtonClicked()
{
Item_IsSelectedPropertyChanged(_tabStrategies.Keys.Last(), null);
}
private void Model_ExitButtonClicked()
{
AccessWindow window = new();
window.Show();
Close();
}
private ITabStrategy? GetStrategy(ISideMenuItem item)
{
_tabStrategies.TryGetValue(item, out ITabStrategy? strategy);
if (strategy == null)
{
foreach (ISideMenuItem key in _tabStrategies.Keys)
{
Type type = key.GetType();
if (type == typeof(DropOutSideMenuItemViewModel))
{
DropOutSideMenuItemViewModel viewModel = (DropOutSideMenuItemViewModel)key;
foreach (SideMenuItemViewModel sideMenuItem in viewModel.SideMenuItems)
{
if (sideMenuItem == item)
{
return _parts[sideMenuItem];
}
}
}
}
}
else
{
return strategy;
}
return null;
}
private void Item_IsSelectedPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
ISideMenuItem item = (ISideMenuItem)sender;
Type type = item.GetType();
ITabStrategy? strategy = GetStrategy(item);
if (type == typeof(SideMenuItemViewModel) && strategy != null && item.IsSelected == true)
{
Control control = strategy.HandleTab((SideMenuItemViewModel)item);
MainSectionGrid.Children.Clear();
MainSectionGrid.Children.Add(control);
}
}
private void ToggleMenu(object sender, RoutedEventArgs e)
{
if (MenuColumn.Width.Value == CollapseButtonColumn.ActualWidth)
{
MenuColumn.SetValue(ColumnDefinition.WidthProperty, new GridLength(0, GridUnitType.Auto));
TabsControl.Visibility = Visibility.Visible;
}
else
{
MenuColumn.SetValue(ColumnDefinition.WidthProperty, new GridLength(CollapseButtonColumn.ActualWidth));
TabsControl.Visibility = Visibility.Collapsed;
}
}
private void UserControl_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Tab)
{
e.Handled = true;
}
}
}
}