Files
ReminderOrganizer/Core/DailySummary.cs

36 lines
1.7 KiB
C#
Raw Normal View History

#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using ReminderOrganizer.Models;
namespace ReminderOrganizer.Core
{
/// <summary>Content of the morning summary. Null when there is nothing worth showing.</summary>
public sealed record DailySummaryData(
IReadOnlyList<ReminderTask> Today,
IReadOnlyList<ReminderTask> Overdue,
IReadOnlyList<ReminderTask> AnniversariesSoon);
public static class DailySummary
{
/// <summary>Days ahead in which anniversaries are announced (tomorrow … in 3 days).</summary>
public const int AnniversaryDays = 3;
public static DailySummaryData? Build(IEnumerable<ReminderTask> tasks, DateTime now)
{
var open = tasks.Where(t => !t.IsNotified).ToList();
var today = open.Where(t => t.ReminderDate.Date == now.Date).OrderBy(t => t.ReminderDate).ToList();
var overdue = open.Where(t => !t.IsRecurring && t.ReminderDate.Date < now.Date).OrderBy(t => t.ReminderDate).ToList();
var soon = open.Where(t => t.IsAnniversary)
.Where(t => (t.ReminderDate.Date - now.Date).Days is > 0 and <= AnniversaryDays)
.OrderBy(t => t.ReminderDate).ToList();
return today.Count + overdue.Count + soon.Count == 0 ? null : new DailySummaryData(today, overdue, soon);
}
/// <summary>Should the summary be shown now? Once per day, from <paramref name="hour"/> on.</summary>
public static bool IsDue(DateTime now, int hour, DateTime? lastShown) =>
now.Hour >= hour && (lastShown == null || lastShown.Value.Date < now.Date);
}
}