113 lines
3.4 KiB
C#
113 lines
3.4 KiB
C#
using domain.UseCase;
|
|
using Presence.Desktop.ViewModels;
|
|
using ReactiveUI;
|
|
using System.Collections.ObjectModel;
|
|
using System.Windows.Input;
|
|
using System.Linq;
|
|
using System.Reactive.Linq;
|
|
using System;
|
|
|
|
public class MainWindowViewModel : ViewModelBase
|
|
{
|
|
private readonly GroupUseCase _groupUseCase;
|
|
private ObservableCollection<GroupPresenter> _groups;
|
|
public ObservableCollection<GroupPresenter> Groups => _groups;
|
|
|
|
private GroupPresenter? _selectedGroupItem;
|
|
public GroupPresenter? SelectedGroupItem
|
|
{
|
|
get => _selectedGroupItem;
|
|
set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value);
|
|
}
|
|
|
|
private ObservableCollection<UserPresenter> _users;
|
|
public ObservableCollection<UserPresenter> Users => _users;
|
|
|
|
public UserPresenter? SelectedUser
|
|
{
|
|
get => _selectedUser;
|
|
set => this.RaiseAndSetIfChanged(ref _selectedUser, value);
|
|
}
|
|
private UserPresenter? _selectedUser;
|
|
|
|
public ObservableCollection<UserPresenter> SelectedUsers { get; } = new ObservableCollection<UserPresenter>();
|
|
|
|
public ICommand RemoveSelectedStudentsCommand { get; }
|
|
public ICommand EditStudentCommand { get; }
|
|
public ICommand RemoveSingleStudentCommand { get; }
|
|
|
|
public MainWindowViewModel(GroupUseCase groupUseCase)
|
|
{
|
|
_groupUseCase = groupUseCase;
|
|
|
|
_groups = new ObservableCollection<GroupPresenter>(
|
|
groupUseCase.GetAllGroupWithStident().Select(group => new GroupPresenter
|
|
{
|
|
Id = group.Id,
|
|
Name = group.Name,
|
|
users = group.Users?.Select(user => new UserPresenter
|
|
{
|
|
Name = user.FIO,
|
|
Guid = user.Guid,
|
|
Group = new GroupPresenter { Id = group.Id, Name = group.Name }
|
|
}).ToList()
|
|
})
|
|
);
|
|
|
|
_users = new ObservableCollection<UserPresenter>();
|
|
|
|
this.WhenAnyValue(vm => vm.SelectedGroupItem)
|
|
.ObserveOn(RxApp.MainThreadScheduler)
|
|
.Subscribe(_ => SetUsers(), ex => Console.WriteLine($"Ошибка: {ex.Message}"));
|
|
|
|
EditStudentCommand = ReactiveCommand.Create(EditStudent);
|
|
RemoveSingleStudentCommand = ReactiveCommand.Create(RemoveSingleStudent);
|
|
RemoveSelectedStudentsCommand = ReactiveCommand.Create(RemoveSelectedStudents);
|
|
}
|
|
|
|
private void RemoveSelectedStudents()
|
|
{
|
|
if (SelectedUsers.Count == 0) return;
|
|
|
|
foreach (var user in SelectedUsers.ToList())
|
|
{
|
|
_groupUseCase.RemoveStudentFromGroup(user.Group.Id, user.Guid);
|
|
|
|
// Удаляем из локального списка
|
|
SelectedGroupItem?.users?.ToList().Remove(user);
|
|
}
|
|
|
|
SelectedUsers.Clear();
|
|
SetUsers();
|
|
}
|
|
|
|
private void RemoveSingleStudent()
|
|
{
|
|
if (SelectedUser == null) return;
|
|
|
|
_groupUseCase.RemoveStudentFromGroup(SelectedUser.Group.Id, SelectedUser.Guid);
|
|
|
|
SelectedGroupItem?.users?.ToList().Remove(SelectedUser);
|
|
SetUsers();
|
|
}
|
|
|
|
private void EditStudent()
|
|
{
|
|
if (SelectedUser == null) return;
|
|
|
|
// Пример изменения имени
|
|
SelectedUser.Name = "Изменённое имя";
|
|
}
|
|
|
|
private void SetUsers()
|
|
{
|
|
Users.Clear();
|
|
if (SelectedGroupItem?.users == null) return;
|
|
|
|
foreach (var user in SelectedGroupItem.users)
|
|
{
|
|
Users.Add(user);
|
|
}
|
|
}
|
|
}
|