Skip to content

Commit

Permalink
Add CustomerEditorWindow.xaml
Browse files Browse the repository at this point in the history
  • Loading branch information
ousiax committed Dec 10, 2023
1 parent 6527b32 commit d02bb30
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/Leo.Wpf.App/Abstractions/ICustomerEditorWindowService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Leo.Wpf.App
{
public interface ICustomerEditorWindowService
{
bool? ShowDialog(string customerId);
}
}
3 changes: 3 additions & 0 deletions src/Leo.Wpf.App/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public static IServiceCollection AddLeoViewModels(this IServiceCollection servic
services.AddTransient<NewCustomerDetailViewModel>();
services.AddTransient<INewCustomerDetailWindowService, NewCustomerDetailWindowService>();

services.AddTransient<CustomerEditorViewModel>();
services.AddTransient<ICustomerEditorWindowService, CustomerEditorWindowService>();

return services;
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/Leo.Wpf.App/Services/CustomerEditorWindowService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Leo.Wpf.App.ViewModels;
using Leo.Wpf.App.Views;
using Microsoft.Extensions.DependencyInjection;
using System.Windows;

namespace Leo.Wpf.App.Services
{
internal sealed class CustomerEditorWindowService(IServiceProvider _services) : ICustomerEditorWindowService
{
public bool? ShowDialog(string customerId)
{
var viewModel = _services.GetRequiredService<CustomerEditorViewModel>();
_ = viewModel.LoadSeletedCustomerAsync(customerId);
var window = new CustomerEditorWindow(viewModel)
{
Owner = Application.Current.MainWindow,
WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner
};
return window.ShowDialog();
}
}
}
73 changes: 73 additions & 0 deletions src/Leo.Wpf.App/ViewModels/CustomerEditorViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using AutoMapper;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Leo.Data.Domain.Dtos;
using Leo.UI;
using Leo.Wpf.App.Messages;

namespace Leo.Wpf.App.ViewModels
{
public partial class CustomerEditorViewModel : ObservableObject
{
private CustomerViewModel? _seletedCustomer;

private readonly ICustomerService _customerService;
private readonly IMapper _mapper;
private readonly IMessenger _messenger;

public CustomerEditorViewModel(
ICustomerService customerService,
IMapper mapper,
IMessenger messenger)
{
_customerService = customerService;
_mapper = mapper;
_messenger = messenger;
SaveCommand = new AsyncRelayCommand(SaveAsync, () => SeletedCustomer != null);
}

public CustomerViewModel? SeletedCustomer
{
get { return _seletedCustomer; }
set
{
if (_seletedCustomer != value)
{
SetProperty(ref _seletedCustomer, value);
SaveCommand.NotifyCanExecuteChanged();
}
}
}

public IRelayCommand SaveCommand { get; init; }

public event Action? CloseAction;

public async Task LoadSeletedCustomerAsync(string customerId)
{
ArgumentException.ThrowIfNullOrEmpty(customerId, nameof(customerId));

var dto = await _customerService.GetAsync(Guid.Parse(customerId));
var customerViewModel = _mapper.Map<CustomerViewModel>(dto);
if (customerViewModel != null)
{
SeletedCustomer = customerViewModel;
}
}

private async Task SaveAsync()
{
if (SeletedCustomer != null)
{
var dto = _mapper.Map<CustomerDto>(this);
await _customerService.UpdateAsync(dto);
_messenger.Send(new CustomerCreatedMessage(SeletedCustomer.Id.ToString()));
}
Close();
}

[RelayCommand]
private void Close() => CloseAction?.Invoke();
}
}
12 changes: 12 additions & 0 deletions src/Leo.Wpf.App/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public sealed partial class MainWindowViewModel : ObservableRecipient, IDisposab
private readonly ICustomerDetailService _detailService;
private readonly IMapper _mapper;
private readonly INewCustomerWindowService _newCustomerWindow;
private readonly ICustomerEditorWindowService _customerEditorWindow;
private readonly INewCustomerDetailWindowService _newCustomerDetailWindow;
private readonly IFindWindowService _findWindow;

Expand All @@ -26,6 +27,7 @@ public MainWindowViewModel(
ICustomerDetailService detailService,
IMapper mapper,
INewCustomerWindowService newCustomerWindowService,
ICustomerEditorWindowService customerEditorWindow,
INewCustomerDetailWindowService newCustomerDetailWindowService,
IFindWindowService findWindowService,
IMessenger messenger) : base(messenger)
Expand All @@ -34,6 +36,7 @@ public MainWindowViewModel(
_detailService = detailService;
_mapper = mapper;
_newCustomerWindow = newCustomerWindowService;
_customerEditorWindow = customerEditorWindow;
_newCustomerDetailWindow = newCustomerDetailWindowService;
_findWindow = findWindowService;

Expand All @@ -59,6 +62,15 @@ private void NewCustomer()
_newCustomerWindow.ShowDialog();
}

[RelayCommand]
private void UpdateCustomer()
{
if (CurrentCustomer != null)
{
_customerEditorWindow.ShowDialog(CurrentCustomer.Id.ToString());
}
}

[RelayCommand]
private void NewCustomerDetail(Guid customerId)
{
Expand Down
84 changes: 84 additions & 0 deletions src/Leo.Wpf.App/Views/CustomerEditorWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<Window x:Class="Leo.Wpf.App.Views.CustomerEditorWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModel="clr-namespace:Leo.Wpf.App.ViewModels"
xmlns:infra="clr-namespace:Leo.Wpf.App.Infrastructure"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance viewModel:CustomerEditorViewModel}"
Title="CustomerEditorWindow"
WindowStyle="ToolWindow"
ResizeMode="NoResize"
SizeToContent="WidthAndHeight"
WindowStartupLocation="CenterOwner">
<Window.Resources>
<Style x:Key="BaseStyle">
<Setter Property="Control.MinWidth" Value="150" />
<Setter Property="Control.Margin" Value="2,0,5,0" />
<Setter Property="Control.VerticalContentAlignment" Value="Center" />
<Setter Property="Control.Margin" Value="0,1,0,1" />
</Style>
<Style TargetType="Label">
<Setter Property="MinWidth" Value="80"/>
<Setter Property="Background" Value="Gainsboro"/>
<Setter Property="HorizontalContentAlignment" Value="Right"/>
<Setter Property="Margin" Value="0,1,2,1" />
</Style>
<Style TargetType="TextBox" BasedOn="{StaticResource BaseStyle}">
</Style>
<Style TargetType="ComboBox" BasedOn="{StaticResource BaseStyle}">
</Style>
<Style TargetType="DatePickerTextBox" BasedOn="{StaticResource BaseStyle}">
</Style>
<Style TargetType="Button">
<Setter Property="Margin" Value="0,5,5,0" />
<Setter Property="MinWidth" Value="80" />
<Setter Property="MinHeight" Value="30" />
</Style>
</Window.Resources>
<Grid Margin="5,5,5,5">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<Label Content="宝宝姓名: " Target="{Binding ElementName=txtName}" />
<TextBox x:Name="txtName" Text="{Binding SeletedCustomer.Name}" />
</StackPanel>
<StackPanel Grid.Row="1" Orientation="Horizontal">
<Label Content="出生日期: " />
<DatePickerTextBox x:Name="txtBirthday" Text="{Binding SeletedCustomer.Birthday, StringFormat=yyyy-MM-dd}"/>
</StackPanel>
<StackPanel Grid.Row="2" Orientation="Horizontal">
<Label Content="性别: " />
<ComboBox x:Name="txtGender"
ItemsSource="{x:Static infra:Constants.Genders}"
SelectedValuePath="Key"
DisplayMemberPath="Value"
SelectedValue="{Binding SeletedCustomer.Gender}"/>
</StackPanel>
<StackPanel Grid.Row="3" Orientation="Horizontal">
<Label Content="手机: " />
<TextBox x:Name="txtPhone" Text="{Binding SeletedCustomer.Phone}" />
</StackPanel>
<StackPanel Grid.Row="4" Orientation="Horizontal">
<Label Content="会员卡号: " />
<TextBox x:Name="txtCarNo" Text="{Binding SeletedCustomer.CardNo}" />
</StackPanel>
<StackPanel Grid.Row="5" Orientation="Horizontal" FlowDirection="RightToLeft">
<Button x:Name="btnOK"
Command="{Binding SaveCommand}"
Content="保存"
IsDefault="True" />
<Button x:Name="btnCancel"
Command="{Binding CloseCommand}"
Content="取消"
IsCancel="True" />
</StackPanel>
</Grid>
</Window>
18 changes: 18 additions & 0 deletions src/Leo.Wpf.App/Views/CustomerEditorWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Leo.Wpf.App.ViewModels;
using System.Windows;

namespace Leo.Wpf.App.Views
{
/// <summary>
/// Interaction logic for CustomerEditorWindow.xaml
/// </summary>
public partial class CustomerEditorWindow : Window
{
public CustomerEditorWindow(CustomerEditorViewModel viewModel)
{
InitializeComponent();
DataContext = viewModel;
viewModel.CloseAction += () => Close();
}
}
}
2 changes: 1 addition & 1 deletion src/Leo.Wpf.App/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<ToolBarTray Grid.Row="0">
<ToolBar>
<Button Content="新增" Command="{Binding NewCustomerCommand}"/>
<Button Content="修改" />
<Button Content="修改" Command="{Binding UpdateCustomerCommand}" />
<Button Content="查找" Command="{Binding FindCustomerCommand}" />
<Button Content="回音" />
</ToolBar>
Expand Down

0 comments on commit d02bb30

Please sign in to comment.