58 lines
1.7 KiB
C#
58 lines
1.7 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(AppDomain.CurrentDomain.BaseDirectory, "settings.json");
|
|
|
|
private static string ConfigPath
|
|
{
|
|
get
|
|
{
|
|
string dir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "TimerApp");
|
|
return Path.Combine(dir, "settings.json");
|
|
}
|
|
}
|
|
|
|
public static AppSettings Load()
|
|
{
|
|
try
|
|
{
|
|
string 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
|
|
{
|
|
Directory.CreateDirectory(Path.GetDirectoryName(ConfigPath)!);
|
|
string json = JsonSerializer.Serialize(this, new JsonSerializerOptions { WriteIndented = true });
|
|
File.WriteAllText(ConfigPath, json);
|
|
}
|
|
catch
|
|
{
|
|
// ignore
|
|
}
|
|
}
|
|
}
|
|
}
|