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>
This commit is contained in:
@@ -9,6 +9,26 @@ namespace ReminderOrganizer.Services
|
||||
private const string RegistryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
|
||||
private const string AppName = "ReminderOrganizer";
|
||||
|
||||
/// <summary>Registry value: quoted exe path + --minimized (start in tray).</summary>
|
||||
public static string BuildCommand(string exePath) => $"\"{exePath}\" --minimized";
|
||||
|
||||
/// <summary>True if the stored command points at this exe (old entries without quotes count too).</summary>
|
||||
public static bool PointsTo(string? stored, string? exePath)
|
||||
{
|
||||
if (string.IsNullOrEmpty(stored) || string.IsNullOrEmpty(exePath)) return false;
|
||||
var s = stored.Trim();
|
||||
if (s.StartsWith("\""))
|
||||
{
|
||||
var end = s.IndexOf('"', 1);
|
||||
s = end > 0 ? s.Substring(1, end - 1) : s.Trim('"');
|
||||
}
|
||||
else if (s.EndsWith(" --minimized"))
|
||||
{
|
||||
s = s[..^" --minimized".Length];
|
||||
}
|
||||
return string.Equals(s, exePath, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
public bool IsAutostartEnabled
|
||||
{
|
||||
get
|
||||
@@ -16,8 +36,7 @@ namespace ReminderOrganizer.Services
|
||||
try
|
||||
{
|
||||
using var key = Registry.CurrentUser.OpenSubKey(RegistryKey, false);
|
||||
var path = key?.GetValue(AppName) as string;
|
||||
return path == Environment.ProcessPath;
|
||||
return PointsTo(key?.GetValue(AppName) as string, Environment.ProcessPath);
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -42,7 +61,7 @@ namespace ReminderOrganizer.Services
|
||||
var path = Environment.ProcessPath;
|
||||
if (!string.IsNullOrEmpty(path))
|
||||
{
|
||||
key.SetValue(AppName, path);
|
||||
key.SetValue(AppName, BuildCommand(path));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,11 +2,17 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using ReminderOrganizer.Models;
|
||||
|
||||
namespace ReminderOrganizer.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// Stores tasks in %APPDATA%\ReminderOrganizer\tasks.json.
|
||||
/// Writes are serialized and atomic (temp file + replace), a corrupt file is
|
||||
/// backed up instead of being overwritten.
|
||||
/// </summary>
|
||||
public class DataService : IDataService
|
||||
{
|
||||
private static readonly JsonSerializerOptions _jsonOptions = new()
|
||||
@@ -15,11 +21,38 @@ namespace ReminderOrganizer.Services
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
|
||||
private readonly SemaphoreSlim _writeLock = new(1, 1);
|
||||
|
||||
public string DataFilePath { get; }
|
||||
|
||||
public DataService()
|
||||
/// <summary>%APPDATA%\ReminderOrganizer – or REMINDER_ORGANIZER_DATA (for tests/screenshots).</summary>
|
||||
public static string DefaultDataDirectory =>
|
||||
Environment.GetEnvironmentVariable("REMINDER_ORGANIZER_DATA") is { Length: > 0 } custom
|
||||
? custom
|
||||
: Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ReminderOrganizer");
|
||||
|
||||
public DataService() : this(DefaultDataDirectory) { }
|
||||
|
||||
public DataService(string dataDirectory)
|
||||
{
|
||||
DataFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tasks.json");
|
||||
Directory.CreateDirectory(dataDirectory);
|
||||
DataFilePath = Path.Combine(dataDirectory, "tasks.json");
|
||||
}
|
||||
|
||||
/// <summary>Copies tasks.json from an old location (next to the exe) if no data exists yet.</summary>
|
||||
public void MigrateFrom(string? legacyDirectory)
|
||||
{
|
||||
if (string.IsNullOrEmpty(legacyDirectory) || File.Exists(DataFilePath)) return;
|
||||
var legacy = Path.Combine(legacyDirectory, "tasks.json");
|
||||
try
|
||||
{
|
||||
if (File.Exists(legacy) && !string.Equals(Path.GetFullPath(legacy), Path.GetFullPath(DataFilePath), StringComparison.OrdinalIgnoreCase))
|
||||
File.Copy(legacy, DataFilePath);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"[DataService] Migration failed: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<AppData> LoadAsync()
|
||||
@@ -36,22 +69,43 @@ namespace ReminderOrganizer.Services
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"[DataService] Load failed: {ex.Message}");
|
||||
BackupCorruptFile();
|
||||
return new AppData();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SaveAsync(AppData data)
|
||||
private void BackupCorruptFile()
|
||||
{
|
||||
try
|
||||
{
|
||||
var backup = DataFilePath + $".corrupt-{DateTime.Now:yyyyMMdd-HHmmss}";
|
||||
File.Copy(DataFilePath, backup, overwrite: true);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
public async Task SaveAsync(AppData data)
|
||||
{
|
||||
await _writeLock.WaitAsync();
|
||||
try
|
||||
{
|
||||
var json = JsonSerializer.Serialize(data, _jsonOptions);
|
||||
await File.WriteAllTextAsync(DataFilePath, json);
|
||||
var tmp = DataFilePath + ".tmp";
|
||||
await File.WriteAllTextAsync(tmp, json);
|
||||
if (File.Exists(DataFilePath))
|
||||
File.Replace(tmp, DataFilePath, DataFilePath + ".bak");
|
||||
else
|
||||
File.Move(tmp, DataFilePath);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"[DataService] Save failed: {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_writeLock.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,15 +2,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using ReminderOrganizer.Models;
|
||||
using ReminderOrganizer.Services;
|
||||
|
||||
namespace ReminderOrganizer.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// Loads UI strings from Locales/<lang>.json, embedded into the exe
|
||||
/// (resource name "Locales.<lang>.json"). Missing keys fall back to English, then to the key.
|
||||
/// </summary>
|
||||
public class LocalizationService : ILocalizationService
|
||||
{
|
||||
public static readonly string[] SupportedLanguages = { "en", "de" };
|
||||
|
||||
private Dictionary<string, string> _strings = new();
|
||||
private Dictionary<string, string> _fallback = new();
|
||||
private string _currentLanguage = "en";
|
||||
private AppData? _appData;
|
||||
private readonly IDataService _dataService;
|
||||
@@ -22,23 +29,31 @@ namespace ReminderOrganizer.Services
|
||||
public LocalizationService(IDataService dataService)
|
||||
{
|
||||
_dataService = dataService;
|
||||
LoadLanguage(_currentLanguage);
|
||||
_fallback = LoadStrings("en");
|
||||
_strings = _fallback;
|
||||
}
|
||||
|
||||
public void SetLanguage(string languageCode)
|
||||
{
|
||||
if (Array.IndexOf(SupportedLanguages, languageCode) < 0) languageCode = "en";
|
||||
if (_currentLanguage == languageCode) return;
|
||||
_currentLanguage = languageCode;
|
||||
LoadLanguage(languageCode);
|
||||
_strings = LoadStrings(languageCode);
|
||||
if (_appData != null)
|
||||
{
|
||||
_appData.Language = languageCode;
|
||||
_ = _dataService.SaveAsync(_appData);
|
||||
_ = SaveQuietly(_appData);
|
||||
}
|
||||
LanguageChanged?.Invoke(this, EventArgs.Empty);
|
||||
StringsChanged?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
private async System.Threading.Tasks.Task SaveQuietly(AppData data)
|
||||
{
|
||||
try { await _dataService.SaveAsync(data); }
|
||||
catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"[Localization] Save failed: {ex.Message}"); }
|
||||
}
|
||||
|
||||
public void Initialize(AppData appData)
|
||||
{
|
||||
_appData = appData;
|
||||
@@ -50,87 +65,27 @@ namespace ReminderOrganizer.Services
|
||||
|
||||
public string GetString(string key)
|
||||
{
|
||||
return _strings.TryGetValue(key, out var value) ? value : key;
|
||||
if (_strings.TryGetValue(key, out var value)) return value;
|
||||
return _fallback.TryGetValue(key, out var fb) ? fb : key;
|
||||
}
|
||||
|
||||
private void LoadLanguage(string languageCode)
|
||||
public static Dictionary<string, string> LoadStrings(string languageCode)
|
||||
{
|
||||
var basePath = AppDomain.CurrentDomain.BaseDirectory;
|
||||
var filePath = Path.Combine(basePath, "Locales", $"{languageCode}.json");
|
||||
|
||||
if (File.Exists(filePath))
|
||||
try
|
||||
{
|
||||
try
|
||||
var asm = Assembly.GetExecutingAssembly();
|
||||
using var stream = asm.GetManifestResourceStream($"Locales.{languageCode}.json");
|
||||
if (stream != null)
|
||||
{
|
||||
var json = File.ReadAllText(filePath);
|
||||
_strings = JsonSerializer.Deserialize<Dictionary<string, string>>(json) ?? new();
|
||||
return;
|
||||
using var reader = new StreamReader(stream);
|
||||
return JsonSerializer.Deserialize<Dictionary<string, string>>(reader.ReadToEnd()) ?? new();
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
_strings = GetDefaultStrings(languageCode);
|
||||
}
|
||||
|
||||
private static Dictionary<string, string> GetDefaultStrings(string lang)
|
||||
{
|
||||
return lang switch
|
||||
catch (Exception ex)
|
||||
{
|
||||
"en" => new Dictionary<string, string>
|
||||
{
|
||||
["AppTitle"] = "Reminder Organizer",
|
||||
["File"] = "File",
|
||||
["Exit"] = "Exit",
|
||||
["Settings"] = "Settings",
|
||||
["Language"] = "Language",
|
||||
["German"] = "German",
|
||||
["English"] = "English",
|
||||
["AddFolder"] = "+ Folder",
|
||||
["AddTask"] = "+ Task",
|
||||
["Welcome"] = "Welcome",
|
||||
["NoFolderSelected"] = "No folder selected",
|
||||
["Name"] = "Name",
|
||||
["Description"] = "Description",
|
||||
["Date"] = "Date",
|
||||
["Save"] = "Save",
|
||||
["Cancel"] = "Cancel",
|
||||
["OK"] = "OK",
|
||||
["SelectFolder"] = "Please select a folder first",
|
||||
["AutostartEnabled"] = "Autostart enabled",
|
||||
["AutostartDisabled"] = "Autostart disabled",
|
||||
["SaveError"] = "Error saving data",
|
||||
["Open"] = "Open",
|
||||
["Delete"] = "Delete",
|
||||
["Rename"] = "Rename"
|
||||
},
|
||||
_ => new Dictionary<string, string>
|
||||
{
|
||||
["AppTitle"] = "Reminder Organizer",
|
||||
["File"] = "Datei",
|
||||
["Exit"] = "Beenden",
|
||||
["Settings"] = "Einstellungen",
|
||||
["Language"] = "Sprache",
|
||||
["German"] = "Deutsch",
|
||||
["English"] = "Englisch",
|
||||
["AddFolder"] = "+ Ordner",
|
||||
["AddTask"] = "+ Task",
|
||||
["Welcome"] = "Willkommen",
|
||||
["NoFolderSelected"] = "Kein Ordner ausgewählt",
|
||||
["Name"] = "Name",
|
||||
["Description"] = "Beschreibung",
|
||||
["Date"] = "Datum",
|
||||
["Save"] = "Speichern",
|
||||
["Cancel"] = "Abbrechen",
|
||||
["OK"] = "OK",
|
||||
["SelectFolder"] = "Bitte erst einen Ordner auswählen",
|
||||
["AutostartEnabled"] = "Autostart aktiviert",
|
||||
["AutostartDisabled"] = "Autostart deaktiviert",
|
||||
["SaveError"] = "Fehler beim Speichern",
|
||||
["Open"] = "Öffnen",
|
||||
["Delete"] = "Löschen",
|
||||
["Rename"] = "Umbenennen"
|
||||
}
|
||||
};
|
||||
System.Diagnostics.Debug.WriteLine($"[Localization] Load {languageCode} failed: {ex.Message}");
|
||||
}
|
||||
return new();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,8 +30,9 @@ namespace ReminderOrganizer.Services
|
||||
{
|
||||
if (SharedTrayIcon != null)
|
||||
{
|
||||
SharedTrayIcon.BalloonTipTitle = title;
|
||||
SharedTrayIcon.BalloonTipText = message;
|
||||
// ShowBalloonTip throws on empty text – tasks without description would never notify.
|
||||
SharedTrayIcon.BalloonTipTitle = string.IsNullOrWhiteSpace(title) ? "Reminder Organizer" : title;
|
||||
SharedTrayIcon.BalloonTipText = string.IsNullOrWhiteSpace(message) ? " " : message;
|
||||
SharedTrayIcon.BalloonTipIcon = ToolTipIcon.Info;
|
||||
SharedTrayIcon.ShowBalloonTip(5000);
|
||||
System.Diagnostics.Debug.WriteLine("[NotificationService] BalloonTip shown!");
|
||||
|
||||
80
Services/TaskCsv.cs
Normal file
80
Services/TaskCsv.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
#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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user