presence/Presence.Desktop/ViewModels/MainWindowViewModel.cs
2024-12-02 13:04:59 +03:00

69 lines
2.2 KiB
C#

using DocumentFormat.OpenXml.Bibliography;
using domain.UseCase;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive.Linq;
namespace Presence.Desktop.ViewModels
{
public class MainWindowViewModel : ViewModelBase
{
private readonly GroupUseCase _groupUseCase;
private List<GroupPresenter> groupPresentersDataSource = new List<GroupPresenter>();
private ObservableCollection<GroupPresenter> _groups;
public ObservableCollection<GroupPresenter> Groups => _groups;
public GroupPresenter? SelectedGroupItem
{
get => _selectedGroupItem;
set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value);
}
private GroupPresenter? _selectedGroupItem;
public ObservableCollection<UserPresenter> Users { get => _users; }
private ObservableCollection<UserPresenter> _users;
public MainWindowViewModel(GroupUseCase groupUseCase)
{
_groupUseCase = groupUseCase;
foreach (var item in _groupUseCase.GetAllGroupWithStident())
{
GroupPresenter groupPresenter = new GroupPresenter
{
Id = item.Id,
Name = item.Name,
users = item.Users?.Select(user => new UserPresenter
{
Name = user.FIO,
Guid = user.Guid,
Group = new GroupPresenter { Id = item.Id, Name = item.Name }
}).ToList()
};
groupPresentersDataSource.Add(groupPresenter);
}
_groups = new ObservableCollection<GroupPresenter>(groupPresentersDataSource);
_users = new ObservableCollection<UserPresenter>();
this.WhenAnyValue(vm => vm.SelectedGroupItem)
.Subscribe(vm => SetUsers());
}
private void SetUsers()
{
if (SelectedGroupItem?.users == null) return;
Users.Clear();
foreach (var item in SelectedGroupItem.users)
{
Users.Add(item);
}
}
}
}