2024-02-28 02:44:21 +00:00
|
|
|
|
using System;
|
|
|
|
|
using MSFSPopoutPanelManager.DomainModel.DataFile;
|
2023-07-12 22:41:31 +00:00
|
|
|
|
using MSFSPopoutPanelManager.DomainModel.Setting;
|
|
|
|
|
using MSFSPopoutPanelManager.Shared;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
namespace MSFSPopoutPanelManager.Orchestration
|
|
|
|
|
{
|
|
|
|
|
public class AppSettingDataManager
|
|
|
|
|
{
|
|
|
|
|
private const string APP_SETTING_DATA_FILENAME = "appsettingdata.json";
|
|
|
|
|
|
|
|
|
|
public static ApplicationSetting ReadAppSetting()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine("Reading application settings data file...");
|
|
|
|
|
|
2024-02-28 02:44:21 +00:00
|
|
|
|
using var reader = new StreamReader(Path.Combine(FileIo.GetUserDataFilePath(), APP_SETTING_DATA_FILENAME));
|
|
|
|
|
var fileContent = reader.ReadToEnd();
|
2023-07-12 22:41:31 +00:00
|
|
|
|
|
2024-02-28 02:44:21 +00:00
|
|
|
|
return JsonConvert.DeserializeObject<AppSettingFile>(fileContent).ApplicationSetting;
|
2023-07-12 22:41:31 +00:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void WriteAppSetting(ApplicationSetting appSetting)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine("Saving application settings data file...");
|
|
|
|
|
|
2024-02-28 02:44:21 +00:00
|
|
|
|
var dataFilePath = FileIo.GetUserDataFilePath();
|
2023-07-12 22:41:31 +00:00
|
|
|
|
|
2024-02-28 02:44:21 +00:00
|
|
|
|
if (string.IsNullOrEmpty(dataFilePath))
|
|
|
|
|
throw new Exception("Unable to get app setting data file path.");
|
2023-07-12 22:41:31 +00:00
|
|
|
|
|
2024-02-28 02:44:21 +00:00
|
|
|
|
if (!Directory.Exists(dataFilePath))
|
|
|
|
|
Directory.CreateDirectory(dataFilePath);
|
2023-07-12 22:41:31 +00:00
|
|
|
|
|
2024-02-28 02:44:21 +00:00
|
|
|
|
using var file = File.CreateText(Path.Combine(dataFilePath, APP_SETTING_DATA_FILENAME));
|
|
|
|
|
var serializer = new JsonSerializer();
|
|
|
|
|
serializer.Serialize(file, new AppSettingFile { ApplicationSetting = appSetting });
|
2023-07-12 22:41:31 +00:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2024-02-28 02:44:21 +00:00
|
|
|
|
FileLogger.WriteLog($"Unable to write app setting data file: {APP_SETTING_DATA_FILENAME}", StatusMessageType.Error);
|
2023-07-12 22:41:31 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|