Files
ThreadPilot/ViewModels/PowerPlanViewModel.cs
T
PrimeBuild-pc acfd8dca7e Switch project license to AGPLv3 and test visibility
Replaces the project's license from GNU GPL v3 to the GNU Affero General Public License v3: updated LICENSE (full AGPL text) and updated license headers across source files. Adds InternalsVisibleTo("ThreadPilot.Core.Tests") (plus using System.Runtime.CompilerServices) in AssemblyInfo to allow test access to internal types. Also introduces Services/TaskSafety.cs and two new test files under Tests/ThreadPilot.Core.Tests to cover task safety. These changes apply the AGPLv3 terms and enable internal API access for unit tests.
2026-04-12 16:57:52 +02:00

181 lines
6.2 KiB
C#

/*
* 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 Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.Logging;
using ThreadPilot.Models;
using ThreadPilot.Services;
using ThreadPilot.ViewModels;
namespace ThreadPilot.ViewModels
{
public partial class PowerPlanViewModel : BaseViewModel
{
private readonly IPowerPlanService _powerPlanService;
private System.Timers.Timer? _refreshTimer;
[ObservableProperty]
private ObservableCollection<PowerPlanModel> powerPlans = new();
[ObservableProperty]
private ObservableCollection<PowerPlanModel> customPowerPlans = new();
[ObservableProperty]
private PowerPlanModel? selectedPowerPlan;
[ObservableProperty]
private PowerPlanModel? selectedCustomPlan;
[ObservableProperty]
private PowerPlanModel? activePowerPlan;
public PowerPlanViewModel(
ILogger<PowerPlanViewModel> logger,
IPowerPlanService powerPlanService,
IEnhancedLoggingService? enhancedLoggingService = null)
: base(logger, enhancedLoggingService)
{
_powerPlanService = powerPlanService;
SetupRefreshTimer();
}
private void SetupRefreshTimer()
{
_refreshTimer = new System.Timers.Timer(10000); // PERFORMANCE OPTIMIZATION: Increased to 10 second refresh - power plans change infrequently
_refreshTimer.Elapsed += async (s, e) =>
{
try
{
// Marshal timer callback to UI thread to prevent cross-thread access exceptions
await System.Windows.Application.Current.Dispatcher.InvokeAsync(async () =>
{
await RefreshPowerPlansCommand.ExecuteAsync(null);
});
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Power plan refresh timer error: {ex.Message}");
}
};
_refreshTimer.Start();
}
[RelayCommand]
public async Task LoadPowerPlans()
{
try
{
SetStatus("Loading power plans...");
PowerPlans = await _powerPlanService.GetPowerPlansAsync();
CustomPowerPlans = await _powerPlanService.GetCustomPowerPlansAsync();
ActivePowerPlan = await _powerPlanService.GetActivePowerPlan();
ClearStatus();
}
catch (Exception ex)
{
SetStatus($"Error loading power plans: {ex.Message}", false);
}
}
[RelayCommand]
private async Task RefreshPowerPlans()
{
if (IsBusy) return;
try
{
var currentPlans = await _powerPlanService.GetPowerPlansAsync();
var currentActive = await _powerPlanService.GetActivePowerPlan();
var customPlans = await _powerPlanService.GetCustomPowerPlansAsync();
// Update power plans
PowerPlans = new ObservableCollection<PowerPlanModel>(currentPlans);
CustomPowerPlans = new ObservableCollection<PowerPlanModel>(customPlans);
ActivePowerPlan = currentActive;
// Update selected plan if it exists
if (SelectedPowerPlan != null)
{
SelectedPowerPlan = PowerPlans.FirstOrDefault(p => p.Guid == SelectedPowerPlan.Guid);
}
}
catch (Exception ex)
{
SetStatus($"Error refreshing power plans: {ex.Message}", false);
}
}
[RelayCommand]
private async Task SetActivePlan()
{
if (SelectedPowerPlan == null) return;
try
{
SetStatus($"Setting active power plan to {SelectedPowerPlan.Name}...");
var success = await _powerPlanService.SetActivePowerPlan(SelectedPowerPlan);
if (success)
{
ActivePowerPlan = SelectedPowerPlan;
await RefreshPowerPlans();
ClearStatus();
}
else
{
SetStatus($"Failed to set power plan {SelectedPowerPlan.Name}", false);
}
}
catch (Exception ex)
{
SetStatus($"Error setting power plan: {ex.Message}", false);
}
}
[RelayCommand]
private async Task ImportCustomPlan()
{
if (SelectedCustomPlan == null) return;
try
{
SetStatus($"Importing custom power plan {SelectedCustomPlan.Name}...");
var success = await _powerPlanService.ImportCustomPowerPlan(SelectedCustomPlan.FilePath);
if (success)
{
await RefreshPowerPlans();
ClearStatus();
}
else
{
SetStatus($"Failed to import power plan {SelectedCustomPlan.Name}", false);
}
}
catch (Exception ex)
{
SetStatus($"Error importing power plan: {ex.Message}", false);
}
}
}
}