presence/Presence.Desktop/ViewModels/GroupViewModel.cs

237 lines
7.6 KiB
C#
Raw Normal View History

2024-12-05 08:26:58 +00:00
using Demo.Domain.UseCase;
using Presence.Desktop.Models;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using Avalonia.Controls;
using System.IO;
using Demo.Domain.Models;
namespace Presence.Desktop.ViewModels
{
public class GroupViewModel : ViewModelBase, IRoutableViewModel
{
private readonly List<GroupPresenter> _groupPresentersDataSource = new List<GroupPresenter>();
private ObservableCollection<GroupPresenter> _groups;
public ObservableCollection<GroupPresenter> Groups => _groups;
public GroupPresenter? SelectedGroupItem {
get => _selectedGroupItem;
2024-12-22 16:52:52 +00:00
set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value);
}
2024-12-05 08:26:58 +00:00
private GroupPresenter? _selectedGroupItem;
2024-12-22 16:52:52 +00:00
public ObservableCollection<UserPresenter> Users { get => _users;}
public ObservableCollection<UserPresenter> _users;
private IGroupUseCase _groupUseCase;
private IUserUseCase _userUseCase;
2024-12-05 08:26:58 +00:00
2024-12-22 16:52:52 +00:00
private ObservableCollection<string> _secondComboBoxItems = new ObservableCollection<string> { "no sort", "name sort", "name sort rev" };
public ObservableCollection<string> SecondComboBoxItems => _secondComboBoxItems;
private string? _selectedSecondItem;
public ReactiveCommand<Unit, Unit> DeleteUsersByGroupCommand { get; }
2024-12-05 08:26:58 +00:00
public ReactiveCommand<Unit, Unit> ImportStudentsCommand { get; }
2024-12-22 16:52:52 +00:00
public string? SelectedSecondItem
{
get => _selectedSecondItem;
set => this.RaiseAndSetIfChanged(ref _selectedSecondItem, value);
}
private UserPresenter? _selectedUser;
public UserPresenter? SelectedUser
{
get => _selectedUser;
set => this.RaiseAndSetIfChanged(ref _selectedUser, value);
}
private string? _newFIO;
public string? NewFIO
{
get => _newFIO;
set => this.RaiseAndSetIfChanged(ref _newFIO, value);
}
public ReactiveCommand<UserPresenter, Unit> DeleteUserCommand { get; }
public ReactiveCommand<UserPresenter, Unit> UpdateUserFIOCommand { get; }
2024-12-05 08:26:58 +00:00
public GroupViewModel(IGroupUseCase groupUseCase, IUserUseCase userUseCase)
{
2024-12-22 16:52:52 +00:00
_groupUseCase = groupUseCase;
_userUseCase = userUseCase;
2024-12-05 08:26:58 +00:00
_groups = new ObservableCollection<GroupPresenter>(_groupPresentersDataSource);
_users = new ObservableCollection<UserPresenter>();
2024-12-22 16:52:52 +00:00
RefreshGroups();
2024-12-05 08:26:58 +00:00
this.WhenAnyValue(vm => vm.SelectedGroupItem)
2024-12-22 16:52:52 +00:00
.Subscribe(_ =>
{
RefreshGroups();
SetUsers();
});
this.WhenAnyValue(vm => vm.SelectedSecondItem)
2024-12-05 08:26:58 +00:00
.Subscribe(_ => SetUsers());
2024-12-22 16:52:52 +00:00
SelectedSecondItem = _secondComboBoxItems.First();
DeleteUsersByGroupCommand = ReactiveCommand.Create(() => DeleteUsersByGroupID());
ImportStudentsCommand = ReactiveCommand.CreateFromTask(async () => await ImportStudents());
DeleteUserCommand = ReactiveCommand.Create<UserPresenter>(DeleteUser);
UpdateUserFIOCommand = ReactiveCommand.Create<UserPresenter>(UpdateUserFIO);
}
private void RefreshGroups()
{
_groupPresentersDataSource.Clear();
foreach (var group in _groupUseCase.GetAllGroupsWithUsers())
2024-12-05 08:26:58 +00:00
{
2024-12-22 16:52:52 +00:00
var groupPresenter = new GroupPresenter
2024-12-05 08:26:58 +00:00
{
2024-12-22 16:52:52 +00:00
Id = group.ID,
Name = group.Name,
users = group.Users?.Select(user => new UserPresenter
2024-12-05 08:26:58 +00:00
{
2024-12-22 16:52:52 +00:00
Name = user.FIO,
Guid = user.Guid,
Group = new GroupPresenter { Id = group.ID, Name = group.Name }
}).ToList()
2024-12-05 08:26:58 +00:00
};
2024-12-22 16:52:52 +00:00
_groupPresentersDataSource.Add(groupPresenter);
}
2024-12-05 08:26:58 +00:00
2024-12-22 16:52:52 +00:00
_groups = new ObservableCollection<GroupPresenter>(_groupPresentersDataSource);
2024-12-05 08:26:58 +00:00
}
2024-12-22 16:52:52 +00:00
private async System.Threading.Tasks.Task ImportStudents()
2024-12-05 08:26:58 +00:00
{
2024-12-22 16:52:52 +00:00
if (SelectedGroupItem == null || SelectedGroupItem.Id == null) return;
2024-12-05 08:26:58 +00:00
2024-12-22 16:52:52 +00:00
var openFileDialog = new OpenFileDialog
2024-12-05 08:26:58 +00:00
{
2024-12-22 16:52:52 +00:00
Filters = new List<FileDialogFilter>
2024-12-05 08:26:58 +00:00
{
2024-12-22 16:52:52 +00:00
new FileDialogFilter { Name = "CSV Files", Extensions = { "csv" } }
},
AllowMultiple = false
};
2024-12-05 08:26:58 +00:00
2024-12-22 16:52:52 +00:00
var result = await openFileDialog.ShowAsync(new Window());
if (result?.Length > 0)
{
var filePath = result[0];
var lines = File.ReadAllLines(filePath);
2024-12-05 08:26:58 +00:00
2024-12-22 16:52:52 +00:00
foreach (var line in lines.Skip(1))
2024-12-05 08:26:58 +00:00
{
2024-12-22 16:52:52 +00:00
var columns = line.Split(',');
if (columns.Length < 1) continue;
var fio = columns[0];
var newUser = new User
2024-12-05 08:26:58 +00:00
{
2024-12-22 16:52:52 +00:00
FIO = fio,
Group = new Group
{
Name = SelectedGroupItem.Name,
ID = SelectedGroupItem.Id
}
2024-12-05 08:26:58 +00:00
};
2024-12-22 16:52:52 +00:00
var isCreated = _userUseCase.CreateUser(newUser);
if (isCreated)
{
RefreshGroups();
SetUsers();
}
2024-12-05 08:26:58 +00:00
}
}
}
2024-12-22 16:52:52 +00:00
private void DeleteUsersByGroupID() {
2024-12-05 08:26:58 +00:00
if (SelectedGroupItem == null)
return;
var groupID = SelectedGroupItem.Id;
2024-12-22 16:52:52 +00:00
bool res = _userUseCase.RemoveUsersByGroupID(groupID);
2024-12-05 08:26:58 +00:00
if (res) {
Users.Clear();
}
}
private void SetUsers()
{
2024-12-22 16:52:52 +00:00
if (SelectedGroupItem == null) return;
2024-12-05 08:26:58 +00:00
Users.Clear();
2024-12-22 16:52:52 +00:00
var group = _groups.FirstOrDefault(it => it.Id == SelectedGroupItem.Id);
if (group?.users == null) return;
var sortedUsers = SelectedSecondItem switch
{
"name sort" => group.users.OrderBy(user => user.Name),
"name sort rev" => group.users.OrderByDescending(user => user.Name),
_ => group.users
};
foreach (var user in sortedUsers)
{
Users.Add(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)
{
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);
if (isUpdated != null)
2024-12-05 08:26:58 +00:00
{
2024-12-22 16:52:52 +00:00
NewFIO = string.Empty;
RefreshGroups();
SetUsers();
2024-12-05 08:26:58 +00:00
}
}
public string? UrlPathSegment { get; }
public IScreen HostScreen { get; }
}
}