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 groupPresenters = new List(); private ObservableCollection _groups; public ObservableCollection 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 _students; public ObservableCollection Students { get => _students; set => this.RaiseAndSetIfChanged(ref _students, value); } public MainWindowViewModel() { _groupService = null; _groups = new(); _students = new(); } public MainWindowViewModel(IGroupUseCase gService, IGroupUseCase groupService, ObservableCollection groups, ObservableCollection 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); } } }