Catalog/imagecatalog/Services/PickerPreferenceService.cs
MaddoScientisto c261557a29 feat: Add Face Matcher functionality and related settings
- Implemented FilePathToBitmapConverter for image loading.
- Enhanced DataModel with commands and properties for Face Matcher.
- Created FaceMatcherResultItem model to store results.
- Updated SettingsDto to include Face Matcher paths and tolerance.
- Introduced PickerPreferenceService for managing folder paths.
- Expanded AiSettingsViewModel to manage Face Matcher settings and results.
2026-05-09 20:27:44 +02:00

93 lines
No EOL
3.1 KiB
C#

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 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();
}
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;
}
}