Skip to content

Commit fbe2e8b

Browse files
committed
refactor: move Tasks list to view model + non-view code from code behind
1 parent 894d6a9 commit fbe2e8b

2 files changed

Lines changed: 36 additions & 19 deletions

File tree

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,38 @@
1-
using Desktop.Common;
1+
using System.Collections.ObjectModel;
2+
using System.Windows.Input;
3+
using CommunityToolkit.Mvvm.Input;
4+
using Desktop.Common;
5+
using Desktop.Tasks;
6+
using Task = Desktop.Domain.Task;
7+
using SystemTask = System.Threading.Tasks.Task;
28

39
namespace Desktop.Project;
410

511
public class TaskListViewModel : ViewModelBase
612
{
7-
public TaskListViewModel()
13+
private readonly ITaskRepository taskRepository;
14+
public ObservableCollection<Task> Tasks { get; } = [];
15+
16+
public TaskListViewModel(ITaskRepository taskRepository)
17+
{
18+
this.taskRepository = taskRepository;
19+
20+
Add = new RelayCommand<TaskCreationViewModel>(taskCreationViewModel =>
21+
{
22+
if (taskCreationViewModel!.CreatedTask is null)
23+
return;
24+
25+
Tasks.Add(taskCreationViewModel.CreatedTask);
26+
});
27+
}
28+
29+
public ICommand Add { get; }
30+
31+
public void PopulateTasks()
832
{
33+
// TODO: load this async and deferred from the ctor.
34+
var retrievedTasks = SystemTask.Run(taskRepository.All).Result;
35+
foreach (var task in retrievedTasks)
36+
Tasks.Add(task);
937
}
1038
}

Desktop/Project/TasksList.xaml.cs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
using System.Collections.ObjectModel;
2-
using System.Windows;
1+
using System.Windows;
32
using System.Windows.Controls;
43
using System.Windows.Input;
54
using Desktop.Tasks;
65
using Task = Desktop.Domain.Task;
76

87
namespace Desktop.Project;
98

10-
using SystemTask = System.Threading.Tasks.Task;
11-
129
public partial class TasksList : UserControl
1310
{
1411
private readonly ITaskRepository taskRepository;
1512
private readonly TaskListViewModel viewModel;
1613
private readonly TaskCreationViewModel taskCreationViewModel;
17-
private readonly ObservableCollection<Task> tasks = [];
1814

1915
public TasksList(
2016
ITaskRepository taskRepository,
@@ -26,23 +22,16 @@ public TasksList(
2622
this.taskCreationViewModel = taskCreationViewModel;
2723

2824
InitializeComponent();
29-
Tasks.ItemsSource = tasks;
25+
Tasks.ItemsSource = viewModel.Tasks;
3026

31-
// TODO: load this async and deferred from the ctor.
32-
var retrievedTasks = SystemTask.Run(taskRepository.All).Result;
33-
foreach (var task in retrievedTasks)
34-
tasks.Add(task);
27+
viewModel.PopulateTasks();
3528
}
3629

3730
private void Add(object sender, RoutedEventArgs e)
3831
{
39-
var taskCreationWindow = new TaskCreation(taskCreationViewModel);
40-
taskCreationWindow.ShowDialog();
41-
42-
if (taskCreationWindow.CreatedTask is null)
43-
return;
32+
new TaskCreation(taskCreationViewModel).ShowDialog();
4433

45-
tasks.Add(taskCreationWindow.CreatedTask);
34+
viewModel.Add.Execute(taskCreationViewModel);
4635
}
4736

4837
private void Edit(object sender, MouseButtonEventArgs e)
@@ -61,7 +50,7 @@ private void Delete(object sender, RoutedEventArgs e)
6150

6251
var toRemove = button.DataContext as Task;
6352

64-
tasks.Remove(toRemove!);
53+
viewModel.Tasks.Remove(toRemove!);
6554
taskRepository.Delete(toRemove!);
6655
}
6756
}

0 commit comments

Comments
 (0)