using System.Threading.Tasks; using FluentTerminal.App.Services; using FluentTerminal.App.Services.Utilities; using FluentTerminal.App.ViewModels.Infrastructure; using FluentTerminal.Models; namespace FluentTerminal.App.ViewModels.Profiles { /// /// Used for common profiles (PowerShell, CMD, WSL). /// public class CommonProfileProviderViewModel : ProfileProviderViewModelBase { #region Fields private readonly IFileSystemService _fileSystemService; #endregion Fields #region Properties public bool PreInstalled { get; private set; } private string _location; public string Location { get => _location; set => Set(ref _location, value); } private string _workingDirectory; public string WorkingDirectory { get => _workingDirectory; set => Set(ref _workingDirectory, value); } private string _arguments; public string Arguments { get => _arguments; set => Set(ref _arguments, value); } #endregion Properties #region Commands public IAsyncCommand BrowseForCustomShellCommand { get; } public IAsyncCommand BrowseForWorkingDirectoryCommand { get; } #endregion Commands #region Constructor public CommonProfileProviderViewModel(ISettingsService settingsService, IApplicationView applicationView, IFileSystemService fileSystemService, ShellProfile original = null) : base(settingsService, applicationView, original) { _fileSystemService = fileSystemService; BrowseForCustomShellCommand = new AsyncCommand(BrowseForCustomShell); BrowseForWorkingDirectoryCommand = new AsyncCommand(BrowseForWorkingDirectory); Initialize(Model); } #endregion Constructor #region Methods private void Initialize(ShellProfile profile) { PreInstalled = profile.PreInstalled; Location = profile.Location; Arguments = profile.Arguments; WorkingDirectory = profile.WorkingDirectory; } protected override void LoadFromProfile(ShellProfile profile) { base.LoadFromProfile(profile); Initialize(profile); } protected override async Task CopyToProfileAsync(ShellProfile profile) { await base.CopyToProfileAsync(profile); profile.Location = _location; profile.Arguments = _arguments; profile.WorkingDirectory = _workingDirectory; } public override async Task ValidateAsync() { var error = await base.ValidateAsync(); if (!string.IsNullOrEmpty(error)) { return error; } if (!string.IsNullOrEmpty(_location)) { return null; } error = I18N.Translate("LocationCantBeEmpty"); return string.IsNullOrEmpty(error) ? "Location cannot be empty." : error; } public override bool HasChanges() { return base.HasChanges() || !Model.Location.NullableEqualTo(_location) || !Model.WorkingDirectory.NullableEqualTo(_workingDirectory) || !Model.Arguments.NullableEqualTo(_arguments); } private async Task BrowseForCustomShell() { var file = await _fileSystemService.OpenFile(new[] { ".exe" }).ConfigureAwait(true); if (file != null) { Location = file.Path; } } private async Task BrowseForWorkingDirectory() { var directory = await _fileSystemService.BrowseForDirectory().ConfigureAwait(true); if (directory != null) { WorkingDirectory = directory; } } #endregion Methods } }