appdata added

This commit is contained in:
2024-03-07 20:46:30 +01:00
Unverified
parent b303185727
commit a383b13b82
10 changed files with 181 additions and 3 deletions

View File

@@ -0,0 +1,15 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VDownload.Services.Data.Application
{
public class ApplicationData
{
[JsonProperty("common")]
public CommonApplicationData Common { get; set; }
}
}

View File

@@ -0,0 +1,104 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VDownload.Services.Data.Configuration;
namespace VDownload.Services.Data.Application
{
public interface IApplicationDataService
{
#region PROPERTIES
ApplicationData Data { get; }
#endregion
#region METHODS
Task Load();
Task Restore();
Task Save();
#endregion
}
public class ApplicationDataService : IApplicationDataService
{
#region SERVICES
protected readonly IConfigurationService _configurationService;
#endregion
#region FIELDS
protected readonly string _filePath;
#endregion
#region PROPERTIES
public ApplicationData Data { get; private set; }
#endregion
#region CONSTRUCTORS
public ApplicationDataService(IConfigurationService configurationService)
{
_configurationService = configurationService;
string appdataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string appdataDirectoryName = _configurationService.Common.Path.Appdata.DirectoryName;
string appdataAuthenticationFilename = _configurationService.Common.Path.Appdata.SettingsFile;
_filePath = Path.Combine(appdataPath, appdataDirectoryName, appdataAuthenticationFilename);
}
#endregion
#region PUBLIC METHODS
public async Task Load()
{
if (File.Exists(_filePath))
{
string content = await File.ReadAllTextAsync(_filePath);
Data = JsonConvert.DeserializeObject<ApplicationData>(content);
}
else
{
Data = new ApplicationData();
}
}
public async Task Save()
{
Directory.CreateDirectory(Path.GetDirectoryName(_filePath));
string content = JsonConvert.SerializeObject(Data);
await File.WriteAllTextAsync(_filePath, content);
}
public async Task Restore()
{
Data = new ApplicationData();
await Save();
}
#endregion
}
}

View File

@@ -0,0 +1,15 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VDownload.Services.Data.Application
{
public class CommonApplicationData
{
[JsonProperty("last_output_directory")]
public string? LastOutputDirectory { get; set; } = null;
}
}

View File

@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\VDownload.Services.Data.Configuration\VDownload.Services.Data.Configuration.csproj" />
</ItemGroup>
</Project>

View File

@@ -18,5 +18,8 @@ namespace VDownload.Services.Data.Configuration.Models
[ConfigurationKeyName("settings_file")]
public string SettingsFile { get; set; }
[ConfigurationKeyName("data_file")]
public string DataFile { get; set; }
}
}

View File

@@ -63,7 +63,7 @@ namespace VDownload.Services.Data.Settings
string appdataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string appdataDirectoryName = _configurationService.Common.Path.Appdata.DirectoryName;
string appdataAuthenticationFilename = _configurationService.Common.Path.Appdata.SettingsFile;
string appdataAuthenticationFilename = _configurationService.Common.Path.Appdata.DataFile;
_filePath = Path.Combine(appdataPath, appdataDirectoryName, appdataAuthenticationFilename);
}