e3530e5342
Introduces new models, services, and view models for process monitoring, power plan association, application settings, and enhanced logging. Updates main application files and views to support new features. Adds developer and architecture documentation.
188 lines
5.3 KiB
C#
188 lines
5.3 KiB
C#
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using ThreadPilot.Services;
|
|
|
|
namespace ThreadPilot.ViewModels
|
|
{
|
|
/// <summary>
|
|
/// Base ViewModel with common functionality for all ViewModels
|
|
/// </summary>
|
|
public abstract partial class BaseViewModel : ObservableObject, IDisposable
|
|
{
|
|
protected readonly ILogger Logger;
|
|
protected readonly IEnhancedLoggingService? EnhancedLoggingService;
|
|
private bool _disposed;
|
|
|
|
[ObservableProperty]
|
|
private bool isBusy;
|
|
|
|
[ObservableProperty]
|
|
private string statusMessage = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private bool hasError;
|
|
|
|
[ObservableProperty]
|
|
private string errorMessage = string.Empty;
|
|
|
|
protected BaseViewModel(ILogger logger, IEnhancedLoggingService? enhancedLoggingService = null)
|
|
{
|
|
Logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
EnhancedLoggingService = enhancedLoggingService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set status message and busy state
|
|
/// </summary>
|
|
protected void SetStatus(string message, bool isBusyState = true)
|
|
{
|
|
StatusMessage = message;
|
|
IsBusy = isBusyState;
|
|
ClearError();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clear status and busy state
|
|
/// </summary>
|
|
protected void ClearStatus()
|
|
{
|
|
StatusMessage = string.Empty;
|
|
IsBusy = false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set error message and clear busy state
|
|
/// </summary>
|
|
protected void SetError(string message, Exception? exception = null)
|
|
{
|
|
ErrorMessage = message;
|
|
HasError = true;
|
|
IsBusy = false;
|
|
|
|
if (exception != null)
|
|
{
|
|
Logger.LogError(exception, "Error in {ViewModelType}: {Message}", GetType().Name, message);
|
|
}
|
|
else
|
|
{
|
|
Logger.LogWarning("Error in {ViewModelType}: {Message}", GetType().Name, message);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clear error state
|
|
/// </summary>
|
|
protected void ClearError()
|
|
{
|
|
ErrorMessage = string.Empty;
|
|
HasError = false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Execute an async operation with error handling and status updates
|
|
/// </summary>
|
|
protected async Task ExecuteAsync(Func<Task> operation, string? statusMessage = null, string? successMessage = null)
|
|
{
|
|
try
|
|
{
|
|
if (!string.IsNullOrEmpty(statusMessage))
|
|
{
|
|
SetStatus(statusMessage);
|
|
}
|
|
|
|
await operation();
|
|
|
|
if (!string.IsNullOrEmpty(successMessage))
|
|
{
|
|
SetStatus(successMessage, false);
|
|
}
|
|
else
|
|
{
|
|
ClearStatus();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
SetError($"Operation failed: {ex.Message}", ex);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Execute an async operation with return value and error handling
|
|
/// </summary>
|
|
protected async Task<T?> ExecuteAsync<T>(Func<Task<T>> operation, string? statusMessage = null, string? successMessage = null)
|
|
{
|
|
try
|
|
{
|
|
if (!string.IsNullOrEmpty(statusMessage))
|
|
{
|
|
SetStatus(statusMessage);
|
|
}
|
|
|
|
var result = await operation();
|
|
|
|
if (!string.IsNullOrEmpty(successMessage))
|
|
{
|
|
SetStatus(successMessage, false);
|
|
}
|
|
else
|
|
{
|
|
ClearStatus();
|
|
}
|
|
|
|
return result;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
SetError($"Operation failed: {ex.Message}", ex);
|
|
return default;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Log user action for audit purposes
|
|
/// </summary>
|
|
protected async Task LogUserActionAsync(string action, string details, string? context = null)
|
|
{
|
|
try
|
|
{
|
|
if (EnhancedLoggingService != null)
|
|
{
|
|
await EnhancedLoggingService.LogUserActionAsync(action, details, context);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.LogError(ex, "Failed to log user action: {Action}", action);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initialize the ViewModel - override in derived classes
|
|
/// </summary>
|
|
public virtual async Task InitializeAsync()
|
|
{
|
|
// Base implementation does nothing
|
|
await Task.CompletedTask;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Cleanup resources - override in derived classes
|
|
/// </summary>
|
|
protected virtual void OnDispose()
|
|
{
|
|
// Base implementation does nothing
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (!_disposed)
|
|
{
|
|
OnDispose();
|
|
_disposed = true;
|
|
}
|
|
}
|
|
}
|
|
} |