mirror of
https://github.com/denglihong2007/CRSim
synced 2026-08-13 04:55:43 +08:00
@@ -1,26 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CRSim.Core.Converters
|
||||
{
|
||||
public class TimeSpanConverter : JsonConverter<TimeSpan>
|
||||
{
|
||||
public override void Write(Utf8JsonWriter writer, TimeSpan value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteNumberValue(value.TotalMinutes); // 以分钟为单位进行序列化
|
||||
}
|
||||
|
||||
// 反序列化:将数值转换回 TimeSpan 类型
|
||||
public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
// 这里假设 JSON 存储的是分钟的数值
|
||||
double minutes = reader.GetDouble();
|
||||
return TimeSpan.FromMinutes(minutes);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,22 +5,17 @@ namespace CRSim.Core.Models
|
||||
{
|
||||
public class Settings
|
||||
{
|
||||
[JsonConverter(typeof(TimeSpanConverter))]
|
||||
public TimeSpan TimeOffset { get; set; } = TimeSpan.Zero;
|
||||
|
||||
[JsonConverter(typeof(TimeSpanConverter))]
|
||||
public TimeSpan DepartureCheckInAdvanceDuration { get; set; } = TimeSpan.FromMinutes(20);
|
||||
|
||||
[JsonConverter(typeof(TimeSpanConverter))]
|
||||
public TimeSpan PassingCheckInAdvanceDuration { get; set; } = TimeSpan.FromMinutes(10);
|
||||
|
||||
/// <summary>
|
||||
/// 直到发车前多久停止显示
|
||||
/// </summary>
|
||||
[JsonConverter(typeof(TimeSpanConverter))]
|
||||
public TimeSpan StopDisplayUntilDepartureDuration { get; set; } = TimeSpan.FromMinutes(1);
|
||||
|
||||
[JsonConverter(typeof(TimeSpanConverter))]
|
||||
public TimeSpan StopCheckInAdvanceDuration { get; set; } = TimeSpan.FromMinutes(2);
|
||||
|
||||
public int MaxPages { get; set; } = 3;
|
||||
|
||||
@@ -1,38 +1,46 @@
|
||||
using CRSim.Core.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.Win32;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CRSim.Core.Services
|
||||
{
|
||||
public class SettingsService : ISettingsService
|
||||
{
|
||||
private readonly string _settingsFilePath;
|
||||
private Settings _settings;
|
||||
private readonly JsonSerializerOptions options = new() { WriteIndented = true };
|
||||
public SettingsService(string settingsFilePath)
|
||||
private RegistryKey _key = Registry.CurrentUser.OpenSubKey(@"Software\CRSim\Settings",true);
|
||||
public SettingsService()
|
||||
{
|
||||
_settingsFilePath = settingsFilePath;
|
||||
LoadSettings();
|
||||
}
|
||||
public void SaveSettings()
|
||||
{
|
||||
string jsonString = JsonSerializer.Serialize(_settings, options);
|
||||
File.WriteAllText(_settingsFilePath, jsonString);
|
||||
_key.SetValue("TimeOffset", (int)_settings.TimeOffset.TotalMinutes);
|
||||
_key.SetValue("SwitchPageSeconds", _settings.SwitchPageSeconds);
|
||||
_key.SetValue("MaxPages", _settings.MaxPages);
|
||||
_key.SetValue("StopCheckInAdvanceDuration", (int)_settings.StopCheckInAdvanceDuration.TotalMinutes);
|
||||
_key.SetValue("StopDisplayUntilDepartureDuration", (int)_settings.StopDisplayUntilDepartureDuration.TotalMinutes);
|
||||
_key.SetValue("PassingCheckInAdvanceDuration", (int)_settings.PassingCheckInAdvanceDuration.TotalMinutes);
|
||||
_key.SetValue("DepartureCheckInAdvanceDuration", (int)_settings.DepartureCheckInAdvanceDuration.TotalMinutes);
|
||||
}
|
||||
private void LoadSettings()
|
||||
{
|
||||
if (File.Exists(_settingsFilePath))
|
||||
if (_key==null)
|
||||
{
|
||||
string jsonString = File.ReadAllText(_settingsFilePath);
|
||||
_settings = JsonSerializer.Deserialize<Settings>(jsonString);
|
||||
Registry.CurrentUser.CreateSubKey(@"Software\CRSim\Settings");
|
||||
_key = Registry.CurrentUser.OpenSubKey(@"Software\CRSim\Settings",true);
|
||||
_settings = new Settings();
|
||||
SaveSettings();
|
||||
}
|
||||
else
|
||||
{
|
||||
_settings = new Settings();
|
||||
_settings.TimeOffset = TimeSpan.FromMinutes((int)_key.GetValue("TimeOffset"));
|
||||
_settings.SwitchPageSeconds = (int)_key.GetValue("SwitchPageSeconds");
|
||||
_settings.MaxPages = (int)_key.GetValue("MaxPages");
|
||||
_settings.StopCheckInAdvanceDuration = TimeSpan.FromMinutes((int)_key.GetValue("StopCheckInAdvanceDuration"));
|
||||
_settings.StopDisplayUntilDepartureDuration = TimeSpan.FromMinutes((int)_key.GetValue("StopDisplayUntilDepartureDuration"));
|
||||
_settings.PassingCheckInAdvanceDuration = TimeSpan.FromMinutes((int)_key.GetValue("PassingCheckInAdvanceDuration"));
|
||||
_settings.DepartureCheckInAdvanceDuration = TimeSpan.FromMinutes((int)_key.GetValue("DepartureCheckInAdvanceDuration"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
private static readonly IHost _host = Host.CreateDefaultBuilder()
|
||||
.ConfigureServices((context, services) =>
|
||||
{
|
||||
services.AddSingleton<ISettingsService,SettingsService>(sp => new SettingsService("app.config"));
|
||||
services.AddSingleton<ISettingsService,SettingsService>();
|
||||
services.AddSingleton<ITimeService, TimeService>();
|
||||
services.AddSingleton<INavigationService, NavigationService>();
|
||||
services.AddSingleton<IDialogService, DialogService>();
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
<LangVersion>13.0</LangVersion>
|
||||
<StartupObject>CRSim.App</StartupObject>
|
||||
<ApplicationIcon>Assets\CRSimIcon.ico</ApplicationIcon>
|
||||
<Version>1.2.4.0</Version>
|
||||
<Version>1.2.5.0</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user