contextmenu for one item
This commit is contained in:
parent
5626470f65
commit
f06104e789
@ -21,110 +21,153 @@ namespace Presence.Desktop.ViewModels
|
||||
|
||||
public GroupPresenter? SelectedGroupItem {
|
||||
get => _selectedGroupItem;
|
||||
set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value); }
|
||||
set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
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)
|
||||
{
|
||||
foreach (var item in groupUseCase.GetAllGroupsWithUsers())
|
||||
{
|
||||
GroupPresenter groupPresenter = new GroupPresenter
|
||||
_groupUseCase = groupUseCase;
|
||||
_userUseCase = userUseCase;
|
||||
|
||||
_groups = new ObservableCollection<GroupPresenter>(_groupPresentersDataSource);
|
||||
_users = new ObservableCollection<UserPresenter>();
|
||||
|
||||
RefreshGroups();
|
||||
|
||||
this.WhenAnyValue(vm => vm.SelectedGroupItem)
|
||||
.Subscribe(_ =>
|
||||
{
|
||||
Id = item.ID,
|
||||
Name = item.Name,
|
||||
users = item.Users?.Select(user => new UserPresenter
|
||||
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()
|
||||
{
|
||||
_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 = item.ID, Name = item.Name }
|
||||
}
|
||||
).ToList()
|
||||
Group = new GroupPresenter { Id = group.ID, Name = group.Name }
|
||||
}).ToList()
|
||||
};
|
||||
_groupPresentersDataSource.Add(groupPresenter);
|
||||
}
|
||||
|
||||
_groups = new ObservableCollection<GroupPresenter>(_groupPresentersDataSource);
|
||||
|
||||
_users = new ObservableCollection<UserPresenter>();
|
||||
|
||||
DeleteUsersByGroupCommand = ReactiveCommand.Create(() => DeleteUsersByGroupID(userUseCase));
|
||||
|
||||
this.WhenAnyValue(vm => vm.SelectedGroupItem)
|
||||
.Subscribe(_ => SetUsers());
|
||||
|
||||
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 ImportStudents(string filePath, IUserUseCase userUseCase)
|
||||
private async System.Threading.Tasks.Task ImportStudents()
|
||||
{
|
||||
if (SelectedGroupItem == null || SelectedGroupItem.Id == null)
|
||||
if (SelectedGroupItem == null || SelectedGroupItem.Id == null) return;
|
||||
|
||||
var openFileDialog = new OpenFileDialog
|
||||
{
|
||||
return;
|
||||
}
|
||||
var lines = File.ReadAllLines(filePath);
|
||||
Filters = new List<FileDialogFilter>
|
||||
{
|
||||
new FileDialogFilter { Name = "CSV Files", Extensions = { "csv" } }
|
||||
},
|
||||
AllowMultiple = false
|
||||
};
|
||||
|
||||
foreach (var line in lines.Skip(1))
|
||||
var result = await openFileDialog.ShowAsync(new Window());
|
||||
if (result?.Length > 0)
|
||||
{
|
||||
var columns = line.Split(',');
|
||||
if (columns.Length < 1) continue;
|
||||
var filePath = result[0];
|
||||
var lines = File.ReadAllLines(filePath);
|
||||
|
||||
var fio = columns[0];
|
||||
|
||||
var newUser = new User
|
||||
foreach (var line in lines.Skip(1))
|
||||
{
|
||||
FIO = fio,
|
||||
Group = new Group
|
||||
{
|
||||
Name = SelectedGroupItem.Name,
|
||||
ID = SelectedGroupItem.Id
|
||||
}
|
||||
};
|
||||
var columns = line.Split(',');
|
||||
if (columns.Length < 1) continue;
|
||||
|
||||
var result = userUseCase.CreateUser(newUser);
|
||||
|
||||
if (result)
|
||||
{
|
||||
var userPresenter = new UserPresenter
|
||||
var fio = columns[0];
|
||||
var newUser = new User
|
||||
{
|
||||
Name = fio,
|
||||
Guid = Guid.NewGuid(),
|
||||
Group = SelectedGroupItem
|
||||
FIO = fio,
|
||||
Group = new Group
|
||||
{
|
||||
Name = SelectedGroupItem.Name,
|
||||
ID = SelectedGroupItem.Id
|
||||
}
|
||||
};
|
||||
_users.Add(userPresenter);
|
||||
|
||||
var isCreated = _userUseCase.CreateUser(newUser);
|
||||
if (isCreated)
|
||||
{
|
||||
RefreshGroups();
|
||||
SetUsers();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void DeleteUsersByGroupID(IUserUseCase userUseCase) {
|
||||
private void DeleteUsersByGroupID() {
|
||||
|
||||
if (SelectedGroupItem == null)
|
||||
return;
|
||||
|
||||
var groupID = SelectedGroupItem.Id;
|
||||
bool res = userUseCase.RemoveUsersByGroupID(groupID);
|
||||
bool res = _userUseCase.RemoveUsersByGroupID(groupID);
|
||||
|
||||
if (res) {
|
||||
Users.Clear();
|
||||
@ -133,12 +176,58 @@ namespace Presence.Desktop.ViewModels
|
||||
|
||||
private void SetUsers()
|
||||
{
|
||||
if(SelectedGroupItem == null) return;
|
||||
if (SelectedGroupItem.users == null) return;
|
||||
if (SelectedGroupItem == null) return;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -27,12 +27,19 @@
|
||||
</DataTemplate>
|
||||
</ComboBox.ItemTemplate>
|
||||
</ComboBox>
|
||||
<ComboBox ItemsSource="{Binding SecondComboBoxItems}" SelectedValue="{Binding SelectedSecondItem}">
|
||||
<ComboBox.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<TextBlock Text="{Binding}"/>
|
||||
</DataTemplate>
|
||||
</ComboBox.ItemTemplate>
|
||||
</ComboBox>
|
||||
<Button Content="Delete Users"
|
||||
Command="{Binding DeleteUsersByGroupCommand}"/>
|
||||
<Button Content="Import Students" Command="{Binding ImportStudentsCommand}"/>
|
||||
</StackPanel>
|
||||
<Border>
|
||||
<ListBox Background="Black" ItemsSource="{Binding Users}">
|
||||
<ListBox Background="Black" ItemsSource="{Binding Users}" SelectedItem="{Binding SelectedUser}">
|
||||
<ListBox.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<StackPanel Orientation="Horizontal">
|
||||
@ -40,6 +47,21 @@
|
||||
</StackPanel>
|
||||
</DataTemplate>
|
||||
</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>
|
||||
</Border>
|
||||
</DockPanel>
|
||||
|
11
students.csv
11
students.csv
@ -1,3 +1,10 @@
|
||||
FIO
|
||||
Zamir Bezgeev
|
||||
Yasha Lava
|
||||
Алексей Смирнов
|
||||
Сергей Петров
|
||||
Анна Кузнецова
|
||||
Елена Попова
|
||||
Борис Васильев
|
||||
Владимир Сидоров
|
||||
Алина Фролова
|
||||
Георгий Крылов
|
||||
Зоя Тимофеева
|
|
Loading…
Reference in New Issue
Block a user