Files
Jugenddienst-Stunden/Jugenddienst Stunden/ViewModels/StundenViewModel.cs
2024-09-25 22:22:05 +02:00

197 lines
5.1 KiB
C#

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Windows.Input;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Networking;
using ZXing.Net.Maui;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Jugenddienst_Stunden.Types;
namespace Jugenddienst_Stunden.ViewModels {
internal class StundenViewModel : ObservableObject {
public string Name => AppInfo.Name;
public string Surname => AppInfo.VersionString;
public string MoreInfoUrl => "https://aka.ms/maui";
public string Message => "Hier werden deine geleisteten Arbeitsstunden aufgelistet";
public string LoadOverview => "Zeige Summen für " + DateTime.Today.ToString("MMMM");
public static DateTime GetDay = DateTime.Today;
public string ShowDay => "Zeit an Tag " + GetDay.ToString("ddd d. MMM") + ": ";
public int id { get; set; }
public ICommand NewEntryCommand { get; }
public ICommand SelectEntryCommand { get; }
public ICommand LoadDataCommand { get; private set; }
public object Stunden { get; }
private List<DayTime> _stunde;
public List<DayTime> Stunde {
get => _stunde;
}
private Hours _hour;
public Hours Hours {
get => _hour;
}
public string ZeitDone {
get => _hour.zeit;
}
public string ZeitCalculated {
get => _hour.zeit_total;
}
public string Nominal {
get => _hour.nominal;
}
public string Overtime {
get => _hour.overtime;
}
public string OvertimeMonth {
get => _hour.overtime_month;
}
public string Holiday {
get => _hour.holiday;
}
public TimeOnly DayTotal { get; set; }
public List<DayTime> DayTimes {
get => _hour.daytime;
set {
if (_hour.daytime != value) {
_hour.daytime = value;
OnPropertyChanged();
}
}
}
public DateTime MinimumDate {
//get => _hour.MinDate;
//get => DateTime.Today.AddDays(-21);
get => DateTime.Today.AddDays(-365);
}
public DateTime MaximumDate {
//get => _hour.MaxDate;
get => DateTime.Today.AddDays(60);
}
private DateTime dateToday = DateTime.Today;
public DateTime DateToday {
get => dateToday;
set {
if (dateToday != value) {
dateToday = value;
GetDay = value;
OnPropertyChanged();
_ = LoadDay(value); // Use discard operator to explicitly ignore the returned Task
//RefreshProperties();
OnPropertyChanged(nameof(TimeDay));
OnPropertyChanged(nameof(ShowDay));
OnPropertyChanged(nameof(DayTimes));
}
}
}
public DateTime Date {
get => _hour.Date;
}
public List<TimeDay> ZeitTotalDaily {
get => _hour.zeit_total_daily_api;
}
public string Title { get; set; } = Preferences.Default.Get("name", "") + " " + Preferences.Default.Get("surname", "");
public List<TimeDay> TimeDay { get; set; }
public StundenViewModel() {
_hour = new Types.Hours();
LoadDataCommand = new AsyncRelayCommand(LoadData);
NewEntryCommand = new AsyncRelayCommand(NewEntryAsync);
SelectEntryCommand = new AsyncRelayCommand<DayTime>(SelectEntryAsync);
_ = LoadDay(DateTime.Today);
OnPropertyChanged(nameof(DayTimes));
}
private async Task NewEntryAsync() {
//Hier muss das Datum übergeben werden
await Shell.Current.GoToAsync(nameof(Views.StundePage));
}
private async Task SelectEntryAsync(DayTime entry) {
if (entry != null && entry.id != null)
await Shell.Current.GoToAsync($"{nameof(Views.StundePage)}?load={entry.id}");
else AlertEvent?.Invoke(this, "Auswahl enthält keine Daten");
}
public event EventHandler<string> AlertEvent;
private async Task LoadData() {
try {
_hour = await Models.Stunde.LoadData();
RefreshProperties();
} catch (Exception e) {
AlertEvent?.Invoke(this, e.Message);
}
}
private async Task LoadDay(DateTime date) {
try {
_hour.daytime = await Models.Stunde.LoadDay(date);
////if (_hour.zeit_total_daily_api != null) {
////TimeDay = _hour.zeit_total_daily_api.Where(static p => p.Day == GetDay.Day).ToList() ?? new List<TimeDay> { new TimeDay { Day = GetDay.Day, Hours = 0 } };
//RefreshProperties();
TimeSpan span = TimeSpan.Zero;
foreach (DayTime dt in _hour.daytime) {
span += dt.end - dt.begin;
}
DayTotal = TimeOnly.FromTimeSpan(span);
OnPropertyChanged(nameof(ShowDay));
OnPropertyChanged(nameof(TimeDay));
OnPropertyChanged(nameof(DayTotal));
OnPropertyChanged(nameof(DayTimes));
////}
} catch (Exception e) {
DayTimes = new List<DayTime>();
AlertEvent?.Invoke(this, e.Message);
}
}
private void RefreshProperties() {
OnPropertyChanged(nameof(Nominal));
OnPropertyChanged(nameof(Overtime));
OnPropertyChanged(nameof(OvertimeMonth));
OnPropertyChanged(nameof(ZeitCalculated));
OnPropertyChanged(nameof(ZeitDone));
OnPropertyChanged(nameof(Holiday));
OnPropertyChanged(nameof(Hours));
OnPropertyChanged(nameof(Title));
OnPropertyChanged(nameof(ZeitTotalDaily));
OnPropertyChanged(nameof(TimeDay));
OnPropertyChanged(nameof(MinimumDate));
OnPropertyChanged(nameof(MaximumDate));
OnPropertyChanged(nameof(ShowDay));
//OnPropertyChanged(nameof(DateToday));
}
}
}