d85f2e0fb3
Bootstraper läd jetzt die farbeinstellung sofort und nicht nachträglich cleanup and rework PDF switch to HTML export fix Colorsystem Farben für knöpfe hinzugefügt als anpassung button color button color primary and secondary change
360 lines
12 KiB
C#
360 lines
12 KiB
C#
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 Microsoft.Maui.Graphics;
|
|
using System.Collections.ObjectModel;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
|
|
namespace cfs_tracker.ViewModels;
|
|
|
|
public partial class QuickEntryViewModel : ObservableObject
|
|
{
|
|
private readonly HealthEntryRepository _healthEntryRepository;
|
|
private readonly ActivityRepository _activityRepository;
|
|
private readonly SymptomRepository _symptomRepository;
|
|
private readonly AppearanceService _appearanceService;
|
|
private readonly CalendarStateService _calendarStateService;
|
|
|
|
private string _dateQuery = string.Empty;
|
|
private int? _entryId;
|
|
|
|
[ObservableProperty]
|
|
private DateTime selectedDate = DateTime.Today;
|
|
|
|
[ObservableProperty]
|
|
private string selectedDateText = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private bool isToday;
|
|
|
|
[ObservableProperty]
|
|
private bool isPastDate;
|
|
|
|
[ObservableProperty]
|
|
private bool isFutureDate;
|
|
|
|
[ObservableProperty]
|
|
private bool isEditing;
|
|
|
|
[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 ActivityPickerItemViewModel? selectedActivity;
|
|
|
|
[ObservableProperty]
|
|
private DayPartOptionItemViewModel? selectedPart;
|
|
|
|
[ObservableProperty]
|
|
private HealthState? selectedState;
|
|
|
|
public ObservableCollection<ActivityPickerItemViewModel> Activities { get; } = new();
|
|
public ObservableCollection<SelectableSymptomItemViewModel> Symptoms { get; } = new();
|
|
public ObservableCollection<DayPartOptionItemViewModel> Parts { get; } = new();
|
|
|
|
public string ModeText => IsFutureDate
|
|
? "Zukünftige Tage können nicht gespeichert werden."
|
|
: string.Empty;
|
|
|
|
public bool CanSave => !IsFutureDate;
|
|
public bool ShowSaveButton => IsEditing;
|
|
public bool CanExplicitSave => IsEditing && SelectedState.HasValue && !IsFutureDate;
|
|
|
|
public string SelectedStateSummaryText => SelectedState.HasValue
|
|
? $"Gewählter Status: {GetStateText(SelectedState.Value)}"
|
|
: "Bitte Status wählen";
|
|
|
|
public IAsyncRelayCommand SaveGreenCommand { get; }
|
|
public IAsyncRelayCommand SaveYellowCommand { get; }
|
|
public IAsyncRelayCommand SaveRedCommand { get; }
|
|
public IAsyncRelayCommand SaveGrayCommand { get; }
|
|
public IAsyncRelayCommand SaveCommand { get; }
|
|
public IAsyncRelayCommand CancelCommand { get; }
|
|
|
|
public QuickEntryViewModel(
|
|
HealthEntryRepository healthEntryRepository,
|
|
ActivityRepository activityRepository,
|
|
SymptomRepository symptomRepository,
|
|
AppearanceService appearanceService,
|
|
CalendarStateService calendarStateService)
|
|
{
|
|
_healthEntryRepository = healthEntryRepository;
|
|
_activityRepository = activityRepository;
|
|
_symptomRepository = symptomRepository;
|
|
_appearanceService = appearanceService;
|
|
_calendarStateService = calendarStateService;
|
|
|
|
SaveGreenCommand = new AsyncRelayCommand(() => SelectOrSaveAsync(HealthState.Green));
|
|
SaveYellowCommand = new AsyncRelayCommand(() => SelectOrSaveAsync(HealthState.Yellow));
|
|
SaveRedCommand = new AsyncRelayCommand(() => SelectOrSaveAsync(HealthState.Red));
|
|
SaveGrayCommand = new AsyncRelayCommand(() => SelectOrSaveAsync(HealthState.Gray));
|
|
SaveCommand = new AsyncRelayCommand(SaveSelectedAsync);
|
|
CancelCommand = new AsyncRelayCommand(CancelAsync);
|
|
}
|
|
|
|
partial void OnIsTodayChanged(bool value) => RaiseModeChanged();
|
|
partial void OnIsPastDateChanged(bool value) => RaiseModeChanged();
|
|
partial void OnIsFutureDateChanged(bool value) => RaiseModeChanged();
|
|
|
|
partial void OnIsEditingChanged(bool value)
|
|
{
|
|
OnPropertyChanged(nameof(ShowSaveButton));
|
|
OnPropertyChanged(nameof(CanExplicitSave));
|
|
OnPropertyChanged(nameof(SelectedStateSummaryText));
|
|
}
|
|
|
|
partial void OnSelectedStateChanged(HealthState? value)
|
|
{
|
|
OnPropertyChanged(nameof(CanExplicitSave));
|
|
OnPropertyChanged(nameof(SelectedStateSummaryText));
|
|
}
|
|
|
|
public void ApplyQuery(string date, string entryId)
|
|
{
|
|
_dateQuery = date ?? string.Empty;
|
|
_entryId = int.TryParse(entryId, NumberStyles.Integer, CultureInfo.InvariantCulture, out var parsedId)
|
|
? parsedId
|
|
: null;
|
|
}
|
|
|
|
public async Task InitializeAsync()
|
|
{
|
|
SelectedDate = DateTime.TryParseExact(
|
|
_dateQuery,
|
|
"yyyy-MM-dd",
|
|
CultureInfo.InvariantCulture,
|
|
DateTimeStyles.None,
|
|
out var parsedDate)
|
|
? parsedDate.Date
|
|
: _calendarStateService.SelectedDate;
|
|
|
|
UpdateDateMode();
|
|
SelectedDateText = DateHelper.ToGermanDateText(SelectedDate);
|
|
|
|
LoadAppearance();
|
|
await LoadActivitiesAsync();
|
|
await LoadSymptomsAsync();
|
|
LoadDayParts();
|
|
|
|
if (_entryId.HasValue)
|
|
await LoadExistingEntryAsync();
|
|
else
|
|
SelectedPart = Parts.FirstOrDefault(x => x.Value == DayPart.Morning);
|
|
|
|
OnPropertyChanged(nameof(ModeText));
|
|
OnPropertyChanged(nameof(CanSave));
|
|
OnPropertyChanged(nameof(ShowSaveButton));
|
|
OnPropertyChanged(nameof(CanExplicitSave));
|
|
OnPropertyChanged(nameof(SelectedStateSummaryText));
|
|
}
|
|
|
|
private async Task LoadActivitiesAsync()
|
|
{
|
|
Activities.Clear();
|
|
|
|
Activities.Add(new ActivityPickerItemViewModel
|
|
{
|
|
Id = null,
|
|
Name = "Keine Tätigkeit"
|
|
});
|
|
|
|
var items = await _activityRepository.GetActiveAsync();
|
|
foreach (var item in items.Where(x => !string.Equals(x.Name, "Keine Tätigkeit", StringComparison.OrdinalIgnoreCase)))
|
|
{
|
|
Activities.Add(new ActivityPickerItemViewModel
|
|
{
|
|
Id = item.Id,
|
|
Name = item.Name
|
|
});
|
|
}
|
|
|
|
SelectedActivity ??= Activities.FirstOrDefault();
|
|
}
|
|
|
|
private async Task LoadSymptomsAsync()
|
|
{
|
|
Symptoms.Clear();
|
|
|
|
var items = await _symptomRepository.GetActiveAsync();
|
|
foreach (var item in items)
|
|
{
|
|
Symptoms.Add(new SelectableSymptomItemViewModel
|
|
{
|
|
Id = item.Id,
|
|
Name = item.Name
|
|
});
|
|
}
|
|
}
|
|
|
|
private void LoadDayParts()
|
|
{
|
|
if (Parts.Count > 0)
|
|
return;
|
|
|
|
Parts.Add(new DayPartOptionItemViewModel
|
|
{
|
|
Value = DayPart.Morning,
|
|
Name = "Vormittag"
|
|
});
|
|
|
|
Parts.Add(new DayPartOptionItemViewModel
|
|
{
|
|
Value = DayPart.Afternoon,
|
|
Name = "Nachmittag"
|
|
});
|
|
}
|
|
|
|
private async Task LoadExistingEntryAsync()
|
|
{
|
|
var entry = await _healthEntryRepository.GetByIdAsync(_entryId!.Value);
|
|
if (entry is null)
|
|
return;
|
|
|
|
IsEditing = true;
|
|
SelectedDate = entry.EntryDate.Date;
|
|
UpdateDateMode();
|
|
SelectedDateText = DateHelper.ToGermanDateText(SelectedDate);
|
|
|
|
SelectedPart = Parts.FirstOrDefault(x => x.Value == (entry.DayPart ?? DayPart.Morning))
|
|
?? Parts.First();
|
|
|
|
SelectedActivity = Activities.FirstOrDefault(x => x.Id == entry.ActivityId)
|
|
?? Activities.FirstOrDefault();
|
|
|
|
SelectedState = entry.State;
|
|
|
|
var symptomIds = await _symptomRepository.GetSymptomIdsForEntryAsync(entry.Id);
|
|
foreach (var symptom in Symptoms)
|
|
symptom.IsSelected = symptomIds.Contains(symptom.Id);
|
|
}
|
|
|
|
private async Task SelectOrSaveAsync(HealthState state)
|
|
{
|
|
if (IsFutureDate)
|
|
return;
|
|
|
|
SelectedState = state;
|
|
|
|
if (IsEditing)
|
|
return;
|
|
|
|
await SaveAsync(state);
|
|
}
|
|
|
|
private async Task SaveSelectedAsync()
|
|
{
|
|
if (!IsEditing || !SelectedState.HasValue || IsFutureDate)
|
|
return;
|
|
|
|
await SaveAsync(SelectedState.Value);
|
|
}
|
|
|
|
private async Task SaveAsync(HealthState state)
|
|
{
|
|
if (IsFutureDate)
|
|
return;
|
|
|
|
var activityId = SelectedActivity?.Id;
|
|
var symptomIds = Symptoms.Where(x => x.IsSelected).Select(x => x.Id).ToList();
|
|
|
|
if (IsEditing && _entryId.HasValue)
|
|
{
|
|
await _healthEntryRepository.UpdateEntryAsync(
|
|
entryId: _entryId.Value,
|
|
date: SelectedDate,
|
|
state: state,
|
|
activityId: activityId,
|
|
symptomIds: symptomIds,
|
|
part: IsPastDate ? SelectedPart?.Value ?? DayPart.Morning : null,
|
|
source: IsPastDate ? EntrySource.Backfill : EntrySource.App);
|
|
}
|
|
else if (IsPastDate)
|
|
{
|
|
await _healthEntryRepository.UpsertBackfillAsync(
|
|
date: SelectedDate,
|
|
part: SelectedPart?.Value ?? DayPart.Morning,
|
|
state: state,
|
|
activityId: activityId,
|
|
symptomIds: symptomIds,
|
|
source: EntrySource.Backfill);
|
|
}
|
|
else
|
|
{
|
|
await _healthEntryRepository.AddQuickEntryAsync(
|
|
state: state,
|
|
activityId: activityId,
|
|
symptomIds: symptomIds,
|
|
timestamp: DateTime.Now,
|
|
source: EntrySource.App);
|
|
}
|
|
|
|
_calendarStateService.SelectedDate = SelectedDate;
|
|
await Shell.Current.GoToAsync("..");
|
|
}
|
|
|
|
private Task CancelAsync() => Shell.Current.GoToAsync("..");
|
|
|
|
private void UpdateDateMode()
|
|
{
|
|
var today = DateTime.Today;
|
|
IsToday = SelectedDate.Date == today;
|
|
IsPastDate = SelectedDate.Date < today;
|
|
IsFutureDate = SelectedDate.Date > today;
|
|
}
|
|
|
|
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");
|
|
|
|
OnPropertyChanged(nameof(SelectedStateSummaryText));
|
|
}
|
|
|
|
private void RaiseModeChanged()
|
|
{
|
|
OnPropertyChanged(nameof(ModeText));
|
|
OnPropertyChanged(nameof(CanSave));
|
|
OnPropertyChanged(nameof(CanExplicitSave));
|
|
}
|
|
|
|
private string GetStateText(HealthState state) => state switch
|
|
{
|
|
HealthState.Green => GreenStatusText,
|
|
HealthState.Yellow => YellowStatusText,
|
|
HealthState.Red => RedStatusText,
|
|
HealthState.Gray => GrayStatusText,
|
|
_ => string.Empty
|
|
};
|
|
} |