120 lines
3.1 KiB
C#
120 lines
3.1 KiB
C#
using System.Collections.ObjectModel;
|
|
using System.Globalization;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using cfs_tracker.Data;
|
|
using cfs_tracker.Models;
|
|
|
|
namespace cfs_tracker.ViewModels;
|
|
|
|
public partial class QuickEntryViewModel : ObservableObject
|
|
{
|
|
private readonly HealthEntryRepository _repository;
|
|
|
|
[ObservableProperty]
|
|
private DateTime selectedDate = DateTime.Today;
|
|
|
|
[ObservableProperty]
|
|
private string selectedDateText = "";
|
|
|
|
[ObservableProperty]
|
|
private bool isPastDate;
|
|
|
|
[ObservableProperty]
|
|
private bool isFutureDate;
|
|
|
|
[ObservableProperty]
|
|
private string selectedPartText = "Vormittag";
|
|
|
|
public ObservableCollection<string> AvailableParts { get; } = new()
|
|
{
|
|
"Vormittag",
|
|
"Nachmittag"
|
|
};
|
|
|
|
public IAsyncRelayCommand SaveGreenCommand { get; }
|
|
public IAsyncRelayCommand SaveYellowCommand { get; }
|
|
public IAsyncRelayCommand SaveRedCommand { get; }
|
|
public IAsyncRelayCommand SaveGrayCommand { get; }
|
|
|
|
public QuickEntryViewModel(HealthEntryRepository repository)
|
|
{
|
|
_repository = repository;
|
|
|
|
SaveGreenCommand = new AsyncRelayCommand(() => SaveAsync(HealthState.Green));
|
|
SaveYellowCommand = new AsyncRelayCommand(() => SaveAsync(HealthState.Yellow));
|
|
SaveRedCommand = new AsyncRelayCommand(() => SaveAsync(HealthState.Red));
|
|
SaveGrayCommand = new AsyncRelayCommand(() => SaveAsync(HealthState.Gray));
|
|
|
|
UpdateState();
|
|
}
|
|
|
|
public void SetDateFromQuery(string? dateText)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(dateText) &&
|
|
DateTime.TryParseExact(
|
|
dateText,
|
|
"yyyy-MM-dd",
|
|
CultureInfo.InvariantCulture,
|
|
DateTimeStyles.None,
|
|
out var parsed))
|
|
{
|
|
SelectedDate = parsed.Date;
|
|
}
|
|
else
|
|
{
|
|
SelectedDate = DateTime.Today;
|
|
}
|
|
|
|
UpdateState();
|
|
}
|
|
|
|
partial void OnSelectedDateChanged(DateTime value)
|
|
{
|
|
UpdateState();
|
|
}
|
|
|
|
private async Task SaveAsync(HealthState state)
|
|
{
|
|
if (IsFutureDate)
|
|
return;
|
|
|
|
if (IsPastDate)
|
|
{
|
|
await _repository.UpsertBackfillAsync(
|
|
SelectedDate,
|
|
GetSelectedPart(),
|
|
state,
|
|
EntrySource.App);
|
|
}
|
|
else
|
|
{
|
|
await _repository.AddEntryAsync(
|
|
state,
|
|
EntrySource.App,
|
|
DateTime.Now);
|
|
}
|
|
|
|
await Shell.Current.GoToAsync("..");
|
|
}
|
|
|
|
private DayPart GetSelectedPart()
|
|
{
|
|
return SelectedPartText == "Nachmittag"
|
|
? DayPart.Afternoon
|
|
: DayPart.Morning;
|
|
}
|
|
|
|
private void UpdateState()
|
|
{
|
|
var today = DateTime.Today;
|
|
|
|
IsPastDate = SelectedDate.Date < today;
|
|
IsFutureDate = SelectedDate.Date > today;
|
|
|
|
SelectedDateText = SelectedDate.ToString("dddd, dd. MMMM yyyy", new CultureInfo("de-DE"));
|
|
|
|
if (!IsPastDate)
|
|
SelectedPartText = "Vormittag";
|
|
}
|
|
} |