72 lines
2.1 KiB
C#
72 lines
2.1 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
|
|
namespace TimerApp
|
|
{
|
|
public class AppSettings
|
|
{
|
|
public int WorkMinutes { get; set; } = 20;
|
|
public int RestMinutes { get; set; } = 1;
|
|
public int IdleThresholdSeconds { get; set; } = 30;
|
|
public bool IsDarkMode { get; set; } = true;
|
|
|
|
private static string LegacyConfigPath => Path.Combine(AppContext.BaseDirectory, "settings.json");
|
|
|
|
private static string ConfigPath
|
|
{
|
|
get
|
|
{
|
|
string dir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "TimerApp");
|
|
return Path.Combine(dir, "settings.json");
|
|
}
|
|
}
|
|
|
|
private static string EffectiveConfigPath => PortableMode.IsPortable ? LegacyConfigPath : ConfigPath;
|
|
|
|
public static AppSettings Load()
|
|
{
|
|
try
|
|
{
|
|
string path;
|
|
if (PortableMode.IsPortable)
|
|
{
|
|
path = LegacyConfigPath;
|
|
}
|
|
else
|
|
{
|
|
path = File.Exists(ConfigPath) ? ConfigPath : LegacyConfigPath;
|
|
}
|
|
|
|
if (File.Exists(path))
|
|
{
|
|
string json = File.ReadAllText(path);
|
|
return JsonSerializer.Deserialize<AppSettings>(json) ?? new AppSettings();
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// ignore errors, return default
|
|
}
|
|
return new AppSettings();
|
|
}
|
|
|
|
public void Save()
|
|
{
|
|
try
|
|
{
|
|
string path = EffectiveConfigPath;
|
|
string? dir = Path.GetDirectoryName(path);
|
|
if (!string.IsNullOrWhiteSpace(dir))
|
|
Directory.CreateDirectory(dir);
|
|
string json = JsonSerializer.Serialize(this, new JsonSerializerOptions { WriteIndented = true });
|
|
File.WriteAllText(path, json);
|
|
}
|
|
catch
|
|
{
|
|
// ignore
|
|
}
|
|
}
|
|
}
|
|
}
|