454 lines
15 KiB
C#
454 lines
15 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 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 string _statusQuery = string.Empty;
|
|
private bool _commitFromNotification;
|
|
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 = "Green";
|
|
|
|
[ObservableProperty]
|
|
private string yellowStatusText = "Yellow";
|
|
|
|
[ObservableProperty]
|
|
private string redStatusText = "Red";
|
|
|
|
[ObservableProperty]
|
|
private string grayStatusText = "Black";
|
|
|
|
[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 TimeSpan activityDuration = TimeSpan.Zero;
|
|
|
|
[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
|
|
? LocalizationService.Translate("QuickEntry_FutureDateMessage")
|
|
: string.Empty;
|
|
|
|
public bool CanSave => !IsFutureDate;
|
|
public bool ShowSaveButton => IsEditing;
|
|
public bool CanExplicitSave => IsEditing && SelectedState.HasValue && !IsFutureDate;
|
|
|
|
public string SelectedStateSummaryText => SelectedState.HasValue
|
|
? LocalizationService.Format("QuickEntry_SelectedStatusFormat", GetStateText(SelectedState.Value))
|
|
: LocalizationService.Translate("QuickEntry_SelectStatusPrompt");
|
|
|
|
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 IRelayCommand<SelectableSymptomItemViewModel> ToggleSymptomCommand { 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);
|
|
ToggleSymptomCommand = new RelayCommand<SelectableSymptomItemViewModel>(ToggleSymptom);
|
|
}
|
|
|
|
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, string? status, string? commit)
|
|
{
|
|
_dateQuery = date ?? string.Empty;
|
|
_statusQuery = status ?? string.Empty;
|
|
_commitFromNotification = string.Equals(commit, "true", StringComparison.OrdinalIgnoreCase);
|
|
|
|
_entryId = int.TryParse(entryId, out var parsedId)
|
|
? parsedId
|
|
: null;
|
|
}
|
|
|
|
public void ApplyQuery(string date, string? entryId)
|
|
{
|
|
ApplyQuery(date, entryId, null, 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);
|
|
|
|
IsEditing = false;
|
|
SelectedState = null;
|
|
ActivityDuration = TimeSpan.Zero;
|
|
|
|
LoadAppearance();
|
|
await LoadActivitiesAsync();
|
|
await LoadSymptomsAsync();
|
|
LoadDayParts();
|
|
|
|
SelectedActivity = Activities.FirstOrDefault();
|
|
SelectedPart = Parts.FirstOrDefault(x => x.Value == DayPart.Morning) ?? Parts.FirstOrDefault();
|
|
|
|
foreach (var symptom in Symptoms)
|
|
symptom.IsSelected = false;
|
|
|
|
if (_entryId.HasValue)
|
|
{
|
|
await LoadExistingEntryAsync();
|
|
}
|
|
else
|
|
{
|
|
if (TryParseNotificationState(_statusQuery, out var notificationState))
|
|
SelectedState = notificationState;
|
|
|
|
if (_commitFromNotification && IsToday && TryParseNotificationState(_statusQuery, out var quickState))
|
|
{
|
|
await SaveAsync(quickState);
|
|
return;
|
|
}
|
|
}
|
|
|
|
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 = LocalizationService.Translate("Activity_None")
|
|
});
|
|
|
|
var items = await _activityRepository.GetActiveAsync();
|
|
foreach (var item in items.Where(x => !LocalizationService.IsNoActivityLabel(x.Name)))
|
|
{
|
|
Activities.Add(new ActivityPickerItemViewModel
|
|
{
|
|
Id = item.Id,
|
|
Name = item.Name
|
|
});
|
|
}
|
|
}
|
|
|
|
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 = LocalizationService.Translate("Common_Morning")
|
|
});
|
|
|
|
Parts.Add(new DayPartOptionItemViewModel
|
|
{
|
|
Value = DayPart.Afternoon,
|
|
Name = LocalizationService.Translate("Common_Afternoon")
|
|
});
|
|
}
|
|
|
|
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();
|
|
|
|
ActivityDuration = TimeSpan.FromMinutes(entry.ActivityDurationMinutes ?? 0);
|
|
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 activityDurationMinutes = ParseActivityDurationMinutes();
|
|
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,
|
|
activityDurationMinutes: activityDurationMinutes,
|
|
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,
|
|
activityDurationMinutes: activityDurationMinutes,
|
|
symptomIds: symptomIds,
|
|
source: EntrySource.Backfill);
|
|
}
|
|
else
|
|
{
|
|
await _healthEntryRepository.AddQuickEntryAsync(
|
|
state: state,
|
|
activityId: activityId,
|
|
activityDurationMinutes: activityDurationMinutes,
|
|
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,
|
|
LocalizationService.GetDefaultStatusText(AppearanceKeys.StatusGreenText));
|
|
YellowStatusText = _appearanceService.GetText(
|
|
AppearanceKeys.StatusYellowText,
|
|
LocalizationService.GetDefaultStatusText(AppearanceKeys.StatusYellowText));
|
|
RedStatusText = _appearanceService.GetText(
|
|
AppearanceKeys.StatusRedText,
|
|
LocalizationService.GetDefaultStatusText(AppearanceKeys.StatusRedText));
|
|
GrayStatusText = _appearanceService.GetText(
|
|
AppearanceKeys.StatusGrayText,
|
|
LocalizationService.GetDefaultStatusText(AppearanceKeys.StatusGrayText));
|
|
|
|
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
|
|
};
|
|
|
|
private static bool TryParseNotificationState(string? value, out HealthState state)
|
|
{
|
|
switch ((value ?? string.Empty).Trim().ToLowerInvariant())
|
|
{
|
|
case "green":
|
|
case "gruen":
|
|
case "grün":
|
|
state = HealthState.Green;
|
|
return true;
|
|
|
|
case "yellow":
|
|
case "gelb":
|
|
state = HealthState.Yellow;
|
|
return true;
|
|
|
|
case "red":
|
|
case "rot":
|
|
state = HealthState.Red;
|
|
return true;
|
|
|
|
case "gray":
|
|
case "grey":
|
|
case "black":
|
|
case "grau":
|
|
case "schwarz":
|
|
state = HealthState.Gray;
|
|
return true;
|
|
|
|
default:
|
|
state = default;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private int? ParseActivityDurationMinutes()
|
|
{
|
|
var minutes = (int)Math.Round(ActivityDuration.TotalMinutes, MidpointRounding.AwayFromZero);
|
|
return minutes > 0 ? minutes : null;
|
|
}
|
|
|
|
private void ToggleSymptom(SelectableSymptomItemViewModel? item)
|
|
{
|
|
if (item is null)
|
|
return;
|
|
|
|
item.IsSelected = !item.IsSelected;
|
|
}
|
|
}
|