diff --git a/src/BuildingBlocks/BuildingBlocks.Application/ViewModels/GenericViewModel.cs b/src/BuildingBlocks/BuildingBlocks.Application/ViewModels/GenericViewModel.cs new file mode 100644 index 0000000..30efade --- /dev/null +++ b/src/BuildingBlocks/BuildingBlocks.Application/ViewModels/GenericViewModel.cs @@ -0,0 +1,26 @@ +using BuildingBlocks.Application.ViewModels.Messages; +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; +using CommunityToolkit.Mvvm.Messaging; + +namespace BuildingBlocks.Application.ViewModels; + +public partial class GenericItemViewModel : ObservableObject + where TModel : ObservableObject, new() +{ + private readonly IMessenger _messenger; + + [ObservableProperty] + private TModel item = new(); + + public GenericItemViewModel(IMessenger messenger) + { + _messenger = messenger; + } + + [RelayCommand] + protected virtual void Save() + { + _messenger.Send(new NewGenericItemCreatedMessage(item)); + } +} \ No newline at end of file diff --git a/src/Inventory.Application/ViewModels/Product/ProductViewModel.cs b/src/Inventory.Application/ViewModels/Product/ProductViewModel.cs index 3898e0e..d7369e1 100644 --- a/src/Inventory.Application/ViewModels/Product/ProductViewModel.cs +++ b/src/Inventory.Application/ViewModels/Product/ProductViewModel.cs @@ -1,4 +1,5 @@ -using CommunityToolkit.Mvvm.ComponentModel; +using BuildingBlocks.Application.ViewModels; +using CommunityToolkit.Mvvm.Messaging; using Inventory.Application.DomainOperations.Product.GetProductById; using Inventory.Application.Models; using Inventory.Application.Services.Navigation; @@ -6,14 +7,12 @@ namespace Inventory.Application.ViewModels.Product; -public sealed partial class ProductViewModel : ObservableObject, INavigationAware +public sealed partial class ProductViewModel : GenericItemViewModel, INavigationAware { private readonly IMediator _mediator; - [ObservableProperty] - private ProductModel? product; - - public ProductViewModel(IMediator mediator) + public ProductViewModel(IMediator mediator, IMessenger messenger) + : base(messenger) { _mediator = mediator; } @@ -29,6 +28,7 @@ public async void OnNavigatedTo(object parameter) return; } - Product = await _mediator.Send(new GetProductByIdQuery(productId)); + var product = await _mediator.Send(new GetProductByIdQuery(productId)); + Item = product ?? new ProductModel() { Name = "Product name" }; } } \ No newline at end of file diff --git a/src/Inventory.Application/ViewModels/Products/ProductsViewModel.cs b/src/Inventory.Application/ViewModels/Products/ProductsViewModel.cs index b91a986..82381cc 100644 --- a/src/Inventory.Application/ViewModels/Products/ProductsViewModel.cs +++ b/src/Inventory.Application/ViewModels/Products/ProductsViewModel.cs @@ -1,6 +1,5 @@ -using System.Collections.ObjectModel; +using System.Threading.Tasks; using BuildingBlocks.Application.ViewModels; -using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Inventory.Application.DomainOperations.Product.GetProducts; using Inventory.Application.Models; @@ -19,6 +18,7 @@ public ProductsViewModel(IMediator mediator, INavigationService navigationServic { _mediator = mediator; _navigationService = navigationService; + IsActive = true; } protected override void OnCreateNewItem() @@ -27,7 +27,7 @@ protected override void OnCreateNewItem() } [RelayCommand] - private async void Loaded() + private async Task Loaded() { await foreach (var item in _mediator.CreateStream(new GetProductsStreamQuery())) { diff --git a/src/Inventory.Infrastructure/AutofacModules/NavigationModule.cs b/src/Inventory.Infrastructure/AutofacModules/NavigationModule.cs index 252a5d3..b94ccbd 100644 --- a/src/Inventory.Infrastructure/AutofacModules/NavigationModule.cs +++ b/src/Inventory.Infrastructure/AutofacModules/NavigationModule.cs @@ -43,7 +43,7 @@ protected override void Load(ContainerBuilder builder) builder.RegisterType().InstancePerDependency(); builder.RegisterType().InstancePerDependency(); builder.RegisterType().InstancePerDependency(); - builder.RegisterType().InstancePerDependency(); + builder.RegisterType().SingleInstance(); builder.RegisterType().InstancePerDependency(); } } \ No newline at end of file diff --git a/src/Inventory.Infrastructure/AutofacModules/ServicesModule.cs b/src/Inventory.Infrastructure/AutofacModules/ServicesModule.cs index 2d7aec3..964318c 100644 --- a/src/Inventory.Infrastructure/AutofacModules/ServicesModule.cs +++ b/src/Inventory.Infrastructure/AutofacModules/ServicesModule.cs @@ -1,4 +1,5 @@ using Autofac; +using CommunityToolkit.Mvvm.Messaging; using CommunityToolkit.WinUI.Helpers; using Inventory.Application.Services.SampleData; using Inventory.Application.Services.ThemeSelector; @@ -20,5 +21,6 @@ protected override void Load(ContainerBuilder builder) builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().SingleInstance(); + builder.RegisterInstance(WeakReferenceMessenger.Default).SingleInstance(); } } \ No newline at end of file diff --git a/src/Inventory.Presentation/Views/Product/ProductView.xaml b/src/Inventory.Presentation/Views/Product/ProductView.xaml index 9c18cea..bf44d8d 100644 --- a/src/Inventory.Presentation/Views/Product/ProductView.xaml +++ b/src/Inventory.Presentation/Views/Product/ProductView.xaml @@ -11,6 +11,9 @@ + +