presence_new/Presence.Desktop/ViewModels/PresenceViewModel.cs

113 lines
3.2 KiB
C#

using Avalonia.Data.Converters;
using presence.data.RemoteData.RemoteDataBase.DAO;
using presence.domain.Models;
using presence.domain.UseCase;
using Presence.Desktop.Models;
using Presence.Desktop.ViewModels;
using ReactiveUI;
using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Reactive;
namespace Presence.Desktop.ViewModels
{
public class PresenceViewModel : ViewModelBase, IRoutableViewModel
{
public string? UrlPathSegment { get; } = "Presence";
public IScreen HostScreen { get; }
private readonly GroupUseCase _groupUseCase;
private readonly PresenceUseCase _presenceUseCase;
public ObservableCollection<PresencePresenter> AttendanceRecords { get; set; } = new();
public ObservableCollection<GroupPresenter> Groups { get; set; } = new();
private GroupPresenter? _selectedGroup;
public GroupPresenter? SelectedGroup
{
get => _selectedGroup;
set
{
this.RaiseAndSetIfChanged(ref _selectedGroup, value);
FilterAttendanceRecords();
}
}
private DateTime? _selectedDate;
public DateTime? SelectedDate
{
get => _selectedDate;
set
{
this.RaiseAndSetIfChanged(ref _selectedDate, value);
FilterAttendanceRecords();
}
}
public ReactiveCommand<Unit, Unit> NavigateBackCommand { get; }
public PresenceViewModel(GroupUseCase groupUseCase, PresenceUseCase presenceUseCase)
{
_groupUseCase = groupUseCase;
_presenceUseCase = presenceUseCase;
NavigateBackCommand = ReactiveCommand.Create(() => { });
this.WhenAnyValue(vm => vm.SelectedGroup)
.Subscribe(_ =>
{
LoadGroups();
});
}
private void LoadGroups()
{
Groups.Clear();
var groups = _groupUseCase.GetAllGroups();
foreach (var group in groups)
{
Groups.Add(new GroupPresenter
{
Id = group.Id,
Name = group.Name
});
}
}
private void FilterAttendanceRecords()
{
if (SelectedGroup == null)
{
AttendanceRecords.Clear();
return;
}
var records = _presenceUseCase.GetPresenceByGroupAndDate(
SelectedGroup.Id,
DateOnly.FromDateTime(SelectedDate.Value));
AttendanceRecords.Clear();
foreach (var record in records)
{
AttendanceRecords.Add(new PresencePresenter
{
Id = record.Id,
Date = record.Date,
ClassNumber = record.ClassNumber,
IsAttendence = record.IsAttendence,
User = new UserPresenter
{
Id = record.User.Id,
Name= record.User.FIO
}
});
}
}
}
}