2026-06-11 16:23:02 +02:00
|
|
|
using System.Text.Json;
|
|
|
|
|
|
|
|
|
|
namespace PalladiumWallet.Core.Storage;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2026-06-16 14:40:06 +02:00
|
|
|
/// Global application configuration (blueprint §8), separate from wallet
|
|
|
|
|
/// files: language, display unit, etc. Persisted in config.json
|
|
|
|
|
/// in the data root (applies to all networks).
|
2026-06-11 16:23:02 +02:00
|
|
|
/// </summary>
|
|
|
|
|
public sealed class AppConfig
|
|
|
|
|
{
|
2026-06-16 14:40:06 +02:00
|
|
|
/// <summary>UI language code.</summary>
|
2026-06-11 18:41:47 +02:00
|
|
|
public string Language { get; set; } = "en";
|
2026-06-11 16:23:02 +02:00
|
|
|
|
2026-06-16 14:40:06 +02:00
|
|
|
/// <summary>Display unit for amounts (see <see cref="Wallet.CoinAmount.Units"/>).</summary>
|
2026-06-11 16:23:02 +02:00
|
|
|
public string Unit { get; set; } = "PLM";
|
|
|
|
|
|
2026-06-18 19:34:36 +02:00
|
|
|
/// <summary>Last successfully connected server, persisted across sessions.</summary>
|
|
|
|
|
public string? LastServerHost { get; set; }
|
|
|
|
|
public int? LastServerPort { get; set; }
|
|
|
|
|
public bool LastServerUseSsl { get; set; } = true;
|
|
|
|
|
|
2026-06-11 16:23:02 +02:00
|
|
|
private static readonly JsonSerializerOptions JsonOptions = new() { WriteIndented = true };
|
|
|
|
|
|
|
|
|
|
public static AppConfig Load(string? path = null)
|
|
|
|
|
{
|
|
|
|
|
path ??= AppPaths.ConfigPath();
|
|
|
|
|
if (!File.Exists(path))
|
|
|
|
|
return new AppConfig();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return JsonSerializer.Deserialize<AppConfig>(File.ReadAllText(path)) ?? new AppConfig();
|
|
|
|
|
}
|
|
|
|
|
catch (JsonException)
|
|
|
|
|
{
|
2026-06-16 14:40:06 +02:00
|
|
|
// Corrupted config: fall back to defaults without blocking startup.
|
2026-06-11 16:23:02 +02:00
|
|
|
return new AppConfig();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Save(string? path = null)
|
|
|
|
|
{
|
|
|
|
|
path ??= AppPaths.ConfigPath();
|
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
|
|
|
|
|
File.WriteAllText(path, JsonSerializer.Serialize(this, JsonOptions));
|
|
|
|
|
}
|
|
|
|
|
}
|