JD2022-TU1/main/tools/modules/JD.Rosetta.Module/ViewModel/RosettaViewModel.cs

172 lines
8.9 KiB
C#

using JD.Localization.Core.Model;
using JD.LocalizationExport.Service.API;
using JD.LocalizationExport.Service.Model.Options;
using JD.Rosetta.Module.ViewModel.ExportOptions;
using Plume.Core.Logging;
using Plume.Perforce.Service.Api;
using Plume.WPF.Commands;
using Plume.WPF.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
namespace JD.Rosetta.Module.ViewModel
{
public class RosettaViewModel : BindableBase
{
private readonly ILogger _logger;
private readonly IPerforceService _perforceService;
private readonly string _perforceCodeHost;
private readonly string _perforceDataHost;
public RosettaViewModel(ILocalizationExportService localizationExportService, IPerforceService perforceService, string dataServer, string codeServer, ILogger logger)
{
_perforceService = perforceService;
_logger = logger;
_perforceCodeHost = codeServer;
_perforceDataHost = dataServer;
void UpdateProperty() => OnPropertyChanged(nameof(IsAtLeastOneExportAndOneLanguageSelected));
ExportLanguages = new ObservableCollection<ExportLanguageViewModel>(UafLanguageMethods.GetAllLanguages()
.Select(l => new ExportLanguageViewModel(l, UpdateProperty)));
Dictionary<ExportType, IEnumerable<string>> availableStreamsForExport = localizationExportService.ExportConfigs
.ToDictionary(ec => ec.Key, ec => GetAvailableStreamsForExport(ec.Key));
PropertyGridDataFactory = new ExportOptionsDescriptorFactory(availableStreamsForExport[ExportType.Console],
availableStreamsForExport[ExportType.MCA]);
ExportTypesAndOptions = new Dictionary<ExportTypeViewModel, ExportOptionsViewModel>(localizationExportService.ExportConfigs
.Where(ec => availableStreamsForExport[ec.Key] != null)
.ToDictionary(ec => new ExportTypeViewModel(ec.Key, ec.Value.Description, UpdateProperty),
ec =>
{
ExportOptionsViewModel eovm;
switch(ec.Key)
{
case ExportType.Console:
eovm = new ConsoleExportOptionsViewModel();
break;
case ExportType.MCA:
eovm = new MCAExportOptionsViewModel();
break;
case ExportType.Online:
eovm = new OnlineExportOptionsViewModel();
break;
default:
throw new Exception($"Unknown export type {ec.Key}");
}
return eovm;
}));
ExportLocalisationCommand = new DelegateCommand(ExportLocalization).ObservesCanExecute(() => IsAtLeastOneExportAndOneLanguageSelected);
}
#region Perforce
// Returns Enumerable.Empty for export that don't need a stream, and null for errors or if no workspace is available on the machine
private IEnumerable<string> GetAvailableStreamsForExport(ExportType exportType)
{
IEnumerable<string> availableStreams;
switch (exportType)
{
case ExportType.Console:
availableStreams = GetAvailableStreams(_perforceDataHost);
if (availableStreams == null || !availableStreams.Any())
{
availableStreams = null; // Ensuring the result is null if we have no data workspace
_logger.Error("No data workspace found on your machine: console export unavailable");
}
break;
case ExportType.MCA:
availableStreams = GetAvailableStreams(_perforceCodeHost);
if (availableStreams == null || !availableStreams.Any())
{
availableStreams = null; // Ensuring the result is null if we have no code workspace
_logger.Error("No code workspace found on your machine: MCA export unavailable");
}
break;
case ExportType.Online:
availableStreams = Enumerable.Empty<string>();
break;
default:
throw new Exception($"Unknown export type {exportType}");
}
return availableStreams;
}
private IEnumerable<string> GetAvailableStreams(string host)
{
string username = Environment.UserName;
return _perforceService.RetrieveClients(host, username)
?.Select(c => c.Stream)
?.Where(s => !string.IsNullOrEmpty(s));
}
#endregion
#region Commands
public DelegateCommand ExportLocalisationCommand { get; }
public event Func<IDictionary<ExportType, ExportOptionsModel>, IEnumerable<UafLanguage>, Task> LocalizationExportLaunched;
#endregion
public ObservableCollection<ExportLanguageViewModel> ExportLanguages { get; set; }
public Dictionary<ExportTypeViewModel, ExportOptionsViewModel> ExportTypesAndOptions { get; set; }
public bool IsAtLeastOneExportAndOneLanguageSelected => ExportLanguages.Any(el => el.IsChecked) && ExportTypesAndOptions.Keys.Any(et => et.IsChecked);
private bool _isUIEnabled = true;
public bool IsUIEnabled
{
get => _isUIEnabled;
set => SetProperty(ref _isUIEnabled, value);
}
public ExportOptionsDescriptorFactory PropertyGridDataFactory { get; }
private async void ExportLocalization()
{
if (LocalizationExportLaunched != null)
{
IEnumerable<UafLanguage> languagesToExport = ExportLanguages.Where(el => el.IsChecked).Select(el => el.Language);
IDictionary<ExportType, ExportOptionsModel> exportTypesAndOptionsToExecute = ExportTypesAndOptions.Where(et => et.Key.IsChecked)
.ToDictionary(et => et.Key.Type,
et => et.Value.ToExportOptions());
IsUIEnabled = false;
await LocalizationExportLaunched.Invoke(exportTypesAndOptionsToExecute, languagesToExport)
.ConfigureAwait(false);
IsUIEnabled = true;
}
}
}
}