presence/Presence.Desktop/ViewModels/MainWindowViewModel.cs

125 lines
3.7 KiB
C#
Raw Normal View History

2024-12-10 05:26:38 +00:00
using domain.UseCase;
using Presence.Desktop.Models;
using System;
2024-12-10 17:47:49 +00:00
using System.Reactive.Linq;
using System.Reactive;
2024-12-10 05:26:38 +00:00
using System.Linq;
using ReactiveUI;
using System.Collections.Generic;
using System.Collections.ObjectModel;
2024-12-10 17:47:49 +00:00
using System.Windows.Input;
using System.Threading.Tasks;
2024-12-10 05:26:38 +00:00
namespace Presence.Desktop.ViewModels
{
public class MainWindowViewModel : ViewModelBase
{
private readonly IGroupUseCase _groupService;
private List<GroupPresenter> groupPresenters = new List<GroupPresenter>();
private ObservableCollection<GroupPresenter> _groups;
public ObservableCollection<GroupPresenter> Groups
{
get => _groups;
set => this.RaiseAndSetIfChanged(ref _groups, value);
}
private GroupPresenter? _selectedGroupItem;
public GroupPresenter? SelectedGroupItem
{
get => _selectedGroupItem;
set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value);
}
private ObservableCollection<StudentPresenter> _students;
public ObservableCollection<StudentPresenter> Students
{
get => _students;
set => this.RaiseAndSetIfChanged(ref _students, value);
}
2024-12-10 17:47:49 +00:00
public Interaction<Unit, string?> ShowOpenFileDialog;
private string? _selectedFile;
public string? SelectedFile
{
get => _selectedFile;
set => this.RaiseAndSetIfChanged(ref _selectedFile, value);
}
public ICommand OpenFileDialog { get; }
2024-12-10 05:26:38 +00:00
public MainWindowViewModel()
{
_groupService = null;
_groups = new();
_students = new();
2024-12-10 17:47:49 +00:00
OpenFileDialog = ReactiveCommand.CreateFromTask(SelectFile);
2024-12-10 05:26:38 +00:00
}
2024-12-10 17:47:49 +00:00
public MainWindowViewModel(IGroupUseCase gService)
2024-12-10 05:26:38 +00:00
{
_groupService = gService;
2024-12-10 17:47:49 +00:00
ShowOpenFileDialog = new Interaction<Unit, string?>();
OpenFileDialog = ReactiveCommand.CreateFromTask(SelectFile);
_students = new();
2024-12-10 05:26:38 +00:00
2024-12-10 17:47:49 +00:00
this.WhenAnyValue(vm => vm.SelectedGroupItem)
.Subscribe(_ =>
{
RefreshGroups();
SetStudents();
});
}
private void RefreshGroups()
{
groupPresenters.Clear();
2024-12-10 05:26:38 +00:00
foreach (var item in _groupService.GetGroupsWithStudents())
{
GroupPresenter groupPresenter = new GroupPresenter
{
Id = item.Id,
Name = item.Name,
students = item.Users?.Select(u => new StudentPresenter
{
Id = u.Id,
FirstName = u.FirstName,
LastName = u.LastName,
Patronymic = u.Patronymic,
Group = new GroupPresenter
{
Id = item.Id,
Name = item.Name
}
})
};
groupPresenters.Add(groupPresenter);
}
_groups = new(groupPresenters);
}
private void SetStudents()
{
if (SelectedGroupItem == null) return;
if (SelectedGroupItem.students == null) return;
Students.Clear();
foreach (var student in SelectedGroupItem.students)
Students.Add(student);
}
2024-12-10 17:47:49 +00:00
private async Task SelectFile()
{
SelectedFile = await ShowOpenFileDialog.Handle(Unit.Default);
if (SelectedFile is object)
{
}
}
2024-12-10 05:26:38 +00:00
}
}