presence/Presence.Desktop/ViewModels/MainWindowViewModel.cs

235 lines
8.0 KiB
C#
Raw Normal View History

2024-12-16 04:10:04 +00:00
using Avalonia.Controls.ApplicationLifetimes;
using domain.Models;
using domain.UseCase;
using Presence.Desktop.Views;
2024-12-02 10:04:59 +00:00
using ReactiveUI;
2024-12-16 04:10:04 +00:00
using System;
using System.Collections.Generic;
2024-12-02 10:04:59 +00:00
using System.Collections.ObjectModel;
using System.Linq;
2024-12-16 04:10:04 +00:00
using System.Reactive;
2024-12-02 10:04:59 +00:00
using System.Reactive.Linq;
2024-12-16 04:10:04 +00:00
using System.Windows.Input;
using Avalonia;
2024-11-24 18:05:17 +00:00
2024-12-16 04:10:04 +00:00
namespace Presence.Desktop.ViewModels
2024-11-24 18:05:17 +00:00
{
2024-12-16 04:10:04 +00:00
public class MainWindowViewModel : ViewModelBase
2024-12-12 08:32:15 +00:00
{
2024-12-16 04:10:04 +00:00
private readonly GroupUseCase _groupUseCase;
private List<GroupPresenter> groupPresentersDataSource = new List<GroupPresenter>();
private ObservableCollection<GroupPresenter> _groups;
public ObservableCollection<GroupPresenter> Groups => _groups;
2024-12-02 10:04:59 +00:00
2024-12-16 04:10:04 +00:00
private GroupPresenter? _selectedGroupItem;
public GroupPresenter? SelectedGroupItem
{
get => _selectedGroupItem;
set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value);
}
2024-12-02 10:04:59 +00:00
2024-12-16 04:10:04 +00:00
public ObservableCollection<UserPresenter> Users { get => _users; }
private ObservableCollection<UserPresenter> _users;
2024-12-04 18:00:36 +00:00
2024-12-16 04:10:04 +00:00
// Список опций сортировки
public List<string> SortOptions { get; } = new List<string> { "По фамилии", "По убыванию" };
2024-12-04 18:00:36 +00:00
2024-12-16 04:10:04 +00:00
private string _selectedSortOption;
public string SelectedSortOption
{
get => _selectedSortOption;
set => this.RaiseAndSetIfChanged(ref _selectedSortOption, value);
}
2024-12-04 18:00:36 +00:00
2024-12-16 04:10:04 +00:00
public bool CanDelete => SelectedUsers?.Count > 0;
public bool CanEdit => SelectedUsers?.Count == 1;
public ObservableCollection<UserPresenter> SelectedUsers { get; set; } = new ObservableCollection<UserPresenter>();
public ReactiveCommand<Unit, Unit> OnDeleteUserClicks { get; }
public ReactiveCommand<Unit, Unit> EditUserCommand { get; }
public ICommand RemoveAllStudentsCommand { get; }
public ICommand AddStudentCommand { get; }
public MainWindowViewModel(GroupUseCase groupUseCase)
{
_groupUseCase = groupUseCase;
2024-12-02 10:04:59 +00:00
2024-12-16 04:10:04 +00:00
OnDeleteUserClicks = ReactiveCommand.Create(OnDeleteUserClick, this.WhenAnyValue(vm => vm.CanDelete));
EditUserCommand = ReactiveCommand.Create(OnEditUserClick, this.WhenAnyValue(vm => vm.CanEdit));
RefreshGroups();
_groups = new ObservableCollection<GroupPresenter>(groupPresentersDataSource);
_users = new ObservableCollection<UserPresenter>();
this.WhenAnyValue(vm => vm.SelectedGroupItem)
.Subscribe(vm => SetUsers());
this.WhenAnyValue(vm => vm.SelectedSortOption)
.Subscribe(_ => SortUsers());
RemoveAllStudentsCommand = ReactiveCommand.Create(RemoveAllStudents);
AddStudentCommand = ReactiveCommand.Create(AddStudent);
SelectedUsers.CollectionChanged += (s, e) =>
2024-12-02 10:04:59 +00:00
{
2024-12-16 04:10:04 +00:00
this.RaisePropertyChanged(nameof(CanDelete));
this.RaisePropertyChanged(nameof(CanEdit));
};
}
2024-12-12 08:32:15 +00:00
2024-12-16 04:10:04 +00:00
private void SetUsers()
{
if (SelectedGroupItem?.users == null) return;
2024-12-12 08:32:15 +00:00
2024-12-16 04:10:04 +00:00
Users.Clear();
foreach (var item in SelectedGroupItem.users)
{
Users.Add(item);
}
2024-12-12 08:32:15 +00:00
2024-12-16 04:10:04 +00:00
SortUsers();
}
2024-12-04 18:00:36 +00:00
2024-12-16 04:10:04 +00:00
private void SortUsers()
{
if (SelectedGroupItem?.users == null) return;
var sortedUsers = SelectedGroupItem.users.ToList();
switch (SelectedSortOption)
{
case "По фамилии":
sortedUsers = sortedUsers.OrderBy(u => u.Name).ToList();
break;
case "По убыванию":
sortedUsers = sortedUsers.OrderByDescending(u => u.Name).ToList();
break;
}
Users.Clear();
foreach (var item in sortedUsers)
{
Users.Add(item);
}
}
2024-12-04 18:00:36 +00:00
2024-12-16 04:10:04 +00:00
private void RemoveAllStudents()
2024-12-04 18:00:36 +00:00
{
2024-12-16 04:10:04 +00:00
if (SelectedGroupItem == null) return;
2024-12-04 18:00:36 +00:00
2024-12-16 04:10:04 +00:00
_groupUseCase.RemoveAllStudentsFromGroup(SelectedGroupItem.Id);
SelectedGroupItem.users = new List<UserPresenter>();
SetUsers();
2024-12-04 18:00:36 +00:00
}
2024-12-16 04:10:04 +00:00
private void AddStudent()
{
if (SelectedGroupItem == null) return;
2024-12-04 18:00:36 +00:00
2024-12-16 04:10:04 +00:00
var newStudent = new UserPresenter
{
Name = "Новый студент",
Guid = Guid.NewGuid(),
Group = SelectedGroupItem
};
2024-12-04 18:00:36 +00:00
2024-12-16 04:10:04 +00:00
_groupUseCase.AddStudentToGroup(SelectedGroupItem.Id, new User
{
Guid = newStudent.Guid,
FIO = newStudent.Name
});
var updatedUsers = SelectedGroupItem.users?.ToList() ?? new List<UserPresenter>();
updatedUsers.Add(newStudent);
SelectedGroupItem.users = updatedUsers;
SetUsers();
}
2024-12-04 18:00:36 +00:00
2024-12-16 04:10:04 +00:00
public void OnDeleteUserClick()
{
if (SelectedUsers.Count == 0 || SelectedGroupItem?.users == null)
return;
2024-12-04 18:00:36 +00:00
2024-12-16 04:10:04 +00:00
foreach (var user in SelectedUsers.ToList())
{
_groupUseCase.RemoveUserByGuid(user.Guid);
2024-12-04 18:00:36 +00:00
2024-12-16 04:10:04 +00:00
var updatedUsers = SelectedGroupItem.users.Where(u => u.Guid != user.Guid).ToList();
SelectedGroupItem.users = new List<UserPresenter>(updatedUsers);
}
2024-12-04 18:00:36 +00:00
2024-12-16 04:10:04 +00:00
SetUsers();
SelectedUsers.Clear();
this.RaisePropertyChanged(nameof(CanDelete));
this.RaisePropertyChanged(nameof(CanEdit));
}
public async void OnEditUserClick()
{
var user = SelectedUsers.FirstOrDefault();
if (user == null) return;
var groups = _groupUseCase.GetAllGroups();
// Преобразуем группы из domain.Models.Group в GroupPresenter
var groupPresenters = groups.Select(g => new GroupPresenter
{
Id = g.Id,
Name = g.Name,
users = g.Users?.Select(u => new UserPresenter
{
Name = u.FIO,
Guid = u.Guid,
Group = new GroupPresenter { Id = g.Id, Name = g.Name }
}).ToList()
}).ToList();
var editDialog = new EditUserDialog(user.Guid, user.Name, user.Group.Id, groupPresenters);
var mainWindow = (Application.Current.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.MainWindow;
if (mainWindow == null) return;
var result = await editDialog.ShowDialog(mainWindow);
2024-12-04 18:00:36 +00:00
2024-12-16 04:10:04 +00:00
if (result != (null, null))
{
var newName = result.Item1;
var newGroup = result.Item2;
user.Name = newName;
user.Group = newGroup;
_groupUseCase.UpdateUser(user.Guid, user.Name, user.Group.Id);
SetUsers();
SelectedUsers.Clear();
this.RaisePropertyChanged(nameof(CanEdit));
this.RaisePropertyChanged(nameof(CanDelete));
}
RefreshGroups();
}
private void RefreshGroups()
2024-12-12 08:32:15 +00:00
{
2024-12-16 04:10:04 +00:00
groupPresentersDataSource.Clear();
foreach (var item in _groupUseCase.GetAllGroups())
{
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);
2024-11-24 18:05:17 +00:00
}
}
2024-12-16 04:10:04 +00:00
}