JD2022-TU1/main/tools/modules/JD.Guinness.WPF.Module/View/WorkspaceSelectionBox.xaml.cs

150 lines
4.5 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using JD.Guinness.WPF.Module.Configuration;
using JD.Guinness.WPF.Module.Extension;
using Microsoft.Win32;
using Plume.Perforce.Service.Api;
using Ubisoft.Perforce.Data;
namespace JD.Guinness.WPF.Module.View
{
public sealed partial class WorkspaceSelectionBox : INotifyPropertyChanged
{
public sealed class Workspace
{
public string Name { get; }
public string Path { get; }
public Workspace(ClientInfo client)
{
Name = client.Client;
Path = client.Root;
}
}
private readonly IPerforceService _perforceService;
private string _perforceServer;
private IEnumerable<Workspace> _workspaces;
private Workspace _selectedWorkspace;
private string _filePath;
public DelegateCommand SelectFileCommand { get; }
public DelegateCommand OkCommand { get; }
public DelegateCommand CancelCommand { get; }
public IEnumerable<Workspace> Workspaces
{
get => _workspaces;
private set
{
_workspaces = value;
OnPropertyChanged();
}
}
public string PerforceServer
{
get => _perforceServer;
set
{
_perforceServer = value;
OnPropertyChanged();
Workspaces = string.IsNullOrEmpty(value)
? Array.Empty<Workspace>()
: _perforceService
.RetrieveClients(value, Environment.UserName)
.Where(c => !string.IsNullOrEmpty(c.Stream))
.Select(c => new Workspace(c)).ToArray();
}
}
public Workspace SelectedWorkspace
{
get => _selectedWorkspace;
set
{
_selectedWorkspace = value;
OnPropertyChanged();
}
}
public string FilePath
{
get => _filePath;
set
{
_filePath = value;
OnPropertyChanged();
}
}
private WorkspaceSelectionBox(IPerforceService perforceService, GuinnessSettings settings)
{
_perforceService = perforceService;
FilePath = settings.GameSaveFilePath ?? string.Empty;
PerforceServer = settings.PerforceServer;
SelectedWorkspace = Workspaces.FirstOrDefault(w => w.Name == settings.WorkspaceName);
DataContext = this;
SelectFileCommand = new DelegateCommand(OnSelectFile, () => SelectedWorkspace != null);
OkCommand = new DelegateCommand(OnOk, () => SelectedWorkspace != null && !string.IsNullOrEmpty(FilePath));
CancelCommand = new DelegateCommand(OnCancel);
InitializeComponent();
}
private void OnSelectFile()
{
OpenFileDialog dialog = new OpenFileDialog
{
Title = "Select Guinness save file",
Filter = "guinness|*.gui",
InitialDirectory = Path.Combine(SelectedWorkspace.Path, FilePath ?? string.Empty),
CheckFileExists = false
};
if (dialog.ShowDialog() is true)
{
FilePath = dialog.FileName.ToRelativePath(SelectedWorkspace.Path);
}
}
private void OnOk() => DialogResult = true;
private void OnCancel() => DialogResult = false;
public static bool Show(IPerforceService perforceService, GuinnessSettings settings)
{
WorkspaceSelectionBox box = new WorkspaceSelectionBox(perforceService, settings);
if (box.ShowDialog() is true)
{
settings.PerforceServer = box.PerforceServer;
settings.WorkspaceName = box.SelectedWorkspace.Name;
settings.WorkspacePath = box.SelectedWorkspace.Path;
settings.GameSaveFilePath = box.FilePath;
return true;
}
return false;
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
#endregion
}
}