CreateDeviceViewModel.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using CommunityToolkit.Mvvm.ComponentModel;
  2. using CommunityToolkit.Mvvm.Input;
  3. using GlobalData;
  4. using System.Collections.ObjectModel;
  5. using System.Windows;
  6. namespace DeviceScanner.ViewModels.Dialogs;
  7. internal partial class CreateDeviceViewModel(DeviceCollection deviceCollection) : ObservableObject, IDialogAware
  8. {
  9. [ObservableProperty]
  10. private DeviceInfo_VM? _TempDevice;
  11. public DialogCloseListener RequestClose { get; set; }
  12. public bool CanCloseDialog()
  13. {
  14. return true;
  15. }
  16. public void OnDialogClosed()
  17. {
  18. }
  19. public void OnDialogOpened(IDialogParameters parameters)
  20. {
  21. if (!parameters.TryGetValue<DeviceInfo_VM>("Device", out DeviceInfo_VM? device) || device is null)
  22. {
  23. this.RequestClose.Invoke();
  24. return;
  25. }
  26. this.TempDevice = device;
  27. }
  28. [RelayCommand]
  29. private void SaveDevice()
  30. {
  31. if (this.TempDevice is null)
  32. return;
  33. if (!deviceCollection.Devices.TryGetValue(this.TempDevice.DeviceModel, out ObservableCollection<DeviceInfo_VM>? devices) || devices is null)
  34. return;
  35. if (devices.Where(t => t.DeviceName == this.TempDevice.DeviceName).Any())
  36. {
  37. MessageBox.Show($"Device Name \"{this.TempDevice.DeviceName}\" already exist", "Warning", MessageBoxButton.OK, MessageBoxImage.Error);
  38. return;
  39. }
  40. if (devices.Where(t => t.IP == this.TempDevice.IP).Any())
  41. {
  42. MessageBox.Show($"IP Address \"{this.TempDevice.IP}\" already exist", "Warning", MessageBoxButton.OK, MessageBoxImage.Error);
  43. return;
  44. }
  45. devices.Add(this.TempDevice);
  46. this.RequestClose.Invoke();
  47. }
  48. [RelayCommand]
  49. private void Close()
  50. {
  51. this.RequestClose.Invoke();
  52. }
  53. }