set-refresh-changes-and-filepicker

This commit is contained in:
1eG0ist 2024-12-08 18:27:37 +03:00
parent 255a13337b
commit 61d110fd62
4 changed files with 85 additions and 30 deletions

View File

@ -1,5 +1,7 @@
using domain.Service;
using domain.UseCase;
using domain.Request;
using domain.Service;
using Microsoft.AspNetCore.Mvc;
using Presence.Api.Response;
@ -32,5 +34,12 @@ namespace Presence.Api.Controllers
}).ToList();
return Ok(new GroupResponse());
}
[HttpPost("/admin/{groupId}/students")]
public void AddGroup(int groupId, [FromBody] AddGroupRequest addGroupRequest)
{
Console.WriteLine(groupId);
_groupService.AddGroup(addGroupRequest);
}
}
}

View File

@ -1,6 +1,4 @@
using domain.UseCase;
using DynamicData;
using DynamicData.Binding;
using Presence.Desktop.Models;
using ReactiveUI;
using System;
@ -8,12 +6,23 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive.Linq;
using Tmds.DBus.Protocol;
using System.Threading.Tasks;
using System.Windows.Input;
namespace Presence.Desktop.ViewModels
{
public class GroupViewModel : ViewModelBase, IRoutableViewModel
{
public ICommand OpenFileDialog { get; }
public Interaction<string?, string?> SelectFileInteraction => _SelectFileInteraction;
public readonly Interaction<string?, string?> _SelectFileInteraction;
private string? _selectedFile;
public string? SelectedFile
{
get => _selectedFile;
set => this.RaiseAndSetIfChanged(ref _selectedFile, value);
}
private readonly List<GroupPresenter> _groupPresentersDataSource = new List<GroupPresenter>();
private ObservableCollection<GroupPresenter> _groups;
public ObservableCollection<GroupPresenter> Groups => _groups;
@ -27,12 +36,40 @@ namespace Presence.Desktop.ViewModels
private GroupPresenter? _selectedGroupItem;
private IGroupUseCase _groupUseCase;
public ObservableCollection<UserPresenter> Users { get => _users; }
public ObservableCollection<UserPresenter> _users;
public GroupViewModel(IGroupUseCase groupUseCase)
{
foreach (var item in groupUseCase.GetGroupsWithStudents())
_groupUseCase = groupUseCase;
_SelectFileInteraction = new Interaction<string?, string?>();
OpenFileDialog = ReactiveCommand.CreateFromTask(SelectFile);
_users = new ObservableCollection<UserPresenter>();
RefreshGroups();
this.WhenAnyValue(vm => vm.SelectedGroupItem)
.Subscribe(_ =>
{
RefreshGroups();
SetUsers();
});
}
private void SetUsers()
{
if (SelectedGroupItem == null) return;
Users.Clear();
var group = _groups.First(it => it.Id == SelectedGroupItem.Id);
if (group.users == null) return;
foreach (var item in group.users)
{
Users.Add(item);
}
}
private void RefreshGroups()
{
_groupPresentersDataSource.Clear();
foreach (var item in _groupUseCase.GetGroupsWithStudents())
{
GroupPresenter groupPresenter = new GroupPresenter
{
@ -49,26 +86,15 @@ namespace Presence.Desktop.ViewModels
_groupPresentersDataSource.Add(groupPresenter);
}
_groups = new ObservableCollection<GroupPresenter>(_groupPresentersDataSource);
_users = new ObservableCollection<UserPresenter>();
this.WhenAnyValue(vm => vm.SelectedGroupItem)
.Subscribe(_ => SetUsers());
}
private void SetUsers()
private async Task SelectFile()
{
if (SelectedGroupItem == null) return;
if (SelectedGroupItem.users == null) return;
Users.Clear();
foreach (var item in SelectedGroupItem.users)
{
Users.Add(item);
Console.WriteLine("clock");
SelectedFile = await _SelectFileInteraction.Handle("Chose csv file");
}
}
public string? UrlPathSegment { get; }
public IScreen HostScreen { get; }
}
}

View File

@ -4,7 +4,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="using:Presence.Desktop.ViewModels"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Presence.Desktop.GroupView"
x:Class="Presence.Desktop.Views.GroupView"
x:DataType="vm:GroupViewModel">
<Design.DataContext>
@ -27,7 +27,7 @@
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<ComboBox/>
<Button Width="100" Command="{Binding OpenFileDialog}"/>
<ComboBox/>
</StackPanel>
<Border>

View File

@ -1,17 +1,37 @@
using Avalonia;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Platform.Storage;
using Avalonia.ReactiveUI;
using Presence.Desktop.ViewModels;
using ReactiveUI;
namespace Presence.Desktop;
namespace Presence.Desktop.Views
{
public partial class GroupView : ReactiveUserControl<GroupViewModel>
{
public GroupView()
{
this.WhenActivated(disposables => { });
this.WhenActivated(action =>
{
action(ViewModel!.SelectFileInteraction.RegisterHandler(ShowFileDialog));
});
AvaloniaXamlLoader.Load(this);
}
private async Task ShowFileDialog(IInteractionContext<string?, string?> context)
{
var topLevel = TopLevel.GetTopLevel(this);
var storageFile = await topLevel.StorageProvider.OpenFilePickerAsync(
new FilePickerOpenOptions()
{
AllowMultiple = false,
Title = context.Input
}
);
context.SetOutput(storageFile.First().Path.ToString());
}
}
}