Persist last-used dialog folders in user preferences

Dialogs now remember last-used folders for source, destination, logo, and settings files by storing these paths in a user preferences file under LocalApplicationData. Preferences are saved on form close, reducing unnecessary writes. SettingsService now uses a temporary ParametriSetup for settings files to avoid polluting user preferences. Error handling ensures preference save failures do not disrupt the user. This separation improves user experience and keeps user preferences distinct from project settings.
This commit is contained in:
MaddoScientisto 2026-02-14 20:38:51 +01:00
commit 5cb491f1b5
3 changed files with 113 additions and 19 deletions

View file

@ -24,40 +24,41 @@ namespace ImageCatalog_2.Services
public Task SaveSettingsAsync(string filePath, object settings)
{
// Use a dedicated ParametriSetup instance for the target settings file so the injected
// user preferences store is not modified. This keeps user prefs (singleton) intact.
return Task.Run(() =>
{
_parametriSetup.NomeFileSetup = filePath;
var fileParams = new ParametriSetup(filePath);
// Convert ViewModel to DTO
var dto = ViewModelToDto(settings);
// Use reflection on DTO properties with XmlElement attribute
var properties = typeof(SettingsDto).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var prop in properties)
{
// Get the XmlElement attribute to determine the XML property name
var xmlAttr = prop.GetCustomAttribute<XmlElementAttribute>();
if (xmlAttr == null)
continue; // Skip properties without XmlElement attribute
continue;
var xmlName = xmlAttr.ElementName;
var value = prop.GetValue(dto);
_parametriSetup.AggiornaParametro(xmlName, value);
fileParams.AggiornaParametro(xmlName, value);
}
_parametriSetup.SalvaParametriSetup();
fileParams.SalvaParametriSetup();
});
}
public async Task LoadSettingsAsync(string filePath, object settings)
{
// Step 1: Load XML and read into DTO on background thread
// Step 1: Load XML into a temporary ParametriSetup so we don't mutate the injected
// user-preferences instance (singleton).
var dto = await Task.Run(() =>
{
_parametriSetup.NomeFileSetup = filePath;
_parametriSetup.CaricaParametriSetup();
var fileParams = new ParametriSetup(filePath);
var loadedDto = new SettingsDto();
var properties = typeof(SettingsDto).GetProperties(BindingFlags.Public | BindingFlags.Instance);
@ -76,11 +77,11 @@ namespace ImageCatalog_2.Services
object value;
if (prop.PropertyType == typeof(string))
{
value = _parametriSetup.LeggiParametroString(xmlName);
value = fileParams.LeggiParametroString(xmlName);
}
else if (prop.PropertyType == typeof(bool))
{
value = _parametriSetup.LeggiParametroBoolean(xmlName);
value = fileParams.LeggiParametroBoolean(xmlName);
}
else if (prop.PropertyType == typeof(int))
{