Skip to content

Commit 3d9fb0b

Browse files
committed
refactor: move Task deletion from code behind to view model
1 parent 263156b commit 3d9fb0b

5 files changed

Lines changed: 16 additions & 25 deletions

File tree

Desktop/MainWindow.xaml.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ public MainWindow(
2424

2525
private void NavigateToTaskList()
2626
{
27-
var tasksList = new TasksList(
28-
taskRepository,
29-
taskListViewModel);
27+
var tasksList = new TasksList(taskListViewModel);
3028
Content.Content = tasksList;
3129
}
3230

Desktop/Project/TaskListViewModel.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,18 @@ public TaskListViewModel(
3030

3131
Tasks.Add(taskCreationViewModel.CreatedTask);
3232
});
33+
34+
Delete = new RelayCommand<Task>(taskToRemove =>
35+
{
36+
Tasks.Remove(taskToRemove!);
37+
taskRepository.Delete(taskToRemove!);
38+
});
3339
}
3440

3541
public ICommand Add { get; }
3642

43+
public ICommand Delete { get; }
44+
3745
public void PopulateTasks()
3846
{
3947
// TODO: load this async and deferred from the ctor.

Desktop/Project/TasksList.xaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<UserControl x:Class="Desktop.Project.TasksList"
2+
x:Name="Root"
23
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
34
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
45
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
@@ -22,7 +23,9 @@
2223
<DataTemplate DataType="{x:Type domain:Task}">
2324
<StackPanel Orientation="Horizontal">
2425
<TextBlock Text="{Binding Name}" />
25-
<Button Content="Delete" Click="Delete" />
26+
<Button Content="Delete"
27+
Command="{Binding DataContext.Delete, ElementName=Root}"
28+
CommandParameter="{Binding}" />
2629
</StackPanel>
2730
</DataTemplate>
2831
</ListBox.ItemTemplate>

Desktop/Project/TasksList.xaml.cs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Windows;
2-
using System.Windows.Controls;
1+
using System.Windows.Controls;
32
using System.Windows.Input;
43
using Desktop.Tasks;
54
using Task = Desktop.Domain.Task;
@@ -8,15 +7,8 @@ namespace Desktop.Project;
87

98
public partial class TasksList : UserControl
109
{
11-
private readonly ITaskRepository taskRepository;
12-
private readonly TaskListViewModel viewModel;
13-
14-
public TasksList(
15-
ITaskRepository taskRepository,
16-
TaskListViewModel viewModel)
10+
public TasksList(TaskListViewModel viewModel)
1711
{
18-
this.taskRepository = taskRepository;
19-
this.viewModel = viewModel;
2012
DataContext = viewModel;
2113

2214
InitializeComponent();
@@ -35,15 +27,4 @@ private void Edit(object sender, MouseButtonEventArgs e)
3527
var window = new TaskEditing(task);
3628
window.ShowDialog();
3729
}
38-
39-
private void Delete(object sender, RoutedEventArgs e)
40-
{
41-
if (sender is not Button button)
42-
return;
43-
44-
var toRemove = button.DataContext as Task;
45-
46-
viewModel.Tasks.Remove(toRemove!);
47-
taskRepository.Delete(toRemove!);
48-
}
4930
}

Desktop/Tasks/TaskCreationViewModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ public TaskCreationViewModel(ITaskRepository taskRepository)
2020
}
2121

2222
public IRelayCommand SaveTask { get; }
23+
2324
public Task? CreatedTask { get; private set; }
2425
}

0 commit comments

Comments
 (0)