2024-12-05 08:51:07 +00:00
|
|
|
using Demo.Domain.Models;
|
2024-12-04 19:33:12 +00:00
|
|
|
using Demo.Domain.UseCase;
|
|
|
|
using DynamicData;
|
|
|
|
using DynamicData.Binding;
|
|
|
|
using Presence.Desktop.Models;
|
|
|
|
using ReactiveUI;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Collections.ObjectModel;
|
2024-12-05 08:51:07 +00:00
|
|
|
using System.IO;
|
2024-12-04 19:33:12 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Reactive;
|
|
|
|
using System.Reactive.Linq;
|
2024-12-05 08:51:07 +00:00
|
|
|
using Avalonia.Controls;
|
2024-12-22 19:41:45 +00:00
|
|
|
using Presence.Desktop.Views;
|
|
|
|
using Avalonia.Input;
|
2024-12-04 19:33:12 +00:00
|
|
|
|
|
|
|
namespace Presence.Desktop.ViewModels
|
|
|
|
{
|
|
|
|
public class GroupViewModel : ViewModelBase, IRoutableViewModel
|
|
|
|
{
|
2024-12-05 08:51:07 +00:00
|
|
|
private readonly IGroupUseCase _groupUseCase;
|
|
|
|
private readonly IUserUseCase _userUseCase;
|
|
|
|
|
2024-12-04 19:33:12 +00:00
|
|
|
private ObservableCollection<GroupPresenter> _groups;
|
|
|
|
public ObservableCollection<GroupPresenter> Groups => _groups;
|
2024-12-05 08:13:50 +00:00
|
|
|
|
2024-12-05 08:51:07 +00:00
|
|
|
private ObservableCollection<UserPresenter> _users;
|
|
|
|
public ObservableCollection<UserPresenter> Users => _users;
|
2024-12-05 08:13:50 +00:00
|
|
|
|
2024-12-05 08:51:07 +00:00
|
|
|
private GroupPresenter? _selectedGroupItem;
|
|
|
|
public GroupPresenter? SelectedGroupItem
|
|
|
|
{
|
|
|
|
get => _selectedGroupItem;
|
|
|
|
set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value);
|
2024-12-04 19:33:12 +00:00
|
|
|
}
|
|
|
|
|
2024-12-05 08:51:07 +00:00
|
|
|
private ObservableCollection<string> _secondComboBoxItems = new ObservableCollection<string> { "no sort", "name sort", "name sort rev" };
|
|
|
|
public ObservableCollection<string> SecondComboBoxItems => _secondComboBoxItems;
|
2024-12-04 19:33:12 +00:00
|
|
|
|
2024-12-05 08:51:07 +00:00
|
|
|
private string? _selectedSecondItem;
|
2024-12-05 08:13:50 +00:00
|
|
|
public string? SelectedSecondItem
|
|
|
|
{
|
|
|
|
get => _selectedSecondItem;
|
|
|
|
set => this.RaiseAndSetIfChanged(ref _selectedSecondItem, value);
|
|
|
|
}
|
2024-12-05 08:51:07 +00:00
|
|
|
|
2024-12-22 19:41:45 +00:00
|
|
|
private UserPresenter? _selectedUser;
|
|
|
|
public UserPresenter? SelectedUser
|
|
|
|
{
|
|
|
|
get => _selectedUser;
|
|
|
|
set => this.RaiseAndSetIfChanged(ref _selectedUser, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
private ObservableCollection<UserPresenter> _selectedUsers = new ObservableCollection<UserPresenter>();
|
|
|
|
public ObservableCollection<UserPresenter> SelectedUsers
|
|
|
|
{
|
|
|
|
get => _selectedUsers;
|
|
|
|
set => this.RaiseAndSetIfChanged(ref _selectedUsers, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool _isMultipleUsersSelected;
|
|
|
|
public bool IsMultipleUsersSelected
|
|
|
|
{
|
|
|
|
get => _isMultipleUsersSelected;
|
|
|
|
set => this.RaiseAndSetIfChanged(ref _isMultipleUsersSelected, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
private string? _newFIO;
|
|
|
|
public string? NewFIO
|
|
|
|
{
|
|
|
|
get => _newFIO;
|
|
|
|
set => this.RaiseAndSetIfChanged(ref _newFIO, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public UserPresenter? SelectedUserForUpdate
|
|
|
|
{
|
|
|
|
get => SelectedUsers?.FirstOrDefault();
|
|
|
|
set => this.RaiseAndSetIfChanged(ref _selectedUser, value);
|
|
|
|
}
|
|
|
|
|
2024-12-05 08:51:07 +00:00
|
|
|
public ReactiveCommand<Unit, Unit> ButtonRemoveUsersByGroup { get; }
|
|
|
|
public ReactiveCommand<Unit, Unit> ImportStudentsCommand { get; }
|
2024-12-22 19:41:45 +00:00
|
|
|
public ReactiveCommand<IEnumerable<UserPresenter>, Unit> RemoveSelectedUsersCommand { get; }
|
|
|
|
public ReactiveCommand<UserPresenter, Unit> UpdateUserFIOCommand { get; }
|
2024-12-05 08:51:07 +00:00
|
|
|
|
2024-12-04 19:33:12 +00:00
|
|
|
public GroupViewModel(IGroupUseCase groupUseCase, IUserUseCase userUseCase)
|
|
|
|
{
|
2024-12-05 08:51:07 +00:00
|
|
|
_groupUseCase = groupUseCase;
|
|
|
|
_userUseCase = userUseCase;
|
2024-12-04 19:33:12 +00:00
|
|
|
|
2024-12-05 08:51:07 +00:00
|
|
|
_groups = new ObservableCollection<GroupPresenter>();
|
2024-12-04 19:33:12 +00:00
|
|
|
_users = new ObservableCollection<UserPresenter>();
|
|
|
|
|
2024-12-05 08:51:07 +00:00
|
|
|
RefreshGroups();
|
|
|
|
|
2024-12-04 19:33:12 +00:00
|
|
|
this.WhenAnyValue(vm => vm.SelectedGroupItem)
|
2024-12-05 08:51:07 +00:00
|
|
|
.Subscribe(_ =>
|
|
|
|
{
|
|
|
|
RefreshGroups();
|
|
|
|
SetUsers();
|
|
|
|
});
|
2024-12-05 08:13:50 +00:00
|
|
|
|
|
|
|
this.WhenAnyValue(vm => vm.SelectedSecondItem)
|
|
|
|
.Subscribe(_ => SetUsers());
|
|
|
|
|
2024-12-22 19:41:45 +00:00
|
|
|
this.WhenAnyValue(vm => vm.SelectedUsers.Count)
|
|
|
|
.Subscribe(count => IsMultipleUsersSelected = count > 1);
|
|
|
|
|
2024-12-05 08:13:50 +00:00
|
|
|
SelectedSecondItem = _secondComboBoxItems.First();
|
2024-12-05 08:51:07 +00:00
|
|
|
|
|
|
|
ButtonRemoveUsersByGroup = ReactiveCommand.Create(() => RemoveUsersByGroup());
|
|
|
|
ImportStudentsCommand = ReactiveCommand.CreateFromTask(async () => await ImportStudents());
|
2024-12-22 19:41:45 +00:00
|
|
|
RemoveSelectedUsersCommand = ReactiveCommand.Create<IEnumerable<UserPresenter>>(DeleteUsers);
|
|
|
|
UpdateUserFIOCommand = ReactiveCommand.Create<UserPresenter>(UpdateUserFIO);
|
2024-12-04 19:33:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void SetUsers()
|
|
|
|
{
|
2024-12-05 08:51:07 +00:00
|
|
|
if (SelectedGroupItem == null) return;
|
|
|
|
|
|
|
|
Users.Clear();
|
|
|
|
|
|
|
|
var group = _groups.FirstOrDefault(it => it.Id == SelectedGroupItem.Id);
|
|
|
|
if (group?.users == null) return;
|
|
|
|
|
2024-12-05 08:13:50 +00:00
|
|
|
var sortedUsers = SelectedSecondItem switch
|
|
|
|
{
|
2024-12-05 08:51:07 +00:00
|
|
|
"name sort" => group.users.OrderBy(user => user.Name),
|
|
|
|
"name sort rev" => group.users.OrderByDescending(user => user.Name),
|
|
|
|
_ => group.users
|
2024-12-05 08:13:50 +00:00
|
|
|
};
|
|
|
|
|
2024-12-05 08:51:07 +00:00
|
|
|
foreach (var user in sortedUsers)
|
2024-12-04 19:33:12 +00:00
|
|
|
{
|
2024-12-05 08:51:07 +00:00
|
|
|
Users.Add(user);
|
2024-12-04 19:33:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-05 08:51:07 +00:00
|
|
|
private void RefreshGroups()
|
2024-12-04 19:33:12 +00:00
|
|
|
{
|
2024-12-22 19:41:45 +00:00
|
|
|
_groups.Clear();
|
2024-12-05 08:51:07 +00:00
|
|
|
|
|
|
|
foreach (var group in _groupUseCase.GetAllGroupsWithUsers())
|
|
|
|
{
|
|
|
|
var groupPresenter = 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()
|
|
|
|
};
|
2024-12-22 19:41:45 +00:00
|
|
|
_groups.Add(groupPresenter);
|
2024-12-04 19:33:12 +00:00
|
|
|
}
|
2024-12-05 08:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void RemoveUsersByGroup()
|
|
|
|
{
|
|
|
|
if (SelectedGroupItem == null) return;
|
|
|
|
|
|
|
|
_groupUseCase.RemoveUsersByGroup(SelectedGroupItem.Id);
|
|
|
|
RefreshGroups();
|
|
|
|
SetUsers();
|
|
|
|
}
|
|
|
|
|
|
|
|
private async System.Threading.Tasks.Task ImportStudents()
|
|
|
|
{
|
|
|
|
if (SelectedGroupItem == null || SelectedGroupItem.Id == null) return;
|
|
|
|
|
|
|
|
var openFileDialog = new OpenFileDialog
|
|
|
|
{
|
|
|
|
Filters = new List<FileDialogFilter>
|
|
|
|
{
|
|
|
|
new FileDialogFilter { Name = "CSV Files", Extensions = { "csv" } }
|
|
|
|
},
|
|
|
|
AllowMultiple = false
|
|
|
|
};
|
|
|
|
|
|
|
|
var result = await openFileDialog.ShowAsync(new Window());
|
|
|
|
if (result?.Length > 0)
|
2024-12-04 19:33:12 +00:00
|
|
|
{
|
2024-12-05 08:51:07 +00:00
|
|
|
var filePath = result[0];
|
|
|
|
var lines = File.ReadAllLines(filePath);
|
|
|
|
|
|
|
|
foreach (var line in lines.Skip(1))
|
|
|
|
{
|
|
|
|
var columns = line.Split(',');
|
|
|
|
if (columns.Length < 1) continue;
|
|
|
|
|
|
|
|
var fio = columns[0];
|
|
|
|
var newUser = new User
|
|
|
|
{
|
|
|
|
FIO = fio,
|
|
|
|
Group = new Group
|
|
|
|
{
|
|
|
|
Name = SelectedGroupItem.Name,
|
|
|
|
ID = SelectedGroupItem.Id
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var isCreated = _userUseCase.CreateUser(newUser);
|
|
|
|
if (isCreated)
|
|
|
|
{
|
|
|
|
RefreshGroups();
|
|
|
|
SetUsers();
|
|
|
|
}
|
|
|
|
}
|
2024-12-04 19:33:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-22 19:41:45 +00:00
|
|
|
private void DeleteUsers(IEnumerable<UserPresenter> users)
|
|
|
|
{
|
|
|
|
if (users == null) return;
|
|
|
|
|
|
|
|
var usersToDelete = new List<UserPresenter>(users);
|
|
|
|
|
|
|
|
foreach (var user in usersToDelete)
|
|
|
|
{
|
|
|
|
var isDeleted = _userUseCase.RemoveUserByGuid(user.Guid);
|
|
|
|
if (isDeleted)
|
|
|
|
{
|
|
|
|
Users.Remove(user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void DeleteUser(UserPresenter user) {
|
|
|
|
|
|
|
|
if (user == null) return;
|
|
|
|
|
|
|
|
var isDeleted = _userUseCase.RemoveUserByGuid(user.Guid);
|
|
|
|
if (isDeleted) {
|
|
|
|
Users.Remove(user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void UpdateUserFIO(UserPresenter user)
|
|
|
|
{
|
|
|
|
Console.WriteLine("start");
|
|
|
|
|
|
|
|
if (user == null || string.IsNullOrWhiteSpace(NewFIO)) return;
|
|
|
|
|
|
|
|
user.Name = NewFIO;
|
|
|
|
var updatedUser = new User
|
|
|
|
{
|
|
|
|
Guid = user.Guid,
|
|
|
|
FIO = NewFIO,
|
|
|
|
Group = new Group
|
|
|
|
{
|
|
|
|
ID = SelectedGroupItem.Id,
|
|
|
|
Name = SelectedGroupItem.Name
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var isUpdated = _userUseCase.UpdateUser(updatedUser);
|
|
|
|
|
|
|
|
Console.WriteLine("end");
|
|
|
|
|
|
|
|
if (isUpdated != null)
|
|
|
|
{
|
|
|
|
NewFIO = string.Empty;
|
|
|
|
// SelectedUsers.Clear(); // Очистить выбранных пользователей
|
|
|
|
// SelectedUser = null; // Обнулить выбранного пользователя
|
|
|
|
// SelectedUserForUpdate = null;
|
|
|
|
RefreshGroups();
|
|
|
|
SetUsers();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-04 19:33:12 +00:00
|
|
|
public string? UrlPathSegment { get; }
|
|
|
|
public IScreen HostScreen { get; }
|
|
|
|
}
|
2024-12-05 08:51:07 +00:00
|
|
|
}
|