presence_sample_v2/Presence.Desktop/ViewModels/GroupViewModel.cs

101 lines
3.5 KiB
C#
Raw Normal View History

2024-12-04 08:11:13 +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-06 08:04:11 +00:00
using System.Threading.Tasks;
using System.Windows.Input;
2024-12-04 08:11:13 +00:00
using Tmds.DBus.Protocol;
namespace Presence.Desktop.ViewModels
{
public class GroupViewModel : ViewModelBase, IRoutableViewModel
{
2024-12-06 08:04:11 +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-04 08:11:13 +00:00
private readonly List<GroupPresenter> _groupPresentersDataSource = new List<GroupPresenter>();
private ObservableCollection<GroupPresenter> _groups;
public ObservableCollection<GroupPresenter> Groups => _groups;
2024-12-06 08:04:11 +00:00
2024-12-04 08:11:13 +00:00
public GroupPresenter? SelectedGroupItem {
get => _selectedGroupItem;
set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value); }
private GroupPresenter? _selectedGroupItem;
2024-12-05 08:35:52 +00:00
private IGroupUseCase _groupUseCase;
2024-12-04 08:11:13 +00:00
public ObservableCollection<UserPresenter> Users { get => _users;}
public ObservableCollection<UserPresenter> _users;
public GroupViewModel(IGroupUseCase groupUseCase)
{
2024-12-05 08:35:52 +00:00
_groupUseCase = groupUseCase;
2024-12-06 08:04:11 +00:00
_SelectFileInteraction = new Interaction<string?, string?>();
OpenFileDialog = ReactiveCommand.CreateFromTask(SelectFile);
2024-12-04 08:11:13 +00:00
_users = new ObservableCollection<UserPresenter>();
2024-12-05 08:35:52 +00:00
RefreshGroups();
2024-12-04 08:11:13 +00:00
this.WhenAnyValue(vm => vm.SelectedGroupItem)
2024-12-05 08:35:52 +00:00
.Subscribe(_ =>
{ RefreshGroups();
SetUsers();
});
2024-12-04 08:11:13 +00:00
}
private void SetUsers()
{
if(SelectedGroupItem == null) return;
Users.Clear();
2024-12-05 08:35:52 +00:00
var group = _groups.First(it => it.Id == SelectedGroupItem.Id);
if(group.users == null) return;
foreach (var item in group.users)
2024-12-04 08:11:13 +00:00
{
Users.Add(item);
}
}
2024-12-05 08:35:52 +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.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-06 08:04:11 +00:00
private async Task SelectFile()
{
Console.WriteLine("clock");
SelectedFile = await _SelectFileInteraction.Handle("Chose csv file");
}
2024-12-04 08:11:13 +00:00
public string? UrlPathSegment { get; }
public IScreen HostScreen { get; }
}
}