Skip to content

Commit 5520db7

Browse files
committed
Use composition for DeviceViewModel.
1 parent d056a69 commit 5520db7

File tree

5 files changed

+38
-69
lines changed

5 files changed

+38
-69
lines changed

ControlR.Web.Client/Components/ContentWindows/DeviceContentWindow.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div class="title-bar p-2">
33
<div class="title-description">
44
<MudText Typo="Typo.h6" Color="MudBlazor.Color.Primary">
5-
@(ContentInstance.DeviceUpdate?.Name) - @(ContentInstance.ContentTypeName)
5+
@(ContentInstance.DeviceUpdate?.Dto.Name) - @(ContentInstance.ContentTypeName)
66
</MudText>
77
</div>
88
<div class="text-end" style="white-space: nowrap">

ControlR.Web.Client/Components/Dashboard.razor

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
Device Access
5151
</MudMenuItem>
5252

53-
@if (context.Item.IsOnline)
53+
@if (context.Item.Dto.IsOnline)
5454
{
5555
<MudMenuItem Icon="@(Icons.Material.Filled.ScreenShare)" IconColor="Color.Info"
5656
OnClick="@(() => LaunchRemoteControl(context.Item))">
@@ -99,23 +99,23 @@
9999
</CellTemplate>
100100
</TemplateColumn>
101101

102-
<PropertyColumn Title="Name" Property="x => x.Name" Sortable="true" Filterable="true" StickyLeft="true">
102+
<PropertyColumn Title="Name" Property="x => x.Dto.Name" Sortable="true" Filterable="true" StickyLeft="true">
103103
<CellTemplate>
104-
@if (string.IsNullOrWhiteSpace(context.Item.Alias))
104+
@if (string.IsNullOrWhiteSpace(context.Item.Dto.Alias))
105105
{
106-
@(context.Item.Name)
106+
@(context.Item.Dto.Name)
107107
}
108108
else
109109
{
110-
@($"{context.Item.Name} ({context.Item.Alias})")
110+
@($"{context.Item.Dto.Name} ({context.Item.Dto.Alias})")
111111
}
112112
</CellTemplate>
113113

114114
</PropertyColumn>
115115

116-
<PropertyColumn Property="@(x => x.IsOnline)" Sortable="true" Filterable="true" Title="Online">
116+
<PropertyColumn Property="@(x => x.Dto.IsOnline)" Sortable="true" Filterable="true" Title="Online">
117117
<CellTemplate>
118-
@if (context.Item.IsOnline)
118+
@if (context.Item.Dto.IsOnline)
119119
{
120120
if (context.Item.IsOutdated)
121121
{
@@ -138,9 +138,9 @@
138138
}
139139
</CellTemplate>
140140
</PropertyColumn>
141-
<PropertyColumn Property="@(x => x.Platform)" Sortable="true" Filterable="true" Title="Platform">
141+
<PropertyColumn Property="@(x => x.Dto.Platform)" Sortable="true" Filterable="true" Title="Platform">
142142
<CellTemplate>
143-
@switch (context.Item.Platform)
143+
@switch (context.Item.Dto.Platform)
144144
{
145145
case SystemPlatform.Windows:
146146
<MudTooltip Text="Windows">
@@ -170,27 +170,27 @@
170170
</CellTemplate>
171171
</PropertyColumn>
172172

173-
<PropertyColumn Property="x => x!.CurrentUsers" Title="Current Users" Sortable="false" Filterable="false">
173+
<PropertyColumn Property="x => x.Dto.CurrentUsers" Title="Current Users" Sortable="false" Filterable="false">
174174
<CellTemplate>
175-
@(string.Join(", ", context.Item.CurrentUsers))
175+
@(string.Join(", ", context.Item.Dto.CurrentUsers))
176176
</CellTemplate>
177177
</PropertyColumn>
178178

179-
<PropertyColumn Property="@(x => x.CpuUtilization)" Title="CPU" Sortable="true" Filterable="true">
179+
<PropertyColumn Property="@(x => x.Dto.CpuUtilization)" Title="CPU" Sortable="true" Filterable="true">
180180
<CellTemplate>
181-
@($"{Math.Round(context.Item.CpuUtilization * 100, 2)}%")
181+
@($"{Math.Round(context.Item.Dto.CpuUtilization * 100, 2)}%")
182182
</CellTemplate>
183183
</PropertyColumn>
184184

185-
<PropertyColumn Property="@(x => x.UsedMemoryPercent)" Title="Memory" Sortable="true" Filterable="true">
185+
<PropertyColumn Property="@(x => x.Dto.UsedMemoryPercent)" Title="Memory" Sortable="true" Filterable="true">
186186
<CellTemplate>
187-
@($"{Math.Round(context.Item.UsedMemoryPercent * 100, 2)}%")
187+
@($"{Math.Round(context.Item.Dto.UsedMemoryPercent * 100, 2)}%")
188188
</CellTemplate>
189189
</PropertyColumn>
190190

191-
<PropertyColumn Property="@(x => x.UsedStoragePercent)" Title="Storage" Sortable="true" Filterable="true">
191+
<PropertyColumn Property="@(x => x.Dto.UsedStoragePercent)" Title="Storage" Sortable="true" Filterable="true">
192192
<CellTemplate>
193-
@($"{Math.Round(context.Item.UsedStoragePercent * 100, 2)}%")
193+
@($"{Math.Round(context.Item.Dto.UsedStoragePercent * 100, 2)}%")
194194
</CellTemplate>
195195
</PropertyColumn>
196196

ControlR.Web.Client/Components/Dashboard.razor.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public partial class Dashboard
1111
private readonly ManualResetEventAsync _componentLoadedSignal = new(false);
1212
private readonly Dictionary<string, SortDefinition<DeviceViewModel>> _sortDefinitions = new()
1313
{
14-
["IsOnline"] = new SortDefinition<DeviceViewModel>(nameof(DeviceViewModel.IsOnline), true, 0, x => x.IsOnline),
15-
["Name"] = new SortDefinition<DeviceViewModel>(nameof(DeviceViewModel.Name), false, 1, x => x.Name)
14+
["IsOnline"] = new SortDefinition<DeviceViewModel>(nameof(DeviceViewModel.Dto.IsOnline), true, 0, x => x.Dto.IsOnline),
15+
["Name"] = new SortDefinition<DeviceViewModel>(nameof(DeviceViewModel.Dto.Name), false, 1, x => x.Dto.Name)
1616
};
1717

1818
private Version? _agentReleaseVersion;
@@ -80,8 +80,8 @@ protected override async Task OnInitializedAsync()
8080

8181
private async Task HandleDeviceDtoReceived(object subscriber, DtoReceivedMessage<DeviceDto> message)
8282
{
83-
var viewModel = message.Dto.CloneAs<DeviceViewModel>();
84-
viewModel.Dto = message.Dto;
83+
var isOutdated = IsOutdated(message.Dto);
84+
var viewModel = new DeviceViewModel(message.Dto, isOutdated);
8585
if (_dataGrid?.FilteredItems.Any(x => x.Id == viewModel.Id) == true)
8686
{
8787
await ReloadGridData();
@@ -109,7 +109,7 @@ private async Task HideOfflineDevicesChanged(bool isChecked)
109109
await ReloadGridData();
110110
}
111111

112-
private bool IsOutdated(DeviceViewModel device)
112+
private bool IsOutdated(DeviceDto device)
113113
{
114114
return
115115
_agentReleaseVersion is not null &&
@@ -181,9 +181,8 @@ private async Task<GridData<DeviceViewModel>> LoadServerData(GridState<DeviceVie
181181
var viewModels = result.Value.Items
182182
.Select(dto =>
183183
{
184-
var viewModel = dto.CloneAs<DeviceViewModel>();
185-
viewModel.IsOutdated = IsOutdated(viewModel);
186-
viewModel.Dto = dto;
184+
var isOutdated = IsOutdated(dto);
185+
var viewModel = new DeviceViewModel(dto, isOutdated);
187186
return viewModel;
188187
})
189188
.ToArray();
@@ -286,7 +285,7 @@ private async Task RestartDevice(DeviceViewModel device)
286285
{
287286
var result = await DialogService.ShowMessageBox(
288287
"Confirm Restart",
289-
$"Are you sure you want to restart {device.Name}?",
288+
$"Are you sure you want to restart {device.Dto.Name}?",
290289
"Yes",
291290
"No");
292291

@@ -318,7 +317,7 @@ private async Task ShutdownDevice(DeviceViewModel device)
318317
{
319318
var result = await DialogService.ShowMessageBox(
320319
"Confirm Shutdown",
321-
$"Are you sure you want to shut down {device.Name}?",
320+
$"Are you sure you want to shut down {device.Dto.Name}?",
322321
"Yes",
323322
"No");
324323

@@ -341,7 +340,7 @@ private async Task UninstallAgent(DeviceViewModel device)
341340
{
342341
var result = await DialogService.ShowMessageBox(
343342
"Confirm Uninstall",
344-
$"Are you sure you want to uninstall the agent from {device.Name}?",
343+
$"Are you sure you want to uninstall the agent from {device.Dto.Name}?",
345344
"Yes",
346345
"No");
347346

@@ -376,13 +375,13 @@ private async Task WakeDevice(DeviceViewModel device)
376375
{
377376
try
378377
{
379-
if (device.MacAddresses.Length == 0)
378+
if (device.Dto.MacAddresses.Length == 0)
380379
{
381380
Snackbar.Add("No MAC addresses on device", Severity.Warning);
382381
return;
383382
}
384383

385-
await MainHub.Server.SendWakeDevice(device.Id, device.MacAddresses);
384+
await MainHub.Server.SendWakeDevice(device.Id, device.Dto.MacAddresses);
386385
Snackbar.Add("Wake command sent", Severity.Success);
387386
}
388387
catch (Exception ex)

ControlR.Web.Client/Components/Dialogs/DesktopSessionSelectDialog.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<MudDialog>
22
<DialogContent>
33
<MudText Typo="Typo.subtitle1" Color="Color.Info" GutterBottom>
4-
Sessions on @(Device.Name):
4+
Sessions on @(Device.Dto.Name):
55
</MudText>
66

77
@if (Sessions.Length == 0)

ControlR.Web.Client/ViewModels/DeviceViewModel.cs

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,13 @@
1-
using System.Runtime.InteropServices;
1+
namespace ControlR.Web.Client.ViewModels;
22

3-
namespace ControlR.Web.Client.ViewModels;
4-
5-
public class DeviceViewModel : IEquatable<DeviceViewModel>
3+
public class DeviceViewModel(
4+
DeviceDto deviceDto,
5+
bool isOutdated) : IEquatable<DeviceViewModel>
66
{
7-
public string AgentVersion { get; set; } = string.Empty;
8-
public string Alias { get; set; } = string.Empty;
9-
public string ConnectionId { get; set; } = string.Empty;
10-
public double CpuUtilization { get; set; }
11-
public string[] CurrentUsers { get; set; } = [];
12-
public IReadOnlyList<Drive> Drives { get; set; } = [];
13-
public DeviceDto? Dto { get; set; }
14-
public Guid Id
15-
{
16-
get => Dto?.Id ?? Guid.Empty;
17-
}
18-
public bool Is64Bit { get; set; }
19-
public bool IsOnline { get; set; }
20-
public bool IsOutdated { get; set; }
7+
public DeviceDto Dto => deviceDto;
8+
public Guid Id => deviceDto.Id;
9+
public bool IsOutdated => isOutdated;
2110
public bool IsVisible { get; set; }
22-
public DateTimeOffset LastSeen { get; set; }
23-
public string LocalIpV4 { get; set; } = string.Empty;
24-
public string LocalIpV6 { get; set; } = string.Empty;
25-
public string[] MacAddresses { get; set; } = [];
26-
public string Name { get; set; } = string.Empty;
27-
public Architecture OsArchitecture { get; set; }
28-
public string OsDescription { get; set; } = string.Empty;
29-
public SystemPlatform Platform { get; set; }
30-
public int ProcessorCount { get; set; }
31-
public string PublicIpV4 { get; set; } = string.Empty;
32-
public string PublicIpV6 { get; set; } = string.Empty;
33-
public Guid[]? TagIds { get; set; }
34-
public Guid TenantId { get; set; }
35-
public double TotalMemory { get; set; }
36-
public double TotalStorage { get; set; }
37-
public double UsedMemory { get; set; }
38-
public double UsedMemoryPercent => UsedMemory / TotalMemory;
39-
public double UsedStorage { get; set; }
40-
public double UsedStoragePercent => UsedStorage / TotalStorage;
4111

4212
public bool Equals(DeviceViewModel? other)
4313
{

0 commit comments

Comments
 (0)