#nullable enable using System; using System.IO; using System.Text.Json; using System.Threading.Tasks; using ReminderOrganizer.Models; namespace ReminderOrganizer.Services { public class DataService : IDataService { private static readonly JsonSerializerOptions _jsonOptions = new() { WriteIndented = true, PropertyNameCaseInsensitive = true }; public string DataFilePath { get; } public DataService() { DataFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tasks.json"); } public async Task LoadAsync() { if (!File.Exists(DataFilePath)) return new AppData(); try { var json = await File.ReadAllTextAsync(DataFilePath); var data = JsonSerializer.Deserialize(json, _jsonOptions); return data ?? new AppData(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"[DataService] Load failed: {ex.Message}"); return new AppData(); } } public async Task SaveAsync(AppData data) { try { var json = JsonSerializer.Serialize(data, _jsonOptions); await File.WriteAllTextAsync(DataFilePath, json); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"[DataService] Save failed: {ex.Message}"); throw; } } } }