using System; using System.IO; using System.Text.Json; using Notify.Models; using Notify.Serialization; namespace Notify.Services; /// /// 加载/保存弹窗设置,并在变更时通知订阅者 /// public sealed class SettingsService { private static readonly string Dir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ClaudeCodeNotify"); private static readonly string FilePath = Path.Combine(Dir, "settings.json"); public ToastSettings Current { get; private set; } = new(); /// /// 设置被保存后触发 /// public event Action? Changed; public void Load() { try { if (File.Exists(FilePath)) { var json = File.ReadAllText(FilePath); var loaded = JsonSerializer.Deserialize(json, AppJsonContext.Default.ToastSettings); if (loaded is not null) { Current = loaded; } } } catch { // 配置损坏则回退到默认值,不影响保活 Current = new ToastSettings(); } } public void Save(ToastSettings settings) { Current = settings; try { Directory.CreateDirectory(Dir); var json = JsonSerializer.Serialize(settings, AppJsonContext.Default.ToastSettings); File.WriteAllText(FilePath, json); } catch { // 落盘失败不致命,内存里仍生效 } Changed?.Invoke(Current); } }