Migration to MVVM
This commit is contained in:
parent
0c1bb50dce
commit
1db874ce77
6 changed files with 946 additions and 575 deletions
99
imagecatalog/Services/SettingsService.cs
Normal file
99
imagecatalog/Services/SettingsService.cs
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using ImageCatalog;
|
||||
|
||||
namespace ImageCatalog_2.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// Modern settings service that uses reflection to automatically save/load all properties
|
||||
/// </summary>
|
||||
public class SettingsService : ISettingsService
|
||||
{
|
||||
private readonly ParametriSetup _parametriSetup;
|
||||
|
||||
public SettingsService(ParametriSetup parametriSetup)
|
||||
{
|
||||
_parametriSetup = parametriSetup;
|
||||
}
|
||||
|
||||
public Task SaveSettingsAsync(string filePath, object settings)
|
||||
{
|
||||
return Task.Run(() =>
|
||||
{
|
||||
_parametriSetup.NomeFileSetup = filePath;
|
||||
|
||||
// Use reflection to get all properties and save them
|
||||
var properties = settings.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
|
||||
.Where(p => p.CanRead && IsSerializableType(p.PropertyType));
|
||||
|
||||
foreach (var prop in properties)
|
||||
{
|
||||
var value = prop.GetValue(settings);
|
||||
_parametriSetup.AggiornaParametro(prop.Name, value);
|
||||
}
|
||||
|
||||
_parametriSetup.SalvaParametriSetup();
|
||||
});
|
||||
}
|
||||
|
||||
public Task LoadSettingsAsync(string filePath, object settings)
|
||||
{
|
||||
return Task.Run(() =>
|
||||
{
|
||||
_parametriSetup.NomeFileSetup = filePath;
|
||||
_parametriSetup.CaricaParametriSetup();
|
||||
|
||||
// Use reflection to get all properties and load them
|
||||
var properties = settings.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
|
||||
.Where(p => p.CanWrite && IsSerializableType(p.PropertyType));
|
||||
|
||||
foreach (var prop in properties)
|
||||
{
|
||||
try
|
||||
{
|
||||
object value;
|
||||
if (prop.PropertyType == typeof(string))
|
||||
{
|
||||
value = _parametriSetup.LeggiParametroString(prop.Name);
|
||||
}
|
||||
else if (prop.PropertyType == typeof(bool))
|
||||
{
|
||||
value = _parametriSetup.LeggiParametroBoolean(prop.Name);
|
||||
}
|
||||
else if (prop.PropertyType == typeof(int))
|
||||
{
|
||||
value = _parametriSetup.LeggiParametro<int>(prop.Name, (int)(prop.GetValue(settings) ?? 0));
|
||||
}
|
||||
else if (prop.PropertyType == typeof(double))
|
||||
{
|
||||
value = _parametriSetup.LeggiParametro<double>(prop.Name, (double)(prop.GetValue(settings) ?? 0.0));
|
||||
}
|
||||
else
|
||||
{
|
||||
continue; // Skip unsupported types
|
||||
}
|
||||
|
||||
prop.SetValue(settings, value);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Skip properties that can't be loaded
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private bool IsSerializableType(Type type)
|
||||
{
|
||||
return type == typeof(string) ||
|
||||
type == typeof(int) ||
|
||||
type == typeof(bool) ||
|
||||
type == typeof(double) ||
|
||||
type == typeof(float) ||
|
||||
type == typeof(decimal);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue