57 lines
2.2 KiB
C#
57 lines
2.2 KiB
C#
using System.Collections.ObjectModel;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using LehrerApp.Desktop.Services;
|
|
|
|
namespace LehrerApp.Desktop.ViewModels.Groups;
|
|
|
|
public partial class WebUntisClassSelectionViewModel(WebUntisIntegrationService untis) : ObservableObject
|
|
{
|
|
public ObservableCollection<UntisSchoolYearDto> SchoolYears { get; } = [];
|
|
public ObservableCollection<UntisClassDto> Classes { get; } = [];
|
|
[ObservableProperty] private UntisSchoolYearDto? _selectedSchoolYear;
|
|
[ObservableProperty] private UntisClassDto? _selectedClass;
|
|
[ObservableProperty] private string _status = "";
|
|
[ObservableProperty] private bool _busy;
|
|
public bool CanConfirm => SelectedClass is not null && !Busy;
|
|
|
|
partial void OnSelectedClassChanged(UntisClassDto? value) => OnPropertyChanged(nameof(CanConfirm));
|
|
partial void OnBusyChanged(bool value) => OnPropertyChanged(nameof(CanConfirm));
|
|
|
|
public async Task InitializeAsync()
|
|
{
|
|
Busy = true;
|
|
try
|
|
{
|
|
foreach (var year in (await untis.GetSchoolYearsAsync()).OrderByDescending(x => x.StartDate))
|
|
SchoolYears.Add(year);
|
|
SelectedSchoolYear = SchoolYears.FirstOrDefault(x => x.StartDate <= Today() && x.EndDate >= Today())
|
|
?? SchoolYears.FirstOrDefault();
|
|
await LoadClasses();
|
|
}
|
|
catch (WebUntisIntegrationException ex) { Status = ex.Message; }
|
|
finally { Busy = false; }
|
|
}
|
|
|
|
[RelayCommand]
|
|
private async Task LoadClasses()
|
|
{
|
|
if (SelectedSchoolYear is null) return;
|
|
Busy = true; Classes.Clear(); SelectedClass = null;
|
|
try
|
|
{
|
|
foreach (var entry in (await untis.GetClassesAsync(SelectedSchoolYear.UntisId)).OrderBy(x => x.Name))
|
|
Classes.Add(entry);
|
|
Status = Classes.Count == 0 ? "Keine Klassen gefunden." : $"{Classes.Count} Klassen gefunden.";
|
|
}
|
|
catch (WebUntisIntegrationException ex) { Status = ex.Message; }
|
|
finally { Busy = false; }
|
|
}
|
|
|
|
private static int Today()
|
|
{
|
|
var date = DateOnly.FromDateTime(DateTime.Today);
|
|
return date.Year * 10000 + date.Month * 100 + date.Day;
|
|
}
|
|
}
|