Skip to content

Commit 0b55ad2

Browse files
committed
Code refactoring
1 parent 57ebb1f commit 0b55ad2

File tree

7 files changed

+84
-74
lines changed

7 files changed

+84
-74
lines changed

samples/MvvmSample.Core/ViewModels/ObservableValidatorPageViewModel.cs

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,80 +2,14 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System;
6-
using System.ComponentModel.DataAnnotations;
7-
using System.Linq;
8-
using CommunityToolkit.Mvvm.ComponentModel;
9-
using CommunityToolkit.Mvvm.Input;
105
using MvvmSample.Core.Services;
116

127
namespace MvvmSample.Core.ViewModels;
138

149
public partial class ObservableValidatorPageViewModel : SamplePageViewModel
1510
{
16-
public ObservableValidatorPageViewModel(IFilesService filesService, IDialogService dialogService)
11+
public ObservableValidatorPageViewModel(IFilesService filesService)
1712
: base(filesService)
1813
{
19-
Form = new ObservableForm(dialogService);
20-
}
21-
22-
public ObservableForm Form { get; }
23-
24-
public partial class ObservableForm : ObservableValidator
25-
{
26-
private readonly IDialogService DialogService;
27-
28-
public ObservableForm(IDialogService dialogService)
29-
{
30-
DialogService = dialogService;
31-
}
32-
33-
public event EventHandler? FormSubmissionCompleted;
34-
public event EventHandler? FormSubmissionFailed;
35-
36-
[ObservableProperty]
37-
[Required]
38-
[MinLength(2)]
39-
[MaxLength(100)]
40-
private string? firstName;
41-
42-
[ObservableProperty]
43-
[Required]
44-
[MinLength(2)]
45-
[MaxLength(100)]
46-
private string? lastName;
47-
48-
[ObservableProperty]
49-
[Required]
50-
[EmailAddress]
51-
private string? email;
52-
53-
[ObservableProperty]
54-
[Required]
55-
[Phone]
56-
private string? phoneNumber;
57-
58-
[ICommand]
59-
private void Submit()
60-
{
61-
ValidateAllProperties();
62-
63-
if (HasErrors)
64-
{
65-
FormSubmissionFailed?.Invoke(this, EventArgs.Empty);
66-
}
67-
else
68-
{
69-
FormSubmissionCompleted?.Invoke(this, EventArgs.Empty);
70-
}
71-
}
72-
73-
[ICommand]
74-
private void ShowErrors()
75-
{
76-
string message = string.Join(Environment.NewLine, GetErrors().Select(e => e.ErrorMessage));
77-
78-
_ = DialogService.ShowMessageDialogAsync("Validation errors", message);
79-
}
8014
}
8115
}

samples/MvvmSample.Core/ViewModels/Widgets/SubredditWidgetViewModel.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Collections.ObjectModel;
77
using System.Threading.Tasks;
88
using CommunityToolkit.Mvvm.ComponentModel;
9-
using CommunityToolkit.Mvvm.DependencyInjection;
109
using CommunityToolkit.Mvvm.Input;
1110
using MvvmSample.Core.Models;
1211
using MvvmSample.Core.Services;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.ComponentModel.DataAnnotations;
7+
using System.Linq;
8+
using CommunityToolkit.Mvvm.ComponentModel;
9+
using CommunityToolkit.Mvvm.Input;
10+
using MvvmSample.Core.Services;
11+
12+
namespace MvvmSample.Core.ViewModels.Widgets;
13+
14+
/// <summary>
15+
/// A viewmodel for the validation widget.
16+
/// </summary>
17+
public partial class ValidationFormWidgetViewModel : ObservableValidator
18+
{
19+
private readonly IDialogService DialogService;
20+
21+
public ValidationFormWidgetViewModel(IDialogService dialogService)
22+
{
23+
DialogService = dialogService;
24+
}
25+
26+
public event EventHandler? FormSubmissionCompleted;
27+
public event EventHandler? FormSubmissionFailed;
28+
29+
[ObservableProperty]
30+
[Required]
31+
[MinLength(2)]
32+
[MaxLength(100)]
33+
private string? firstName;
34+
35+
[ObservableProperty]
36+
[Required]
37+
[MinLength(2)]
38+
[MaxLength(100)]
39+
private string? lastName;
40+
41+
[ObservableProperty]
42+
[Required]
43+
[EmailAddress]
44+
private string? email;
45+
46+
[ObservableProperty]
47+
[Required]
48+
[Phone]
49+
private string? phoneNumber;
50+
51+
[ICommand]
52+
private void Submit()
53+
{
54+
ValidateAllProperties();
55+
56+
if (HasErrors)
57+
{
58+
FormSubmissionFailed?.Invoke(this, EventArgs.Empty);
59+
}
60+
else
61+
{
62+
FormSubmissionCompleted?.Invoke(this, EventArgs.Empty);
63+
}
64+
}
65+
66+
[ICommand]
67+
private void ShowErrors()
68+
{
69+
string message = string.Join(Environment.NewLine, GetErrors().Select(e => e.ErrorMessage));
70+
71+
_ = DialogService.ShowMessageDialogAsync("Validation errors", message);
72+
}
73+
}

samples/MvvmSampleUwp/App.xaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ protected override void OnLaunched(LaunchActivatedEventArgs e)
5757
.AddTransient<MessengerPageViewModel>()
5858
.AddTransient<ObservableObjectPageViewModel>()
5959
.AddTransient<ObservableValidatorPageViewModel>()
60+
.AddTransient<ValidationFormWidgetViewModel>()
6061
.AddTransient<RelayCommandPageViewModel>()
6162
.AddTransient<SamplePageViewModel>()
6263
.BuildServiceProvider());

samples/MvvmSampleUwp/Properties/Default.rd.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@
3333
<Type Name="CommunityToolkit.Mvvm.Messaging.__Internals.__IMessengerExtensions" Dynamic="Required Public" />
3434

3535
<!-- Validation properties -->
36-
<Type Name="MvvmSample.Core.ViewModels.ObservableValidatorPageViewModel.ObservableForm" Dynamic="Required Public" />
36+
<Type Name="MvvmSample.Core.ViewModels.Widgets.ValidationFormWidgetViewModel" Dynamic="Required Public" />
3737
</Application>
3838
</Directives>

samples/MvvmSampleUwp/Views/ObservableValidatorPage.xaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<controls:DocumentationBlock Text="{x:Bind ViewModel.GetParagraph('Simple property'), Mode=OneWay}" />
2424
<controls:InteractiveSample xml:space="preserve">
2525
<controls:InteractiveSample.Content>
26-
<widgets:ValidationFormWidget DataContext="{x:Bind ViewModel.Form}" />
26+
<widgets:ValidationFormWidget />
2727
</controls:InteractiveSample.Content>
2828
<controls:InteractiveSample.XamlCode>
2929
&lt;StackPanel Spacing=&quot;16&quot;&gt;
@@ -98,11 +98,11 @@
9898
&lt;/StackPanel&gt;
9999
</controls:InteractiveSample.XamlCode>
100100
<controls:InteractiveSample.CSharpCode>
101-
public partial class ObservableForm : ObservableValidator
101+
public partial class ValidationFormWidgetViewModel : ObservableValidator
102102
{
103103
private readonly IDialogService DialogService;
104104

105-
public ObservableForm(IDialogService dialogService)
105+
public ValidationFormWidgetViewModel(IDialogService dialogService)
106106
{
107107
DialogService = dialogService;
108108
}

samples/MvvmSampleUwp/Views/Widgets/ValidationFormWidget.xaml.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using MvvmSample.Core.ViewModels;
5+
using CommunityToolkit.Mvvm.DependencyInjection;
6+
using MvvmSample.Core.ViewModels.Widgets;
67
using Windows.UI.Xaml.Controls;
78

89
namespace MvvmSampleUwp.Views.Widgets
@@ -12,8 +13,10 @@ public sealed partial class ValidationFormWidget : UserControl
1213
public ValidationFormWidget()
1314
{
1415
this.InitializeComponent();
16+
17+
DataContext = Ioc.Default.GetRequiredService<ValidationFormWidgetViewModel>();
1518
}
1619

17-
public ObservableValidatorPageViewModel.ObservableForm ViewModel => (ObservableValidatorPageViewModel.ObservableForm)DataContext;
20+
public ValidationFormWidgetViewModel ViewModel => (ValidationFormWidgetViewModel)DataContext;
1821
}
1922
}

0 commit comments

Comments
 (0)