Catalog/imagecatalog/Services/PickerPreferenceService.cs

122 lines
4 KiB
C#
Raw Normal View History

using Avalonia.Platform.Storage;
using ImageCatalog;
using System;
using System.IO;
using System.Threading.Tasks;
namespace ImageCatalog_2.Services;
public static class PickerPreferenceKeys
{
public const string SourceFolder = "Picker.SourceFolder.LastPath";
public const string DestinationFolder = "Picker.DestinationFolder.LastPath";
public const string LogoFile = "Picker.LogoFile.LastPath";
public const string ModelsFolder = "Picker.ModelsFolder.LastPath";
public const string CsvOutput = "Picker.CsvOutput.LastPath";
public const string SettingsFile = "Picker.SettingsFile.LastPath";
public const string LastSettingsFile = "Settings.LastFilePath";
public const string FaceExecutableFolder = "Picker.FaceExecutableFolder.LastPath";
public const string FaceOutputFolder = "Picker.FaceOutputFolder.LastPath";
public const string FaceMatcherExecutable = "Picker.FaceMatcherExecutable.LastPath";
public const string FaceMatcherImage = "Picker.FaceMatcherImage.LastPath";
public const string FaceMatcherEncodings = "Picker.FaceMatcherEncodings.LastPath";
public const string FaceMatcherOutput = "Picker.FaceMatcherOutput.LastPath";
public const string FaceMatcherLog = "Picker.FaceMatcherLog.LastPath";
}
public sealed class PickerPreferenceService
{
private readonly ParametriSetup _userPreferences;
public PickerPreferenceService(ParametriSetup userPreferences)
{
_userPreferences = userPreferences;
}
public async Task<IStorageFolder?> TryGetStartFolderAsync(IStorageProvider storageProvider, string preferenceKey, string? currentPath = null)
{
var startPath = GetPreferredStartDirectory(preferenceKey, currentPath);
if (string.IsNullOrWhiteSpace(startPath))
{
return null;
}
try
{
return await storageProvider.TryGetFolderFromPathAsync(new Uri(startPath)).ConfigureAwait(true);
}
catch
{
return null;
}
}
public void RememberPath(string preferenceKey, string? selectedPath)
{
var directory = TryGetExistingDirectory(selectedPath);
if (string.IsNullOrWhiteSpace(directory))
{
return;
}
_userPreferences.AggiornaParametro(preferenceKey, directory);
_userPreferences.SalvaParametriSetup();
}
public string? GetRememberedValue(string preferenceKey)
{
var value = _userPreferences.LeggiParametroString(preferenceKey);
return string.IsNullOrWhiteSpace(value)
? null
: value.Trim().Trim('"');
}
public void RememberValue(string preferenceKey, string? value)
{
if (string.IsNullOrWhiteSpace(value))
{
return;
}
_userPreferences.AggiornaParametro(preferenceKey, value.Trim().Trim('"'));
_userPreferences.SalvaParametriSetup();
}
public void ForgetValue(string preferenceKey)
{
if (_userPreferences.RimuoviParametro(preferenceKey))
{
_userPreferences.SalvaParametriSetup();
}
}
private string? GetPreferredStartDirectory(string preferenceKey, string? currentPath)
{
var storedPath = _userPreferences.LeggiParametroString(preferenceKey);
return TryGetExistingDirectory(storedPath) ?? TryGetExistingDirectory(currentPath);
}
private static string? TryGetExistingDirectory(string? path)
{
if (string.IsNullOrWhiteSpace(path))
{
return null;
}
var normalizedPath = path.Trim().Trim('"');
if (Directory.Exists(normalizedPath))
{
return normalizedPath;
}
if (File.Exists(normalizedPath))
{
return Path.GetDirectoryName(normalizedPath);
}
var containingDirectory = Path.GetDirectoryName(normalizedPath);
return !string.IsNullOrWhiteSpace(containingDirectory) && Directory.Exists(containingDirectory)
? containingDirectory
: null;
}
}