Files
ReminderOrganizer/Core/TaskQuery.cs

91 lines
4.0 KiB
C#
Raw Normal View History

#nullable enable
using System;
using System.Collections;
using System.Globalization;
using ReminderOrganizer.Models;
namespace ReminderOrganizer.Core
{
public enum TaskFilterMode { All, Today, Overdue }
/// <summary>Groups in the "Upcoming" view, in display order.</summary>
public enum TaskBucket { Overdue, Today, Tomorrow, ThisWeek, Later, Done }
/// <summary>Pure filter/sort/group rules for the task list.</summary>
public static class TaskQuery
{
public static TaskBucket Bucket(ReminderTask t, DateTime now)
{
if (t.IsNotified) return TaskBucket.Done;
if (t.ReminderDate < now) return TaskBucket.Overdue;
var days = (t.ReminderDate.Date - now.Date).Days;
return days switch
{
0 => TaskBucket.Today,
1 => TaskBucket.Tomorrow,
< 7 => TaskBucket.ThisWeek,
_ => TaskBucket.Later
};
}
/// <summary>Category filter value for "appointments only" (everything that is not an anniversary).</summary>
public const string AppointmentsFilter = "_appointments";
public static bool MatchesCategory(ReminderTask t, string? categoryFilter) => categoryFilter switch
{
null or "" => true,
AppointmentsFilter => !t.IsAnniversary,
_ => t.IsAnniversary && (t.CategoryId ?? Category.BirthdayId) == categoryFilter
};
/// <summary>Group key for month grouping: first day of the month.</summary>
public static DateTime MonthKey(ReminderTask t) => new(t.ReminderDate.Year, t.ReminderDate.Month, 1);
public static bool Matches(ReminderTask t, string? search, TaskFilterMode mode, bool hideDone, DateTime now, string? categoryFilter = null)
{
if (hideDone && t.IsNotified) return false;
if (!MatchesCategory(t, categoryFilter)) return false;
switch (mode)
{
case TaskFilterMode.Today when t.ReminderDate.Date != now.Date: return false;
case TaskFilterMode.Overdue when t.IsNotified || t.ReminderDate >= now: return false;
}
if (string.IsNullOrWhiteSpace(search)) return true;
var s = search.Trim();
return Contains(t.Name, s) || Contains(t.Description, s) || Contains(t.FolderName, s) || Contains(t.Category?.Name, s);
}
private static bool Contains(string? text, string s) =>
text != null && CultureInfo.CurrentCulture.CompareInfo.IndexOf(text, s, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace) >= 0;
/// <summary>Sort: bucket (overdue, today, … done), then date, then name.</summary>
public sealed class Comparer : IComparer
{
private readonly Func<DateTime> _now;
private readonly bool _chronological;
/// <param name="chronological">true = plain date order (month grouping), false = overdue/today/…/done.</param>
public Comparer(Func<DateTime>? now = null, bool chronological = false)
{
_now = now ?? (() => DateTime.Now);
_chronological = chronological;
}
public int Compare(object? x, object? y)
{
if (x is not ReminderTask a || y is not ReminderTask b) return 0;
if (_chronological)
{
int d = a.ReminderDate.CompareTo(b.ReminderDate);
return d != 0 ? d : string.Compare(a.Name, b.Name, StringComparison.CurrentCultureIgnoreCase);
}
var now = _now();
int c = Bucket(a, now).CompareTo(Bucket(b, now));
if (c != 0) return c;
c = a.IsNotified ? b.ReminderDate.CompareTo(a.ReminderDate) // done: newest first
: a.ReminderDate.CompareTo(b.ReminderDate);
return c != 0 ? c : string.Compare(a.Name, b.Name, StringComparison.CurrentCultureIgnoreCase);
}
}
}
}