Skip to content

Commit

Permalink
A little refactoring has been done
Browse files Browse the repository at this point in the history
  • Loading branch information
TTLC198 committed Nov 11, 2023
1 parent 44f8b38 commit c37c237
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 57 deletions.
23 changes: 13 additions & 10 deletions HSMonitor/ViewModels/Settings/ConnectionSettingsTabViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using HSMonitor.Models;
using HSMonitor.Services;
using HSMonitor.Utils.Serial;
using HSMonitor.Utils.Usb.Serial;

namespace HSMonitor.ViewModels.Settings;

public class ConnectionSettingsTabViewModel : SettingsTabBaseViewModel
{
private IEnumerable<string> _availablePorts = SerialPort.GetPortNames();
public IEnumerable<string> AvailablePorts
private IEnumerable<DeviceInfo> _availablePorts = Serial.GetPorts();
public IEnumerable<DeviceInfo> AvailablePorts
{
get => _availablePorts;
private set
Expand All @@ -35,10 +38,12 @@ private set
921600
};

public string SelectedPort
public DeviceInfo SelectedDevice
{
get => SettingsService.Settings.LastSelectedPort ?? "COM1";
set => SettingsService.Settings.LastSelectedPort = value;
get =>
AvailablePorts.FirstOrDefault(p => p.PortName ==
(SettingsService.Settings.LastSelectedPort ?? "COM1")) ?? new DeviceInfo();
set => SettingsService.Settings.LastSelectedPort = value.PortName;
}

public int SelectedBaudRate
Expand All @@ -61,14 +66,12 @@ public int DeviceDisplayBrightness

public void UpdateAvailablePorts()
{
AvailablePorts = new List<string>();
AvailablePorts = SerialPort.GetPortNames();
AvailablePorts = new List<DeviceInfo>();
AvailablePorts = Serial.GetPorts();
}

public ConnectionSettingsTabViewModel(SettingsService settingsService)
public ConnectionSettingsTabViewModel(SettingsService settingsService, Serial serial)
: base(settingsService, 0, "Connection")
{
if (SettingsService is {Settings: not null})
SelectedPort = SettingsService.Settings.LastSelectedPort ?? (AvailablePorts.FirstOrDefault() ?? "COM1");
}
}
79 changes: 35 additions & 44 deletions HSMonitor/ViewModels/Settings/HardwareSettingsTabViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,45 @@
using System.Linq;
using HSMonitor.Services;
using LibreHardwareMonitor.Hardware;
using LibreHardwareMonitor.Hardware.Cpu;

namespace HSMonitor.ViewModels.Settings;

public class HardwareSettingsTabViewModel : SettingsTabBaseViewModel
{
private readonly SettingsService _settingsService;

public IEnumerable<IHardware> Processors => HardwareMonitorService.GetProcessors();
public List<IHardware> Processors => HardwareMonitorService.GetProcessors().ToList();

public IEnumerable<IHardware> GraphicCards => HardwareMonitorService.GetGraphicCards();
public List<IHardware> GraphicCards => HardwareMonitorService.GetGraphicCards().ToList();

public IHardware SelectedCpu
{
get => HardwareMonitorService
.GetProcessors()
.FirstOrDefault(c =>
c.Identifier
.ToString()
.Contains(_settingsService.Settings.CpuId ?? ""))
?? HardwareMonitorService.GetProcessors().First();
set => _settingsService.Settings.CpuId =
(HardwareMonitorService
.GetProcessors()
.FirstOrDefault(c =>
c.Identifier
.ToString()
.Contains(value.Identifier.ToString()))
?? HardwareMonitorService.GetProcessors().First()).Identifier
.ToString();
get =>
Processors
.FirstOrDefault(c =>
c.Identifier.ToString()!.Contains(_settingsService.Settings.CpuId ?? ""))
?? Processors.First();
set =>
_settingsService.Settings.CpuId =
(Processors
.FirstOrDefault(c =>
c.Identifier.ToString()!.Contains(value.Identifier.ToString() ?? string.Empty))
?? Processors.First()).Identifier
.ToString();
}

public IHardware SelectedGpu
{
get => HardwareMonitorService
.GetGraphicCards()
get => GraphicCards
.FirstOrDefault(c =>
c.Identifier
.ToString()
.Contains(_settingsService.Settings.GpuId ?? ""))
?? HardwareMonitorService.GetGraphicCards().First();
c.Identifier.ToString()!.Contains(_settingsService.Settings.GpuId ?? ""))
?? GraphicCards.First();
set => _settingsService.Settings.GpuId =
(HardwareMonitorService
.GetGraphicCards()
(GraphicCards
.FirstOrDefault(c =>
c.Identifier
.ToString()
.Contains(value.Identifier.ToString()))
?? HardwareMonitorService.GetGraphicCards().First()).Identifier
c.Identifier.ToString()!.Contains(value.Identifier.ToString() ?? string.Empty))
?? GraphicCards.First()).Identifier
.ToString();
}

Expand All @@ -70,19 +61,19 @@ public HardwareSettingsTabViewModel(SettingsService settingsService)
{
_settingsService = settingsService;
if (SettingsService is not {Settings: not null}) return;
SelectedCpu = HardwareMonitorService
.GetProcessors()
.FirstOrDefault(c =>
c.Identifier
.ToString()
.Contains(_settingsService.Settings.CpuId ?? ""))
?? HardwareMonitorService.GetProcessors().First();
SelectedGpu = HardwareMonitorService
.GetGraphicCards()
.FirstOrDefault(c =>
c.Identifier
.ToString()
.Contains(_settingsService.Settings.GpuId ?? ""))
?? HardwareMonitorService.GetGraphicCards().First();
if (Processors is {Count: > 0})
{
SelectedCpu = Processors
.FirstOrDefault(c =>
c.Identifier.ToString()!.Contains(_settingsService.Settings.CpuId ?? ""))
?? Processors.First();
}
if (GraphicCards is {Count: > 0})
{
SelectedGpu = GraphicCards
.FirstOrDefault(c =>
c.Identifier.ToString()!.Contains(_settingsService.Settings.GpuId ?? ""))
?? GraphicCards.First();
}
}
}
4 changes: 1 addition & 3 deletions HSMonitor/Views/Settings/ConnectionSettingsTabView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Text.RegularExpressions;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Controls;

namespace HSMonitor.Views.Settings;

Expand Down

0 comments on commit c37c237

Please sign in to comment.