Skip to content

Commit 6c53483

Browse files
committed
refactor: move request of showing the TaskCreationView to the TaskListViewModel thus completely removing the Add code behind
1 parent fbe2e8b commit 6c53483

5 files changed

Lines changed: 20 additions & 20 deletions

File tree

Desktop/Common/IShowable.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 IShowable
4+
{
5+
bool? ShowDialog();
6+
}

Desktop/Project/TaskListViewModel.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,19 @@ public class TaskListViewModel : ViewModelBase
1313
private readonly ITaskRepository taskRepository;
1414
public ObservableCollection<Task> Tasks { get; } = [];
1515

16-
public TaskListViewModel(ITaskRepository taskRepository)
16+
public Func<TaskCreationViewModel, IShowable> TaskCreationViewCreator { get; set; }
17+
18+
public TaskListViewModel(
19+
ITaskRepository taskRepository,
20+
TaskCreationViewModel taskCreationViewModel)
1721
{
1822
this.taskRepository = taskRepository;
1923

20-
Add = new RelayCommand<TaskCreationViewModel>(taskCreationViewModel =>
24+
Add = new RelayCommand(() =>
2125
{
22-
if (taskCreationViewModel!.CreatedTask is null)
26+
TaskCreationViewCreator!(taskCreationViewModel).ShowDialog();
27+
28+
if (taskCreationViewModel.CreatedTask is null)
2329
return;
2430

2531
Tasks.Add(taskCreationViewModel.CreatedTask);

Desktop/Project/TasksList.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<StackPanel>
1515
<StackPanel Orientation="Horizontal">
1616
<Label Content="Tasks:" />
17-
<Button AutomationProperties.AutomationId="AddTask" Content="_Add task" Click="Add" />
17+
<Button AutomationProperties.AutomationId="AddTask" Content="_Add task" Command="{Binding Add}" />
1818
</StackPanel>
1919

2020
<ListBox Name="Tasks">

Desktop/Project/TasksList.xaml.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,15 @@ public TasksList(
1919
{
2020
this.taskRepository = taskRepository;
2121
this.viewModel = viewModel;
22+
DataContext = viewModel;
2223
this.taskCreationViewModel = taskCreationViewModel;
2324

2425
InitializeComponent();
2526
Tasks.ItemsSource = viewModel.Tasks;
2627

2728
viewModel.PopulateTasks();
28-
}
29-
30-
private void Add(object sender, RoutedEventArgs e)
31-
{
32-
new TaskCreation(taskCreationViewModel).ShowDialog();
33-
34-
viewModel.Add.Execute(taskCreationViewModel);
29+
viewModel.TaskCreationViewCreator = creationViewModel =>
30+
new TaskCreation(creationViewModel);
3531
}
3632

3733
private void Edit(object sender, MouseButtonEventArgs e)

Desktop/Tasks/TaskCreation.xaml.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
using System.Windows;
22
using Desktop.Common;
3-
using Task = Desktop.Domain.Task;
43

54
namespace Desktop.Tasks;
65

7-
using Task = Task;
8-
9-
public partial class TaskCreation : Window, ICloseable
6+
public partial class TaskCreation : Window, ICloseable, IShowable
107
{
11-
private readonly TaskCreationViewModel viewModel;
12-
138
public TaskCreation(TaskCreationViewModel viewModel)
149
{
1510
InitializeComponent();
1611
DataContext = viewModel;
17-
this.viewModel = viewModel;
1812
}
19-
20-
public Task? CreatedTask => viewModel.CreatedTask;
2113
}

0 commit comments

Comments
 (0)