Save and load report PDF data to json

This commit is contained in:
2026-02-16 18:39:19 +09:00
parent 833e97fbea
commit b4d09c0d8b
5 changed files with 131 additions and 30 deletions
+21 -5
View File
@@ -12,14 +12,18 @@ namespace ReceiptPDFBuilder.Models;
class PDFReport : ChangeNotifier
{
private string _baseFolder;
private string _name;
private string _title;
private List<ReportFile> _files;
private DateTime _lastSaved;
private DateTime? _lastGenerated;
public PDFReport()
{
_baseFolder = "";
_name = "";
_title = "";
_files = [];
_lastSaved = DateTime.Now;
_lastGenerated = null;
}
public string BaseFolder
@@ -28,10 +32,10 @@ class PDFReport : ChangeNotifier
set { _baseFolder = value; NotifyPropertyChanged(); }
}
public string Name
public string Title
{
get => _name;
set { _name = value; NotifyPropertyChanged(); }
get => _title;
set { _title = value; NotifyPropertyChanged(); }
}
public List<ReportFile> Files
@@ -39,4 +43,16 @@ class PDFReport : ChangeNotifier
get => _files;
set { _files = value; NotifyPropertyChanged(); }
}
public DateTime LastSaved
{
get => _lastSaved;
set { _lastSaved = value; NotifyPropertyChanged(); }
}
public DateTime? LastGenerated
{
get => _lastGenerated;
set { _lastGenerated = value; NotifyPropertyChanged(); }
}
}
+4 -13
View File
@@ -5,6 +5,7 @@ using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using ReceiptPDFBuilder.Helpers;
using ReceiptPDFBuilders.Helpers;
namespace ReceiptPDFBuilder.Models;
@@ -42,19 +43,9 @@ class Settings : ChangeNotifier
return Path.Combine(path, GetSettingsFileName());
}
private static JsonSerializerOptions GetSerializerOptions()
{
var opts = new JsonSerializerOptions
{
WriteIndented = false,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
};
return opts;
}
public async Task<string> SaveSettingsAsync()
{
var jsonContext = new SourceGenerationContext(GetSerializerOptions());
var jsonContext = new SourceGenerationContext(Utilities.GetSerializerOptions());
using MemoryStream memoryStream = new MemoryStream();
await JsonSerializer.SerializeAsync(memoryStream, this, jsonContext.Settings);
memoryStream.Position = 0;
@@ -72,14 +63,14 @@ class Settings : ChangeNotifier
return new Settings();
}
var json = File.ReadAllText(GetSettingsPath());
var jsonContext = new SourceGenerationContext(GetSerializerOptions());
var jsonContext = new SourceGenerationContext(Utilities.GetSerializerOptions());
return JsonSerializer.Deserialize<Settings>(json, jsonContext.Settings) ?? new Settings();
}
public static async Task<Settings> LoadSettingsAsync()
{
using FileStream fileStream = File.OpenRead(GetSettingsPath());
var jsonContext = new SourceGenerationContext(GetSerializerOptions());
var jsonContext = new SourceGenerationContext(Utilities.GetSerializerOptions());
var output = await JsonSerializer.DeserializeAsync<Settings>(fileStream, jsonContext.Settings) ?? new Settings();
return output;
}