Files
UFL/Views/WorkerEditWindow.axaml.cs

118 lines
3.8 KiB
C#
Raw Permalink Normal View History

2025-12-17 15:34:34 -07:00
using System;
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Platform.Storage;
using UnifiedFarmLauncher.Models;
using UnifiedFarmLauncher.Services;
using UnifiedFarmLauncher.ViewModels;
using MsBox.Avalonia;
using MsBox.Avalonia.Enums;
namespace UnifiedFarmLauncher.Views
{
public partial class WorkerEditWindow : Window
{
private readonly WorkerEditViewModel _viewModel;
private bool _result;
2025-12-17 16:15:11 -07:00
public WorkerEditWindow() : this(null)
{
}
public WorkerEditWindow(WorkerConfig? worker)
2025-12-17 15:34:34 -07:00
{
InitializeComponent();
var configService = new ConfigService();
_viewModel = new WorkerEditViewModel(configService, worker);
DataContext = _viewModel;
SetupEventHandlers();
}
private void InitializeComponent()
{
Avalonia.Markup.Xaml.AvaloniaXamlLoader.Load(this);
}
private void SetupEventHandlers()
{
2025-12-17 16:17:27 -07:00
this.FindControl<Button>("OkButton")!.Click += OkButton_Click;
this.FindControl<Button>("CancelButton")!.Click += CancelButton_Click;
this.FindControl<Button>("AddDriveButton")!.Click += AddDriveButton_Click;
this.FindControl<Button>("RemoveDriveButton")!.Click += RemoveDriveButton_Click;
this.FindControl<Button>("AddPathButton")!.Click += AddPathButton_Click;
this.FindControl<Button>("RemovePathButton")!.Click += RemovePathButton_Click;
2025-12-17 15:34:34 -07:00
}
private async void OkButton_Click(object? sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(_viewModel.Name))
{
var box = MessageBoxManager.GetMessageBoxStandard("Error",
"Worker name is required.",
2025-12-17 15:55:45 -07:00
ButtonEnum.Ok, MsBox.Avalonia.Enums.Icon.Error);
2025-12-17 15:34:34 -07:00
await box.ShowAsync();
return;
}
try
{
_viewModel.Save();
_result = true;
Close();
}
catch (Exception ex)
{
var errorBox = MessageBoxManager.GetMessageBoxStandard("Error",
$"Failed to save worker: {ex.Message}",
2025-12-17 15:55:45 -07:00
ButtonEnum.Ok, MsBox.Avalonia.Enums.Icon.Error);
2025-12-17 15:34:34 -07:00
await errorBox.ShowAsync();
}
}
private void CancelButton_Click(object? sender, RoutedEventArgs e)
{
_result = false;
Close();
}
2025-12-17 16:15:11 -07:00
private void AddDriveButton_Click(object? sender, RoutedEventArgs e)
2025-12-17 15:34:34 -07:00
{
// Simplified: use a simple input box
// In a full implementation, you'd use a proper input dialog
_viewModel.NetworkDrives.Add("A:");
}
private void RemoveDriveButton_Click(object? sender, RoutedEventArgs e)
{
2025-12-17 16:17:27 -07:00
if (this.FindControl<ListBox>("NetworkDrivesListBox")?.SelectedItem is string drive)
2025-12-17 15:34:34 -07:00
{
_viewModel.NetworkDrives.Remove(drive);
}
}
2025-12-17 16:15:11 -07:00
private void AddPathButton_Click(object? sender, RoutedEventArgs e)
2025-12-17 15:34:34 -07:00
{
// Simplified: use a simple input box
// In a full implementation, you'd use a proper input dialog
_viewModel.NetworkPaths.Add("\\\\SERVER\\share");
}
private void RemovePathButton_Click(object? sender, RoutedEventArgs e)
{
2025-12-17 16:17:27 -07:00
if (this.FindControl<ListBox>("NetworkPathsListBox")?.SelectedItem is string path)
2025-12-17 15:34:34 -07:00
{
_viewModel.NetworkPaths.Remove(path);
}
}
public async Task<bool> ShowDialogAsync(Window parent)
{
await base.ShowDialog(parent);
return _result;
}
}
}