Skip to content

Commit

Permalink
Added simple messenger configuration for generic view models #28
Browse files Browse the repository at this point in the history
  • Loading branch information
LeftTwixWand committed Oct 27, 2022
1 parent 2aa9a41 commit 283f969
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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<TModel> : 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<TModel>(item));
}
}
14 changes: 7 additions & 7 deletions src/Inventory.Application/ViewModels/Product/ProductViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
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;
using MediatR;

namespace Inventory.Application.ViewModels.Product;

public sealed partial class ProductViewModel : ObservableObject, INavigationAware
public sealed partial class ProductViewModel : GenericItemViewModel<ProductModel>, INavigationAware
{
private readonly IMediator _mediator;

[ObservableProperty]
private ProductModel? product;

public ProductViewModel(IMediator mediator)
public ProductViewModel(IMediator mediator, IMessenger messenger)
: base(messenger)
{
_mediator = mediator;
}
Expand All @@ -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" };
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,6 +18,7 @@ public ProductsViewModel(IMediator mediator, INavigationService navigationServic
{
_mediator = mediator;
_navigationService = navigationService;
IsActive = true;
}

protected override void OnCreateNewItem()
Expand All @@ -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()))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected override void Load(ContainerBuilder builder)
builder.RegisterType<ContentGridViewModel>().InstancePerDependency();
builder.RegisterType<ListDetailsViewModel>().InstancePerDependency();
builder.RegisterType<WebViewViewModel>().InstancePerDependency();
builder.RegisterType<ProductsViewModel>().InstancePerDependency();
builder.RegisterType<ProductsViewModel>().SingleInstance();
builder.RegisterType<ProductViewModel>().InstancePerDependency();
}
}
2 changes: 2 additions & 0 deletions src/Inventory.Infrastructure/AutofacModules/ServicesModule.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Autofac;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.WinUI.Helpers;
using Inventory.Application.Services.SampleData;
using Inventory.Application.Services.ThemeSelector;
Expand All @@ -20,5 +21,6 @@ protected override void Load(ContainerBuilder builder)

builder.RegisterType<SampleDataService>().As<ISampleDataService>().SingleInstance();
builder.RegisterType<ThemeSelectorService>().As<IThemeSelectorService>().SingleInstance();
builder.RegisterInstance<IMessenger>(WeakReferenceMessenger.Default).SingleInstance();
}
}
9 changes: 6 additions & 3 deletions src/Inventory.Presentation/Views/Product/ProductView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<Grid x:Name="ContentArea">
<ScrollViewer IsTabStop="True">
<StackPanel x:Name="contentPanel">

<Button Content="Save" Command="{x:Bind ViewModel.SaveCommand}"/>

<RelativePanel>
<Grid
x:Name="itemHero"
Expand All @@ -26,7 +29,7 @@
<Image
HorizontalAlignment="Center"
VerticalAlignment="Center"
Source="{x:Bind ViewModel.Product.ImageSource, Mode=OneWay}" />
Source="{x:Bind ViewModel.Item.ImageSource, Mode=OneWay}" />
</Grid>

<TextBlock
Expand All @@ -35,11 +38,11 @@
RelativePanel.AlignTopWithPanel="True"
RelativePanel.RightOf="itemHero"
Style="{ThemeResource TitleTextBlockStyle}"
Text="{x:Bind ViewModel.Product.Category, Mode=OneWay}" />
Text="{x:Bind ViewModel.Item.Category, Mode=OneWay}" />

<StackPanel x:Name="propertiesGroup1" RelativePanel.Below="itemHero">

<TextBlock Text="{x:Bind ViewModel.Product.Name}" />
<TextBlock Text="{x:Bind ViewModel.Item.Name}" />

<StackPanel x:Name="statusGroup" Margin="{StaticResource SmallTopMargin}">
<TextBlock Style="{ThemeResource SubtitleTextBlockStyle}" Text="Status" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
animations:Connected.ListItemKey="animationKeyContentGrid"
IsItemClickEnabled="True"
ItemClickCommand="{x:Bind ViewModel.ItemClickedCommand}"
ItemsSource="{x:Bind ViewModel.Products, Mode=OneWay}"
ItemsSource="{x:Bind ViewModel.Items, Mode=OneWay}"
SelectionMode="None"
StretchContentForSingleRow="False">

Expand Down

0 comments on commit 283f969

Please sign in to comment.