small bug fixes
This commit is contained in:
+125
-43
@@ -15,6 +15,7 @@ public partial class YearViewModel : ObservableObject
|
||||
private readonly AppearanceService _appearanceService;
|
||||
private readonly CalendarStateService _calendarStateService;
|
||||
private readonly IPdfExportService _pdfExportService;
|
||||
private CancellationTokenSource? _refreshCancellationTokenSource;
|
||||
|
||||
[ObservableProperty]
|
||||
private DateTime selectedDate;
|
||||
@@ -59,60 +60,44 @@ public partial class YearViewModel : ObservableObject
|
||||
|
||||
public async Task RefreshAsync()
|
||||
{
|
||||
var refreshCancellationTokenSource = BeginRefresh();
|
||||
var cancellationToken = refreshCancellationTokenSource.Token;
|
||||
|
||||
SelectedDate = _calendarStateService.SelectedDate.Date;
|
||||
UpdateHeader();
|
||||
|
||||
var yearStart = new DateTime(SelectedDate.Year, 1, 1);
|
||||
var yearEnd = yearStart.AddYears(1);
|
||||
|
||||
var entries = await _healthEntryRepository.GetEntriesBetweenAsync(yearStart, yearEnd);
|
||||
Months.Clear();
|
||||
|
||||
for (var monthNumber = 1; monthNumber <= 12; monthNumber++)
|
||||
try
|
||||
{
|
||||
var monthStart = new DateTime(SelectedDate.Year, monthNumber, 1);
|
||||
var daysInMonth = DateTime.DaysInMonth(monthStart.Year, monthStart.Month);
|
||||
var selectedYear = SelectedDate.Year;
|
||||
var sections = CreateMonthSections(selectedYear);
|
||||
|
||||
var section = new MonthSectionViewModel
|
||||
Months.Clear();
|
||||
foreach (var section in sections)
|
||||
Months.Add(section);
|
||||
|
||||
for (var monthNumber = 1; monthNumber <= sections.Count; monthNumber++)
|
||||
{
|
||||
Title = monthStart.ToString("MMMM")
|
||||
};
|
||||
|
||||
var leadingEmptyCells = GetMondayBasedColumnIndex(monthStart.DayOfWeek);
|
||||
for (var i = 0; i < leadingEmptyCells; i++)
|
||||
section.Cells.Add(CreatePlaceholderCell());
|
||||
|
||||
for (var day = 1; day <= daysInMonth; day++)
|
||||
{
|
||||
var date = new DateTime(monthStart.Year, monthStart.Month, day);
|
||||
var dayEntries = entries.Where(x => x.EntryDate.Date == date.Date).ToList();
|
||||
var worstState = CalculateWorstState(dayEntries);
|
||||
|
||||
section.Cells.Add(new MonthCellViewModel
|
||||
{
|
||||
Date = date,
|
||||
DayNumberText = date.Day.ToString(),
|
||||
IsCurrentMonth = true,
|
||||
IsToday = date.Date == DateTime.Today,
|
||||
HasEntries = dayEntries.Count > 0,
|
||||
BackgroundColor = GetCellBackgroundColor(worstState),
|
||||
TextColor = GetCellTextColor(worstState),
|
||||
BorderColor = date.Date == DateTime.Today
|
||||
? _appearanceService.GetColor(AppearanceKeys.StateYellow, "#B89A42")
|
||||
: _appearanceService.GetColor(AppearanceKeys.Border, "#2A3E55")
|
||||
});
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
await LoadMonthSectionAsync(
|
||||
sections[monthNumber - 1],
|
||||
selectedYear,
|
||||
monthNumber,
|
||||
cancellationToken);
|
||||
}
|
||||
|
||||
while (section.Cells.Count % 7 != 0)
|
||||
section.Cells.Add(CreatePlaceholderCell());
|
||||
|
||||
Months.Add(section);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// Ein neueres Refresh hat diese Aktualisierung ersetzt.
|
||||
}
|
||||
finally
|
||||
{
|
||||
EndRefresh(refreshCancellationTokenSource);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateHeader()
|
||||
{
|
||||
HeaderText = SelectedDate.Year.ToString();
|
||||
HeaderText = DateHelper.ToGermanYear(SelectedDate);
|
||||
}
|
||||
|
||||
private async Task OpenDayAsync(MonthCellViewModel? item)
|
||||
@@ -157,11 +142,108 @@ public partial class YearViewModel : ObservableObject
|
||||
{
|
||||
MainThread.BeginInvokeOnMainThread(async () =>
|
||||
{
|
||||
if (SelectedDate.Date == date.Date)
|
||||
return;
|
||||
|
||||
SelectedDate = date.Date;
|
||||
await RefreshAsync();
|
||||
});
|
||||
}
|
||||
|
||||
private CancellationTokenSource BeginRefresh()
|
||||
{
|
||||
var refreshCancellationTokenSource = new CancellationTokenSource();
|
||||
var previousRefresh = Interlocked.Exchange(ref _refreshCancellationTokenSource, refreshCancellationTokenSource);
|
||||
|
||||
previousRefresh?.Cancel();
|
||||
previousRefresh?.Dispose();
|
||||
|
||||
return refreshCancellationTokenSource;
|
||||
}
|
||||
|
||||
private void EndRefresh(CancellationTokenSource refreshCancellationTokenSource)
|
||||
{
|
||||
if (ReferenceEquals(_refreshCancellationTokenSource, refreshCancellationTokenSource))
|
||||
_refreshCancellationTokenSource = null;
|
||||
|
||||
refreshCancellationTokenSource.Dispose();
|
||||
}
|
||||
|
||||
private List<MonthSectionViewModel> CreateMonthSections(int year)
|
||||
{
|
||||
return Enumerable.Range(1, 12)
|
||||
.Select(monthNumber => new MonthSectionViewModel
|
||||
{
|
||||
Title = new DateTime(year, monthNumber, 1).ToString("MMMM", LocalizationService.GetCurrentCulture()),
|
||||
IsLoading = true
|
||||
})
|
||||
.ToList();
|
||||
}
|
||||
|
||||
private async Task LoadMonthSectionAsync(
|
||||
MonthSectionViewModel section,
|
||||
int year,
|
||||
int monthNumber,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var monthStart = new DateTime(year, monthNumber, 1);
|
||||
var monthEnd = monthStart.AddMonths(1);
|
||||
var entries = await _healthEntryRepository.GetEntriesBetweenAsync(monthStart, monthEnd);
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
var cells = BuildMonthCells(monthStart, entries);
|
||||
|
||||
await MainThread.InvokeOnMainThreadAsync(() =>
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
return;
|
||||
|
||||
section.ReplaceCells(cells);
|
||||
});
|
||||
}
|
||||
|
||||
private List<MonthCellViewModel> BuildMonthCells(DateTime monthStart, IReadOnlyList<HealthEntry> entries)
|
||||
{
|
||||
var cells = new List<MonthCellViewModel>();
|
||||
var daysInMonth = DateTime.DaysInMonth(monthStart.Year, monthStart.Month);
|
||||
var entriesByDay = entries
|
||||
.GroupBy(x => x.EntryDate.Date)
|
||||
.ToDictionary(group => group.Key, group => (IReadOnlyCollection<HealthEntry>)group.ToList());
|
||||
|
||||
var leadingEmptyCells = GetMondayBasedColumnIndex(monthStart.DayOfWeek);
|
||||
for (var i = 0; i < leadingEmptyCells; i++)
|
||||
cells.Add(CreatePlaceholderCell());
|
||||
|
||||
for (var day = 1; day <= daysInMonth; day++)
|
||||
{
|
||||
var date = new DateTime(monthStart.Year, monthStart.Month, day);
|
||||
var dayEntries = entriesByDay.TryGetValue(date.Date, out var matches)
|
||||
? matches
|
||||
: Array.Empty<HealthEntry>();
|
||||
var worstState = CalculateWorstState(dayEntries);
|
||||
|
||||
cells.Add(new MonthCellViewModel
|
||||
{
|
||||
Date = date,
|
||||
DayNumberText = date.Day.ToString(),
|
||||
IsCurrentMonth = true,
|
||||
IsToday = date.Date == DateTime.Today,
|
||||
HasEntries = dayEntries.Count > 0,
|
||||
BackgroundColor = GetCellBackgroundColor(worstState),
|
||||
TextColor = GetCellTextColor(worstState),
|
||||
BorderColor = date.Date == DateTime.Today
|
||||
? _appearanceService.GetColor(AppearanceKeys.StateYellow, "#B89A42")
|
||||
: _appearanceService.GetColor(AppearanceKeys.Border, "#2A3E55")
|
||||
});
|
||||
}
|
||||
|
||||
while (cells.Count % 7 != 0)
|
||||
cells.Add(CreatePlaceholderCell());
|
||||
|
||||
return cells;
|
||||
}
|
||||
|
||||
private Color GetCellBackgroundColor(HealthState? state) => state switch
|
||||
{
|
||||
HealthState.Green => _appearanceService.GetColor(AppearanceKeys.StateGreen, "#4E8B69"),
|
||||
@@ -216,4 +298,4 @@ public partial class YearViewModel : ObservableObject
|
||||
BorderColor = Colors.Transparent
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user