Skip to content

Commit

Permalink
Update DeviceViewModel.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
nefarius committed Jul 11, 2024
1 parent a8056cf commit 6c3db24
Showing 1 changed file with 28 additions and 33 deletions.
61 changes: 28 additions & 33 deletions ControlApp/ViewModels/UserControls/DeviceViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public partial class DeviceViewModel : ObservableObject
/// Editing allowed, changes saved only if applying settings with custom settings mode selected
/// </summary>
[ObservableProperty]
[SuppressMessage("ReSharper", "InconsistentNaming")]
private SettingsEditorViewModel _deviceCustomsVM = new() { AllowEditing = true };

[ObservableProperty]
Expand Down Expand Up @@ -67,7 +68,7 @@ public partial class DeviceViewModel : ObservableObject
private bool _isProfileSelectorVisible;

[ObservableProperty]
public List<ProfileData> _listOfProfiles;
private List<ProfileData> _listOfProfiles;

/// <summary>
/// The desired Bluetooth pairing mode for the device when plugging via cable or applying settings
Expand All @@ -80,7 +81,7 @@ public partial class DeviceViewModel : ObservableObject
[ObservableProperty]
private ProfileData? _selectedProfile;

private readonly DeviceData deviceUserData;
private readonly DeviceData _deviceUserData;


// ------------------------------------------------------ CONSTRUCTOR
Expand All @@ -95,7 +96,7 @@ internal DeviceViewModel(PnPDevice device, DshmDevMan dshmDevMan, DshmConfigMana
_appSnackbarMessagesService = appSnackbarMessagesService;
_contentDialogService = contentDialogService;
_batteryQuery = new Timer(UpdateBatteryStatus, null, 10000, 10000);
deviceUserData = _dshmConfigManager.GetDeviceData(DeviceAddress);
_deviceUserData = _dshmConfigManager.GetDeviceData(DeviceAddress);
// Loads correspondent controller data based on controller's MAC address


Expand All @@ -116,7 +117,7 @@ internal DeviceViewModel(PnPDevice device, DshmDevMan dshmDevMan, DshmConfigMana
/// <summary>
/// The Hid Mode the device is expected to be based on the device's user data
/// </summary>
public SettingsContext ExpectedHidMode => _dshmConfigManager.GetDeviceExpectedHidMode(deviceUserData);
public SettingsContext ExpectedHidMode => _dshmConfigManager.GetDeviceExpectedHidMode(_deviceUserData);


/// <summary>
Expand All @@ -134,15 +135,12 @@ public string DeviceSettingsStatus
string activeProfile = "";
if (CurrentDeviceSettingsMode != SettingsModes.Custom)
{
switch (CurrentDeviceSettingsMode)
activeProfile = CurrentDeviceSettingsMode switch
{
case SettingsModes.Global:
activeProfile = $"{_dshmConfigManager.GlobalProfile}";
break;
case SettingsModes.Profile:
activeProfile = $"{SelectedProfile}";
break;
}
SettingsModes.Global => $"{_dshmConfigManager.GlobalProfile}",
SettingsModes.Profile => $"{SelectedProfile}",
_ => activeProfile
};

activeProfile = $" 🡪 {activeProfile}";
}
Expand Down Expand Up @@ -384,28 +382,25 @@ private void AdjustSettingsTabState()
IsProfileSelectorVisible = CurrentDeviceSettingsMode != SettingsModes.Custom;
IsProfileSelectorEnabled = CurrentDeviceSettingsMode == SettingsModes.Profile;
IsEditorVisible = CurrentDeviceSettingsMode == SettingsModes.Custom;
switch (CurrentDeviceSettingsMode)
SelectedProfile = CurrentDeviceSettingsMode switch
{
case SettingsModes.Global:
SelectedProfile = _dshmConfigManager.GlobalProfile;
break;
case SettingsModes.Profile:
SelectedProfile = _dshmConfigManager.GetProfile(deviceUserData.GuidOfProfileToUse);
break;
}
SettingsModes.Global => _dshmConfigManager.GlobalProfile,
SettingsModes.Profile => _dshmConfigManager.GetProfile(_deviceUserData.GuidOfProfileToUse),
_ => SelectedProfile
};
}

[RelayCommand]
public void RefreshDeviceSettings()
{
Log.Logger.Debug($"Refreshing ViewModel of Device '{DeviceAddress}'");
// Bluetooth
PairingMode = (int)deviceUserData.BluetoothPairingMode;
CustomPairingAddress = deviceUserData.PairingAddress;
PairingMode = (int)_deviceUserData.BluetoothPairingMode;
CustomPairingAddress = _deviceUserData.PairingAddress;

// Settings and selected profile
CurrentDeviceSettingsMode = deviceUserData.SettingsMode;
DeviceCustomsVM.LoadDatasToAllGroups(deviceUserData.Settings);
CurrentDeviceSettingsMode = _deviceUserData.SettingsMode;
DeviceCustomsVM.LoadDatasToAllGroups(_deviceUserData.Settings);
ListOfProfiles = _dshmConfigManager.GetListOfProfilesWithDefault();

Log.Logger.Information(
Expand All @@ -424,25 +419,25 @@ public void RefreshDeviceSettings()
private void ApplyChanges()
{
Log.Logger.Information($"Saving and applying changes made to Device '{DeviceAddress}'");
deviceUserData.BluetoothPairingMode = (BluetoothPairingMode)PairingMode;
_deviceUserData.BluetoothPairingMode = (BluetoothPairingMode)PairingMode;

string formattedCustomMacAddress = Regex.Replace(CustomPairingAddress, @"[^a-fA-F0-9]", "").ToUpper();
if (formattedCustomMacAddress.Length > 12)
{
formattedCustomMacAddress = formattedCustomMacAddress.Substring(0, 12);
}

deviceUserData.PairingAddress = formattedCustomMacAddress;
_deviceUserData.PairingAddress = formattedCustomMacAddress;

deviceUserData.SettingsMode = CurrentDeviceSettingsMode;
_deviceUserData.SettingsMode = CurrentDeviceSettingsMode;
if (CurrentDeviceSettingsMode == SettingsModes.Custom)
{
DeviceCustomsVM.SaveAllChangesToBackingData(deviceUserData.Settings);
DeviceCustomsVM.SaveAllChangesToBackingData(_deviceUserData.Settings);
}

if (CurrentDeviceSettingsMode == SettingsModes.Profile)
{
deviceUserData.GuidOfProfileToUse = SelectedProfile.ProfileGuid;
_deviceUserData.GuidOfProfileToUse = SelectedProfile.ProfileGuid;
}

_dshmConfigManager.SaveChangesAndUpdateDsHidMiniConfigFile();
Expand All @@ -463,19 +458,19 @@ private void RestartDevice()
[RelayCommand]
private async Task TriggerPairingOnHotReload()
{
deviceUserData.BluetoothPairingMode = (BluetoothPairingMode)PairingMode;
_deviceUserData.BluetoothPairingMode = (BluetoothPairingMode)PairingMode;
string formattedCustomMacAddress = Regex.Replace(CustomPairingAddress, @"[^a-fA-F0-9]", "").ToUpper();
if (formattedCustomMacAddress.Length > 12)
{
formattedCustomMacAddress = formattedCustomMacAddress.Substring(0, 12);
}

deviceUserData.PairingAddress = formattedCustomMacAddress;
_deviceUserData.PairingAddress = formattedCustomMacAddress;

deviceUserData.PairOnHotReload = true;
_deviceUserData.PairOnHotReload = true;
_dshmConfigManager.SaveChangesAndUpdateDsHidMiniConfigFile();
await ShowPairingDialog();
deviceUserData.PairOnHotReload = false;
_deviceUserData.PairOnHotReload = false;
_dshmConfigManager.SaveChangesAndUpdateDsHidMiniConfigFile();
OnPropertyChanged(nameof(HostAddress));
OnPropertyChanged(nameof(LastPairingStatusIcon));
Expand Down

0 comments on commit 6c3db24

Please sign in to comment.