ChineseChess/ChineseChess.GUI/Services/PersistAndRestoreService.cs
2021-05-30 22:57:53 +08:00

50 lines
1.6 KiB
C#

using System;
using System.Collections;
using System.IO;
using ChineseChess.GUI.Contracts.Services;
using ChineseChess.GUI.Core.Contracts.Services;
using ChineseChess.GUI.Models;
using Microsoft.Extensions.Options;
namespace ChineseChess.GUI.Services
{
public class PersistAndRestoreService : IPersistAndRestoreService
{
private readonly IFileService _fileService;
private readonly AppConfig _appConfig;
private readonly string _localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
public PersistAndRestoreService(IFileService fileService, IOptions<AppConfig> appConfig)
{
_fileService = fileService;
_appConfig = appConfig.Value;
}
public void PersistData()
{
if (App.Current.Properties != null)
{
var folderPath = Path.Combine(_localAppData, _appConfig.ConfigurationsFolder);
var fileName = _appConfig.AppPropertiesFileName;
_fileService.Save(folderPath, fileName, App.Current.Properties);
}
}
public void RestoreData()
{
var folderPath = Path.Combine(_localAppData, _appConfig.ConfigurationsFolder);
var fileName = _appConfig.AppPropertiesFileName;
var properties = _fileService.Read<IDictionary>(folderPath, fileName);
if (properties != null)
{
foreach (DictionaryEntry property in properties)
{
App.Current.Properties.Add(property.Key, property.Value);
}
}
}
}
}