Initial commit: ReminderOrganizer WPF app
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
57
Services/DataService.cs
Normal file
57
Services/DataService.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
#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<AppData> LoadAsync()
|
||||
{
|
||||
if (!File.Exists(DataFilePath))
|
||||
return new AppData();
|
||||
|
||||
try
|
||||
{
|
||||
var json = await File.ReadAllTextAsync(DataFilePath);
|
||||
var data = JsonSerializer.Deserialize<AppData>(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user