presence/Presence.Desktop/ViewModels/PresenceViewModel.cs

146 lines
4.9 KiB
C#
Raw Normal View History

2025-04-30 01:13:44 +00:00
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Threading.Tasks;
using Avalonia.Controls;
using Demo.Domain.Models;
using Presence.Desktop.Models;
using presence_client.ApiClients.Interfaces;
2024-12-05 08:26:58 +00:00
using ReactiveUI;
2025-04-30 01:13:44 +00:00
namespace Presence.Desktop.ViewModels
2024-12-05 08:26:58 +00:00
{
2025-04-30 01:13:44 +00:00
public class PresenceViewModel : ViewModelBase, IRoutableViewModel
{
private readonly IPresenceApiClient _presenceApiClient;
private readonly IGroupApiClient _groupApiClient;
private Group _selectedGroup;
private DateTime _selectedDate = DateTime.Today;
public ObservableCollection<Group> Groups { get; } = new();
public ObservableCollection<UserPresencePresenter> AttendanceRecords { get; } = new();
public Group SelectedGroup
{
get => _selectedGroup;
set => this.RaiseAndSetIfChanged(ref _selectedGroup, value);
}
public DateTime SelectedDate
{
get => _selectedDate;
set => this.RaiseAndSetIfChanged(ref _selectedDate, value);
}
private List<UserPresencePresenter> _selectedAttendanceRecords = new();
public List<UserPresencePresenter> SelectedAttendanceRecords
{
get => _selectedAttendanceRecords;
set => this.RaiseAndSetIfChanged(ref _selectedAttendanceRecords, value);
}
public ReactiveCommand<Unit, Unit> GoBackCommand { get; }
public ReactiveCommand<Unit, Unit> LoadAttendanceCommand { get; }
public ReactiveCommand<List<UserPresencePresenter>, Unit> DeleteAttendanceRecordsCommand { get; }
public PresenceViewModel(
IPresenceApiClient presenceApiClient,
IGroupApiClient groupApiClient,
IScreen hostScreen)
{
_presenceApiClient = presenceApiClient;
_groupApiClient = groupApiClient;
HostScreen = hostScreen;
GoBackCommand = ReactiveCommand.Create(() =>
{
HostScreen.Router.Navigate.Execute(new StartViewModel(HostScreen));
});
LoadAttendanceCommand = ReactiveCommand.CreateFromTask(LoadAttendanceDataAsync);
DeleteAttendanceRecordsCommand = ReactiveCommand.CreateFromTask<List<UserPresencePresenter>>(DeleteAttendanceRecords);
this.WhenAnyValue(x => x.SelectedGroup, x => x.SelectedDate)
.Throttle(TimeSpan.FromMilliseconds(200))
.ObserveOn(RxApp.MainThreadScheduler)
.Select(_ => Unit.Default)
.InvokeCommand(LoadAttendanceCommand);
_ = LoadDataAsync();
}
private async Task LoadDataAsync()
{
try
{
var groups = await _groupApiClient.GetGroupsAsync();
Groups.Clear();
foreach (var group in groups ?? Enumerable.Empty<Group>())
Groups.Add(group);
if (Groups.Any())
{
SelectedGroup = Groups.First();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private async Task LoadAttendanceDataAsync()
{
if (SelectedGroup == null) return;
try
{
var response = await _presenceApiClient.GetPresenceAsync(
SelectedGroup.ID,
SelectedDate.ToString("dd.MM.yyyy"),
SelectedDate.ToString("dd.MM.yyyy"));
AttendanceRecords.Clear();
foreach (var record in response?.Users?.Select(u => new UserPresencePresenter
{
GuidUser = u.Guid,
FIO = u.FIO,
LessonNumber = u.LessonNumber,
Date = u.Date,
IsAttendance = u.IsAttendance
}) ?? Enumerable.Empty<UserPresencePresenter>())
{
AttendanceRecords.Add(record);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
AttendanceRecords.Clear();
}
}
private async Task DeleteAttendanceRecords(List<UserPresencePresenter> selectedItems)
{
foreach (var item in selectedItems)
{
AttendanceRecords.Remove(item);
var success = await _presenceApiClient.DeletePresenceRecords(
item.Date.ToString(), item.LessonNumber, item.GuidUser);
if (success)
{
await LoadDataAsync();
}
}
SelectedAttendanceRecords.Clear();
}
public string? UrlPathSegment => "presence";
public IScreen HostScreen { get; }
}
2024-12-05 08:26:58 +00:00
}