presence/Presence.Desktop/ViewModels/MainWindowViewModel.cs

260 lines
8.2 KiB
C#
Raw Normal View History

2024-12-10 05:26:38 +00:00
using domain.UseCase;
using Presence.Desktop.Models;
2024-12-12 07:20:21 +00:00
using Presence.Desktop.Helpers;
2024-12-10 05:26:38 +00:00
using System;
2024-12-10 17:47:49 +00:00
using System.Reactive.Linq;
using System.Reactive;
2024-12-10 05:26:38 +00:00
using System.Linq;
using ReactiveUI;
using System.Collections.Generic;
using System.Collections.ObjectModel;
2024-12-12 07:20:21 +00:00
using System.Diagnostics;
using System.IO;
2024-12-10 17:47:49 +00:00
using System.Windows.Input;
using System.Threading.Tasks;
2024-12-12 07:20:21 +00:00
using Avalonia.Controls.Selection;
using domain.Request;
using System.Text;
2024-12-10 05:26:38 +00:00
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);
}
2024-12-10 17:47:49 +00:00
public Interaction<Unit, string?> ShowOpenFileDialog;
private string? _selectedFile;
public string? SelectedFile
{
get => _selectedFile;
set => this.RaiseAndSetIfChanged(ref _selectedFile, value);
}
2024-12-12 07:20:21 +00:00
private bool _MultipleSelected = false;
public bool MultipleSelected { get => _MultipleSelected; set => this.RaiseAndSetIfChanged(ref _MultipleSelected, value); }
public SelectionModel<StudentPresenter> Selection { get; }
private string _selectedSort = "Релевантность";
public string SelectedSort
{
get => _selectedSort;
set => this.RaiseAndSetIfChanged(ref _selectedSort, value);
}
public List<string> Sorting { get; set; } =
[
"Релевантность",
"А-Я",
"Я-А"
];
2024-12-10 17:47:49 +00:00
public ICommand OpenFileDialog { get; }
2024-12-12 07:20:21 +00:00
public ICommand EditCommand { get; }
public ICommand DeleteCommand { get; }
public ICommand DeleteSelectedCommand { get; }
public ICommand DeleteAllCommand { get; }
2024-12-10 05:26:38 +00:00
public MainWindowViewModel()
{
_groupService = null;
_groups = new();
_students = new();
2024-12-10 17:47:49 +00:00
OpenFileDialog = ReactiveCommand.CreateFromTask(SelectFile);
2024-12-10 05:26:38 +00:00
}
2024-12-10 17:47:49 +00:00
public MainWindowViewModel(IGroupUseCase gService)
2024-12-10 05:26:38 +00:00
{
_groupService = gService;
2024-12-10 17:47:49 +00:00
ShowOpenFileDialog = new Interaction<Unit, string?>();
OpenFileDialog = ReactiveCommand.CreateFromTask(SelectFile);
_students = new();
2024-12-10 05:26:38 +00:00
2024-12-12 07:20:21 +00:00
this.WhenAnyValue(vm => vm.SelectedGroupItem,
x => x.SelectedSort)
2024-12-10 17:47:49 +00:00
.Subscribe(_ =>
{
RefreshGroups();
2024-12-12 07:20:21 +00:00
SetStudents(SelectedSort);
2024-12-10 17:47:49 +00:00
});
2024-12-12 07:20:21 +00:00
Selection = new SelectionModel<StudentPresenter>();
Selection.SingleSelect = false;
Selection.SelectionChanged += (sender, args) =>
{
MultipleSelected = Selection.SelectedItems.Count > 1;
};
DeleteCommand = ReactiveCommand.Create(DeleteStudent);
DeleteSelectedCommand = ReactiveCommand.Create(DeleteStudent);
DeleteAllCommand = ReactiveCommand.Create(DeleteAllStudents);
2024-12-10 17:47:49 +00:00
}
private void RefreshGroups()
{
groupPresenters.Clear();
2024-12-10 05:26:38 +00:00
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
}
2024-12-12 07:20:21 +00:00
}).ToList()
2024-12-10 05:26:38 +00:00
};
groupPresenters.Add(groupPresenter);
}
_groups = new(groupPresenters);
}
2024-12-12 07:20:21 +00:00
private void SetStudents(string selectedSort)
2024-12-10 05:26:38 +00:00
{
if (SelectedGroupItem == null) return;
if (SelectedGroupItem.students == null) return;
Students.Clear();
2024-12-12 07:20:21 +00:00
List<StudentPresenter> students = new(_groupService.GetGroupsWithStudents()
.Single(g => g.Id == SelectedGroupItem.Id)
.Users
.Select(u => new StudentPresenter
{
Id = u.Id,
FirstName = u.FirstName,
LastName = u.LastName,
Patronymic = u.Patronymic,
Group = new GroupPresenter()
{
Id = SelectedGroupItem.Id,
Name = SelectedGroupItem.Name
}
})
);
students = selectedSort switch
{
"Релевантность" => students.OrderBy(s => s.Id).ToList(),
"А-Я" => students.OrderBy(s => s.LastName).ToList(),
"Я-А" => students.OrderByDescending(s => s.LastName).ToList(),
_ => students
};
foreach (var student in students)
2024-12-10 05:26:38 +00:00
Students.Add(student);
}
2024-12-10 17:47:49 +00:00
private async Task SelectFile()
{
2024-12-12 07:20:21 +00:00
try
{
SelectedFile = await ShowOpenFileDialog.Handle(Unit.Default);
}
catch (Exception)
{
return;
}
if (SelectedGroupItem == null) return;
try
{
var studentsToAdd = CsvHelper.ReadCsvStudents(SelectedFile);
2024-12-10 17:47:49 +00:00
2024-12-12 07:20:21 +00:00
if (studentsToAdd.Any())
{
_groupService.AddStudentsToGroup(SelectedGroupItem.Id, studentsToAdd);
foreach (var studentRequest in studentsToAdd)
{
var studentPresenter = new StudentPresenter
{
LastName = studentRequest.LastName,
FirstName = studentRequest.FirstName,
Patronymic = studentRequest.Patronymic,
Group = SelectedGroupItem
};
SelectedGroupItem.students.Add(studentPresenter);
Students.Add(studentPresenter);
}
RefreshGroups();
SetStudents(SelectedSort);
}
}
catch (Exception ex)
2024-12-10 17:47:49 +00:00
{
2024-12-12 07:20:21 +00:00
Debug.WriteLine(ex.Message);
}
}
private void DeleteStudent()
{
if (Selection.SelectedItems == null || Selection.SelectedItems.Count == 0) return;
var studentIds = Selection.SelectedItems.Select(s => s.Id).ToList();
_groupService.RemoveStudentsFromGroupByIds(SelectedGroupItem.Id, studentIds);
2024-12-10 17:47:49 +00:00
2024-12-12 07:20:21 +00:00
foreach (var student in Selection.SelectedItems.ToList())
{
SelectedGroupItem.students.Remove(student);
Students.Remove(student);
2024-12-10 17:47:49 +00:00
}
2024-12-12 07:20:21 +00:00
SetStudents(SelectedSort);
RefreshGroups();
}
private void DeleteAllStudents()
{
if (Students.Count == 0 && SelectedGroupItem == null) return;
_groupService.RemoveStudentsFromGroup(SelectedGroupItem.Id);
SelectedGroupItem.students.Clear();
Students.Clear();
RefreshGroups();
SetStudents(SelectedSort);
2024-12-10 17:47:49 +00:00
}
2024-12-10 05:26:38 +00:00
}
}