/*
* ThreadPilot - Advanced Windows Process and Power Plan Manager
* Copyright (C) 2025 Prime Build
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 only.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using ThreadPilot.Services;
namespace ThreadPilot.ViewModels
{
///
/// Factory for creating and managing ViewModel instances
///
public interface IViewModelFactory
{
///
/// Create a ViewModel instance of the specified type
///
T CreateViewModel() where T : BaseViewModel;
///
/// Create a ViewModel instance with initialization
///
Task CreateAndInitializeViewModelAsync() where T : BaseViewModel;
///
/// Dispose all managed ViewModels
///
void DisposeAllViewModels();
}
///
/// Implementation of ViewModel factory with dependency injection support
///
public class ViewModelFactory : IViewModelFactory, IDisposable
{
private readonly IServiceProvider _serviceProvider;
private readonly ILogger _logger;
private readonly List _managedViewModels = new();
private bool _disposed;
public ViewModelFactory(IServiceProvider serviceProvider, ILogger logger)
{
_serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public T CreateViewModel() where T : BaseViewModel
{
try
{
var viewModel = _serviceProvider.GetRequiredService();
_managedViewModels.Add(viewModel);
_logger.LogDebug("Created ViewModel of type {ViewModelType}", typeof(T).Name);
return viewModel;
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to create ViewModel of type {ViewModelType}", typeof(T).Name);
throw;
}
}
public async Task CreateAndInitializeViewModelAsync() where T : BaseViewModel
{
var viewModel = CreateViewModel();
try
{
await viewModel.InitializeAsync();
_logger.LogDebug("Initialized ViewModel of type {ViewModelType}", typeof(T).Name);
return viewModel;
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to initialize ViewModel of type {ViewModelType}", typeof(T).Name);
// Dispose the failed ViewModel
viewModel.Dispose();
_managedViewModels.Remove(viewModel);
throw;
}
}
public void DisposeAllViewModels()
{
_logger.LogInformation("Disposing all managed ViewModels");
foreach (var viewModel in _managedViewModels)
{
try
{
viewModel.Dispose();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error disposing ViewModel {ViewModelType}", viewModel.GetType().Name);
}
}
_managedViewModels.Clear();
_logger.LogInformation("All managed ViewModels disposed");
}
public void Dispose()
{
if (!_disposed)
{
DisposeAllViewModels();
_disposed = true;
}
}
}
///
/// Extension methods for ViewModel factory registration
///
public static class ViewModelFactoryExtensions
{
///
/// Register ViewModel factory in dependency injection container
///
public static IServiceCollection AddViewModelFactory(this IServiceCollection services)
{
services.AddSingleton();
return services;
}
}
}