presence/Presence.Desktop/ViewModels/MainWindowViewModel.cs

90 lines
2.8 KiB
C#

using domain.UseCase;
using Presence.Desktop.Models;
using System;
using System.Linq;
using ReactiveUI;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace Presence.Desktop.ViewModels
{
public class MainWindowViewModel : ViewModelBase
{
private readonly IGroupUseCase _groupService;
private List<GroupPresenter> groupPresenters = new List<GroupPresenter>();
private ObservableCollection<GroupPresenter> _groups;
public ObservableCollection<GroupPresenter> Groups
{
get => _groups;
set => this.RaiseAndSetIfChanged(ref _groups, value);
}
private GroupPresenter? _selectedGroupItem;
public GroupPresenter? SelectedGroupItem
{
get => _selectedGroupItem;
set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value);
}
private ObservableCollection<StudentPresenter> _students;
public ObservableCollection<StudentPresenter> Students
{
get => _students;
set => this.RaiseAndSetIfChanged(ref _students, value);
}
public MainWindowViewModel()
{
_groupService = null;
_groups = new();
_students = new();
}
public MainWindowViewModel(IGroupUseCase gService, IGroupUseCase groupService, ObservableCollection<GroupPresenter> groups, ObservableCollection<StudentPresenter> students)
{
_groupService = gService;
foreach (var item in _groupService.GetGroupsWithStudents())
{
GroupPresenter groupPresenter = new GroupPresenter
{
Id = item.Id,
Name = item.Name,
students = item.Users?.Select(u => new StudentPresenter
{
Id = u.Id,
FirstName = u.FirstName,
LastName = u.LastName,
Patronymic = u.Patronymic,
Group = new GroupPresenter
{
Id = item.Id,
Name = item.Name
}
})
};
groupPresenters.Add(groupPresenter);
}
_groups = new(groupPresenters);
_students = new();
this.WhenAnyValue(vm => vm.SelectedGroupItem)
.Subscribe(_ => SetStudents());
}
private void SetStudents()
{
if (SelectedGroupItem == null) return;
if (SelectedGroupItem.students == null) return;
Students.Clear();
foreach (var student in SelectedGroupItem.students)
Students.Add(student);
}
}
}