diff --git a/Platforms/Android/AndroidManifest.xml b/Platforms/Android/AndroidManifest.xml index d1ba07f..f9736d3 100644 --- a/Platforms/Android/AndroidManifest.xml +++ b/Platforms/Android/AndroidManifest.xml @@ -4,4 +4,5 @@ + \ No newline at end of file diff --git a/Platforms/Android/AndroidNotificationScheduler.cs b/Platforms/Android/AndroidNotificationScheduler.cs index a28388b..213ea54 100644 --- a/Platforms/Android/AndroidNotificationScheduler.cs +++ b/Platforms/Android/AndroidNotificationScheduler.cs @@ -1,5 +1,6 @@ using Android.App; using Android.Content; +using Android.Provider; using AndroidX.Core.App; using cfs_tracker.Data; using cfs_tracker.Helpers; @@ -54,12 +55,29 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler public async Task EnsurePermissionAsync() { - var status = await Permissions.CheckStatusAsync(); - if (status == PermissionStatus.Granted) + var notificationStatus = await Permissions.CheckStatusAsync(); + if (notificationStatus != PermissionStatus.Granted) + { + notificationStatus = await Permissions.RequestAsync(); + if (notificationStatus != PermissionStatus.Granted) + return false; + } + + if (!OperatingSystem.IsAndroidVersionAtLeast(31)) return true; - status = await Permissions.RequestAsync(); - return status == PermissionStatus.Granted; + var context = Platform.AppContext; + var alarmManager = (AlarmManager?)context.GetSystemService(Context.AlarmService); + + if (alarmManager is null || alarmManager.CanScheduleExactAlarms()) + return true; + + var intent = new Intent(Settings.ActionRequestScheduleExactAlarm); + intent.SetData(global::Android.Net.Uri.Parse($"package:{context.PackageName}")); + intent.AddFlags(ActivityFlags.NewTask); + context.StartActivity(intent); + + return false; } public Task GetVibrationEnabledAsync() => @@ -78,8 +96,8 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler CancelAllScheduled(context); - var granted = await EnsurePermissionAsync(); - if (!granted) + var notificationStatus = await Permissions.CheckStatusAsync(); + if (notificationStatus != PermissionStatus.Granted) return; SaveActionLabelsToPreferences(); @@ -143,10 +161,19 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler var when = triggerAt.ToUnixTimeMilliseconds(); + if (CanUseExactAlarms(context, alarmManager)) + { + alarmManager.SetExactAndAllowWhileIdle(AlarmType.RtcWakeup, when, pendingIntent); + return; + } + if (OperatingSystem.IsAndroidVersionAtLeast(23)) + { alarmManager.SetAndAllowWhileIdle(AlarmType.RtcWakeup, when, pendingIntent); - else - alarmManager.Set(AlarmType.RtcWakeup, when, pendingIntent); + return; + } + + alarmManager.Set(AlarmType.RtcWakeup, when, pendingIntent); } internal static void ShowNotification( @@ -293,6 +320,14 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler Preferences.Default.Remove(PrefScheduledIds); } + private static bool CanUseExactAlarms(Context context, AlarmManager alarmManager) + { + if (!OperatingSystem.IsAndroidVersionAtLeast(31)) + return true; + + return alarmManager.CanScheduleExactAlarms(); + } + private static PendingIntentFlags BuildPendingIntentFlags() { var flags = PendingIntentFlags.UpdateCurrent; @@ -352,4 +387,4 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler if (flags.HasFlag(NotificationWeekdays.Saturday)) yield return DayOfWeek.Saturday; if (flags.HasFlag(NotificationWeekdays.Sunday)) yield return DayOfWeek.Sunday; } -} +} \ No newline at end of file diff --git a/Resources/AppIcon/appicon.svg b/Resources/AppIcon/appicon.svg index 477eb9b..a7b66ef 100644 --- a/Resources/AppIcon/appicon.svg +++ b/Resources/AppIcon/appicon.svg @@ -1 +1 @@ -C \ No newline at end of file +Cfs \ No newline at end of file diff --git a/Services/PdfExportService.cs b/Services/PdfExportService.cs index 806aa2f..95f3bee 100644 --- a/Services/PdfExportService.cs +++ b/Services/PdfExportService.cs @@ -107,10 +107,10 @@ public sealed class PdfExportService : IPdfExportService var currentDay = weekStart.AddDays(i).Date; var dayEntries = entries.Where(x => x.EntryDate.Date == currentDay).OrderBy(x => x.Timestamp).ToList(); - var averageState = CalculateAverageState(dayEntries); + var worstState = CalculateWorstState(dayEntries); var statusText = dayEntries.Count == 0 ? "—" - : $"{GetStateText(averageState!.Value)} ({dayEntries.Count})"; + : $"{GetStateText(worstState!.Value)} ({dayEntries.Count})"; var activities = dayEntries .Where(x => x.ActivityId.HasValue) @@ -128,8 +128,8 @@ public sealed class PdfExportService : IPdfExportService sb.AppendLine(""); sb.AppendLine($"{Encode(currentDay.ToString("ddd, dd.MM.yyyy"))}"); sb.AppendLine($"{dayEntries.Count}"); - sb.AppendLine(averageState.HasValue - ? StateCell(averageState.Value, statusText) + sb.AppendLine(worstState.HasValue + ? StateCell(worstState.Value, statusText) : "—"); sb.AppendLine($"{Encode(activities.Count > 0 ? string.Join(", ", activities) : "—")}"); sb.AppendLine($"{Encode(symptoms.Count > 0 ? string.Join(", ", symptoms) : "—")}"); @@ -175,10 +175,10 @@ public sealed class PdfExportService : IPdfExportService var currentDay = new DateTime(year, month, day); var dayEntries = entries.Where(x => x.EntryDate.Date == currentDay.Date).OrderBy(x => x.Timestamp).ToList(); - var averageState = CalculateAverageState(dayEntries); + var worstState = CalculateWorstState(dayEntries); var statusText = dayEntries.Count == 0 ? "—" - : GetStateText(averageState!.Value); + : GetStateText(worstState!.Value); var activities = dayEntries .Where(x => x.ActivityId.HasValue) @@ -196,8 +196,8 @@ public sealed class PdfExportService : IPdfExportService sb.AppendLine(""); sb.AppendLine($"{Encode(currentDay.ToString("dd.MM."))}"); sb.AppendLine($"{dayEntries.Count}"); - sb.AppendLine(averageState.HasValue - ? StateCellCompact(averageState.Value, statusText) + sb.AppendLine(worstState.HasValue + ? StateCellCompact(worstState.Value, statusText) : "—"); sb.AppendLine($"{Encode(activities.Count > 0 ? string.Join(", ", activities) : "—")}"); sb.AppendLine($"{Encode(symptoms.Count > 0 ? string.Join(", ", symptoms) : "—")}"); @@ -236,11 +236,11 @@ public sealed class PdfExportService : IPdfExportService .ToList(); var monthDate = new DateTime(year, month, 1); - var averageState = CalculateAverageState(monthEntries); + var worstState = CalculateWorstState(monthEntries); var statusText = monthEntries.Count == 0 ? "—" - : $"{GetStateText(averageState!.Value)} ({monthEntries.Count})"; + : $"{GetStateText(worstState!.Value)} ({monthEntries.Count})"; var activities = monthEntries .Where(x => x.ActivityId.HasValue) @@ -258,8 +258,8 @@ public sealed class PdfExportService : IPdfExportService sb.AppendLine(""); sb.AppendLine($"{Encode(monthDate.ToString("MMMM"))}"); sb.AppendLine($"{monthEntries.Count}"); - sb.AppendLine(averageState.HasValue - ? StateCell(averageState.Value, statusText) + sb.AppendLine(worstState.HasValue + ? StateCell(worstState.Value, statusText) : "—"); sb.AppendLine($"{Encode(activities.Count > 0 ? string.Join(", ", activities) : "—")}"); sb.AppendLine($"{Encode(symptoms.Count > 0 ? string.Join(", ", symptoms) : "—")}"); @@ -384,32 +384,23 @@ public sealed class PdfExportService : IPdfExportService return date.Date.AddDays(-diff); } - private static HealthState? CalculateAverageState(IReadOnlyCollection entries) + private static HealthState? CalculateWorstState(IReadOnlyCollection entries) { if (entries.Count == 0) return null; - var average = entries.Average(x => StateToNumber(x.State)); - var rounded = (int)Math.Round(average, MidpointRounding.AwayFromZero); + if (entries.Any(x => x.State == HealthState.Gray)) + return HealthState.Gray; - return rounded switch - { - <= 0 => HealthState.Green, - 1 => HealthState.Yellow, - 2 => HealthState.Red, - _ => HealthState.Gray - }; + if (entries.Any(x => x.State == HealthState.Red)) + return HealthState.Red; + + if (entries.Any(x => x.State == HealthState.Yellow)) + return HealthState.Yellow; + + return HealthState.Green; } - private static int StateToNumber(HealthState state) => state switch - { - HealthState.Green => 0, - HealthState.Yellow => 1, - HealthState.Red => 2, - HealthState.Gray => 3, - _ => 0 - }; - private static string ToCssHex(Color color) { var r = (int)Math.Round(color.Red * 255); diff --git a/ViewModels/MonthCellViewModel.cs b/ViewModels/MonthCellViewModel.cs index db7bf47..7e9e749 100644 --- a/ViewModels/MonthCellViewModel.cs +++ b/ViewModels/MonthCellViewModel.cs @@ -9,12 +9,11 @@ public partial class MonthCellViewModel : ObservableObject public bool IsCurrentMonth { get; init; } public bool IsToday { get; init; } public bool HasEntries { get; init; } - public string AverageStateText { get; init; } = string.Empty; - public Color StateColor { get; init; } = Colors.Transparent; + + public Color BackgroundColor { get; init; } = Colors.Transparent; public Color TextColor { get; init; } = Colors.White; public Color BorderColor { get; init; } = Colors.Transparent; public bool IsPlaceholder => !IsCurrentMonth; public bool ShowDayNumber => IsCurrentMonth; - public bool ShowAverage => IsCurrentMonth && HasEntries; -} +} \ No newline at end of file diff --git a/ViewModels/MonthViewModel.cs b/ViewModels/MonthViewModel.cs index 977417d..4c8098d 100644 --- a/ViewModels/MonthViewModel.cs +++ b/ViewModels/MonthViewModel.cs @@ -1,11 +1,11 @@ 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 Microsoft.Maui.ApplicationModel; using System.Collections.ObjectModel; -using System.Linq; namespace cfs_tracker.ViewModels; @@ -13,7 +13,6 @@ public partial class MonthViewModel : ObservableObject { private readonly HealthEntryRepository _healthEntryRepository; private readonly AppearanceService _appearanceService; - private readonly AggregationService _aggregationService; private readonly CalendarStateService _calendarStateService; private readonly IPdfExportService _pdfExportService; @@ -34,13 +33,11 @@ public partial class MonthViewModel : ObservableObject public MonthViewModel( HealthEntryRepository healthEntryRepository, AppearanceService appearanceService, - AggregationService aggregationService, CalendarStateService calendarStateService, IPdfExportService pdfExportService) { _healthEntryRepository = healthEntryRepository; _appearanceService = appearanceService; - _aggregationService = aggregationService; _calendarStateService = calendarStateService; _pdfExportService = pdfExportService; @@ -80,7 +77,7 @@ public partial class MonthViewModel : ObservableObject { var date = new DateTime(monthStart.Year, monthStart.Month, day); var dayEntries = entries.Where(x => x.EntryDate.Date == date.Date).ToList(); - var average = _aggregationService.CalculateAverage(dayEntries); + var worstState = CalculateWorstState(dayEntries); Cells.Add(new MonthCellViewModel { @@ -89,9 +86,8 @@ public partial class MonthViewModel : ObservableObject IsCurrentMonth = true, IsToday = date.Date == DateTime.Today, HasEntries = dayEntries.Count > 0, - AverageStateText = _aggregationService.ToDisplayText(average, _appearanceService), - StateColor = _appearanceService.GetColor(average.State.ToAppearanceColorKey(), "#1F242B"), - TextColor = _appearanceService.GetColor(AppearanceKeys.TextPrimary, "#DCE7F3"), + BackgroundColor = GetCellBackgroundColor(worstState), + TextColor = GetCellTextColor(worstState), BorderColor = date.Date == DateTime.Today ? _appearanceService.GetColor(AppearanceKeys.StateYellow, "#B89A42") : _appearanceService.GetColor(AppearanceKeys.Border, "#2A3E55") @@ -154,11 +150,46 @@ public partial class MonthViewModel : ObservableObject }); } + private Color GetCellBackgroundColor(HealthState? state) => state switch + { + HealthState.Green => _appearanceService.GetColor(AppearanceKeys.StateGreen, "#4E8B69"), + HealthState.Yellow => _appearanceService.GetColor(AppearanceKeys.StateYellow, "#B89A42"), + HealthState.Red => _appearanceService.GetColor(AppearanceKeys.StateRed, "#A85C5C"), + HealthState.Gray => _appearanceService.GetColor(AppearanceKeys.StateGray, "#1F242B"), + _ => _appearanceService.GetColor(AppearanceKeys.SurfaceBackgroundAlt, "#1F242B") + }; + + private Color GetCellTextColor(HealthState? state) => state switch + { + HealthState.Yellow => Colors.Black, + HealthState.Green => Colors.White, + HealthState.Red => Colors.White, + HealthState.Gray => Colors.White, + _ => _appearanceService.GetColor(AppearanceKeys.TextPrimary, "#DCE7F3") + }; + private static int GetMondayBasedColumnIndex(DayOfWeek dayOfWeek) { return dayOfWeek == DayOfWeek.Sunday ? 6 : ((int)dayOfWeek - 1); } + private static HealthState? CalculateWorstState(IReadOnlyCollection entries) + { + if (entries.Count == 0) + return null; + + if (entries.Any(x => x.State == HealthState.Gray)) + return HealthState.Gray; + + if (entries.Any(x => x.State == HealthState.Red)) + return HealthState.Red; + + if (entries.Any(x => x.State == HealthState.Yellow)) + return HealthState.Yellow; + + return HealthState.Green; + } + private static MonthCellViewModel CreatePlaceholderCell() { return new MonthCellViewModel @@ -168,10 +199,9 @@ public partial class MonthViewModel : ObservableObject IsCurrentMonth = false, IsToday = false, HasEntries = false, - AverageStateText = string.Empty, - StateColor = Colors.Transparent, + BackgroundColor = Colors.Transparent, TextColor = Colors.Transparent, BorderColor = Colors.Transparent }; } -} +} \ No newline at end of file diff --git a/ViewModels/NotificationScheduleItemViewModel.cs b/ViewModels/NotificationScheduleItemViewModel.cs index cb35128..9c4236a 100644 --- a/ViewModels/NotificationScheduleItemViewModel.cs +++ b/ViewModels/NotificationScheduleItemViewModel.cs @@ -35,6 +35,9 @@ public partial class NotificationScheduleItemViewModel : ObservableObject [ObservableProperty] private bool sunday; + public string IsEnabledButtonText => IsEnabled ? "Aktiv" : "Inaktiv"; + + public IRelayCommand ToggleIsEnabledCommand { get; } public IRelayCommand ToggleMondayCommand { get; } public IRelayCommand ToggleTuesdayCommand { get; } public IRelayCommand ToggleWednesdayCommand { get; } @@ -45,6 +48,7 @@ public partial class NotificationScheduleItemViewModel : ObservableObject public NotificationScheduleItemViewModel() { + ToggleIsEnabledCommand = new RelayCommand(() => IsEnabled = !IsEnabled); ToggleMondayCommand = new RelayCommand(() => Monday = !Monday); ToggleTuesdayCommand = new RelayCommand(() => Tuesday = !Tuesday); ToggleWednesdayCommand = new RelayCommand(() => Wednesday = !Wednesday); @@ -54,6 +58,11 @@ public partial class NotificationScheduleItemViewModel : ObservableObject ToggleSundayCommand = new RelayCommand(() => Sunday = !Sunday); } + partial void OnIsEnabledChanged(bool value) + { + OnPropertyChanged(nameof(IsEnabledButtonText)); + } + public int ToWeekdayMask() { var mask = NotificationWeekdays.None; diff --git a/ViewModels/OptionsViewModel.cs b/ViewModels/OptionsViewModel.cs index 6dbc6fe..062d40c 100644 --- a/ViewModels/OptionsViewModel.cs +++ b/ViewModels/OptionsViewModel.cs @@ -58,6 +58,8 @@ public partial class OptionsViewModel : ObservableObject public bool IsNotificationsTabSelected => SelectedTab == OptionsTab.Notifications; public bool IsDataManagementTabSelected => SelectedTab == OptionsTab.DataManagement; + public string NotificationVibrationButtonText => NotificationVibrationEnabled ? "Aktiv" : "Inaktiv"; + public IAsyncRelayCommand DeleteAllDataCommand { get; } public IAsyncRelayCommand SaveAppearanceCommand { get; } public IAsyncRelayCommand ResetAppearanceCommand { get; } @@ -74,6 +76,7 @@ public partial class OptionsViewModel : ObservableObject public IAsyncRelayCommand DeleteScheduleCommand { get; } public IAsyncRelayCommand SaveDatabaseLocationCommand { get; } public IAsyncRelayCommand ClearExportsCommand { get; } + public IRelayCommand ToggleNotificationVibrationCommand { get; } public IRelayCommand ShowAppearanceTabCommand { get; } public IRelayCommand ShowNotificationsTabCommand { get; } public IRelayCommand ShowDataManagementTabCommand { get; } @@ -118,6 +121,11 @@ public partial class OptionsViewModel : ObservableObject SaveDatabaseLocationCommand = new AsyncRelayCommand(SaveDatabaseLocationAsync); ClearExportsCommand = new AsyncRelayCommand(ClearExportsAsync); + ToggleNotificationVibrationCommand = new RelayCommand(() => + { + NotificationVibrationEnabled = !NotificationVibrationEnabled; + }); + ShowAppearanceTabCommand = new RelayCommand(() => SelectedTab = OptionsTab.Appearance); ShowNotificationsTabCommand = new RelayCommand(() => SelectedTab = OptionsTab.Notifications); ShowDataManagementTabCommand = new RelayCommand(() => SelectedTab = OptionsTab.DataManagement); @@ -135,6 +143,11 @@ public partial class OptionsViewModel : ObservableObject OnPropertyChanged(nameof(IsDataManagementTabSelected)); } + partial void OnNotificationVibrationEnabledChanged(bool value) + { + OnPropertyChanged(nameof(NotificationVibrationButtonText)); + } + public async Task LoadAsync() { await LoadAppearanceAsync(); @@ -447,7 +460,10 @@ public partial class OptionsViewModel : ObservableObject var granted = await _notificationScheduler.EnsurePermissionAsync(); if (!granted) { - StatusText = "Benachrichtigungsberechtigung wurde nicht erlaubt."; + StatusText = OperatingSystem.IsAndroidVersionAtLeast(31) + ? "Bitte Benachrichtigungen und exakte Alarme erlauben und danach erneut speichern." + : "Benachrichtigungsberechtigung wurde nicht erlaubt."; + return; } @@ -530,4 +546,4 @@ public partial class OptionsViewModel : ObservableObject Notifications, DataManagement } -} +} \ No newline at end of file diff --git a/ViewModels/WeekViewModel.cs b/ViewModels/WeekViewModel.cs index 0db4e3b..f8926a0 100644 --- a/ViewModels/WeekViewModel.cs +++ b/ViewModels/WeekViewModel.cs @@ -85,17 +85,20 @@ public partial class WeekViewModel : ObservableObject var date = WeekStart.AddDays(i); var dayEntries = entries.Where(x => x.EntryDate.Date == date.Date).ToList(); - var morningAverage = _aggregationService.CalculateAverage(_aggregationService.FilterForDayPart(dayEntries, DayPart.Morning)); - var afternoonAverage = _aggregationService.CalculateAverage(_aggregationService.FilterForDayPart(dayEntries, DayPart.Afternoon)); + var morningEntries = _aggregationService.FilterForDayPart(dayEntries, DayPart.Morning).ToList(); + var afternoonEntries = _aggregationService.FilterForDayPart(dayEntries, DayPart.Afternoon).ToList(); + + var morningWorstState = CalculateWorstState(morningEntries); + var afternoonWorstState = CalculateWorstState(afternoonEntries); Days.Add(new WeekDaySummaryViewModel { Date = date, DateText = DateHelper.ToGermanShortDay(date), - MorningText = _aggregationService.ToDisplayText(morningAverage, _appearanceService), - AfternoonText = _aggregationService.ToDisplayText(afternoonAverage, _appearanceService), - MorningColor = _appearanceService.GetColor(morningAverage.State.ToAppearanceColorKey(), "#1F242B"), - AfternoonColor = _appearanceService.GetColor(afternoonAverage.State.ToAppearanceColorKey(), "#1F242B"), + MorningText = GetStateText(morningWorstState), + AfternoonText = GetStateText(afternoonWorstState), + MorningColor = GetStateColor(morningWorstState), + AfternoonColor = GetStateColor(afternoonWorstState), ActivitiesText = _aggregationService.BuildDistinctActivityText(dayEntries, activityMap), SymptomsText = _aggregationService.BuildDistinctSymptomText(dayEntries, symptomsMap) }); @@ -137,6 +140,7 @@ public partial class WeekViewModel : ObservableObject _calendarStateService.SelectedDate = SelectedDate; await RefreshAsync(); } + private async Task TodayAsync() { var today = DateTime.Today; @@ -153,4 +157,37 @@ public partial class WeekViewModel : ObservableObject await RefreshAsync(); }); } + + private string GetStateText(HealthState? state) => state switch + { + HealthState.Green => _appearanceService.GetText(AppearanceKeys.StatusGreenText, "Grün"), + HealthState.Yellow => _appearanceService.GetText(AppearanceKeys.StatusYellowText, "Gelb"), + HealthState.Red => _appearanceService.GetText(AppearanceKeys.StatusRedText, "Rot"), + HealthState.Gray => _appearanceService.GetText(AppearanceKeys.StatusGrayText, "Schwarz"), + _ => "-" + }; + + private Color GetStateColor(HealthState? state) + { + return state.HasValue + ? _appearanceService.GetColor(state.Value.ToAppearanceColorKey(), "#1F242B") + : Colors.Transparent; + } + + private static HealthState? CalculateWorstState(IReadOnlyCollection entries) + { + if (entries.Count == 0) + return null; + + if (entries.Any(x => x.State == HealthState.Gray)) + return HealthState.Gray; + + if (entries.Any(x => x.State == HealthState.Red)) + return HealthState.Red; + + if (entries.Any(x => x.State == HealthState.Yellow)) + return HealthState.Yellow; + + return HealthState.Green; + } } \ No newline at end of file diff --git a/ViewModels/YearViewModel.cs b/ViewModels/YearViewModel.cs index 96bbf38..0b75601 100644 --- a/ViewModels/YearViewModel.cs +++ b/ViewModels/YearViewModel.cs @@ -1,11 +1,11 @@ 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 Microsoft.Maui.ApplicationModel; using System.Collections.ObjectModel; -using System.Linq; namespace cfs_tracker.ViewModels; @@ -13,7 +13,6 @@ public partial class YearViewModel : ObservableObject { private readonly HealthEntryRepository _healthEntryRepository; private readonly AppearanceService _appearanceService; - private readonly AggregationService _aggregationService; private readonly CalendarStateService _calendarStateService; private readonly IPdfExportService _pdfExportService; @@ -34,13 +33,11 @@ public partial class YearViewModel : ObservableObject public YearViewModel( HealthEntryRepository healthEntryRepository, AppearanceService appearanceService, - AggregationService aggregationService, CalendarStateService calendarStateService, IPdfExportService pdfExportService) { _healthEntryRepository = healthEntryRepository; _appearanceService = appearanceService; - _aggregationService = aggregationService; _calendarStateService = calendarStateService; _pdfExportService = pdfExportService; @@ -78,7 +75,7 @@ public partial class YearViewModel : ObservableObject var section = new MonthSectionViewModel { - Title = DateHelper.ToGermanMonthYear(monthStart) + Title = monthStart.ToString("MMMM") }; var leadingEmptyCells = GetMondayBasedColumnIndex(monthStart.DayOfWeek); @@ -89,7 +86,7 @@ public partial class YearViewModel : ObservableObject { var date = new DateTime(monthStart.Year, monthStart.Month, day); var dayEntries = entries.Where(x => x.EntryDate.Date == date.Date).ToList(); - var average = _aggregationService.CalculateAverage(dayEntries); + var worstState = CalculateWorstState(dayEntries); section.Cells.Add(new MonthCellViewModel { @@ -98,9 +95,8 @@ public partial class YearViewModel : ObservableObject IsCurrentMonth = true, IsToday = date.Date == DateTime.Today, HasEntries = dayEntries.Count > 0, - AverageStateText = _aggregationService.ToDisplayText(average, _appearanceService), - StateColor = _appearanceService.GetColor(average.State.ToAppearanceColorKey(), "#1F242B"), - TextColor = _appearanceService.GetColor(AppearanceKeys.TextPrimary, "#DCE7F3"), + BackgroundColor = GetCellBackgroundColor(worstState), + TextColor = GetCellTextColor(worstState), BorderColor = date.Date == DateTime.Today ? _appearanceService.GetColor(AppearanceKeys.StateYellow, "#B89A42") : _appearanceService.GetColor(AppearanceKeys.Border, "#2A3E55") @@ -116,7 +112,7 @@ public partial class YearViewModel : ObservableObject private void UpdateHeader() { - HeaderText = DateHelper.ToGermanYear(SelectedDate); + HeaderText = SelectedDate.Year.ToString(); } private async Task OpenDayAsync(MonthCellViewModel? item) @@ -166,11 +162,46 @@ public partial class YearViewModel : ObservableObject }); } + private Color GetCellBackgroundColor(HealthState? state) => state switch + { + HealthState.Green => _appearanceService.GetColor(AppearanceKeys.StateGreen, "#4E8B69"), + HealthState.Yellow => _appearanceService.GetColor(AppearanceKeys.StateYellow, "#B89A42"), + HealthState.Red => _appearanceService.GetColor(AppearanceKeys.StateRed, "#A85C5C"), + HealthState.Gray => _appearanceService.GetColor(AppearanceKeys.StateGray, "#1F242B"), + _ => _appearanceService.GetColor(AppearanceKeys.SurfaceBackgroundAlt, "#1F242B") + }; + + private Color GetCellTextColor(HealthState? state) => state switch + { + HealthState.Yellow => Colors.Black, + HealthState.Green => Colors.White, + HealthState.Red => Colors.White, + HealthState.Gray => Colors.White, + _ => _appearanceService.GetColor(AppearanceKeys.TextPrimary, "#DCE7F3") + }; + private static int GetMondayBasedColumnIndex(DayOfWeek dayOfWeek) { return dayOfWeek == DayOfWeek.Sunday ? 6 : ((int)dayOfWeek - 1); } + private static HealthState? CalculateWorstState(IReadOnlyCollection entries) + { + if (entries.Count == 0) + return null; + + if (entries.Any(x => x.State == HealthState.Gray)) + return HealthState.Gray; + + if (entries.Any(x => x.State == HealthState.Red)) + return HealthState.Red; + + if (entries.Any(x => x.State == HealthState.Yellow)) + return HealthState.Yellow; + + return HealthState.Green; + } + private static MonthCellViewModel CreatePlaceholderCell() { return new MonthCellViewModel @@ -180,10 +211,9 @@ public partial class YearViewModel : ObservableObject IsCurrentMonth = false, IsToday = false, HasEntries = false, - AverageStateText = string.Empty, - StateColor = Colors.Transparent, + BackgroundColor = Colors.Transparent, TextColor = Colors.Transparent, BorderColor = Colors.Transparent }; } -} +} \ No newline at end of file diff --git a/Views/MonthPage.xaml b/Views/MonthPage.xaml index fd5cb2a..ec1243f 100644 --- a/Views/MonthPage.xaml +++ b/Views/MonthPage.xaml @@ -8,96 +8,90 @@ Title="Monat" BackgroundColor="{DynamicResource AppBackground}"> - - - - +