- Task dialog: Appointment | Birthday switch. Birthday = name + "19.03." or "19.03.1986" (no time), live preview with weekday, age and days left - Birthday folders open the birthday form; yearly tasks in such folders are migrated once - Birthdays show "19. Maerz" instead of date + time; every task shows "in 15 days" / "3 days ago" (red when overdue) - View switch: list, compact (one line), cards (calendar tiles), persisted - ICS: birthdays as all-day events - 59 tests Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
93 lines
3.9 KiB
C#
93 lines
3.9 KiB
C#
#nullable enable
|
|
using System;
|
|
using System.Globalization;
|
|
using System.Text;
|
|
using ReminderOrganizer.Models;
|
|
|
|
namespace ReminderOrganizer.Services
|
|
{
|
|
/// <summary>iCalendar (.ics) export for Outlook, Google Calendar, Apple Calendar.</summary>
|
|
public static class IcsExport
|
|
{
|
|
public static string Build(AppData data, DateTime utcNow)
|
|
{
|
|
var sb = new StringBuilder();
|
|
void Line(string s) => sb.Append(Fold(s)).Append("\r\n");
|
|
|
|
Line("BEGIN:VCALENDAR");
|
|
Line("VERSION:2.0");
|
|
Line("PRODID:-//Michael Media//Reminder Organizer//EN");
|
|
Line("CALSCALE:GREGORIAN");
|
|
var stamp = utcNow.ToUniversalTime().ToString("yyyyMMdd'T'HHmmss'Z'", CultureInfo.InvariantCulture);
|
|
|
|
foreach (var folder in data.Folders)
|
|
{
|
|
foreach (var t in folder.Tasks)
|
|
{
|
|
if (t.IsNotified) continue;
|
|
Line("BEGIN:VEVENT");
|
|
Line($"UID:{t.Id}@reminder-organizer");
|
|
Line($"DTSTAMP:{stamp}");
|
|
if (t.IsBirthday)
|
|
{
|
|
// all-day event
|
|
Line($"DTSTART;VALUE=DATE:{t.ReminderDate.ToString("yyyyMMdd", CultureInfo.InvariantCulture)}");
|
|
}
|
|
else
|
|
{
|
|
// floating local time: shows at the same clock time in every calendar
|
|
Line($"DTSTART:{t.ReminderDate.ToString("yyyyMMdd'T'HHmmss", CultureInfo.InvariantCulture)}");
|
|
Line("DURATION:PT30M");
|
|
}
|
|
var summary = t.UpcomingAge is int age ? $"{t.Name} ({age})" : t.Name;
|
|
Line($"SUMMARY:{Escape(summary)}");
|
|
if (!string.IsNullOrWhiteSpace(t.Description)) Line($"DESCRIPTION:{Escape(t.Description)}");
|
|
Line($"CATEGORIES:{Escape(folder.Name)}");
|
|
if (t.IsRecurring)
|
|
{
|
|
var freq = t.RecurrenceType switch
|
|
{
|
|
RecurrenceType.Daily => "DAILY",
|
|
RecurrenceType.Weekly => "WEEKLY",
|
|
RecurrenceType.Monthly => "MONTHLY",
|
|
_ => "YEARLY"
|
|
};
|
|
Line($"RRULE:FREQ={freq}");
|
|
}
|
|
Line("BEGIN:VALARM");
|
|
Line("ACTION:DISPLAY");
|
|
Line($"DESCRIPTION:{Escape(t.Name)}");
|
|
// all-day events start at midnight: remind at 09:00 (same day or lead days before)
|
|
Line(t.IsBirthday
|
|
? (t.LeadDays > 0 ? $"TRIGGER:-P{t.LeadDays - 1}DT15H" : "TRIGGER:PT9H")
|
|
: (t.LeadDays > 0 ? $"TRIGGER:-P{t.LeadDays}D" : "TRIGGER:PT0M"));
|
|
Line("END:VALARM");
|
|
Line("END:VEVENT");
|
|
}
|
|
}
|
|
Line("END:VCALENDAR");
|
|
return sb.ToString();
|
|
}
|
|
|
|
public static string Escape(string s) =>
|
|
s.Replace("\\", "\\\\").Replace(";", "\\;").Replace(",", "\\,").Replace("\r\n", "\\n").Replace("\n", "\\n").Replace("\r", "");
|
|
|
|
/// <summary>RFC 5545: lines longer than 75 octets continue with CRLF + space.</summary>
|
|
public static string Fold(string line)
|
|
{
|
|
var bytes = Encoding.UTF8.GetByteCount(line);
|
|
if (bytes <= 75) return line;
|
|
var sb = new StringBuilder();
|
|
int count = 0;
|
|
foreach (var ch in line)
|
|
{
|
|
int n = Encoding.UTF8.GetByteCount(ch.ToString());
|
|
if (count + n > 75) { sb.Append("\r\n "); count = 1; }
|
|
sb.Append(ch);
|
|
count += n;
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
}
|
|
}
|