Files
ReminderOrganizer/Services/TaskCsv.cs
Michi 1918712f7f Fix data loss risk, reminder bugs; theme, credits, tests
- Store tasks in %APPDATA% (single-file extract dir lost data on update),
  atomic serialized saves, corrupt-file backup, migration of old tasks.json
- Reminders without description never showed (empty balloon text threw)
- Catch up missed reminders on start; monthly recurrence no longer drifts
- Task dialog: date pick kept 00:00, cancel leaked recurrence changes
- CSV import understands quoted fields written by export
- Confirm before deleting; name folders on create; tasks sorted by date
- Single instance, autostart starts hidden in tray, tray icon disposed
- Theme helper for all dialogs, MDL2/Path icons instead of emojis,
  fixed folder selection/badge, localized counts and messages
- Credits (c) Michael Media + Discord link, exe Company/Copyright
- Locales embedded; xUnit tests (24); README, CLAUDE.md, sprite-generator

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 14:08:53 +01:00

81 lines
3.3 KiB
C#
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#nullable enable
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using ReminderOrganizer.Core;
using ReminderOrganizer.Models;
namespace ReminderOrganizer.Services
{
/// <summary>CSV format: Folder;Name;Description;Date(yyyy-MM-dd HH:mm);IsRecurring;RecurrenceType</summary>
public static class TaskCsv
{
public const string Header = "Folder;Name;Description;Date;IsRecurring;RecurrenceType";
public const string DateFormat = "yyyy-MM-dd HH:mm";
public static string Export(AppData data)
{
var sb = new StringBuilder();
sb.Append(Header).Append("\r\n");
foreach (var folder in data.Folders)
{
foreach (var task in folder.Tasks)
{
sb.Append(string.Join(Csv.Separator,
Csv.Escape(folder.Name),
Csv.Escape(task.Name),
Csv.Escape(task.Description),
task.ReminderDate.ToString(DateFormat, CultureInfo.InvariantCulture),
task.IsRecurring.ToString(),
task.RecurrenceType.ToString())).Append("\r\n");
}
}
return sb.ToString();
}
/// <summary>Adds the rows to <paramref name="data"/> (new folders are created). Returns (imported, skipped).</summary>
public static (int Imported, int Skipped) Import(string text, AppData data)
{
int imported = 0, skipped = 0;
var folderMap = new Dictionary<string, Folder>();
foreach (var f in data.Folders) folderMap.TryAdd(f.Name, f);
var rows = Csv.Parse(text.TrimStart(''));
foreach (var parts in rows.Skip(1))
{
if (parts.All(string.IsNullOrWhiteSpace)) continue;
if (parts.Count < 4) { skipped++; continue; }
var folderName = parts[0].Trim();
var name = parts[1].Trim();
var description = parts[2].Trim();
if (string.IsNullOrWhiteSpace(folderName) || string.IsNullOrWhiteSpace(name) ||
!DateTime.TryParseExact(parts[3].Trim(), DateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out var date))
{
skipped++;
continue;
}
if (!folderMap.TryGetValue(folderName, out var target))
{
target = new Folder(folderName);
data.Folders.Add(target);
folderMap[folderName] = target;
}
var task = new ReminderTask(name, description, date);
if (parts.Count >= 5 && bool.TryParse(parts[4].Trim(), out var rec)) task.IsRecurring = rec;
if (parts.Count >= 6 && Enum.TryParse<RecurrenceType>(parts[5].Trim(), out var type)) task.RecurrenceType = type;
// Past one-off dates would fire immediately on import – treat them as already done.
if (!task.IsRecurring && date <= DateTime.Now) task.IsNotified = true;
target.Tasks.Add(task);
imported++;
}
return (imported, skipped);
}
}
}