Files
ReminderOrganizer/Models/ReminderModels.cs

174 lines
5.1 KiB
C#
Raw Normal View History

#nullable enable
using System;
using System.Collections.ObjectModel;
using ReminderOrganizer.Core;
namespace ReminderOrganizer.Models
{
public class ReminderTask : ViewModelBase
{
private string _id = Guid.NewGuid().ToString();
private string _name = "New Task";
private string _description = "";
private DateTime _reminderDate = DateTime.Now.AddHours(1);
private bool _isNotified = false;
private bool _isRecurring = false;
private RecurrenceType _recurrenceType = RecurrenceType.Yearly;
public string Id
{
get => _id;
set => SetProperty(ref _id, value);
}
public string Name
{
get => _name;
set => SetProperty(ref _name, value);
}
public string Description
{
get => _description;
set => SetProperty(ref _description, value);
}
public DateTime ReminderDate
{
get => _reminderDate;
set => SetProperty(ref _reminderDate, value);
}
public bool IsNotified
{
get => _isNotified;
set => SetProperty(ref _isNotified, value);
}
public bool IsRecurring
{
get => _isRecurring;
set => SetProperty(ref _isRecurring, value);
}
public RecurrenceType RecurrenceType
{
get => _recurrenceType;
set => SetProperty(ref _recurrenceType, value);
}
public DateTime GetNextOccurrence() => GetNextOccurrence(DateTime.Now);
/// <summary>
/// First occurrence after <paramref name="now"/>. Steps are counted from the original
/// date, so "31 Jan monthly" gives 29 Feb, 31 Mar, 30 Apr … instead of drifting to the 28th.
/// </summary>
public DateTime GetNextOccurrence(DateTime now)
{
var start = ReminderDate;
if (start > now) return start;
// Jump close to "now" first so long gaps (app off for years) don't loop thousands of times.
int k = RecurrenceType switch
{
RecurrenceType.Daily => Math.Max(0, (int)((now - start).TotalDays) - 1),
RecurrenceType.Weekly => Math.Max(0, (int)((now - start).TotalDays / 7) - 1),
RecurrenceType.Monthly => Math.Max(0, (now.Year - start.Year) * 12 + now.Month - start.Month - 1),
_ => Math.Max(0, now.Year - start.Year - 1)
};
DateTime next;
do
{
k++;
next = RecurrenceType switch
{
RecurrenceType.Daily => start.AddDays(k),
RecurrenceType.Weekly => start.AddDays(7 * k),
RecurrenceType.Monthly => start.AddMonths(k),
_ => start.AddYears(k)
};
} while (next <= now);
return next;
}
/// <summary>
/// Marks every due task: one-off tasks become notified, recurring ones move to their next date.
/// Returns the tasks that should be shown as a notification now.
/// </summary>
public static System.Collections.Generic.List<ReminderTask> ProcessDue(System.Collections.Generic.IEnumerable<Folder> folders, DateTime now)
{
var due = new System.Collections.Generic.List<ReminderTask>();
foreach (var folder in folders)
{
foreach (var task in folder.Tasks)
{
if (task.IsNotified || task.ReminderDate > now) continue;
due.Add(task);
if (task.IsRecurring)
task.ReminderDate = task.GetNextOccurrence(now);
else
task.IsNotified = true;
}
}
return due;
}
public ReminderTask() { }
public ReminderTask(string name, string description, DateTime reminderDate)
{
Name = name;
Description = description;
ReminderDate = reminderDate;
}
}
public enum RecurrenceType
{
Daily,
Weekly,
Monthly,
Yearly
}
public class Folder : ViewModelBase
{
private string _id = Guid.NewGuid().ToString();
private string _name = "New Folder";
private ObservableCollection<ReminderTask> _tasks = new();
public string Id
{
get => _id;
set => SetProperty(ref _id, value);
}
public string Name
{
get => _name;
set => SetProperty(ref _name, value);
}
public ObservableCollection<ReminderTask> Tasks
{
get => _tasks;
set => SetProperty(ref _tasks, value);
}
public Folder() { }
public Folder(string name)
{
Name = name;
}
}
public class AppData
{
public ObservableCollection<Folder> Folders { get; set; } = new();
public string Language { get; set; } = "en";
public bool IsDarkMode { get; set; } = true;
}
}