contextmenu for one item

This commit is contained in:
1billy17 2024-12-22 19:52:52 +03:00
parent 5626470f65
commit f06104e789
3 changed files with 193 additions and 75 deletions

View File

@ -21,46 +21,105 @@ namespace Presence.Desktop.ViewModels
public GroupPresenter? SelectedGroupItem { public GroupPresenter? SelectedGroupItem {
get => _selectedGroupItem; get => _selectedGroupItem;
set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value); } set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value);
}
private GroupPresenter? _selectedGroupItem; private GroupPresenter? _selectedGroupItem;
public ReactiveCommand<Unit, Unit> DeleteUsersByGroupCommand { get; }
public ReactiveCommand<Unit, Unit> ImportStudentsCommand { get; }
public ObservableCollection<UserPresenter> Users { get => _users;} public ObservableCollection<UserPresenter> Users { get => _users;}
public ObservableCollection<UserPresenter> _users; public ObservableCollection<UserPresenter> _users;
private IGroupUseCase _groupUseCase;
private IUserUseCase _userUseCase;
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> DeleteUsersByGroupCommand { get; }
public ReactiveCommand<Unit, Unit> ImportStudentsCommand { get; }
public string? SelectedSecondItem
{
get => _selectedSecondItem;
set => this.RaiseAndSetIfChanged(ref _selectedSecondItem, value);
}
private UserPresenter? _selectedUser;
public UserPresenter? SelectedUser
{
get => _selectedUser;
set => this.RaiseAndSetIfChanged(ref _selectedUser, value);
}
private string? _newFIO;
public string? NewFIO
{
get => _newFIO;
set => this.RaiseAndSetIfChanged(ref _newFIO, value);
}
public ReactiveCommand<UserPresenter, Unit> DeleteUserCommand { get; }
public ReactiveCommand<UserPresenter, Unit> UpdateUserFIOCommand { get; }
public GroupViewModel(IGroupUseCase groupUseCase, IUserUseCase userUseCase) public GroupViewModel(IGroupUseCase groupUseCase, IUserUseCase userUseCase)
{ {
foreach (var item in groupUseCase.GetAllGroupsWithUsers()) _groupUseCase = groupUseCase;
_userUseCase = userUseCase;
_groups = new ObservableCollection<GroupPresenter>(_groupPresentersDataSource);
_users = new ObservableCollection<UserPresenter>();
RefreshGroups();
this.WhenAnyValue(vm => vm.SelectedGroupItem)
.Subscribe(_ =>
{ {
GroupPresenter groupPresenter = new GroupPresenter RefreshGroups();
SetUsers();
});
this.WhenAnyValue(vm => vm.SelectedSecondItem)
.Subscribe(_ => SetUsers());
SelectedSecondItem = _secondComboBoxItems.First();
DeleteUsersByGroupCommand = ReactiveCommand.Create(() => DeleteUsersByGroupID());
ImportStudentsCommand = ReactiveCommand.CreateFromTask(async () => await ImportStudents());
DeleteUserCommand = ReactiveCommand.Create<UserPresenter>(DeleteUser);
UpdateUserFIOCommand = ReactiveCommand.Create<UserPresenter>(UpdateUserFIO);
}
private void RefreshGroups()
{ {
Id = item.ID, _groupPresentersDataSource.Clear();
Name = item.Name,
users = item.Users?.Select(user => new UserPresenter 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, Name = user.FIO,
Guid = user.Guid, Guid = user.Guid,
Group = new GroupPresenter { Id = item.ID, Name = item.Name } Group = new GroupPresenter { Id = group.ID, Name = group.Name }
} }).ToList()
).ToList()
}; };
_groupPresentersDataSource.Add(groupPresenter); _groupPresentersDataSource.Add(groupPresenter);
} }
_groups = new ObservableCollection<GroupPresenter>(_groupPresentersDataSource); _groups = new ObservableCollection<GroupPresenter>(_groupPresentersDataSource);
}
_users = new ObservableCollection<UserPresenter>(); private async System.Threading.Tasks.Task ImportStudents()
DeleteUsersByGroupCommand = ReactiveCommand.Create(() => DeleteUsersByGroupID(userUseCase));
this.WhenAnyValue(vm => vm.SelectedGroupItem)
.Subscribe(_ => SetUsers());
ImportStudentsCommand = ReactiveCommand.CreateFromTask(async () =>
{ {
if (SelectedGroupItem == null || SelectedGroupItem.Id == null) return;
var openFileDialog = new OpenFileDialog var openFileDialog = new OpenFileDialog
{ {
Filters = new List<FileDialogFilter> Filters = new List<FileDialogFilter>
@ -71,19 +130,9 @@ namespace Presence.Desktop.ViewModels
}; };
var result = await openFileDialog.ShowAsync(new Window()); var result = await openFileDialog.ShowAsync(new Window());
if (result?.Length > 0) { if (result?.Length > 0)
{
var filePath = result[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); var lines = File.ReadAllLines(filePath);
foreach (var line in lines.Skip(1)) foreach (var line in lines.Skip(1))
@ -92,7 +141,6 @@ namespace Presence.Desktop.ViewModels
if (columns.Length < 1) continue; if (columns.Length < 1) continue;
var fio = columns[0]; var fio = columns[0];
var newUser = new User var newUser = new User
{ {
FIO = fio, FIO = fio,
@ -103,28 +151,23 @@ namespace Presence.Desktop.ViewModels
} }
}; };
var result = userUseCase.CreateUser(newUser); var isCreated = _userUseCase.CreateUser(newUser);
if (isCreated)
if (result)
{ {
var userPresenter = new UserPresenter RefreshGroups();
{ SetUsers();
Name = fio, }
Guid = Guid.NewGuid(),
Group = SelectedGroupItem
};
_users.Add(userPresenter);
} }
} }
} }
private void DeleteUsersByGroupID(IUserUseCase userUseCase) { private void DeleteUsersByGroupID() {
if (SelectedGroupItem == null) if (SelectedGroupItem == null)
return; return;
var groupID = SelectedGroupItem.Id; var groupID = SelectedGroupItem.Id;
bool res = userUseCase.RemoveUsersByGroupID(groupID); bool res = _userUseCase.RemoveUsersByGroupID(groupID);
if (res) { if (res) {
Users.Clear(); Users.Clear();
@ -134,11 +177,57 @@ namespace Presence.Desktop.ViewModels
private void SetUsers() private void SetUsers()
{ {
if (SelectedGroupItem == null) return; if (SelectedGroupItem == null) return;
if (SelectedGroupItem.users == null) return;
Users.Clear(); Users.Clear();
foreach (var item in SelectedGroupItem.users)
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 DeleteUser(UserPresenter user) {
if (user == null) return;
var isDeleted = _userUseCase.RemoveUserByGuid(user.Guid);
if (isDeleted) {
Users.Remove(user);
}
}
private void UpdateUserFIO(UserPresenter user)
{
if (user == null || string.IsNullOrWhiteSpace(NewFIO)) return;
user.Name = NewFIO;
var updatedUser = new User
{
Guid = user.Guid,
FIO = NewFIO,
Group = new Group
{
ID = SelectedGroupItem.Id,
Name = SelectedGroupItem.Name
}
};
var isUpdated = _userUseCase.UpdateUser(updatedUser);
if (isUpdated != null)
{
NewFIO = string.Empty;
RefreshGroups();
SetUsers();
} }
} }

View File

@ -27,12 +27,19 @@
</DataTemplate> </DataTemplate>
</ComboBox.ItemTemplate> </ComboBox.ItemTemplate>
</ComboBox> </ComboBox>
<ComboBox ItemsSource="{Binding SecondComboBoxItems}" SelectedValue="{Binding SelectedSecondItem}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Button Content="Delete Users" <Button Content="Delete Users"
Command="{Binding DeleteUsersByGroupCommand}"/> Command="{Binding DeleteUsersByGroupCommand}"/>
<Button Content="Import Students" Command="{Binding ImportStudentsCommand}"/> <Button Content="Import Students" Command="{Binding ImportStudentsCommand}"/>
</StackPanel> </StackPanel>
<Border> <Border>
<ListBox Background="Black" ItemsSource="{Binding Users}"> <ListBox Background="Black" ItemsSource="{Binding Users}" SelectedItem="{Binding SelectedUser}">
<ListBox.ItemTemplate> <ListBox.ItemTemplate>
<DataTemplate> <DataTemplate>
<StackPanel Orientation="Horizontal"> <StackPanel Orientation="Horizontal">
@ -40,6 +47,21 @@
</StackPanel> </StackPanel>
</DataTemplate> </DataTemplate>
</ListBox.ItemTemplate> </ListBox.ItemTemplate>
<ListBox.ContextMenu>
<ContextMenu>
<MenuItem Header="Delete User"
Command="{Binding DeleteUserCommand}"
CommandParameter="{Binding SelectedUser}" />
<MenuItem Header="Update FIO">
<StackPanel>
<TextBox Text="{Binding NewFIO, Mode=TwoWay}" Width="150" />
<Button Content="Update"
Command="{Binding UpdateUserFIOCommand}"
CommandParameter="{Binding SelectedUser}" />
</StackPanel>
</MenuItem>
</ContextMenu>
</ListBox.ContextMenu>
</ListBox> </ListBox>
</Border> </Border>
</DockPanel> </DockPanel>

View File

@ -1,3 +1,10 @@
FIO FIO
Zamir Bezgeev Алексей Смирнов
Yasha Lava Сергей Петров
Анна Кузнецова
Елена Попова
Борис Васильев
Владимир Сидоров
Алина Фролова
Георгий Крылов
Зоя Тимофеева
1 FIO
2 Zamir Bezgeev Алексей Смирнов
3 Yasha Lava Сергей Петров
4 Анна Кузнецова
5 Елена Попова
6 Борис Васильев
7 Владимир Сидоров
8 Алина Фролова
9 Георгий Крылов
10 Зоя Тимофеева