2025-06-13 18:54:17 +08:00
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
2025-06-12 18:41:46 +08:00
|
|
|
|
using CommunityToolkit.Mvvm.Input;
|
2025-06-14 19:34:12 +08:00
|
|
|
|
using HandyControl.Controls;
|
|
|
|
|
|
using HandyControl.Data;
|
2025-06-12 18:41:46 +08:00
|
|
|
|
using PMSWPF.Data.Entities;
|
|
|
|
|
|
using PMSWPF.Data.Repositories;
|
2025-06-14 19:34:12 +08:00
|
|
|
|
using PMSWPF.Enums;
|
2025-06-12 18:41:46 +08:00
|
|
|
|
using PMSWPF.Excptions;
|
|
|
|
|
|
using PMSWPF.Extensions;
|
|
|
|
|
|
using PMSWPF.Helper;
|
2025-06-12 13:15:55 +08:00
|
|
|
|
using PMSWPF.Models;
|
|
|
|
|
|
using PMSWPF.Services;
|
|
|
|
|
|
using PMSWPF.ViewModels.Dialogs;
|
|
|
|
|
|
using PMSWPF.Views.Dialogs;
|
2025-06-14 19:34:12 +08:00
|
|
|
|
using MessageBox = System.Windows.MessageBox;
|
|
|
|
|
|
using Notification = PMSWPF.Models.Notification;
|
2025-06-10 20:55:39 +08:00
|
|
|
|
|
2025-06-12 13:15:55 +08:00
|
|
|
|
namespace PMSWPF.ViewModels;
|
|
|
|
|
|
|
|
|
|
|
|
public partial class DevicesViewModel : ViewModelBase
|
2025-06-10 20:55:39 +08:00
|
|
|
|
{
|
2025-06-12 13:15:55 +08:00
|
|
|
|
private readonly IDeviceDialogService _deviceDialogService;
|
2025-06-12 18:41:46 +08:00
|
|
|
|
private readonly DevicesRepositories _devicesRepositories;
|
2025-06-14 19:34:12 +08:00
|
|
|
|
private readonly INotificationService _notificationService;
|
|
|
|
|
|
[ObservableProperty] private ObservableCollection<Device> _devices = new ();
|
|
|
|
|
|
|
|
|
|
|
|
public DevicesViewModel(IDeviceDialogService deviceDialogService, DevicesRepositories devicesRepositories,INotificationService notificationService)
|
2025-06-12 13:15:55 +08:00
|
|
|
|
{
|
|
|
|
|
|
_deviceDialogService = deviceDialogService;
|
2025-06-12 18:41:46 +08:00
|
|
|
|
_devicesRepositories = devicesRepositories;
|
2025-06-14 19:34:12 +08:00
|
|
|
|
_notificationService = notificationService;
|
2025-06-12 13:15:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-13 18:54:17 +08:00
|
|
|
|
public async Task OnLoadedAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
var ds = await _devicesRepositories.GetAll();
|
2025-06-14 19:34:12 +08:00
|
|
|
|
_devices.Clear();
|
2025-06-13 18:54:17 +08:00
|
|
|
|
foreach (var dbDevice in ds)
|
|
|
|
|
|
{
|
2025-06-14 19:34:12 +08:00
|
|
|
|
Device device = new Device();
|
|
|
|
|
|
dbDevice.CopyTo(device);
|
|
|
|
|
|
_devices.Add(device);
|
2025-06-13 18:54:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-14 19:34:12 +08:00
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
public void Test()
|
|
|
|
|
|
{
|
|
|
|
|
|
// GrowlInfo info = new GrowlInfo();
|
|
|
|
|
|
// info.Message = "Hello";
|
|
|
|
|
|
// info.Type = InfoType.Error;
|
|
|
|
|
|
// info.ShowCloseButton = true;
|
|
|
|
|
|
_notificationService.Show( "Hello",NotificationType.Info,true);
|
|
|
|
|
|
// Growl.Error("Hello");
|
|
|
|
|
|
// Growl.Info("Hello");
|
|
|
|
|
|
// Growl.Success("Hello");
|
|
|
|
|
|
// Growl.WarningGlobal("Hello");
|
|
|
|
|
|
// Growl.SuccessGlobal("Hello");
|
|
|
|
|
|
// Growl.FatalGlobal("Hello");
|
|
|
|
|
|
// Growl.Ask("Hello", isConfirmed =>
|
|
|
|
|
|
// {
|
|
|
|
|
|
// Growl.Info(isConfirmed.ToString());
|
|
|
|
|
|
// return true;
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2025-06-12 13:15:55 +08:00
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
public async void AddDevice()
|
2025-06-10 22:13:06 +08:00
|
|
|
|
{
|
2025-06-12 18:41:46 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-06-14 19:34:12 +08:00
|
|
|
|
Device device = await _deviceDialogService.ShowAddDeviceDialog();
|
|
|
|
|
|
if (device != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
DbDevice dbDevice = new DbDevice();
|
|
|
|
|
|
device.CopyTo<DbDevice>(dbDevice);
|
|
|
|
|
|
var rowCount = await _devicesRepositories.Add(dbDevice);
|
|
|
|
|
|
if (rowCount > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
// MessageBox.Show("Device added successfully");
|
|
|
|
|
|
await OnLoadedAsync();
|
|
|
|
|
|
_notificationService.Show(new Notification(){Message = "Hello World!",Type = NotificationType.Success,IsGlobal = true});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-06-12 18:41:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (DbExistException e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine(e);
|
|
|
|
|
|
MessageBox.Show(e.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine(e);
|
|
|
|
|
|
MessageBox.Show(e.Message);
|
|
|
|
|
|
}
|
2025-06-10 22:13:06 +08:00
|
|
|
|
}
|
2025-06-13 18:54:17 +08:00
|
|
|
|
|
|
|
|
|
|
public override void OnLoaded()
|
|
|
|
|
|
{
|
2025-06-14 19:34:12 +08:00
|
|
|
|
OnLoadedAsync().Await((e) => { _deviceDialogService.ShowMessageDialog("", e.Message); }, () => { });
|
2025-06-13 18:54:17 +08:00
|
|
|
|
}
|
2025-06-10 20:55:39 +08:00
|
|
|
|
}
|