2b0797f50f
Linked colors to options menu Options extension Aussehen aufgebohrt: - erlaubt text von status zu ändern, - bessere beschriftung von Farben desing änderung und bedienung - heute status felder anklickbar, - Farbpalette verdunkelt und kontrast reduziert - Kalender Tage anklickbar gemacht - Optionen überarbeitet
210 lines
7.2 KiB
C#
210 lines
7.2 KiB
C#
using System.Collections.ObjectModel;
|
|
using System.Globalization;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using cfs_tracker.Data;
|
|
using cfs_tracker.Helpers;
|
|
using cfs_tracker.Models;
|
|
using cfs_tracker.Services;
|
|
using cfs_tracker.Views;
|
|
|
|
namespace cfs_tracker.ViewModels;
|
|
|
|
public partial class DayViewModel : ObservableObject
|
|
{
|
|
private readonly HealthEntryRepository _repository;
|
|
private readonly ActivityRepository _activityRepository;
|
|
private readonly SymptomRepository _symptomRepository;
|
|
private readonly CalendarStateService _calendarState;
|
|
|
|
[ObservableProperty]
|
|
private DateTime selectedDate = DateTime.Today;
|
|
|
|
[ObservableProperty]
|
|
private string selectedDateText = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private int greenCount;
|
|
|
|
[ObservableProperty]
|
|
private int yellowCount;
|
|
|
|
[ObservableProperty]
|
|
private int redCount;
|
|
|
|
[ObservableProperty]
|
|
private int grayCount;
|
|
|
|
[ObservableProperty]
|
|
private string greenStatusText = ThemeResourceHelper.GetString(ThemeKeys.StatusGreenText, "Grün");
|
|
|
|
[ObservableProperty]
|
|
private string yellowStatusText = ThemeResourceHelper.GetString(ThemeKeys.StatusYellowText, "Gelb");
|
|
|
|
[ObservableProperty]
|
|
private string redStatusText = ThemeResourceHelper.GetString(ThemeKeys.StatusRedText, "Rot");
|
|
|
|
[ObservableProperty]
|
|
private string grayStatusText = ThemeResourceHelper.GetString(ThemeKeys.StatusGrayText, "Grau");
|
|
|
|
public ObservableCollection<DayEntryItemViewModel> Entries { get; } = new();
|
|
|
|
public IAsyncRelayCommand PreviousDayCommand { get; }
|
|
public IAsyncRelayCommand NextDayCommand { get; }
|
|
public IAsyncRelayCommand TodayCommand { get; }
|
|
public IAsyncRelayCommand AddEntryCommand { get; }
|
|
public IAsyncRelayCommand<DayEntryItemViewModel> DeleteEntryCommand { get; }
|
|
|
|
public IAsyncRelayCommand SaveGreenQuickCommand { get; }
|
|
public IAsyncRelayCommand SaveYellowQuickCommand { get; }
|
|
public IAsyncRelayCommand SaveRedQuickCommand { get; }
|
|
public IAsyncRelayCommand SaveGrayQuickCommand { get; }
|
|
|
|
public DayViewModel(
|
|
HealthEntryRepository repository,
|
|
ActivityRepository activityRepository,
|
|
SymptomRepository symptomRepository,
|
|
CalendarStateService calendarState)
|
|
{
|
|
_repository = repository;
|
|
_activityRepository = activityRepository;
|
|
_symptomRepository = symptomRepository;
|
|
_calendarState = calendarState;
|
|
|
|
SelectedDate = _calendarState.SelectedDate;
|
|
_calendarState.SelectedDateChanged += OnSharedSelectedDateChanged;
|
|
|
|
PreviousDayCommand = new AsyncRelayCommand(PreviousDayAsync);
|
|
NextDayCommand = new AsyncRelayCommand(NextDayAsync);
|
|
TodayCommand = new AsyncRelayCommand(SetTodayAsync);
|
|
AddEntryCommand = new AsyncRelayCommand(OpenQuickEntryAsync);
|
|
DeleteEntryCommand = new AsyncRelayCommand<DayEntryItemViewModel>(DeleteEntryAsync);
|
|
|
|
SaveGreenQuickCommand = new AsyncRelayCommand(() => QuickSaveAsync(HealthState.Green));
|
|
SaveYellowQuickCommand = new AsyncRelayCommand(() => QuickSaveAsync(HealthState.Yellow));
|
|
SaveRedQuickCommand = new AsyncRelayCommand(() => QuickSaveAsync(HealthState.Red));
|
|
SaveGrayQuickCommand = new AsyncRelayCommand(() => QuickSaveAsync(HealthState.Gray));
|
|
|
|
UpdateDateText();
|
|
RefreshStatusTexts();
|
|
}
|
|
|
|
partial void OnSelectedDateChanged(DateTime value)
|
|
{
|
|
UpdateDateText();
|
|
}
|
|
|
|
private async void OnSharedSelectedDateChanged(DateTime date)
|
|
{
|
|
SelectedDate = date;
|
|
await RefreshAsync();
|
|
}
|
|
|
|
public async Task RefreshAsync()
|
|
{
|
|
SelectedDate = _calendarState.SelectedDate;
|
|
RefreshStatusTexts();
|
|
|
|
var entries = await _repository.GetEntriesForDateAsync(SelectedDate);
|
|
|
|
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();
|
|
|
|
var activityMap = await _activityRepository.GetNameMapAsync();
|
|
var symptomMapByEntry = await _symptomRepository.GetSymptomsByEntryIdsAsync(entries.Select(x => x.Id));
|
|
|
|
foreach (var entry in entries.OrderBy(x => x.Timestamp))
|
|
{
|
|
string activityText = string.Empty;
|
|
if (entry.ActivityId.HasValue && activityMap.TryGetValue(entry.ActivityId.Value, out var activityName))
|
|
activityText = activityName;
|
|
|
|
symptomMapByEntry.TryGetValue(entry.Id, out var symptoms);
|
|
string symptomsText = symptoms is { Count: > 0 }
|
|
? string.Join(", ", symptoms)
|
|
: string.Empty;
|
|
|
|
Entries.Add(new DayEntryItemViewModel
|
|
{
|
|
Id = entry.Id,
|
|
TimeText = entry.Timestamp.ToString("HH:mm"),
|
|
StateText = ThemeResourceHelper.GetString(
|
|
entry.State.ToTextKey(),
|
|
entry.State.ToFallbackText()),
|
|
ActivityText = activityText,
|
|
SymptomsText = symptomsText,
|
|
BackgroundColor = ThemeResourceHelper.GetColor(entry.State.ToColorKey(), "#203041"),
|
|
TextColor = ThemeResourceHelper.GetColor(ThemeKeys.TextPrimary, "#D9E6F2")
|
|
});
|
|
}
|
|
}
|
|
|
|
private async Task QuickSaveAsync(HealthState state)
|
|
{
|
|
if (SelectedDate.Date == DateTime.Today)
|
|
{
|
|
await _repository.AddEntryAsync(
|
|
state,
|
|
EntrySource.App,
|
|
DateTime.Now);
|
|
|
|
await RefreshAsync();
|
|
return;
|
|
}
|
|
|
|
await OpenQuickEntryAsync();
|
|
}
|
|
|
|
private async Task PreviousDayAsync()
|
|
{
|
|
_calendarState.SelectedDate = SelectedDate.AddDays(-1);
|
|
SelectedDate = _calendarState.SelectedDate;
|
|
await RefreshAsync();
|
|
}
|
|
|
|
private async Task NextDayAsync()
|
|
{
|
|
_calendarState.SelectedDate = SelectedDate.AddDays(1);
|
|
SelectedDate = _calendarState.SelectedDate;
|
|
await RefreshAsync();
|
|
}
|
|
|
|
private async Task SetTodayAsync()
|
|
{
|
|
_calendarState.SelectedDate = DateTime.Today;
|
|
SelectedDate = _calendarState.SelectedDate;
|
|
await RefreshAsync();
|
|
}
|
|
|
|
private async Task OpenQuickEntryAsync()
|
|
{
|
|
string date = SelectedDate.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
|
|
await Shell.Current.GoToAsync($"{nameof(QuickEntryPage)}?date={date}");
|
|
}
|
|
|
|
private async Task DeleteEntryAsync(DayEntryItemViewModel? item)
|
|
{
|
|
if (item is null)
|
|
return;
|
|
|
|
await _repository.DeleteEntryAsync(item.Id);
|
|
await RefreshAsync();
|
|
}
|
|
|
|
private void UpdateDateText()
|
|
{
|
|
SelectedDateText = SelectedDate.ToString("dddd, dd. MMMM yyyy", new CultureInfo("de-DE"));
|
|
}
|
|
|
|
private void RefreshStatusTexts()
|
|
{
|
|
GreenStatusText = ThemeResourceHelper.GetString(ThemeKeys.StatusGreenText, "Grün");
|
|
YellowStatusText = ThemeResourceHelper.GetString(ThemeKeys.StatusYellowText, "Gelb");
|
|
RedStatusText = ThemeResourceHelper.GetString(ThemeKeys.StatusRedText, "Rot");
|
|
GrayStatusText = ThemeResourceHelper.GetString(ThemeKeys.StatusGrayText, "Grau");
|
|
}
|
|
} |