semesterWork/Presence.Desktop/ViewModels/GroupViewModel.cs

101 lines
3.4 KiB
C#
Raw Permalink Normal View History

2024-12-08 15:00:30 +00:00
using domain.UseCase;
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-08 15:27:37 +00:00
using System.Threading.Tasks;
using System.Windows.Input;
2024-12-08 15:00:30 +00:00
namespace Presence.Desktop.ViewModels
{
public class GroupViewModel : ViewModelBase, IRoutableViewModel
{
2024-12-08 15:27:37 +00:00
public ICommand OpenFileDialog { get; }
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-08 15:00:30 +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);
}
private GroupPresenter? _selectedGroupItem;
2024-12-08 15:27:37 +00:00
private IGroupUseCase _groupUseCase;
2024-12-08 15:00:30 +00:00
public ObservableCollection<UserPresenter> Users { get => _users; }
public ObservableCollection<UserPresenter> _users;
public GroupViewModel(IGroupUseCase groupUseCase)
{
2024-12-08 15:27:37 +00:00
_groupUseCase = groupUseCase;
_SelectFileInteraction = new Interaction<string?, string?>();
OpenFileDialog = ReactiveCommand.CreateFromTask(SelectFile);
_users = new ObservableCollection<UserPresenter>();
RefreshGroups();
this.WhenAnyValue(vm => vm.SelectedGroupItem)
.Subscribe(_ =>
{
RefreshGroups();
SetUsers();
});
}
private void SetUsers()
{
if (SelectedGroupItem == null) return;
Users.Clear();
var group = _groups.First(it => it.Id == SelectedGroupItem.Id);
if (group.users == null) return;
foreach (var item in group.users)
{
Users.Add(item);
}
}
private void RefreshGroups()
{
_groupPresentersDataSource.Clear();
foreach (var item in _groupUseCase.GetGroupsWithStudents())
2024-12-08 15:00:30 +00:00
{
GroupPresenter groupPresenter = new GroupPresenter
{
Id = item.Id,
Name = item.Name,
users = item.Users?.Select(user => new UserPresenter
{
Name = user.Name,
Guid = user.Guid,
Group = new GroupPresenter { Id = item.Id, Name = item.Name }
}
).ToList()
};
_groupPresentersDataSource.Add(groupPresenter);
}
_groups = new ObservableCollection<GroupPresenter>(_groupPresentersDataSource);
}
2024-12-08 15:27:37 +00:00
private async Task SelectFile()
2024-12-08 15:00:30 +00:00
{
2024-12-08 15:27:37 +00:00
Console.WriteLine("clock");
SelectedFile = await _SelectFileInteraction.Handle("Chose csv file");
2024-12-08 15:00:30 +00:00
}
public string? UrlPathSegment { get; }
public IScreen HostScreen { get; }
}
}
2024-12-08 15:27:37 +00:00