| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- using CommunityToolkit.Mvvm.ComponentModel;
- using CommunityToolkit.Mvvm.Input;
- using GlobalData;
- using System.Collections.ObjectModel;
- using System.Windows;
- namespace DeviceScanner.ViewModels.Dialogs;
- internal partial class CreateDeviceViewModel(DeviceCollection deviceCollection) : ObservableObject, IDialogAware
- {
- [ObservableProperty]
- private DeviceInfo_VM? _TempDevice;
- public DialogCloseListener RequestClose { get; set; }
- public bool CanCloseDialog()
- {
- return true;
- }
- public void OnDialogClosed()
- {
- }
- public void OnDialogOpened(IDialogParameters parameters)
- {
- if (!parameters.TryGetValue<DeviceInfo_VM>("Device", out DeviceInfo_VM? device) || device is null)
- {
- this.RequestClose.Invoke();
- return;
- }
- this.TempDevice = device;
- }
- [RelayCommand]
- private void SaveDevice()
- {
- if (this.TempDevice is null)
- return;
- if (!deviceCollection.Devices.TryGetValue(this.TempDevice.DeviceModel, out ObservableCollection<DeviceInfo_VM>? devices) || devices is null)
- return;
- if (devices.Where(t => t.DeviceName == this.TempDevice.DeviceName).Any())
- {
- MessageBox.Show($"Device Name \"{this.TempDevice.DeviceName}\" already exist", "Warning", MessageBoxButton.OK, MessageBoxImage.Error);
- return;
- }
- if (devices.Where(t => t.IP == this.TempDevice.IP).Any())
- {
- MessageBox.Show($"IP Address \"{this.TempDevice.IP}\" already exist", "Warning", MessageBoxButton.OK, MessageBoxImage.Error);
- return;
- }
- devices.Add(this.TempDevice);
- this.RequestClose.Invoke();
- }
- [RelayCommand]
- private void Close()
- {
- this.RequestClose.Invoke();
- }
- }
|