199 lines
6.6 KiB
C#
199 lines
6.6 KiB
C#
using Demo.Domain.Models;
|
|
using Demo.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.IO;
|
|
using System.Linq;
|
|
using System.Reactive;
|
|
using System.Reactive.Linq;
|
|
using Avalonia.Controls;
|
|
|
|
namespace Presence.Desktop.ViewModels
|
|
{
|
|
public class GroupViewModel : ViewModelBase, IRoutableViewModel
|
|
{
|
|
private readonly IGroupUseCase _groupUseCase;
|
|
private readonly IUserUseCase _userUseCase;
|
|
private readonly List<GroupPresenter> _groupPresentersDataSource = new List<GroupPresenter>();
|
|
|
|
private ObservableCollection<GroupPresenter> _groups;
|
|
public ObservableCollection<GroupPresenter> Groups => _groups;
|
|
|
|
private ObservableCollection<UserPresenter> _users;
|
|
public ObservableCollection<UserPresenter> Users => _users;
|
|
|
|
private GroupPresenter? _selectedGroupItem;
|
|
public GroupPresenter? SelectedGroupItem
|
|
{
|
|
get => _selectedGroupItem;
|
|
set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value);
|
|
}
|
|
|
|
private ObservableCollection<string> _secondComboBoxItems = new ObservableCollection<string> { "no sort", "name sort", "name sort rev" };
|
|
public ObservableCollection<string> SecondComboBoxItems => _secondComboBoxItems;
|
|
|
|
private string? _selectedSecondItem;
|
|
public string? SelectedSecondItem
|
|
{
|
|
get => _selectedSecondItem;
|
|
set => this.RaiseAndSetIfChanged(ref _selectedSecondItem, value);
|
|
}
|
|
|
|
public ReactiveCommand<Unit, Unit> ButtonRemoveUsersByGroup { get; }
|
|
public ReactiveCommand<Unit, Unit> ImportStudentsCommand { get; }
|
|
public ReactiveCommand<Unit, Unit> RemoveUsersContext { get; }
|
|
|
|
public GroupViewModel(IGroupUseCase groupUseCase, IUserUseCase userUseCase)
|
|
{
|
|
_groupUseCase = groupUseCase;
|
|
_userUseCase = userUseCase;
|
|
|
|
_groups = new ObservableCollection<GroupPresenter>();
|
|
_users = new ObservableCollection<UserPresenter>();
|
|
|
|
RefreshGroups();
|
|
|
|
this.WhenAnyValue(vm => vm.SelectedGroupItem)
|
|
.Subscribe(_ =>
|
|
{
|
|
RefreshGroups();
|
|
SetUsers();
|
|
});
|
|
|
|
this.WhenAnyValue(vm => vm.SelectedSecondItem)
|
|
.Subscribe(_ => SetUsers());
|
|
|
|
SelectedSecondItem = _secondComboBoxItems.First();
|
|
|
|
ButtonRemoveUsersByGroup = ReactiveCommand.Create(() => RemoveUsersByGroup());
|
|
ImportStudentsCommand = ReactiveCommand.CreateFromTask(async () => await ImportStudents());
|
|
RemoveUsersContext = ReactiveCommand.Create(() => RemoveUsersByContextMenu());
|
|
}
|
|
|
|
private void RemoveUsersByContextMenu(){
|
|
if (SelectedGroupItem == null || SelectedGroupItem.Id == null) return;
|
|
|
|
var selectedUsers = Users.Where(u => u.IsSelected).ToList();
|
|
|
|
foreach (var user in selectedUsers)
|
|
{
|
|
var isRemoved = _userUseCase.RemoveUserByFIOnGroup(user.Name, SelectedGroupItem.Id);
|
|
if (isRemoved)
|
|
{
|
|
Users.Remove(user);
|
|
}
|
|
}
|
|
|
|
RefreshGroups();
|
|
SetUsers();
|
|
}
|
|
|
|
private void SetUsers()
|
|
{
|
|
if (SelectedGroupItem == null) return;
|
|
|
|
Users.Clear();
|
|
|
|
var group = _groups.FirstOrDefault(it => it.Id == SelectedGroupItem.Id);
|
|
if (group?.users == null) return;
|
|
|
|
var sortedUsers = SelectedSecondItem switch
|
|
{
|
|
"name sort" => group.users.OrderBy(user => user.Name),
|
|
"name sort rev" => group.users.OrderByDescending(user => user.Name),
|
|
_ => group.users
|
|
};
|
|
|
|
foreach (var user in sortedUsers)
|
|
{
|
|
Users.Add(user);
|
|
}
|
|
}
|
|
|
|
private void RefreshGroups()
|
|
{
|
|
_groupPresentersDataSource.Clear();
|
|
|
|
foreach (var group in _groupUseCase.GetAllGroupsWithUsers())
|
|
{
|
|
var groupPresenter = new GroupPresenter
|
|
{
|
|
Id = group.ID,
|
|
Name = group.Name,
|
|
users = group.Users?.Select(user => new UserPresenter
|
|
{
|
|
Name = user.FIO,
|
|
Guid = user.Guid,
|
|
Group = new GroupPresenter { Id = group.ID, Name = group.Name }
|
|
}).ToList()
|
|
};
|
|
_groupPresentersDataSource.Add(groupPresenter);
|
|
}
|
|
|
|
_groups = new ObservableCollection<GroupPresenter>(_groupPresentersDataSource);
|
|
}
|
|
|
|
private void RemoveUsersByGroup()
|
|
{
|
|
if (SelectedGroupItem == null) return;
|
|
|
|
_groupUseCase.RemoveUsersByGroup(SelectedGroupItem.Id);
|
|
RefreshGroups();
|
|
SetUsers();
|
|
}
|
|
|
|
//import users
|
|
private async System.Threading.Tasks.Task ImportStudents()
|
|
{
|
|
if (SelectedGroupItem == null || SelectedGroupItem.Id == null) return;
|
|
|
|
var openFileDialog = new OpenFileDialog
|
|
{
|
|
Filters = new List<FileDialogFilter>
|
|
{
|
|
new FileDialogFilter { Name = "CSV Files", Extensions = { "csv" } }
|
|
},
|
|
AllowMultiple = false
|
|
};
|
|
|
|
var result = await openFileDialog.ShowAsync(new Window());
|
|
if (result?.Length > 0)
|
|
{
|
|
var filePath = result[0];
|
|
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 isCreated = _userUseCase.CreateUser(newUser);
|
|
if (isCreated)
|
|
{
|
|
RefreshGroups();
|
|
SetUsers();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public string? UrlPathSegment { get; }
|
|
public IScreen HostScreen { get; }
|
|
}
|
|
} |