d11f0e46c0
clean up and fixes - Heute gefixt - Code Cleanup NotificationScheduleItemViewModel -fixed NotificationScheduleItemViewModel for Windows compat push implementation Push fixes Push benachrichtungen eingebaut - Status bei push - Push auch in background - Toggle für symtome angepasst push system change exporter update Optionen fix - Optionen aufgeteilt auf 3 tabs - Anpassungen haben jetzt Farbslider Optimierung fix
355 lines
13 KiB
C#
355 lines
13 KiB
C#
using cfs_tracker.Data;
|
|
using cfs_tracker.Helpers;
|
|
using cfs_tracker.Models;
|
|
using cfs_tracker.Services;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using System.Collections.ObjectModel;
|
|
using System.Globalization;
|
|
|
|
namespace cfs_tracker.ViewModels;
|
|
|
|
public partial class DayViewModel : ObservableObject
|
|
{
|
|
private readonly HealthEntryRepository _healthEntryRepository;
|
|
private readonly ActivityRepository _activityRepository;
|
|
private readonly SymptomRepository _symptomRepository;
|
|
private readonly AppearanceService _appearanceService;
|
|
private readonly CalendarStateService _calendarStateService;
|
|
private readonly IPdfExportService _pdfExportService;
|
|
|
|
[ObservableProperty]
|
|
private DateTime selectedDate;
|
|
|
|
[ObservableProperty]
|
|
private string selectedDateText = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private bool isToday;
|
|
|
|
[ObservableProperty]
|
|
private bool isPastDate;
|
|
|
|
[ObservableProperty]
|
|
private bool isFutureDate;
|
|
|
|
[ObservableProperty]
|
|
private int greenCount;
|
|
|
|
[ObservableProperty]
|
|
private int yellowCount;
|
|
|
|
[ObservableProperty]
|
|
private int redCount;
|
|
|
|
[ObservableProperty]
|
|
private int grayCount;
|
|
|
|
[ObservableProperty]
|
|
private string greenStatusText = "Grün";
|
|
|
|
[ObservableProperty]
|
|
private string yellowStatusText = "Gelb";
|
|
|
|
[ObservableProperty]
|
|
private string redStatusText = "Rot";
|
|
|
|
[ObservableProperty]
|
|
private string grayStatusText = "Schwarz";
|
|
|
|
[ObservableProperty]
|
|
private Color greenStateColor = Color.FromArgb("#4E8B69");
|
|
|
|
[ObservableProperty]
|
|
private Color yellowStateColor = Color.FromArgb("#B89A42");
|
|
|
|
[ObservableProperty]
|
|
private Color redStateColor = Color.FromArgb("#A85C5C");
|
|
|
|
[ObservableProperty]
|
|
private Color grayStateColor = Color.FromArgb("#1F242B");
|
|
|
|
[ObservableProperty]
|
|
private Color primaryTextColor = Color.FromArgb("#DCE7F3");
|
|
|
|
[ObservableProperty]
|
|
private bool hasEntries;
|
|
|
|
public ObservableCollection<DayEntryItemViewModel> Entries { get; } = new();
|
|
|
|
public string GreenCounterText => BuildCounterText(GreenStatusText, GreenCount);
|
|
public string YellowCounterText => BuildCounterText(YellowStatusText, YellowCount);
|
|
public string RedCounterText => BuildCounterText(RedStatusText, RedCount);
|
|
public string GrayCounterText => BuildCounterText(GrayStatusText, GrayCount);
|
|
|
|
public string AddEntryButtonText => IsPastDate ? "+ Nachtrag" : "+ Eintrag";
|
|
public bool CanAddEntry => !IsFutureDate;
|
|
public bool HasNoEntries => !HasEntries;
|
|
|
|
public string DayHintText => IsToday
|
|
? "Heute kannst du Schnellstatus oder einen Eintrag mit Tätigkeit und Symptomen setzen."
|
|
: IsPastDate
|
|
? "Für vergangene Tage legst du einen Nachtrag für Vormittag oder Nachmittag an."
|
|
: "Für zukünftige Tage sind keine Einträge möglich.";
|
|
|
|
public IAsyncRelayCommand PreviousDayCommand { get; }
|
|
public IAsyncRelayCommand NextDayCommand { get; }
|
|
public IAsyncRelayCommand TodayCommand { get; }
|
|
public IAsyncRelayCommand AddEntryCommand { get; }
|
|
public IAsyncRelayCommand SaveGreenQuickCommand { get; }
|
|
public IAsyncRelayCommand SaveYellowQuickCommand { get; }
|
|
public IAsyncRelayCommand SaveRedQuickCommand { get; }
|
|
public IAsyncRelayCommand SaveGrayQuickCommand { get; }
|
|
public IAsyncRelayCommand<DayEntryItemViewModel> EditEntryCommand { get; }
|
|
public IAsyncRelayCommand<DayEntryItemViewModel> DeleteEntryCommand { get; }
|
|
public IAsyncRelayCommand ExportPdfCommand { get; }
|
|
|
|
public DayViewModel(
|
|
HealthEntryRepository healthEntryRepository,
|
|
ActivityRepository activityRepository,
|
|
SymptomRepository symptomRepository,
|
|
AppearanceService appearanceService,
|
|
CalendarStateService calendarStateService,
|
|
IPdfExportService pdfExportService)
|
|
{
|
|
_healthEntryRepository = healthEntryRepository;
|
|
_activityRepository = activityRepository;
|
|
_symptomRepository = symptomRepository;
|
|
_appearanceService = appearanceService;
|
|
_calendarStateService = calendarStateService;
|
|
_pdfExportService = pdfExportService;
|
|
|
|
SelectedDate = _calendarStateService.SelectedDate;
|
|
UpdateDateState();
|
|
UpdateSelectedDateText();
|
|
|
|
PreviousDayCommand = new AsyncRelayCommand(PreviousDayAsync);
|
|
NextDayCommand = new AsyncRelayCommand(NextDayAsync);
|
|
TodayCommand = new AsyncRelayCommand(TodayAsync);
|
|
AddEntryCommand = new AsyncRelayCommand(AddEntryAsync);
|
|
SaveGreenQuickCommand = new AsyncRelayCommand(() => QuickSaveAsync(HealthState.Green));
|
|
SaveYellowQuickCommand = new AsyncRelayCommand(() => QuickSaveAsync(HealthState.Yellow));
|
|
SaveRedQuickCommand = new AsyncRelayCommand(() => QuickSaveAsync(HealthState.Red));
|
|
SaveGrayQuickCommand = new AsyncRelayCommand(() => QuickSaveAsync(HealthState.Gray));
|
|
EditEntryCommand = new AsyncRelayCommand<DayEntryItemViewModel>(EditEntryAsync);
|
|
DeleteEntryCommand = new AsyncRelayCommand<DayEntryItemViewModel>(DeleteEntryAsync);
|
|
ExportPdfCommand = new AsyncRelayCommand(ExportAsync);
|
|
|
|
_calendarStateService.SelectedDateChanged += HandleSharedDateChanged;
|
|
Entries.CollectionChanged += (_, _) =>
|
|
{
|
|
HasEntries = Entries.Count > 0;
|
|
OnPropertyChanged(nameof(HasNoEntries));
|
|
};
|
|
}
|
|
|
|
partial void OnSelectedDateChanged(DateTime value)
|
|
{
|
|
UpdateDateState();
|
|
UpdateSelectedDateText();
|
|
}
|
|
|
|
partial void OnGreenCountChanged(int value) => RaiseCounterTextChanged();
|
|
partial void OnYellowCountChanged(int value) => RaiseCounterTextChanged();
|
|
partial void OnRedCountChanged(int value) => RaiseCounterTextChanged();
|
|
partial void OnGrayCountChanged(int value) => RaiseCounterTextChanged();
|
|
partial void OnGreenStatusTextChanged(string value) => RaiseCounterTextChanged();
|
|
partial void OnYellowStatusTextChanged(string value) => RaiseCounterTextChanged();
|
|
partial void OnRedStatusTextChanged(string value) => RaiseCounterTextChanged();
|
|
partial void OnGrayStatusTextChanged(string value) => RaiseCounterTextChanged();
|
|
partial void OnIsPastDateChanged(bool value) => RaiseDayModeChanged();
|
|
partial void OnIsTodayChanged(bool value) => RaiseDayModeChanged();
|
|
partial void OnIsFutureDateChanged(bool value) => RaiseDayModeChanged();
|
|
|
|
public async Task RefreshAsync()
|
|
{
|
|
SelectedDate = _calendarStateService.SelectedDate;
|
|
LoadAppearance();
|
|
|
|
var entries = await _healthEntryRepository.GetEntriesForDateAsync(SelectedDate);
|
|
var activityMap = await _activityRepository.GetNameMapAsync();
|
|
var symptomsByEntryId = await _symptomRepository.GetSymptomsByEntryIdsAsync(entries.Select(x => x.Id));
|
|
|
|
GreenCount = entries.Count(x => x.State == HealthState.Green);
|
|
YellowCount = entries.Count(x => x.State == HealthState.Yellow);
|
|
RedCount = entries.Count(x => x.State == HealthState.Red);
|
|
GrayCount = entries.Count(x => x.State == HealthState.Gray);
|
|
|
|
Entries.Clear();
|
|
|
|
foreach (var entry in entries.OrderBy(x => x.Timestamp).ThenBy(x => x.Id))
|
|
{
|
|
var slotText = entry.DayPart switch
|
|
{
|
|
DayPart.Morning => "Vormittag",
|
|
DayPart.Afternoon => "Nachmittag",
|
|
_ => entry.Timestamp.ToString("HH:mm", CultureInfo.InvariantCulture)
|
|
};
|
|
|
|
var activityText = string.Empty;
|
|
if (entry.ActivityId.HasValue &&
|
|
activityMap.TryGetValue(entry.ActivityId.Value, out var activityName) &&
|
|
!string.Equals(activityName, "Keine Tätigkeit", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
activityText = activityName;
|
|
}
|
|
|
|
symptomsByEntryId.TryGetValue(entry.Id, out var symptoms);
|
|
var symptomText = symptoms is { Count: > 0 }
|
|
? string.Join(", ", symptoms)
|
|
: string.Empty;
|
|
|
|
var durationText = entry.ActivityDurationMinutes.HasValue && entry.ActivityDurationMinutes.Value > 0
|
|
? TimeSpan.FromMinutes(entry.ActivityDurationMinutes.Value).ToString(@"hh\:mm")
|
|
: string.Empty;
|
|
|
|
Entries.Add(new DayEntryItemViewModel
|
|
{
|
|
Id = entry.Id,
|
|
Date = entry.EntryDate,
|
|
SlotText = slotText,
|
|
StateText = _appearanceService.GetText(
|
|
entry.State.ToAppearanceTextKey(),
|
|
entry.State.ToFallbackText()),
|
|
ActivityText = activityText,
|
|
DurationText = durationText,
|
|
SymptomsText = symptomText,
|
|
IsBackfill = entry.DayPart.HasValue,
|
|
StateColor = _appearanceService.GetColor(entry.State.ToAppearanceColorKey(), "#1F242B"),
|
|
ForegroundColor = PrimaryTextColor
|
|
});
|
|
}
|
|
|
|
HasEntries = Entries.Count > 0;
|
|
OnPropertyChanged(nameof(HasNoEntries));
|
|
}
|
|
|
|
private async Task QuickSaveAsync(HealthState state)
|
|
{
|
|
if (!IsToday)
|
|
{
|
|
await AddEntryAsync();
|
|
return;
|
|
}
|
|
|
|
await _healthEntryRepository.AddQuickEntryAsync(
|
|
state: state,
|
|
activityId: null,
|
|
activityDurationMinutes: null,
|
|
symptomIds: null,
|
|
timestamp: DateTime.Now,
|
|
source: EntrySource.App);
|
|
|
|
await RefreshAsync();
|
|
}
|
|
|
|
private async Task AddEntryAsync()
|
|
{
|
|
if (IsFutureDate)
|
|
return;
|
|
|
|
var date = Uri.EscapeDataString(SelectedDate.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
|
|
await Shell.Current.GoToAsync($"{nameof(Views.QuickEntryPage)}?date={date}");
|
|
}
|
|
|
|
private async Task EditEntryAsync(DayEntryItemViewModel? item)
|
|
{
|
|
if (item is null)
|
|
return;
|
|
|
|
var date = Uri.EscapeDataString(item.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
|
|
await Shell.Current.GoToAsync($"{nameof(Views.QuickEntryPage)}?date={date}&entryId={item.Id}");
|
|
}
|
|
|
|
private async Task DeleteEntryAsync(DayEntryItemViewModel? item)
|
|
{
|
|
if (item is null)
|
|
return;
|
|
|
|
var accepted = await Application.Current!.Windows[0].Page!.DisplayAlertAsync(
|
|
"Eintrag löschen",
|
|
"Soll dieser Eintrag wirklich gelöscht werden?",
|
|
"Ja",
|
|
"Nein");
|
|
|
|
if (!accepted)
|
|
return;
|
|
|
|
await _healthEntryRepository.DeleteEntryAsync(item.Id);
|
|
await RefreshAsync();
|
|
}
|
|
|
|
private async Task ExportAsync()
|
|
{
|
|
var result = await _pdfExportService.ExportDayAsync(SelectedDate);
|
|
await ExportUiHelper.ShowExportSavedActionsAsync(result);
|
|
}
|
|
|
|
private async Task PreviousDayAsync()
|
|
{
|
|
_calendarStateService.SelectedDate = SelectedDate.AddDays(-1);
|
|
await RefreshAsync();
|
|
}
|
|
|
|
private async Task NextDayAsync()
|
|
{
|
|
_calendarStateService.SelectedDate = SelectedDate.AddDays(1);
|
|
await RefreshAsync();
|
|
}
|
|
|
|
private async Task TodayAsync()
|
|
{
|
|
_calendarStateService.ResetToToday();
|
|
await RefreshAsync();
|
|
}
|
|
|
|
private async void HandleSharedDateChanged(DateTime date)
|
|
{
|
|
SelectedDate = date;
|
|
await RefreshAsync();
|
|
}
|
|
|
|
private void UpdateDateState()
|
|
{
|
|
var today = DateTime.Today;
|
|
IsToday = SelectedDate.Date == today;
|
|
IsPastDate = SelectedDate.Date < today;
|
|
IsFutureDate = SelectedDate.Date > today;
|
|
}
|
|
|
|
private void UpdateSelectedDateText()
|
|
{
|
|
SelectedDateText = DateHelper.ToGermanDateText(SelectedDate);
|
|
}
|
|
|
|
private void LoadAppearance()
|
|
{
|
|
GreenStatusText = _appearanceService.GetText(AppearanceKeys.StatusGreenText, "Grün");
|
|
YellowStatusText = _appearanceService.GetText(AppearanceKeys.StatusYellowText, "Gelb");
|
|
RedStatusText = _appearanceService.GetText(AppearanceKeys.StatusRedText, "Rot");
|
|
GrayStatusText = _appearanceService.GetText(AppearanceKeys.StatusGrayText, "Schwarz");
|
|
|
|
GreenStateColor = _appearanceService.GetColor(AppearanceKeys.StateGreen, "#4E8B69");
|
|
YellowStateColor = _appearanceService.GetColor(AppearanceKeys.StateYellow, "#B89A42");
|
|
RedStateColor = _appearanceService.GetColor(AppearanceKeys.StateRed, "#A85C5C");
|
|
GrayStateColor = _appearanceService.GetColor(AppearanceKeys.StateGray, "#1F242B");
|
|
PrimaryTextColor = _appearanceService.GetColor(AppearanceKeys.TextPrimary, "#DCE7F3");
|
|
}
|
|
|
|
private void RaiseCounterTextChanged()
|
|
{
|
|
OnPropertyChanged(nameof(GreenCounterText));
|
|
OnPropertyChanged(nameof(YellowCounterText));
|
|
OnPropertyChanged(nameof(RedCounterText));
|
|
OnPropertyChanged(nameof(GrayCounterText));
|
|
}
|
|
|
|
private void RaiseDayModeChanged()
|
|
{
|
|
OnPropertyChanged(nameof(AddEntryButtonText));
|
|
OnPropertyChanged(nameof(CanAddEntry));
|
|
OnPropertyChanged(nameof(DayHintText));
|
|
}
|
|
|
|
private static string BuildCounterText(string label, int count) => $"{label}\n{count}";
|
|
} |