Skip to content

Commit d53a821

Browse files
committed
refactor(TaskCreation): move Window closing
1 parent da6d091 commit d53a821

6 files changed

Lines changed: 58 additions & 5 deletions

File tree

Desktop/Common/ICloseable.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Desktop.Common;
2+
3+
public interface ICloseable
4+
{
5+
void Close();
6+
}

Desktop/Common/ViewModelBase.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.ComponentModel;
2+
using System.Runtime.CompilerServices;
3+
4+
namespace Desktop.Common;
5+
6+
public abstract class ViewModelBase : INotifyPropertyChanged
7+
{
8+
public event PropertyChangedEventHandler? PropertyChanged;
9+
10+
protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
11+
{
12+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
13+
}
14+
15+
protected bool SetField<T>(
16+
ref T field,
17+
T value,
18+
[CallerMemberName] string? propertyName = null)
19+
{
20+
if (EqualityComparer<T>.Default.Equals(field, value))
21+
return false;
22+
23+
field = value;
24+
OnPropertyChanged(propertyName);
25+
26+
return true;
27+
}
28+
}

Desktop/Desktop.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828

2929
<ItemGroup>
3030
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="6.0.0"/>
31-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.5" />
32-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.5" />
31+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.5"/>
32+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.5"/>
33+
<PackageReference Include="MVVMToolkitExtensions.WPF" Version="1.0.5"/>
3334
</ItemGroup>
3435

3536
<ItemGroup>

Desktop/Tasks/TaskCreation.xaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="clr-namespace:Desktop.Tasks"
67
mc:Ignorable="d"
8+
x:Name="TaskCreationWindow"
79
Title="Task creation" Height="225" Width="400"
810
WindowStartupLocation="CenterScreen">
11+
<Window.DataContext>
12+
<local:TaskCreationViewModel />
13+
</Window.DataContext>
914
<Grid FocusManager.FocusedElement="{Binding ElementName=Name}">
1015
<Grid.RowDefinitions>
1116
<RowDefinition Height="Auto" />
@@ -21,6 +26,8 @@
2126
VerticalAlignment="Stretch" />
2227
<Button AutomationProperties.AutomationId="SaveTask" Grid.Row="4" Content="Add" HorizontalAlignment="Right"
2328
IsDefault="True"
24-
Click="SaveTask" />
29+
Click="SaveTask"
30+
Command="{Binding SaveTask }"
31+
CommandParameter="{Binding ElementName=TaskCreationWindow}" />
2532
</Grid>
2633
</Window>

Desktop/Tasks/TaskCreation.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
using System.Windows;
2+
using Desktop.Common;
23
using Task = Desktop.Domain.Task;
34

45
namespace Desktop.Tasks;
56

67
using Task = Task;
78

8-
public partial class TaskCreation : Window
9+
public partial class TaskCreation : Window, ICloseable
910
{
1011
public TaskCreation()
1112
{
@@ -17,6 +18,5 @@ public TaskCreation()
1718
private void SaveTask(object sender, RoutedEventArgs e)
1819
{
1920
CreatedTask = new Task(name: Name.Text, description: Description.Text);
20-
Close();
2121
}
2222
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Windows.Input;
2+
using CommunityToolkit.Mvvm.Input;
3+
using Desktop.Common;
4+
5+
namespace Desktop.Tasks;
6+
7+
public class TaskCreationViewModel : ViewModelBase
8+
{
9+
public ICommand SaveTask { get; } =
10+
new RelayCommand<ICloseable>(closeable => { closeable.Close(); });
11+
}

0 commit comments

Comments
 (0)