133 lines
4.4 KiB
C#
133 lines
4.4 KiB
C#
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Collections.ObjectModel;
|
||
|
using System.Linq;
|
||
|
using System.Reactive;
|
||
|
using System.Reactive.Linq;
|
||
|
using System.Threading.Tasks;
|
||
|
using Demo.Domain.Models;
|
||
|
using presence_client.ApiClients;
|
||
|
using presence_client.DTO;
|
||
|
using ReactiveUI;
|
||
|
|
||
|
namespace Desktop.UI.ViewModels;
|
||
|
|
||
|
public class GroupWindowViewModel : ReactiveObject, IRoutableViewModel
|
||
|
{
|
||
|
private readonly GroupApiClient _groupApi;
|
||
|
private readonly UserApiClient _userApi;
|
||
|
private readonly PresenceApiClient _presenceApi;
|
||
|
|
||
|
public string UrlPathSegment => "group";
|
||
|
public IScreen HostScreen { get; }
|
||
|
|
||
|
public ObservableCollection<Group> Groups { get; } = new();
|
||
|
public ObservableCollection<User> FilteredAndSortedStudents { get; } = new();
|
||
|
public ObservableCollection<User> SelectedStudents { get; } = new();
|
||
|
|
||
|
public ReactiveCommand<Unit, Unit> DeleteAllStudentsCommand { get; }
|
||
|
public ReactiveCommand<Unit, Unit> DeleteSelectedStudentsCommand { get; }
|
||
|
public ReactiveCommand<User, Unit> DeleteStudentCommand { get; }
|
||
|
public ReactiveCommand<User, Unit> EditStudentCommand { get; }
|
||
|
|
||
|
public List<string> SortOptions { get; } = new() { "Без сортировки", "По фамилии ↑", "По фамилии ↓" };
|
||
|
|
||
|
private Group? _selectedGroup;
|
||
|
public Group? SelectedGroup
|
||
|
{
|
||
|
get => _selectedGroup;
|
||
|
set => this.RaiseAndSetIfChanged(ref _selectedGroup, value);
|
||
|
}
|
||
|
|
||
|
private string _selectedSortOption = "Без сортировки";
|
||
|
public string SelectedSortOption
|
||
|
{
|
||
|
get => _selectedSortOption;
|
||
|
set => this.RaiseAndSetIfChanged(ref _selectedSortOption, value);
|
||
|
}
|
||
|
|
||
|
public GroupWindowViewModel(GroupApiClient groupApi, UserApiClient userApi, PresenceApiClient presenceApi, IScreen screen)
|
||
|
{
|
||
|
_groupApi = groupApi;
|
||
|
_userApi = userApi;
|
||
|
_presenceApi = presenceApi;
|
||
|
HostScreen = screen;
|
||
|
|
||
|
DeleteAllStudentsCommand = ReactiveCommand.CreateFromTask(DeleteAllStudents);
|
||
|
DeleteSelectedStudentsCommand = ReactiveCommand.CreateFromTask(DeleteSelectedStudents);
|
||
|
DeleteStudentCommand = ReactiveCommand.CreateFromTask<User>(DeleteStudent);
|
||
|
EditStudentCommand = ReactiveCommand.Create<User>(EditStudent); // пока-заглушка
|
||
|
|
||
|
this.WhenAnyValue(x => x.SelectedGroup, x => x.SelectedSortOption)
|
||
|
.Throttle(TimeSpan.FromMilliseconds(200))
|
||
|
.ObserveOn(RxApp.MainThreadScheduler)
|
||
|
.Subscribe(async _ => await LoadStudents());
|
||
|
|
||
|
LoadGroups();
|
||
|
}
|
||
|
|
||
|
private async void LoadGroups()
|
||
|
{
|
||
|
var groups = await _groupApi.ClientGetGroups();
|
||
|
Groups.Clear();
|
||
|
foreach (var group in groups)
|
||
|
Groups.Add(group);
|
||
|
}
|
||
|
|
||
|
private async Task LoadStudents()
|
||
|
{
|
||
|
if (SelectedGroup == null)
|
||
|
{
|
||
|
FilteredAndSortedStudents.Clear();
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
var response = await _presenceApi.ClientGetPresence(new PresenceQuery { GroupName = SelectedGroup.Name });
|
||
|
var students = response.Select(p => new User {
|
||
|
FIO = p.FIO,
|
||
|
Guid = Guid.NewGuid(), // или другое значение
|
||
|
Group = SelectedGroup! // ← добавьте это
|
||
|
}).DistinctBy(u => u.FIO).ToList();
|
||
|
|
||
|
switch (SelectedSortOption)
|
||
|
{
|
||
|
case "По фамилии ↑":
|
||
|
students = students.OrderBy(s => s.FIO).ToList();
|
||
|
break;
|
||
|
case "По фамилии ↓":
|
||
|
students = students.OrderByDescending(s => s.FIO).ToList();
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
FilteredAndSortedStudents.Clear();
|
||
|
foreach (var student in students)
|
||
|
FilteredAndSortedStudents.Add(student);
|
||
|
}
|
||
|
|
||
|
private async Task DeleteAllStudents()
|
||
|
{
|
||
|
if (SelectedGroup != null)
|
||
|
{
|
||
|
await _presenceApi.ClientDeletePresenceByGroup(SelectedGroup.Name);
|
||
|
await LoadStudents();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private async Task DeleteSelectedStudents()
|
||
|
{
|
||
|
var guids = SelectedStudents.Select(u => u.Guid).ToList();
|
||
|
await _userApi.ClientDeleteUsers(guids);
|
||
|
await LoadStudents();
|
||
|
}
|
||
|
|
||
|
private async Task DeleteStudent(User user)
|
||
|
{
|
||
|
await _userApi.ClientDeleteUser(user.Guid);
|
||
|
await LoadStudents();
|
||
|
}
|
||
|
|
||
|
private void EditStudent(User user)
|
||
|
{
|
||
|
// открытие окна редактирования
|
||
|
}
|
||
|
}
|