Files
EngineeringSync/EngineeringSync.TrayApp/ViewModels/ProjectManagementViewModel.cs

184 lines
6.5 KiB
C#
Raw Normal View History

using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using EngineeringSync.Domain.Entities;
using EngineeringSync.TrayApp.Services;
namespace EngineeringSync.TrayApp.ViewModels;
public partial class ProjectManagementViewModel(ApiClient api) : ObservableObject
{
[ObservableProperty] private ObservableCollection<ProjectConfig> _projects = [];
[ObservableProperty] private ProjectConfig? _selectedProject;
[ObservableProperty] private bool _isEditing;
[ObservableProperty] private string _editName = string.Empty;
[ObservableProperty] private string _editEngineeringPath = string.Empty;
[ObservableProperty] private string _editSimulationPath = string.Empty;
[ObservableProperty] private string _editFileExtensions = ".jt,.cojt,.xml";
[ObservableProperty] private bool _editIsActive = true;
[ObservableProperty] private bool _editBackupEnabled = true;
[ObservableProperty] private bool _editBackupUseCustomPath = false;
[ObservableProperty] private string _editBackupCustomPath = string.Empty;
[ObservableProperty] private int _editMaxBackupsPerFile = 0;
[ObservableProperty] private bool _isNewProject;
[ObservableProperty] private string _statusMessage = string.Empty;
[ObservableProperty] private string _scanStatus = string.Empty;
[ObservableProperty] private ObservableCollection<ScanResultDto> _scanResults = [];
public bool HasScanResults => ScanResults.Count > 0;
[RelayCommand]
public async Task LoadAsync()
{
var projects = await api.GetProjectsAsync() ?? [];
Projects = new ObservableCollection<ProjectConfig>(projects);
}
[RelayCommand]
public void StartNewProject()
{
SelectedProject = null;
IsNewProject = true;
IsEditing = true;
EditName = string.Empty;
EditEngineeringPath = string.Empty;
EditSimulationPath = string.Empty;
EditFileExtensions = ".jt,.cojt,.xml";
EditIsActive = true;
EditBackupEnabled = true;
EditBackupUseCustomPath = false;
EditBackupCustomPath = string.Empty;
EditMaxBackupsPerFile = 0;
}
[RelayCommand]
public void EditProject(ProjectConfig? project)
{
if (project is null)
{
System.Windows.MessageBox.Show(
"Bitte zuerst ein Projekt auswählen.",
"EngineeringSync",
System.Windows.MessageBoxButton.OK,
System.Windows.MessageBoxImage.Information);
return;
}
SelectedProject = project;
IsNewProject = false;
IsEditing = true;
EditName = project.Name;
EditEngineeringPath = project.EngineeringPath;
EditSimulationPath = project.SimulationPath;
EditFileExtensions = project.FileExtensions;
EditIsActive = project.IsActive;
EditBackupEnabled = project.BackupEnabled;
EditBackupUseCustomPath = project.BackupPath is not null;
EditBackupCustomPath = project.BackupPath ?? string.Empty;
EditMaxBackupsPerFile = project.MaxBackupsPerFile;
}
[RelayCommand]
public async Task SaveAsync()
{
if (string.IsNullOrWhiteSpace(EditName))
{
StatusMessage = "Name darf nicht leer sein.";
return;
}
try
{
if (IsNewProject)
await api.CreateProjectAsync(new CreateProjectDto(
EditName, EditEngineeringPath, EditSimulationPath, EditFileExtensions, EditIsActive,
EditBackupEnabled,
EditBackupUseCustomPath ? EditBackupCustomPath : null,
EditMaxBackupsPerFile));
else
await api.UpdateProjectAsync(SelectedProject!.Id, new UpdateProjectDto(
EditName, EditEngineeringPath, EditSimulationPath, EditFileExtensions, EditIsActive,
EditBackupEnabled,
EditBackupUseCustomPath ? EditBackupCustomPath : null,
EditMaxBackupsPerFile));
IsEditing = false;
await LoadAsync();
StatusMessage = IsNewProject ? "Projekt erstellt." : "Projekt aktualisiert.";
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"[TrayApp] Save Fehler: {ex}");
StatusMessage = $"Fehler: {ex.Message}";
}
}
[RelayCommand]
public void Cancel() => IsEditing = false;
[RelayCommand]
public async Task DeleteAsync(ProjectConfig? project)
{
if (project is null)
{
System.Windows.MessageBox.Show(
"Bitte zuerst ein Projekt auswählen.",
"EngineeringSync",
System.Windows.MessageBoxButton.OK,
System.Windows.MessageBoxImage.Information);
return;
}
try
{
await api.DeleteProjectAsync(project.Id);
await LoadAsync();
StatusMessage = "Projekt gelöscht.";
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"[TrayApp] Save Fehler: {ex}");
StatusMessage = $"Fehler: {ex.Message}";
}
}
[RelayCommand]
public async Task ScanFoldersAsync()
{
if (string.IsNullOrWhiteSpace(EditEngineeringPath) || string.IsNullOrWhiteSpace(EditSimulationPath))
{
ScanStatus = "Bitte beide Ordnerpfade angeben.";
return;
}
if (!System.IO.Directory.Exists(EditEngineeringPath))
{
ScanStatus = $"Engineering-Ordner existiert nicht: {EditEngineeringPath}";
return;
}
if (!System.IO.Directory.Exists(EditSimulationPath))
{
ScanStatus = $"Simulations-Ordner existiert nicht: {EditSimulationPath}";
return;
}
try
{
ScanStatus = "Wird gescannt...";
ScanResults.Clear();
var results = await api.ScanFoldersAsync(EditEngineeringPath, EditSimulationPath, EditFileExtensions);
if (results is null || results.Count == 0)
{
ScanStatus = "Keine Unterschiede gefunden - Ordner sind synchron.";
}
else
{
ScanStatus = $"{results.Count} Unterschied(e) gefunden:";
foreach (var r in results)
ScanResults.Add(r);
}
}
catch (Exception ex)
{
ScanStatus = $"Scan-Fehler: {ex.Message}";
}
}
}