Fixes and mapping

This commit is contained in:
MaddoScientisto 2026-02-04 23:16:06 +01:00
commit ba965e8266
10 changed files with 449 additions and 284 deletions

View file

@ -1,14 +1,19 @@
using ImageCatalog_2.Commands;
using ImageCatalog_2.Services;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;
using AutoMapper;
using MaddoShared;
using Microsoft.Extensions.Logging;
namespace ImageCatalog_2
@ -29,17 +34,25 @@ namespace ImageCatalog_2
private readonly ITestService _service;
private readonly ILogger<DataModel> _logger;
private readonly ISettingsService _settingsService;
private readonly ImageCreationStuff _imageCreationService;
private readonly PicSettings _picSettings;
private readonly IMapper _mapper;
// ComboBox collections
public List<string> AvailableFonts { get; }
public List<string> VerticalPositions { get; } = new() { "Alto", "Centro", "Basso" };
public List<string> HorizontalAlignments { get; } = new() { "Sinistra", "Centro", "Destra" };
public DataModel(ITestService testService, ISettingsService settingsService, ILogger<DataModel> logger)
public DataModel(ITestService testService, ISettingsService settingsService,
ImageCreationStuff imageCreationService, PicSettings picSettings,
IMapper mapper, ILogger<DataModel> logger)
{
_service = testService;
_logger = logger;
_settingsService = settingsService;
_imageCreationService = imageCreationService;
_picSettings = picSettings;
_mapper = mapper;
TestCommand = new RelayCommand(Test);
AsyncTestCommand = new AsyncCommand(TestAsync);
@ -774,6 +787,67 @@ namespace ImageCatalog_2
}
}
// Image processing progress and status
private string _processingStatus = "";
public string ProcessingStatus
{
get => _processingStatus;
set
{
_processingStatus = value;
NotifyPropertyChanged();
}
}
private int _processedImagesCount = 0;
public int ProcessedImagesCount
{
get => _processedImagesCount;
set
{
_processedImagesCount = value;
NotifyPropertyChanged();
}
}
private int _totalImagesCount = 0;
public int TotalImagesCount
{
get => _totalImagesCount;
set
{
_totalImagesCount = value;
NotifyPropertyChanged();
}
}
private int _progressBarValue = 0;
public int ProgressBarValue
{
get => _progressBarValue;
set
{
_progressBarValue = value;
NotifyPropertyChanged();
}
}
private int _progressBarMaximum = 100;
public int ProgressBarMaximum
{
get => _progressBarMaximum;
set
{
_progressBarMaximum = value;
NotifyPropertyChanged();
}
}
private ConcurrentBag<string> _results = new();
private int _currentAmount = 0;
private int _previousAmount = 0;
private System.Threading.Timer? _speedTimer;
private void Test(object parameter)
{
Debug.WriteLine("Yep");
@ -787,6 +861,123 @@ namespace ImageCatalog_2
private async Task ProcessImages()
{
_logger.LogInformation("Avvio elaborazione...");
UiEnabled = false;
MainToken?.Dispose();
MainToken = new CancellationTokenSource();
var token = MainToken.Token;
// Fix paths
FixPaths();
// Reset counters
ProcessingStatus = "Elaborazione in corso...";
TotalImagesCount = 0;
ProcessedImagesCount = 0;
SpeedCounter = "-f/m";
ProgressBarValue = 0;
ProgressBarMaximum = 100;
// Update PicSettings from DataModel using AutoMapper
_mapper.Map(this, _picSettings);
var imageCreationOptions = new ImageCreationStuff.Options
{
AggiornaSottodirectory = UpdateSubdirectories,
CreaSottocartelle = CreateSubfolders,
FilePerCartella = FilesPerFolder,
SuffissoCartelle = FolderSuffix,
CifreContatore = CounterDigits,
NumerazioneType = UseProgressiveNumbering ? NumerazioneType.Progressiva : NumerazioneType.Files,
SourcePath = SourcePath,
DestinationPath = DestinationPath,
MaxThreads = ThreadsCount,
ChunksSize = ChunkSize,
LinearExecution = UseSequentialProcessing
};
try
{
_results = new ConcurrentBag<string>();
_currentAmount = 0;
_previousAmount = 0;
// Start speed timer (every minute)
_speedTimer = new System.Threading.Timer(UpdateSpeedCounter, null, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1));
var time = await _imageCreationService.CreaCatalogoParallel(
imageCreationOptions,
_results,
OnImageProcessed,
token);
SpeedCounter = time;
_speedTimer?.Dispose();
_speedTimer = null;
}
catch (OperationCanceledException)
{
_logger.LogInformation("Operazione Cancellata");
}
catch (Exception ex)
{
_logger.LogError(ex, "Errore durante l'elaborazione delle immagini");
ProcessingStatus = $"Errore: {ex.Message}";
}
finally
{
MainToken?.Dispose();
MainToken = null;
_speedTimer?.Dispose();
_speedTimer = null;
}
ProcessingStatus = "Finito";
UiEnabled = true;
}
private void UpdateSpeedCounter(object? state)
{
_previousAmount = _currentAmount;
_currentAmount = _results.Count;
int diff = _currentAmount - _previousAmount;
SpeedCounter = $"{diff} f/m";
}
private void OnImageProcessed(object? sender, Tuple<string, int> args)
{
ProcessedImagesCount = _results.Count;
TotalImagesCount = args.Item2;
ProgressBarMaximum = args.Item2;
ProgressBarValue = _results.Count;
ProcessingStatus = args.Item1;
}
private void FixPaths()
{
SourcePath = FixPath(SourcePath);
DestinationPath = FixPath(DestinationPath);
}
private string FixPath(string path)
{
if (string.IsNullOrWhiteSpace(path))
{
return string.Empty;
}
// Trim leading/trailing quotes
path = path.Trim().Trim('"');
// Normalize directory separators
path = path.Replace('/', System.IO.Path.DirectorySeparatorChar)
.Replace('\\', System.IO.Path.DirectorySeparatorChar);
// Remove trailing separators then add one back
path = path.TrimEnd(System.IO.Path.DirectorySeparatorChar) + System.IO.Path.DirectorySeparatorChar;
return path;
}
private async Task CancelOperation()