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 _groupPresentersDataSource = new List(); private ObservableCollection _groups; public ObservableCollection Groups => _groups; public GroupPresenter? SelectedGroupItem { get => _selectedGroupItem; set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value); } private GroupPresenter? _selectedGroupItem; public ReactiveCommand DeleteUsersByGroupCommand { get; } public ReactiveCommand ImportStudentsCommand { get; } public ObservableCollection Users { get => _users;} public ObservableCollection _users; public GroupViewModel(IGroupUseCase groupUseCase, IUserUseCase userUseCase) { foreach (var item in groupUseCase.GetAllGroupsWithUsers()) { GroupPresenter groupPresenter = new GroupPresenter { Id = item.ID, Name = item.Name, users = item.Users?.Select(user => new UserPresenter { Name = user.FIO, Guid = user.Guid, Group = new GroupPresenter { Id = item.ID, Name = item.Name } } ).ToList() }; _groupPresentersDataSource.Add(groupPresenter); } _groups = new ObservableCollection(_groupPresentersDataSource); _users = new ObservableCollection(); DeleteUsersByGroupCommand = ReactiveCommand.Create(() => DeleteUsersByGroupID(userUseCase)); this.WhenAnyValue(vm => vm.SelectedGroupItem) .Subscribe(_ => SetUsers()); ImportStudentsCommand = ReactiveCommand.CreateFromTask(async () => { var openFileDialog = new OpenFileDialog { Filters = new List { new FileDialogFilter { Name = "CSV Files", Extensions = { "csv" } } }, AllowMultiple = false }; var result = await openFileDialog.ShowAsync(new Window()); if (result?.Length > 0) { var filePath = result[0]; ImportStudents(filePath, userUseCase); } }); } private void ImportStudents(string filePath, IUserUseCase userUseCase) { if (SelectedGroupItem == null || SelectedGroupItem.Id == null) { return; } var lines = File.ReadAllLines(filePath); foreach (var line in lines.Skip(1)) { var columns = line.Split(','); if (columns.Length < 1) continue; var fio = columns[0]; var newUser = new User { FIO = fio, Group = new Group { Name = SelectedGroupItem.Name, ID = SelectedGroupItem.Id } }; var result = userUseCase.CreateUser(newUser); if (result) { var userPresenter = new UserPresenter { Name = fio, Guid = Guid.NewGuid(), Group = SelectedGroupItem }; _users.Add(userPresenter); } } } private void DeleteUsersByGroupID(IUserUseCase userUseCase) { if (SelectedGroupItem == null) return; var groupID = SelectedGroupItem.Id; bool res = userUseCase.RemoveUsersByGroupID(groupID); if (res) { Users.Clear(); } } private void SetUsers() { if(SelectedGroupItem == null) return; if (SelectedGroupItem.users == null) return; Users.Clear(); foreach (var item in SelectedGroupItem.users) { Users.Add(item); } } public string? UrlPathSegment { get; } public IScreen HostScreen { get; } } }