136 lines
4.3 KiB
C#
136 lines
4.3 KiB
C#
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;
|
|
|
|
public WorkerEditWindow() : this(null)
|
|
{
|
|
}
|
|
|
|
public WorkerEditWindow(WorkerConfig? worker)
|
|
{
|
|
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()
|
|
{
|
|
OkButton.Click += OkButton_Click;
|
|
CancelButton.Click += CancelButton_Click;
|
|
BrowseFlamencoPathButton.Click += BrowseFlamencoPathButton_Click;
|
|
AddDriveButton.Click += AddDriveButton_Click;
|
|
RemoveDriveButton.Click += RemoveDriveButton_Click;
|
|
AddPathButton.Click += AddPathButton_Click;
|
|
RemovePathButton.Click += RemovePathButton_Click;
|
|
}
|
|
|
|
private async void OkButton_Click(object? sender, RoutedEventArgs e)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(_viewModel.Name))
|
|
{
|
|
var box = MessageBoxManager.GetMessageBoxStandard("Error",
|
|
"Worker name is required.",
|
|
ButtonEnum.Ok, MsBox.Avalonia.Enums.Icon.Error);
|
|
await box.ShowAsync();
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
_viewModel.Save();
|
|
_result = true;
|
|
Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var errorBox = MessageBoxManager.GetMessageBoxStandard("Error",
|
|
$"Failed to save worker: {ex.Message}",
|
|
ButtonEnum.Ok, MsBox.Avalonia.Enums.Icon.Error);
|
|
await errorBox.ShowAsync();
|
|
}
|
|
}
|
|
|
|
private void CancelButton_Click(object? sender, RoutedEventArgs e)
|
|
{
|
|
_result = false;
|
|
Close();
|
|
}
|
|
|
|
private async void BrowseFlamencoPathButton_Click(object? sender, RoutedEventArgs e)
|
|
{
|
|
var topLevel = TopLevel.GetTopLevel(this);
|
|
if (topLevel?.StorageProvider.CanPickFolder == true)
|
|
{
|
|
var folders = await topLevel.StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions
|
|
{
|
|
Title = "Select Flamenco Worker Path"
|
|
});
|
|
|
|
if (folders.Count > 0 && folders[0].TryGetLocalPath() is { } localPath)
|
|
{
|
|
_viewModel.FlamencoWorkerPath = localPath;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void AddDriveButton_Click(object? sender, RoutedEventArgs e)
|
|
{
|
|
// 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)
|
|
{
|
|
if (NetworkDrivesListBox.SelectedItem is string drive)
|
|
{
|
|
_viewModel.NetworkDrives.Remove(drive);
|
|
}
|
|
}
|
|
|
|
private void AddPathButton_Click(object? sender, RoutedEventArgs e)
|
|
{
|
|
// 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)
|
|
{
|
|
if (NetworkPathsListBox.SelectedItem is string path)
|
|
{
|
|
_viewModel.NetworkPaths.Remove(path);
|
|
}
|
|
}
|
|
|
|
public async Task<bool> ShowDialogAsync(Window parent)
|
|
{
|
|
await base.ShowDialog(parent);
|
|
return _result;
|
|
}
|
|
}
|
|
}
|
|
|