diff --git a/App.xaml b/App.xaml index c02ef92..a51b561 100644 --- a/App.xaml +++ b/App.xaml @@ -10,7 +10,10 @@ + + + diff --git a/Extensions/TaskExtensions.cs b/Extensions/TaskExtensions.cs new file mode 100644 index 0000000..1886586 --- /dev/null +++ b/Extensions/TaskExtensions.cs @@ -0,0 +1,19 @@ +namespace PMSWPF.Extensions; + +public static class TaskExtensions +{ + + public static async Task Await(this Task task,Action onError=null,Action onComplete=null) + { + try + { + await task; + onComplete?.Invoke(); + } + catch (Exception e) + { + onError?.Invoke(e); + } + } + +} \ No newline at end of file diff --git a/PMSWPF.csproj b/PMSWPF.csproj index 4fef5c5..e208e8a 100644 --- a/PMSWPF.csproj +++ b/PMSWPF.csproj @@ -17,8 +17,5 @@ - - - diff --git a/Resources/DevicesItemTemplateDictionary.xaml b/Resources/DevicesItemTemplateDictionary.xaml new file mode 100644 index 0000000..657234f --- /dev/null +++ b/Resources/DevicesItemTemplateDictionary.xaml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/Services/DeviceDialogService.cs b/Services/DeviceDialogService.cs index e22eac1..25a66d0 100644 --- a/Services/DeviceDialogService.cs +++ b/Services/DeviceDialogService.cs @@ -27,4 +27,9 @@ public class DeviceDialogService : IDeviceDialogService } } + + public void ShowMessageDialog(string title, string message) + { + MessageBox.Show(message); + } } \ No newline at end of file diff --git a/Services/IDeviceDialogService.cs b/Services/IDeviceDialogService.cs index ad5a1a0..2b58c35 100644 --- a/Services/IDeviceDialogService.cs +++ b/Services/IDeviceDialogService.cs @@ -5,4 +5,6 @@ namespace PMSWPF.Services; public interface IDeviceDialogService { Task ShowAddDeviceDialog(); + + void ShowMessageDialog(string title, string message); } \ No newline at end of file diff --git a/Services/NavgatorServices.cs b/Services/NavgatorServices.cs index b0b96fb..3385f01 100644 --- a/Services/NavgatorServices.cs +++ b/Services/NavgatorServices.cs @@ -16,6 +16,7 @@ public class NavgatorServices { currentViewModel = value; OnViewModelChanged?.Invoke(); + currentViewModel.OnLoaded(); } } diff --git a/ViewModels/DataTransformViewModel.cs b/ViewModels/DataTransformViewModel.cs index 7842222..f75088e 100644 --- a/ViewModels/DataTransformViewModel.cs +++ b/ViewModels/DataTransformViewModel.cs @@ -2,5 +2,8 @@ public class DataTransformViewModel:ViewModelBase { - + public override void OnLoaded() + { + + } } \ No newline at end of file diff --git a/ViewModels/DevicesViewModel.cs b/ViewModels/DevicesViewModel.cs index 1276ec8..4c92518 100644 --- a/ViewModels/DevicesViewModel.cs +++ b/ViewModels/DevicesViewModel.cs @@ -1,4 +1,6 @@ -using System.Windows; +using System.Collections.ObjectModel; +using System.Windows; +using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using PMSWPF.Data.Entities; using PMSWPF.Data.Repositories; @@ -16,13 +18,27 @@ public partial class DevicesViewModel : ViewModelBase { private readonly IDeviceDialogService _deviceDialogService; private readonly DevicesRepositories _devicesRepositories; - + [ObservableProperty] + private ObservableCollection _devices; public DevicesViewModel(IDeviceDialogService deviceDialogService,DevicesRepositories devicesRepositories) { _deviceDialogService = deviceDialogService; _devicesRepositories = devicesRepositories; } + public async Task OnLoadedAsync() + { + var ds = await _devicesRepositories.GetAll(); + _devices = new ObservableCollection(); + + foreach (var dbDevice in ds) + { + Device device = new Device(); + dbDevice.CopyTo(device); + _devices.Add(device); + } + } + [RelayCommand] public async void AddDevice() @@ -35,7 +51,11 @@ public partial class DevicesViewModel : ViewModelBase { DbDevice dbDevice = new DbDevice(); device.CopyTo(dbDevice); - await _devicesRepositories.Add(dbDevice); + var rowCount= await _devicesRepositories.Add(dbDevice); + if (rowCount>0) + { + MessageBox.Show("Device added successfully"); + } } } @@ -51,4 +71,15 @@ public partial class DevicesViewModel : ViewModelBase MessageBox.Show(e.Message); } } + + public override void OnLoaded() + { + OnLoadedAsync().Await((e) => + { + _deviceDialogService.ShowMessageDialog("",e.Message); + }, () => + { + + }); + } } \ No newline at end of file diff --git a/ViewModels/HomeViewModel.cs b/ViewModels/HomeViewModel.cs index 2209d82..59662f4 100644 --- a/ViewModels/HomeViewModel.cs +++ b/ViewModels/HomeViewModel.cs @@ -2,5 +2,8 @@ public class HomeViewModel:ViewModelBase { - + public override void OnLoaded() + { + + } } \ No newline at end of file diff --git a/ViewModels/ViewModelBase.cs b/ViewModels/ViewModelBase.cs index 682ecdf..a3020fd 100644 --- a/ViewModels/ViewModelBase.cs +++ b/ViewModels/ViewModelBase.cs @@ -2,10 +2,12 @@ namespace PMSWPF.ViewModels; -public partial class ViewModelBase:ObservableObject +public abstract partial class ViewModelBase:ObservableObject { public ViewModelBase() { } + + public abstract void OnLoaded(); } \ No newline at end of file diff --git a/Views/DevicesView.xaml b/Views/DevicesView.xaml index 75155d4..2517eae 100644 --- a/Views/DevicesView.xaml +++ b/Views/DevicesView.xaml @@ -3,8 +3,11 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" + xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern" + xmlns:ikw="http://schemas.inkore.net/lib/ui/wpf" xmlns:local="clr-namespace:PMSWPF.Views" xmlns:dl="clr-namespace:PMSWPF.Views.Dialogs" + xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:vm="clr-namespace:PMSWPF.ViewModels" d:DataContext="{d:DesignInstance vm:DevicesViewModel}" mc:Ignorable="d" @@ -13,7 +16,14 @@