Skip to content

Commit 7c6aa90

Browse files
author
Georgy Levchenko
committed
Refactoring.
1 parent 20728fb commit 7c6aa90

File tree

12 files changed

+114
-54
lines changed

12 files changed

+114
-54
lines changed

src/DataAccess/SmartThermo.DataAccess.Sqlite/Context.cs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using Microsoft.EntityFrameworkCore;
22
using SmartThermo.DataAccess.Sqlite.Models;
3-
using System;
4-
using System.Collections.Generic;
53

64
namespace SmartThermo.DataAccess.Sqlite
75
{
@@ -19,20 +17,15 @@ public class Context : DbContext
1917

2018
protected override void OnModelCreating(ModelBuilder modelBuilder)
2119
{
22-
modelBuilder.Entity<Setting>().HasData(new Setting
23-
{
24-
Id = 1,
25-
});
26-
27-
modelBuilder.Entity<SelectMode>().HasData(new SelectMode[]
28-
{
29-
new SelectMode { Id = 1, SettingId = 1, Stage = false },
30-
new SelectMode { Id = 2, SettingId = 1, Stage = false },
31-
new SelectMode { Id = 3, SettingId = 1, Stage = false },
32-
new SelectMode { Id = 4, SettingId = 1, Stage = false },
33-
new SelectMode { Id = 5, SettingId = 1, Stage = false },
34-
new SelectMode { Id = 6, SettingId = 1, Stage = false }
35-
});
20+
modelBuilder.Entity<Setting>().HasData(new Setting { Id = 1 });
21+
22+
modelBuilder.Entity<SelectMode>().HasData(
23+
new SelectMode {Id = 1, SettingId = 1, Stage = false},
24+
new SelectMode {Id = 2, SettingId = 1, Stage = false},
25+
new SelectMode {Id = 3, SettingId = 1, Stage = false},
26+
new SelectMode {Id = 4, SettingId = 1, Stage = false},
27+
new SelectMode {Id = 5, SettingId = 1, Stage = false},
28+
new SelectMode {Id = 6, SettingId = 1, Stage = false});
3629
}
3730

3831
protected override void OnConfiguring(DbContextOptionsBuilder options)

src/Modules/SmartThermo.Modules.Analytics/Dialogs/ViewModels/SessionDialogViewModel.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,23 @@
77
using System.Collections.ObjectModel;
88
using System.Linq;
99
using System.Threading.Tasks;
10+
using SmartThermo.Core.Extensions;
1011

1112
namespace SmartThermo.Modules.Analytics.Dialogs.ViewModels
1213
{
1314
public class SessionDialogViewModel : DialogViewModelBase
1415
{
16+
#region Field
17+
1518
private readonly INotifications _notifications;
1619
private ObservableCollection<SessionInfo> _sessionItems = new ObservableCollection<SessionInfo>();
1720
private bool _checkCurrentSession;
1821
private int _sessionItemsSelected;
1922

23+
#endregion
24+
25+
#region Property
26+
2027
public bool CheckCurrentSession
2128
{
2229
get => _checkCurrentSession;
@@ -43,19 +50,29 @@ public int SessionItemSelected
4350

4451
public DelegateCommand CancelCommand { get; }
4552

53+
#endregion
54+
55+
#region Constructor
56+
4657
public SessionDialogViewModel(INotifications notifications)
4758
{
4859
_notifications = notifications;
4960

50-
GetSessionInfo();
61+
GetSessionInfo().AwaitEx(() => { },
62+
exception => _notifications.ShowWarning("Не удалось прочитать данные о сессиях"));
5163

5264
SelectCommand = new DelegateCommand(SelectExecute);
5365
DeleteSelectCommand = new DelegateCommand(DeleteExecute);
5466
DeleteAllCommand = new DelegateCommand(DeleteAllExecute);
5567
CancelCommand = new DelegateCommand(CancelExecute);
5668
}
69+
70+
#endregion
5771

58-
public override void OnDialogOpened(IDialogParameters parameters) => CheckCurrentSession = parameters.GetValue<bool>("CheckCurrentSession");
72+
#region Method
73+
74+
public override void OnDialogOpened(IDialogParameters parameters)
75+
=> CheckCurrentSession = parameters.GetValue<bool>("CheckCurrentSession");
5976

6077
private void SelectExecute()
6178
{
@@ -88,7 +105,7 @@ private async void DeleteExecute()
88105
await Task.WhenAll(sessionDeleteTask);
89106

90107
_notifications.ShowSuccess($"Запись от {_sessionItems[_sessionItemsSelected].DateCreate} была удалена.");
91-
GetSessionInfo();
108+
await GetSessionInfo();
92109
}
93110

94111
private async void DeleteAllExecute()
@@ -109,7 +126,7 @@ private async void DeleteAllExecute()
109126
await Task.WhenAll(sessionDeleteTask);
110127

111128
_notifications.ShowSuccess("Все записи были успешно удалены.");
112-
GetSessionInfo();
129+
await GetSessionInfo();
113130
}
114131

115132
private bool CheckSessionItemsForZero()
@@ -123,7 +140,7 @@ private bool CheckSessionItemsForZero()
123140

124141
private void CancelExecute() => RaiseRequestClose(new DialogResult(ButtonResult.Cancel));
125142

126-
private async void GetSessionInfo()
143+
private async Task GetSessionInfo()
127144
{
128145
var sessionInfoTask = Task.Run(() =>
129146
{
@@ -147,5 +164,7 @@ private async void GetSessionInfo()
147164
SessionItems.AddRange(sessionInfoTask.Result);
148165
SessionItemSelected = 0;
149166
}
167+
168+
#endregion
150169
}
151170
}

src/Modules/SmartThermo.Modules.Analytics/SmartThermo.Modules.Analytics.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,12 @@
1313
<ProjectReference Include="..\..\DataAccess\SmartThermo.DataAccess.Sqlite\SmartThermo.DataAccess.Sqlite.csproj" />
1414
<ProjectReference Include="..\..\SmartThermo.Core\SmartThermo.Core.csproj" />
1515
</ItemGroup>
16+
<ItemGroup>
17+
<Reference Include="ToastNotifications, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null">
18+
<HintPath>..\..\Services\SmartThermo.Services.Notifications\dll\ToastNotifications.dll</HintPath>
19+
</Reference>
20+
<Reference Include="ToastNotifications.Messages, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null">
21+
<HintPath>..\..\Services\SmartThermo.Services.Notifications\dll\ToastNotifications.Messages.dll</HintPath>
22+
</Reference>
23+
</ItemGroup>
1624
</Project>

src/Modules/SmartThermo.Modules.Analytics/Views/AnalyticsWindow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
HorizontalAlignment="Center"
2424
VerticalAlignment="Center"
2525
Style="{DynamicResource SubtitleTextBlockStyle}"
26-
Text="Отсутсвуют данные о сессии" />
26+
Text="Отсутствуют данные о сессии" />
2727
</DataTemplate>
2828
</UserControl.Resources>
2929

src/Modules/SmartThermo.Modules.DataViewer/ViewModels/Represent/LoadDataViewerWindowViewModel.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,10 @@ private void InitCharts()
279279
private async Task LoadTestDataAsync()
280280
{
281281
await Task.Delay(1000);
282-
var now = DateTime.Now;
283282
var random = new Random();
284283

285-
DateTime[] myDates = new DateTime[1000_000];
286-
for (int i = 0; i < 1000_000; i++)
284+
var myDates = new DateTime[1000_000];
285+
for (var i = 0; i < 1000_000; i++)
287286
myDates[i] = DateTime.Now.AddSeconds(i);
288287

289288
await using var context = new Context();

src/Modules/SmartThermo.Modules.Dialog.SettingsDevice/ViewModels/SettingsDeviceDialogViewModel.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
using System.IO.Ports;
1515
using System.Linq;
1616
using SmartThermo.Core.Models;
17-
using ToastNotifications.Core;
1817

1918
namespace SmartThermo.Modules.Dialog.SettingsDevice.ViewModels
2019
{
@@ -245,25 +244,21 @@ private async void WriteExecute()
245244
}
246245
catch (TimeoutException)
247246
{
248-
_notifications.ShowWarning("Не удалось записать настройки устройства. Устройство не отвечает.",
249-
new MessageOptions());
247+
_notifications.ShowWarning("Не удалось записать настройки устройства. Устройство не отвечает.");
250248
_deviceConnector.Close();
251249
}
252250
catch (NotImplementedException)
253251
{
254-
_notifications.ShowWarning("Не удалось записать настройки устройства. Данный функционал еще не реализован.",
255-
new MessageOptions());
252+
_notifications.ShowWarning("Не удалось записать настройки устройства. Данный функционал еще не реализован.");
256253
_deviceConnector.Close();
257254
}
258255
catch (NullReferenceException)
259256
{
260-
_notifications.ShowWarning("Отсутствует соединение с устройством.",
261-
new MessageOptions());
257+
_notifications.ShowWarning("Отсутствует соединение с устройством.");
262258
}
263259
catch (Exception ex)
264260
{
265-
_notifications.ShowWarning("Не удалось записать настройки устройства.\n" + ex.Message,
266-
new MessageOptions());
261+
_notifications.ShowWarning("Не удалось записать настройки устройства.\n" + ex.Message);
267262
_deviceConnector.Close();
268263
}
269264
finally

src/Modules/SmartThermo.Modules.Dialog.SettingsPort/ViewModels/SettingsPortDialogViewModel.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using System.Collections.Generic;
1010
using System.Collections.ObjectModel;
1111
using System.IO.Ports;
12-
using ToastNotifications.Core;
1312

1413
namespace SmartThermo.Modules.Dialog.SettingsPort.ViewModels
1514
{
@@ -129,13 +128,12 @@ private async void ConnectExecute()
129128
}
130129
catch (TimeoutException)
131130
{
132-
_notifications.ShowWarning("Не удалось считать настройки устройства. Устройство не отвечает.",
133-
new MessageOptions());
131+
_notifications.ShowWarning("Не удалось считать настройки устройства. Устройство не отвечает.");
134132
_deviceConnector.Close(false);
135133
}
136134
catch (Exception ex)
137135
{
138-
_notifications.ShowWarning("Не удалось открыть соединение.\n" + ex.Message, new MessageOptions());
136+
_notifications.ShowWarning("Не удалось открыть соединение.\n" + ex.Message);
139137
_deviceConnector.Close(false);
140138
}
141139
finally

src/Services/SmartThermo.Services.DeviceConnector/DeviceConnector.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using System.Threading;
1111
using System.Threading.Tasks;
1212
using SmartThermo.Services.DeviceConnector.Extensions;
13-
using ToastNotifications.Core;
1413

1514
namespace SmartThermo.Services.DeviceConnector
1615
{
@@ -171,12 +170,12 @@ private async void OnTimer(object state)
171170
catch (TimeoutException)
172171
{
173172
Close();
174-
_notifications.ShowWarning("Устройство не отвечает на чтение регистров.", new MessageOptions());
173+
_notifications.ShowWarning("Устройство не отвечает на чтение регистров.");
175174
}
176175
catch (Exception ex)
177176
{
178177
Close();
179-
_notifications.ShowWarning("Не удалось прочитать регистры.\n" + ex.Message, new MessageOptions());
178+
_notifications.ShowWarning("Не удалось прочитать регистры.\n" + ex.Message);
180179
}
181180

182181
var result = dataRead.Select((x, index) => new SensorInfoEventArgs

src/Services/SmartThermo.Services.Notifications/INotifications.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,47 @@ namespace SmartThermo.Services.Notifications
44
{
55
public interface INotifications
66
{
7-
void OnUnloaded();
8-
7+
/// <summary>
8+
/// Показывает уведомление со статусом информации.
9+
/// </summary>
10+
/// <param name="message">Текст уведомления.</param>
911
void ShowInformation(string message);
1012

1113
void ShowInformation(string message, MessageOptions opts);
1214

15+
/// <summary>
16+
/// Показывает уведомление с успешным статусом.
17+
/// </summary>
18+
/// <param name="message">Текст уведомления.</param>
1319
void ShowSuccess(string message);
1420

1521
void ShowSuccess(string message, MessageOptions opts);
1622

17-
void ClearMessages(string msg);
23+
/// <summary>
24+
/// Показывает уведомление со статусом предупреждения.
25+
/// </summary>
26+
/// <param name="message">Текст уведомления.</param>
27+
void ShowWarning(string message);
1828

1929
void ShowWarning(string message, MessageOptions opts);
2030

31+
/// <summary>
32+
/// Показывает уведомление со статусом ошибки.
33+
/// </summary>
34+
/// <param name="message">Текст уведомления.</param>
2135
void ShowError(string message);
2236

2337
void ShowError(string message, MessageOptions opts);
2438

39+
/// <summary>
40+
/// Удаляет выбранное уведомление.
41+
/// </summary>
42+
/// <param name="msg">Уведомление, необходимое для удаления.</param>
43+
void ClearMessages(string msg);
44+
45+
/// <summary>
46+
/// Очищает все открытые уведомления.
47+
/// </summary>
2548
void ClearAll();
2649
}
2750
}

src/Services/SmartThermo.Services.Notifications/Notifications.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ public Notifications(Notifier notifier)
1515
_notifier.ClearMessages(new ClearAll());
1616
}
1717

18-
public void OnUnloaded()
19-
{
20-
_notifier.Dispose();
21-
}
22-
2318
public void ShowInformation(string message)
2419
{
2520
_notifier.ShowInformation(message);
@@ -40,11 +35,11 @@ public void ShowSuccess(string message, MessageOptions opts)
4035
_notifier.ShowSuccess(message, opts);
4136
}
4237

43-
public void ClearMessages(string msg)
38+
public void ShowWarning(string message)
4439
{
45-
_notifier.ClearMessages(new ClearByMessage(msg));
40+
_notifier.ShowWarning(message);
4641
}
47-
42+
4843
public void ShowWarning(string message, MessageOptions opts)
4944
{
5045
_notifier.ShowWarning(message, opts);
@@ -60,6 +55,11 @@ public void ShowError(string message, MessageOptions opts)
6055
_notifier.ShowError(message, opts);
6156
}
6257

58+
public void ClearMessages(string msg)
59+
{
60+
_notifier.ClearMessages(new ClearByMessage(msg));
61+
}
62+
6363
public void ClearAll()
6464
{
6565
_notifier.ClearMessages(new ClearAll());

0 commit comments

Comments
 (0)