refresh local changes

This commit is contained in:
Your Name 2024-12-05 11:51:07 +03:00
parent 5a3b35edb8
commit 2eaeed52db
38 changed files with 302 additions and 64 deletions

View File

@ -1,3 +1,178 @@
// 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 Tmds.DBus.Protocol;
// using Avalonia.Controls;
// namespace Presence.Desktop.ViewModels
// {
// public class GroupViewModel : ViewModelBase, IRoutableViewModel
// {
// private readonly List<GroupPresenter> _groupPresentersDataSource = new List<GroupPresenter>();
// private ObservableCollection<GroupPresenter> _groups;
// public ObservableCollection<GroupPresenter> Groups => _groups;
// private GroupPresenter? _selectedGroupItem;
// public ObservableCollection<UserPresenter> Users { get => _users;}
// public ObservableCollection<UserPresenter> _users;
// private ObservableCollection<string> _secondComboBoxItems = new ObservableCollection<string> { "no sort", "name sort", "name sort rev" };
// public ObservableCollection<string> SecondComboBoxItems => _secondComboBoxItems;
// private string? _selectedSecondItem;
// public ReactiveCommand<Unit, Unit> ButtonRemoveUsersByGroup {get; }
// public ReactiveCommand<Unit, Unit> ImportStudentsCommand { get; }
// public GroupPresenter? SelectedGroupItem
// {
// get => _selectedGroupItem;
// set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value);
// }
// public string? SelectedSecondItem
// {
// get => _selectedSecondItem;
// set => this.RaiseAndSetIfChanged(ref _selectedSecondItem, value);
// }
// 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<GroupPresenter>(_groupPresentersDataSource);
// _users = new ObservableCollection<UserPresenter>();
// this.WhenAnyValue(vm => vm.SelectedGroupItem)
// .Subscribe(_ => SetUsers());
// this.WhenAnyValue(vm => vm.SelectedSecondItem)
// .Subscribe(_ => SetUsers());
// SelectedSecondItem = _secondComboBoxItems.First();
// ButtonRemoveUsersByGroup = ReactiveCommand.Create(() => RemoveUsersByGroup(groupUseCase));
// ImportStudentsCommand = ReactiveCommand.CreateFromTask(async () =>
// {
// 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];
// ImportStudents(filePath, userUseCase);
// }
// });
// }
// private void SetUsers()
// {
// if(SelectedGroupItem == null) return;
// if (SelectedGroupItem.users == null) return;
// var sortedUsers = SelectedSecondItem switch
// {
// "name sort" => SelectedGroupItem.users.OrderBy(user => user.Name),
// "name sort rev" => SelectedGroupItem.users.OrderByDescending(user => user.Name),
// _ => SelectedGroupItem.users
// };
// Users.Clear();
// foreach (var item in sortedUsers)
// {
// Users.Add(item);
// }
// }
// private void RemoveUsersByGroup(IGroupUseCase groupUseCase)
// {
// if (_selectedGroupItem != null){
// groupUseCase.RemoveUsersByGroup(_selectedGroupItem.Id);
// }
// var usersToRemove = Users.Where(user => user.Group.Id == _selectedGroupItem.Id).ToList();
// foreach (var user in usersToRemove)
// {
// Users.Remove(user);
// }
// }
// 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);
// }
// }
// }
// public string? UrlPathSegment { get; }
// public IScreen HostScreen { get; }
// }
// }
using Demo.Domain.Models;
using Demo.Domain.UseCase;
using DynamicData;
using DynamicData.Binding;
@ -6,104 +181,168 @@ 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 Tmds.DBus.Protocol;
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 ObservableCollection<UserPresenter> Users { get => _users;}
public ObservableCollection<UserPresenter> _users;
private ObservableCollection<string> _secondComboBoxItems = new ObservableCollection<string> { "no sort", "name sort", "name sort rev" };
public ObservableCollection<string> SecondComboBoxItems => _secondComboBoxItems;
private string? _selectedSecondItem;
public ReactiveCommand<Unit, Unit> ButtonRemoveUsersByGroup {get; }
// public ReactiveCommand<Unit, Unit> ButtonAddUser { get; }
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 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<GroupPresenter>(_groupPresentersDataSource);
_groupUseCase = groupUseCase;
_userUseCase = userUseCase;
_groups = new ObservableCollection<GroupPresenter>();
_users = new ObservableCollection<UserPresenter>();
RefreshGroups();
this.WhenAnyValue(vm => vm.SelectedGroupItem)
.Subscribe(_ => SetUsers());
.Subscribe(_ =>
{
RefreshGroups();
SetUsers();
});
this.WhenAnyValue(vm => vm.SelectedSecondItem)
.Subscribe(_ => SetUsers());
SelectedSecondItem = _secondComboBoxItems.First();
ButtonRemoveUsersByGroup = ReactiveCommand.Create(() => RemoveUsersByGroup(groupUseCase));
ButtonRemoveUsersByGroup = ReactiveCommand.Create(() => RemoveUsersByGroup());
ImportStudentsCommand = ReactiveCommand.CreateFromTask(async () => await ImportStudents());
}
private void SetUsers()
{
if(SelectedGroupItem == null) return;
if (SelectedGroupItem.users == null) return;
var sortedUsers = SelectedSecondItem switch
{
"name sort" => SelectedGroupItem.users.OrderBy(user => user.Name),
"name sort rev" => SelectedGroupItem.users.OrderByDescending(user => user.Name),
_ => SelectedGroupItem.users
};
if (SelectedGroupItem == null) return;
Users.Clear();
foreach (var item in sortedUsers)
var group = _groups.FirstOrDefault(it => it.Id == SelectedGroupItem.Id);
if (group?.users == null) return;
var sortedUsers = SelectedSecondItem switch
{
Users.Add(item);
"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 RemoveUsersByGroup(IGroupUseCase groupUseCase)
private void RefreshGroups()
{
if (_selectedGroupItem != null){
groupUseCase.RemoveUsersByGroup(_selectedGroupItem.Id);
_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);
}
var usersToRemove = Users.Where(user => user.Group.Id == _selectedGroupItem.Id).ToList();
foreach (var user in usersToRemove)
_groups = new ObservableCollection<GroupPresenter>(_groupPresentersDataSource);
}
private void RemoveUsersByGroup()
{
Users.Remove(user);
if (SelectedGroupItem == null) return;
_groupUseCase.RemoveUsersByGroup(SelectedGroupItem.Id);
RefreshGroups();
SetUsers();
}
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();
}
}
}
}
@ -111,4 +350,3 @@ namespace Presence.Desktop.ViewModels
public IScreen HostScreen { get; }
}
}

View File

@ -14,7 +14,7 @@
<DockPanel Background="Azure">
<StackPanel Width="200" DockPanel.Dock="Right" HorizontalAlignment="Center" Background="Gray">
<Button Content="remove" HorizontalAlignment="Center" Margin="10" Command="{Binding ButtonRemoveUsersByGroup}"/>
<Button Content="create" HorizontalAlignment="Center" Margin="10"/>
<Button Content="create" HorizontalAlignment="Center" Margin="10" Command="{Binding ImportStudentsCommand}"/>
</StackPanel>
<StackPanel DockPanel.Dock="Bottom">

View File

@ -13,10 +13,10 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("Presence.Desktop")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+cd14199d8f8882eb539425bae3cd58b140c7b374")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+5a3b35edb803658a9e980fa84e334c956238d9a9")]
[assembly: System.Reflection.AssemblyProductAttribute("Presence.Desktop")]
[assembly: System.Reflection.AssemblyTitleAttribute("Presence.Desktop")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
// Создано классом WriteCodeFragment MSBuild.
// Generated by the MSBuild WriteCodeFragment class.

View File

@ -1 +1 @@
1601b8a97c679a8fd3f637d2ddc30637f34b37df92211063253b3ecf6124d488
d952627ae154a47540761d913d76d1beb13d7e9e76c65e9ff752362c3bf11044

Binary file not shown.

Binary file not shown.

View File

@ -13,10 +13,10 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("data")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+cd14199d8f8882eb539425bae3cd58b140c7b374")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+5a3b35edb803658a9e980fa84e334c956238d9a9")]
[assembly: System.Reflection.AssemblyProductAttribute("data")]
[assembly: System.Reflection.AssemblyTitleAttribute("data")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
// Создано классом WriteCodeFragment MSBuild.
// Generated by the MSBuild WriteCodeFragment class.

View File

@ -1 +1 @@
cbcf9f65ddea20ee5dba4f0994bf30c3c28bcbacd4b18ea08dddbae4d7329598
59db47dd6079fbc3eeedc9d89e5043d653f86627d896b473ee1563f96c012ce1

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -13,10 +13,10 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("domain")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+cd14199d8f8882eb539425bae3cd58b140c7b374")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+5a3b35edb803658a9e980fa84e334c956238d9a9")]
[assembly: System.Reflection.AssemblyProductAttribute("domain")]
[assembly: System.Reflection.AssemblyTitleAttribute("domain")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
// Создано классом WriteCodeFragment MSBuild.
// Generated by the MSBuild WriteCodeFragment class.

View File

@ -1 +1 @@
6639613f2b7545edc3a375874517b2293f0381addf587f17148b84d4d9f20b3e
74684369dd334aa6def5609e908e0313868255d5ce0e40c2086e66367b6a5e8e

Binary file not shown.

Binary file not shown.