You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.3 KiB

4 days ago
using System.Globalization;
using System.IO;
using Housing.Models;
namespace Housing.Services;
public static class InspectionSettingsStore
{
public static InspectionJudgementSettings Load(string filePath)
{
if (!File.Exists(filePath))
{
var defaultSettings = new InspectionJudgementSettings();
Save(filePath, defaultSettings);
return defaultSettings;
}
var values = IniFile.LoadSection(filePath, "Judgement");
return new InspectionJudgementSettings
{
VMin = GetDecimal(values, "VMin", 0.00m),
VMax = GetDecimal(values, "VMax", 9.99m),
AMin = GetDecimal(values, "AMin", 0.00m),
AMax = GetDecimal(values, "AMax", 9.99m),
RMin = GetOptionalDecimal(values, "RMin"),
RMax = GetOptionalDecimal(values, "RMax")
};
}
public static void Save(string filePath, InspectionJudgementSettings settings)
{
var lines = new[]
{
"[Judgement]",
$"VMin={Format(settings.VMin)}",
$"VMax={Format(settings.VMax)}",
$"AMin={Format(settings.AMin)}",
$"AMax={Format(settings.AMax)}",
$"RMin={(settings.RMin.HasValue ? Format(settings.RMin.Value) : string.Empty)}",
$"RMax={(settings.RMax.HasValue ? Format(settings.RMax.Value) : string.Empty)}"
};
File.WriteAllLines(filePath, lines);
}
private static decimal GetDecimal(Dictionary<string, string> values, string key, decimal defaultValue)
{
if (!values.TryGetValue(key, out var value))
{
return defaultValue;
}
return decimal.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out var result)
? result
: defaultValue;
}
private static string Format(decimal value)
{
return value.ToString("0.############################", CultureInfo.InvariantCulture);
}
private static decimal? GetOptionalDecimal(Dictionary<string, string> values, string key)
{
return values.TryGetValue(key, out var text) &&
decimal.TryParse(text, NumberStyles.Number, CultureInfo.InvariantCulture, out var value)
? value : null;
}
}