211 lines
6.2 KiB
C#
211 lines
6.2 KiB
C#
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using DirectoryCreator.Models;
|
|
using Microsoft.Win32;
|
|
using System.Collections.ObjectModel;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
using System.Windows;
|
|
|
|
namespace DirectoryCreator
|
|
{
|
|
/// <summary>
|
|
/// Interaction logic for Window1.xaml
|
|
/// </summary>
|
|
public partial class ConfigEditorWindow : Window
|
|
{
|
|
public ConfigEditorWindow()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace DirectoryCreator.ViewModels
|
|
{
|
|
|
|
public partial class ConfigEditorViewModel : ObservableObject
|
|
{
|
|
private readonly Window _window;
|
|
private readonly string _configPath;
|
|
private bool _isNewConfig;
|
|
|
|
[ObservableProperty]
|
|
private string configDescription;
|
|
|
|
[ObservableProperty]
|
|
private string basePath;
|
|
|
|
[ObservableProperty]
|
|
private ObservableCollection<FolderStructureViewModel> folderStructures;
|
|
|
|
public ConfigEditorViewModel(Window window, FolderConfig? existingConfig = null, string? configPath = null)
|
|
{
|
|
_window = window;
|
|
_configPath = configPath;
|
|
_isNewConfig = existingConfig == null;
|
|
|
|
FolderStructures = new ObservableCollection<FolderStructureViewModel>();
|
|
|
|
if (existingConfig != null)
|
|
{
|
|
LoadFromConfig(existingConfig);
|
|
}
|
|
else
|
|
{
|
|
ConfigDescription = "Nueva Configuración";
|
|
BasePath = "";
|
|
}
|
|
}
|
|
|
|
private void LoadFromConfig(FolderConfig config)
|
|
{
|
|
ConfigDescription = config.Description;
|
|
BasePath = config.BasePath;
|
|
|
|
foreach (var structure in config.FolderStructures)
|
|
{
|
|
var structureVm = new FolderStructureViewModel
|
|
{
|
|
BasePath = structure.BasePath,
|
|
Subfolders = new ObservableCollection<SubfolderViewModel>(
|
|
structure.Subfolders.Select(s => new SubfolderViewModel { Name = s }))
|
|
};
|
|
FolderStructures.Add(structureVm);
|
|
}
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void SelectBasePath()
|
|
{
|
|
var dialog = new OpenFolderDialog
|
|
{
|
|
Title = "Seleccionar Carpeta Base"
|
|
};
|
|
|
|
if (dialog.ShowDialog() == true)
|
|
{
|
|
BasePath = dialog.FolderName;
|
|
}
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void SelectStructurePath(FolderStructureViewModel structure)
|
|
{
|
|
var dialog = new OpenFolderDialog
|
|
{
|
|
Title = "Seleccionar Carpeta Base para la Estructura"
|
|
};
|
|
|
|
if (dialog.ShowDialog() == true)
|
|
{
|
|
structure.BasePath = dialog.FolderName;
|
|
}
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void AddStructure()
|
|
{
|
|
FolderStructures.Add(new FolderStructureViewModel
|
|
{
|
|
BasePath = "",
|
|
Subfolders = new ObservableCollection<SubfolderViewModel>()
|
|
});
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void RemoveStructure(FolderStructureViewModel structure)
|
|
{
|
|
FolderStructures.Remove(structure);
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void AddSubfolder(FolderStructureViewModel structure)
|
|
{
|
|
structure.Subfolders.Add(new SubfolderViewModel { Name = "Nueva Subcarpeta" });
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void RemoveSubfolder(SubfolderViewModel subfolder)
|
|
{
|
|
foreach (var structure in FolderStructures)
|
|
{
|
|
if (structure.Subfolders.Contains(subfolder))
|
|
{
|
|
structure.Subfolders.Remove(subfolder);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void Save()
|
|
{
|
|
try
|
|
{
|
|
var config = new FolderConfig
|
|
{
|
|
Description = ConfigDescription,
|
|
BasePath = BasePath,
|
|
FolderStructures = FolderStructures.Select(s => new FolderStructure
|
|
{
|
|
BasePath = s.BasePath,
|
|
Subfolders = s.Subfolders.Select(sf => sf.Name).ToList()
|
|
}).ToList()
|
|
};
|
|
|
|
string filePath = _configPath;
|
|
if (_isNewConfig || string.IsNullOrEmpty(filePath))
|
|
{
|
|
var dialog = new SaveFileDialog
|
|
{
|
|
Filter = "Archivos JSON (*.json)|*.json",
|
|
InitialDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Configurations"),
|
|
FileName = "config.json"
|
|
};
|
|
|
|
if (dialog.ShowDialog() != true)
|
|
return;
|
|
|
|
filePath = dialog.FileName;
|
|
}
|
|
|
|
var jsonString = JsonSerializer.Serialize(config, new JsonSerializerOptions
|
|
{
|
|
WriteIndented = true
|
|
});
|
|
|
|
File.WriteAllText(filePath, jsonString);
|
|
MessageBox.Show("Configuración guardada exitosamente!", "Éxito", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
_window.DialogResult = true;
|
|
_window.Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show($"Error al guardar la configuración: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void Cancel()
|
|
{
|
|
_window.DialogResult = false;
|
|
_window.Close();
|
|
}
|
|
}
|
|
|
|
public partial class FolderStructureViewModel : ObservableObject
|
|
{
|
|
[ObservableProperty]
|
|
private string basePath;
|
|
|
|
[ObservableProperty]
|
|
private ObservableCollection<SubfolderViewModel> subfolders;
|
|
}
|
|
|
|
public partial class SubfolderViewModel : ObservableObject
|
|
{
|
|
[ObservableProperty]
|
|
private string name;
|
|
}
|
|
} |