presence_new/Presence.Desktop/ViewModels/GroupViewModel.cs

178 lines
6.6 KiB
C#
Raw Permalink Normal View History

2024-12-13 07:21:14 +00:00
using domain.UseCase;
using DynamicData;
using DynamicData.Binding;
using Presence.Desktop.Models;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive.Linq;
2024-12-17 09:13:12 +00:00
using System.Threading.Tasks;
using System.Windows.Input;
2024-12-21 11:14:33 +00:00
using Avalonia.Controls.Selection;
using presence.data.RemoteData.RemoteDataBase;
using presence.data.Repository;
using presence.domain.UseCase;
2024-12-23 11:56:27 +00:00
using System.Reactive;
using Splat;
2024-12-13 07:21:14 +00:00
namespace Presence.Desktop.ViewModels
{
public class GroupViewModel : ViewModelBase, IRoutableViewModel
{
2024-12-23 11:56:27 +00:00
public string? UrlPathSegment { get; }
public IScreen HostScreen { get; }
private readonly PresenceUseCase _presenceUseCase;
public ICommand OpenFileDialog { get; }
2024-12-17 09:13:12 +00:00
public Interaction<string?, string?> SelectFileInteraction => _SelectFileInteraction;
public readonly Interaction<string?, string?> _SelectFileInteraction;
private string? _selectedFile;
public string? SelectedFile
{
get => _selectedFile;
set => this.RaiseAndSetIfChanged(ref _selectedFile, value);
}
2024-12-13 07:21:14 +00:00
private readonly List<GroupPresenter> _groupPresentersDataSource = new List<GroupPresenter>();
private ObservableCollection<GroupPresenter> _groups;
public ObservableCollection<GroupPresenter> Groups => _groups;
public GroupPresenter? SelectedGroupItem {
get => _selectedGroupItem;
set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value); }
2024-12-21 11:14:33 +00:00
private bool _MultipleSelected = false;
public bool MultipleSelected { get => _MultipleSelected; set => this.RaiseAndSetIfChanged(ref _MultipleSelected, value); }
public SelectionModel<UserPresenter> Selection { get; }
2024-12-13 07:21:14 +00:00
private GroupPresenter? _selectedGroupItem;
private IGroupUseCase _groupUseCase;
public ObservableCollection<UserPresenter> Users { get => _users;}
public ObservableCollection<UserPresenter> _users;
2024-12-21 11:14:33 +00:00
public ICommand RemoveUserCommand { get; }
public ICommand EditUserCommand { get; }
public ICommand RemoveAllSelectedCommand { get; }
2024-12-23 11:56:27 +00:00
public ReactiveCommand<Unit, Unit> NavigateToPresenceCommand { get; }
2024-12-21 11:14:33 +00:00
2024-12-23 11:56:27 +00:00
public GroupViewModel(IGroupUseCase groupUseCase, PresenceUseCase presenceUseCase, IScreen? screen = null)
2024-12-13 07:21:14 +00:00
{
2024-12-23 11:56:27 +00:00
HostScreen = screen ?? Locator.Current.GetService<IScreen>()!;
2024-12-13 07:21:14 +00:00
_groupUseCase = groupUseCase;
2024-12-23 11:56:27 +00:00
_presenceUseCase = presenceUseCase;
2024-12-21 11:14:33 +00:00
2024-12-17 09:13:12 +00:00
_SelectFileInteraction = new Interaction<string?, string?>();
OpenFileDialog = ReactiveCommand.CreateFromTask(SelectFile);
2024-12-21 11:14:33 +00:00
_groups = new ObservableCollection<GroupPresenter>(_groupPresentersDataSource);
2024-12-13 07:21:14 +00:00
_users = new ObservableCollection<UserPresenter>();
RefreshGroups();
this.WhenAnyValue(vm => vm.SelectedGroupItem)
.Subscribe(_ =>
{
RefreshGroups();
SetUsers();
});
2024-12-21 11:14:33 +00:00
Selection = new SelectionModel<UserPresenter>();
Selection.SingleSelect = false;
Selection.SelectionChanged += SelectionChanged;
RemoveUserCommand = ReactiveCommand.Create<UserPresenter>(RemoveUser);
EditUserCommand = ReactiveCommand.Create<UserPresenter>(EditUser);
RemoveAllSelectedCommand = ReactiveCommand.Create(RemoveAllSelected);
NavigateToPresenceCommand = ReactiveCommand.Create(NavigateToPresence);
2024-12-13 07:21:14 +00:00
}
private void SetUsers()
{
if(SelectedGroupItem == null) return;
if (SelectedGroupItem.Users == null) return;
Users.Clear();
GroupPresenter group = _groups.First(it => it.Id == SelectedGroupItem.Id);
if(group.Users == null) return;
foreach (var item in group.Users)
{
Users.Add(item);
}
2024-12-17 09:13:12 +00:00
}
2024-12-13 07:21:14 +00:00
private void RefreshGroups()
{
_groupPresentersDataSource.Clear();
foreach (var item in _groupUseCase.GetGroupsWithStudents())
{
GroupPresenter groupPresenter = new GroupPresenter
{
Id = item.Id,
Name = item.Name,
Users = item.User?.Select(user => new UserPresenter
{
Name = user.FIO,
Id = user.Id,
Group = new GroupPresenter { Id = item.Id, Name = item.Name }
}
).ToList()
};
_groupPresentersDataSource.Add(groupPresenter);
}
_groups = new ObservableCollection<GroupPresenter>(_groupPresentersDataSource);
}
2024-12-21 11:14:33 +00:00
void SelectionChanged(object sender, SelectionModelSelectionChangedEventArgs e)
{
MultipleSelected = Selection.SelectedItems.Count > 1;
}
2024-12-17 09:13:12 +00:00
private async Task SelectFile()
{
Console.WriteLine("clock");
SelectedFile = await _SelectFileInteraction.Handle("Chose csv file");
}
2024-12-21 11:14:33 +00:00
private void RemoveUser(UserPresenter user)
{
if (user == null || SelectedGroupItem == null) return;
_groupUseCase.RemoveUserFromGroup(user.Id);
RefreshGroups();
SetUsers();
}
private void EditUser(UserPresenter user)
{
if (user == null) return;
// TODO: Добавить логику редактирования пользователя
// Можно реализовать через диалоговое окно или навигацию на форму редактирования
}
private void RemoveAllSelected()
{
if (SelectedGroupItem == null) return;
var selectedUsers = Selection.SelectedItems.ToList();
foreach (var user in selectedUsers)
{
_groupUseCase.RemoveUserFromGroup(user.Id);
}
RefreshGroups();
SetUsers();
}
private void NavigateToPresence()
{
2024-12-23 11:56:27 +00:00
var groupRepository = new SQLGroupRepositoryImpl(new RemoteDataBaseContext());
var groupUseCase = new GroupUseCase(groupRepository);
2024-12-23 11:56:27 +00:00
// Добавляем логику навигации к PresenceViewModel
var presenceViewModel = new PresenceViewModel(groupUseCase, _presenceUseCase);
HostScreen.Router.Navigate.Execute(presenceViewModel);
}
2024-12-13 07:21:14 +00:00
}
}