diff --git a/App.xaml b/App.xaml index a51b561..e99c47f 100644 --- a/App.xaml +++ b/App.xaml @@ -11,6 +11,9 @@ + + + diff --git a/App.xaml.cs b/App.xaml.cs index fb36400..a77d419 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -24,6 +24,7 @@ namespace PMSWPF container.AddSingleton(); container.AddSingleton(); container.AddSingleton(); + container.AddSingleton(); container.AddSingleton(); container.AddSingleton(); container.AddSingleton(); diff --git a/Enums/NotificationType.cs b/Enums/NotificationType.cs new file mode 100644 index 0000000..5ea17f2 --- /dev/null +++ b/Enums/NotificationType.cs @@ -0,0 +1,13 @@ +namespace PMSWPF.Enums; + +public enum NotificationType +{ + Info, + Warning, + Error, + Fatal, + Success, + Clear, + Ask + +} \ No newline at end of file diff --git a/Models/Notification.cs b/Models/Notification.cs new file mode 100644 index 0000000..2bb069a --- /dev/null +++ b/Models/Notification.cs @@ -0,0 +1,10 @@ +using PMSWPF.Enums; + +namespace PMSWPF.Models; + +public class Notification +{ + public string Message { get; set; } + public NotificationType Type { get; set; } + public bool IsGlobal { get; set; } +} \ No newline at end of file diff --git a/Services/GrowlNotificationService.cs b/Services/GrowlNotificationService.cs new file mode 100644 index 0000000..d5e046b --- /dev/null +++ b/Services/GrowlNotificationService.cs @@ -0,0 +1,77 @@ +using HandyControl.Controls; +using PMSWPF.Enums; +using Notification = PMSWPF.Models.Notification; + +namespace PMSWPF.Services; + +public class GrowlNotificationService : INotificationService +{ + public void Show(Notification notification) + { + if (notification == null ) + { + return; + } + + if (notification.IsGlobal) + { + switch (notification.Type) + { + case NotificationType.Info: + Growl.InfoGlobal(notification.Message); + break; + case NotificationType.Error: + Growl.ErrorGlobal(notification.Message); + break; + case NotificationType.Warning: + Growl.WarningGlobal(notification.Message); + break; + case NotificationType.Success: + Growl.SuccessGlobal(notification.Message); + break; + case NotificationType.Fatal: + Growl.FatalGlobal(notification.Message); + break; + case NotificationType.Clear: + Growl.ClearGlobal(); + break; + default: + Growl.InfoGlobal(notification.Message); + break; + } + + } + else + { + switch (notification.Type) + { + case NotificationType.Info: + Growl.Info(notification.Message); + break; + case NotificationType.Error: + Growl.Error(notification.Message); + break; + case NotificationType.Warning: + Growl.Warning(notification.Message); + break; + case NotificationType.Success: + Growl.Success(notification.Message); + break; + case NotificationType.Fatal: + Growl.Fatal(notification.Message); + break; + case NotificationType.Clear: + Growl.Clear(); + break; + default: + Growl.Info(notification.Message); + break; + } + } + } + + public void Show(string message, NotificationType type=NotificationType.Info,bool IsGlobal=true) + { + Show(new Notification(){Message = message,Type = type,IsGlobal = IsGlobal}); + } +} \ No newline at end of file diff --git a/Services/INotificationService.cs b/Services/INotificationService.cs new file mode 100644 index 0000000..d5a5064 --- /dev/null +++ b/Services/INotificationService.cs @@ -0,0 +1,10 @@ +using PMSWPF.Enums; +using PMSWPF.Models; + +namespace PMSWPF.Services; + +public interface INotificationService +{ + void Show(Notification notification); + void Show(string message, NotificationType type, bool IsGlobal); +} \ No newline at end of file diff --git a/ViewModels/DevicesViewModel.cs b/ViewModels/DevicesViewModel.cs index 8a95d02..7e5eafc 100644 --- a/ViewModels/DevicesViewModel.cs +++ b/ViewModels/DevicesViewModel.cs @@ -1,9 +1,11 @@ using System.Collections.ObjectModel; -using System.Windows; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; +using HandyControl.Controls; +using HandyControl.Data; using PMSWPF.Data.Entities; using PMSWPF.Data.Repositories; +using PMSWPF.Enums; using PMSWPF.Excptions; using PMSWPF.Extensions; using PMSWPF.Helper; @@ -11,6 +13,8 @@ using PMSWPF.Models; using PMSWPF.Services; using PMSWPF.ViewModels.Dialogs; using PMSWPF.Views.Dialogs; +using MessageBox = System.Windows.MessageBox; +using Notification = PMSWPF.Models.Notification; namespace PMSWPF.ViewModels; @@ -18,59 +22,74 @@ public partial class DevicesViewModel : ViewModelBase { private readonly IDeviceDialogService _deviceDialogService; private readonly DevicesRepositories _devicesRepositories; - [ObservableProperty] - private ObservableCollection _devices; - public DevicesViewModel(IDeviceDialogService deviceDialogService,DevicesRepositories devicesRepositories) + private readonly INotificationService _notificationService; + [ObservableProperty] private ObservableCollection _devices = new (); + + public DevicesViewModel(IDeviceDialogService deviceDialogService, DevicesRepositories devicesRepositories,INotificationService notificationService) { _deviceDialogService = deviceDialogService; _devicesRepositories = devicesRepositories; - _devices = new ObservableCollection(); + _notificationService = notificationService; } public async Task OnLoadedAsync() { var ds = await _devicesRepositories.GetAll(); - + _devices.Clear(); foreach (var dbDevice in ds) { - var deviceExist= _devices.FirstOrDefault(d => d.Id == dbDevice.Id); - if (deviceExist == null) - { - Device device = new Device(); - dbDevice.CopyTo(device); - _devices.Add(device); - } - + Device device = new Device(); + dbDevice.CopyTo(device); + _devices.Add(device); } - } + [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; + // }); + + + } [RelayCommand] public async void AddDevice() { try { - - Device device= await _deviceDialogService.ShowAddDeviceDialog(); - if (device != null) - { - DbDevice dbDevice = new DbDevice(); - device.CopyTo(dbDevice); - var rowCount= await _devicesRepositories.Add(dbDevice); - if (rowCount>0) - { - // MessageBox.Show("Device added successfully"); - await OnLoadedAsync(); - } - } - + Device device = await _deviceDialogService.ShowAddDeviceDialog(); + if (device != null) + { + DbDevice dbDevice = new DbDevice(); + device.CopyTo(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}); + } + } } catch (DbExistException e) { Console.WriteLine(e); MessageBox.Show(e.Message); - } catch (Exception e) { @@ -81,12 +100,6 @@ public partial class DevicesViewModel : ViewModelBase public override void OnLoaded() { - OnLoadedAsync().Await((e) => - { - _deviceDialogService.ShowMessageDialog("",e.Message); - }, () => - { - - }); + OnLoadedAsync().Await((e) => { _deviceDialogService.ShowMessageDialog("", e.Message); }, () => { }); } } \ No newline at end of file diff --git a/Views/DevicesView.xaml b/Views/DevicesView.xaml index 2517eae..743574d 100644 --- a/Views/DevicesView.xaml +++ b/Views/DevicesView.xaml @@ -9,13 +9,18 @@ xmlns:dl="clr-namespace:PMSWPF.Views.Dialogs" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:vm="clr-namespace:PMSWPF.ViewModels" + xmlns:hc="https://handyorg.github.io/handycontrol" d:DataContext="{d:DesignInstance vm:DevicesViewModel}" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> - - + + +