presence/Presence.Desktop/ViewModels/PresenceViewModel.cs
2025-04-21 12:00:17 +03:00

152 lines
5.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// using System.Reactive;
// using ReactiveUI;
// namespace Presence.Desktop.ViewModels;
// public class PresenceViewModel: ViewModelBase, IRoutableViewModel
// {
// public string? UrlPathSegment { get; }
// public IScreen HostScreen { get; }
// public ReactiveCommand<Unit, Unit> GoBackCommand { get; }
// public PresenceViewModel(IScreen hostScreen)
// {
// HostScreen = hostScreen;
// GoBackCommand = ReactiveCommand.Create(() =>
// {
// HostScreen.Router.Navigate.Execute(new StartViewModel(HostScreen));
// });
// }
// }
using System;
using System.Reactive;
using ReactiveUI;
using Demo.Domain.Models;
using Demo.Domain.UseCase;
using System.Collections.ObjectModel;
using Presence.Desktop.Models;
namespace Presence.Desktop.ViewModels
{
public class PresenceViewModel : ViewModelBase, IRoutableViewModel
{
public string? UrlPathSegment { get; }
public IScreen HostScreen { get; }
public ReactiveCommand<Unit, Unit> GoBackCommand { get; }
public ReactiveCommand<Unit, Unit> FilterAttendanceCommand { get; }
public ObservableCollection<PresencePresenter> AttendanceRecords { get; set; } = new();
public ObservableCollection<Group> Groups { get; set; } = new();
public ObservableCollection<string> AttendanceTypes { get; set; } = new()
{
"Присутствовал",
"Отсутствовал",
};
private Group? _selectedGroup;
public Group? 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();
}
}
private readonly IGroupUseCase _groupUseCase;
private readonly IPresenceUseCase _presenceUseCase;
public PresenceViewModel(IScreen hostScreen, IGroupUseCase groupUseCase, IPresenceUseCase presenceUseCase)
{
HostScreen = hostScreen;
_groupUseCase = groupUseCase;
_presenceUseCase = presenceUseCase;
GoBackCommand = ReactiveCommand.Create(() =>
{
HostScreen.Router.Navigate.Execute(new StartViewModel(HostScreen));
});
FilterAttendanceCommand = ReactiveCommand.Create(FilterAttendanceRecords);
LoadGroups();
}
private void LoadGroups()
{
Groups.Clear();
var groups = _groupUseCase.GetAllGroups();
foreach (var group in groups)
{
Groups.Add(group);
}
}
private void FilterAttendanceRecords()
{
if (SelectedGroup == null || SelectedDate == null)
{
AttendanceRecords.Clear();
return;
}
var dateOnly = DateOnly.FromDateTime(SelectedDate.Value);
var records = _presenceUseCase.GetPresenceByGroupByTime(
SelectedGroup.ID,
dateOnly);
AttendanceRecords.Clear();
foreach (var record in records)
{
// Преобразуем значение IsAttedance в строку прямо здесь
var attendanceStatus = record.IsAttedance ? "Был на уроке" : "Не был на уроке";
AttendanceRecords.Add(new PresencePresenter
{
UserGuid = record.User.Guid,
IsAttedance = record.IsAttedance, // оставляем bool в модели
Date = record.Date,
LessonNumber = record.LessonNumber,
user = new UserPresenter
{
Name = record.User.FIO,
Guid = record.User.Guid
},
AttendanceStatus = attendanceStatus // добавляем строковое представление
});
}
}
// Новый метод для обработки изменения типа посещаемости
public void OnAttendanceTypeChanged(PresencePresenter presence, string newType)
{
presence.IsAttedance = newType == "Присутствовал";
if (_presenceUseCase.UpdateAttendance(presence.LessonNumber, presence.Date, presence.UserGuid, presence.IsAttedance))
{
Console.WriteLine($"Изменен тип посещаемости для {presence.user.Name}: {newType}");
}
else
{
Console.WriteLine($"Не удалось обновить тип посещаемости для {presence.user.Name}");
}
}
}
}