CodeMerger/ViewModels/MainViewModel.cs

220 lines
6.0 KiB
C#

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Win32;
using System.IO;
using System;
using CodeMerger.Models;
using ICSharpCode.AvalonEdit.Document;
using System.Windows;
namespace CodeMerger.ViewModels;
public partial class MainViewModel : ObservableObject
{
private readonly cCodeMerger _codeMerger;
private readonly AppSettings _settings;
private readonly LogViewModel _logViewModel;
[ObservableProperty]
private string originalFilePath;
[ObservableProperty]
private string llmFilePath;
[ObservableProperty]
private string outputFilePath;
[ObservableProperty]
private string originalFileName;
[ObservableProperty]
private string llmFileName;
[ObservableProperty]
private string outputFileName;
[ObservableProperty]
private TextDocument originalDocument;
[ObservableProperty]
private TextDocument lLMDocument;
[ObservableProperty]
private TextDocument mergedDocument;
public MainViewModel()
{
_codeMerger = new cCodeMerger();
_settings = AppSettings.Load();
_logViewModel = new LogViewModel();
// Initialize documents
OriginalDocument = new TextDocument();
LLMDocument = new TextDocument();
MergedDocument = new TextDocument();
LoadLastUsedFiles();
}
private void LoadLastUsedFiles()
{
if (!string.IsNullOrEmpty(_settings.LastOriginalFilePath) && File.Exists(_settings.LastOriginalFilePath))
{
OriginalFilePath = _settings.LastOriginalFilePath;
OriginalFileName = Path.GetFileName(OriginalFilePath);
OriginalDocument.Text = File.ReadAllText(OriginalFilePath);
}
if (!string.IsNullOrEmpty(_settings.LastLLMFilePath) && File.Exists(_settings.LastLLMFilePath))
{
LlmFilePath = _settings.LastLLMFilePath;
LlmFileName = Path.GetFileName(LlmFilePath);
LLMDocument.Text = File.ReadAllText(LlmFilePath);
}
if (!string.IsNullOrEmpty(_settings.LastOutputFilePath))
{
OutputFilePath = _settings.LastOutputFilePath;
OutputFileName = Path.GetFileName(OutputFilePath);
LoadMergedFile();
}
}
private void LoadMergedFile()
{
if (!string.IsNullOrEmpty(OutputFilePath) && File.Exists(OutputFilePath))
{
try
{
MergedDocument.Text = File.ReadAllText(OutputFilePath);
_logViewModel.AddLog($"Loaded merged file: {OutputFilePath}", LogLevel.Info);
}
catch (Exception ex)
{
_logViewModel.AddLog($"Error loading merged file: {ex.Message}", LogLevel.Error);
}
}
}
[RelayCommand]
private void SelectOriginalFile()
{
var dialog = new OpenFileDialog
{
Filter = "C# files (*.cs)|*.cs|All files (*.*)|*.*"
};
if (dialog.ShowDialog() == true)
{
OriginalFilePath = dialog.FileName;
OriginalFileName = Path.GetFileName(OriginalFilePath);
OriginalDocument.Text = File.ReadAllText(OriginalFilePath);
_settings.LastOriginalFilePath = OriginalFilePath;
_settings.Save();
}
}
[RelayCommand]
private void SelectLLMFile()
{
var dialog = new OpenFileDialog
{
Filter = "C# files (*.cs)|*.cs|All files (*.*)|*.*"
};
if (dialog.ShowDialog() == true)
{
LlmFilePath = dialog.FileName;
LlmFileName = Path.GetFileName(LlmFilePath);
LLMDocument.Text = File.ReadAllText(LlmFilePath);
_settings.LastLLMFilePath = LlmFilePath;
_settings.Save();
}
}
[RelayCommand]
private void SelectOutputFile()
{
var dialog = new SaveFileDialog
{
Filter = "C# files (*.cs)|*.cs|All files (*.*)|*.*",
DefaultExt = ".cs"
};
if (dialog.ShowDialog() == true)
{
OutputFilePath = dialog.FileName;
OutputFileName = Path.GetFileName(OutputFilePath);
_settings.LastOutputFilePath = OutputFilePath;
_settings.Save();
LoadMergedFile();
}
}
[RelayCommand]
private void MergeCode()
{
if (string.IsNullOrWhiteSpace(OriginalFilePath) ||
string.IsNullOrWhiteSpace(LlmFilePath) ||
string.IsNullOrWhiteSpace(OutputFilePath))
{
throw new InvalidOperationException("Please select all required files first.");
}
var result = _codeMerger.MergeCode(OriginalDocument.Text, LLMDocument.Text, OutputFilePath, _logViewModel);
if (result.Success)
{
MergedDocument.Text = result.MergedCode;
}
else
{
throw new InvalidOperationException($"Merge failed:\n{string.Join("\n", result.Diagnostics)}");
}
}
[RelayCommand]
private void ShowLog()
{
var logWindow = new Views.LogWindow(_logViewModel);
logWindow.Owner = App.Current.MainWindow;
logWindow.Show();
}
[RelayCommand]
private void PasteLLM()
{
try
{
var text = Clipboard.GetText();
if (!string.IsNullOrEmpty(text))
{
LLMDocument.Text = text;
_logViewModel.AddLog("LLM code pasted from clipboard", LogLevel.Info);
}
}
catch (Exception ex)
{
_logViewModel.AddLog($"Error pasting from clipboard: {ex.Message}", LogLevel.Error);
}
}
[RelayCommand]
private void CopyMerged()
{
try
{
Clipboard.SetText(MergedDocument.Text);
_logViewModel.AddLog("Merged code copied to clipboard", LogLevel.Info);
}
catch (Exception ex)
{
_logViewModel.AddLog($"Error copying to clipboard: {ex.Message}", LogLevel.Error);
}
}
}