139 lines
3.4 KiB
C#
139 lines
3.4 KiB
C#
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using Sms.TaskTwo.Core.Models;
|
|
using Sms.TaskTwo.Core.Services;
|
|
|
|
namespace Sms.TaskTwo.ViewModels;
|
|
|
|
public sealed partial class EnvironmentVariableRowViewModel : ObservableObject
|
|
{
|
|
private readonly EnvironmentVariablesService _service;
|
|
private readonly Action? _onCustomRemoved;
|
|
private bool _isLoading;
|
|
|
|
public EnvironmentVariableRowViewModel(
|
|
EnvironmentVariableRow row,
|
|
EnvironmentVariablesService service,
|
|
Action? onCustomRemoved = null)
|
|
{
|
|
Field = row.Field;
|
|
IsFromAppSettings = row.IsFromAppSettings;
|
|
IsCustom = row.IsCustom;
|
|
_service = service;
|
|
_onCustomRemoved = onCustomRemoved;
|
|
ApplySnapshot(row, suppressSave: true);
|
|
RefreshActualValue(_service.GetProcessValue(Field));
|
|
}
|
|
|
|
public string Field { get; }
|
|
|
|
public bool IsFromAppSettings { get; }
|
|
|
|
public bool IsCustom { get; }
|
|
|
|
public bool CanChangeUserStore => !IsFromAppSettings;
|
|
|
|
[ObservableProperty]
|
|
private bool _useUserStore;
|
|
|
|
[ObservableProperty]
|
|
private string _actualValue = string.Empty;
|
|
|
|
public string ActualValueDisplay =>
|
|
string.IsNullOrEmpty(ActualValue) ? "<нет>" : ActualValue;
|
|
|
|
[ObservableProperty]
|
|
private string _requiredValue = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private string _comment = string.Empty;
|
|
|
|
partial void OnUseUserStoreChanged(bool value)
|
|
{
|
|
RowAppearanceChanged?.Invoke(this, EventArgs.Empty);
|
|
|
|
if (_isLoading)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (IsFromAppSettings && !value)
|
|
{
|
|
BeginLoad();
|
|
UseUserStore = true;
|
|
EndLoad();
|
|
return;
|
|
}
|
|
|
|
if (value)
|
|
{
|
|
_service.SaveValue(Field, RequiredValue);
|
|
RefreshActualValue(_service.GetProcessValue(Field));
|
|
return;
|
|
}
|
|
|
|
if (IsCustom)
|
|
{
|
|
_service.RemoveVariable(Field);
|
|
_onCustomRemoved?.Invoke();
|
|
return;
|
|
}
|
|
|
|
_service.DeleteFromUserStore(Field);
|
|
BeginLoad();
|
|
RequiredValue = _service.GetDisplayValue(Field);
|
|
EndLoad();
|
|
RefreshActualValue(_service.GetProcessValue(Field));
|
|
}
|
|
|
|
partial void OnActualValueChanged(string value) => OnPropertyChanged(nameof(ActualValueDisplay));
|
|
|
|
partial void OnRequiredValueChanged(string value)
|
|
{
|
|
if (_isLoading || !UseUserStore)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_service.SaveValue(Field, value);
|
|
RefreshActualValue(_service.GetProcessValue(Field));
|
|
}
|
|
|
|
public event EventHandler? RowAppearanceChanged;
|
|
|
|
public void ApplySnapshot(EnvironmentVariableRow row, bool suppressSave = false)
|
|
{
|
|
if (suppressSave)
|
|
{
|
|
BeginLoad();
|
|
}
|
|
|
|
RequiredValue = row.Value;
|
|
Comment = row.Comment;
|
|
UseUserStore = row.IsFromAppSettings || row.IsPersistedInUserStore;
|
|
|
|
if (suppressSave)
|
|
{
|
|
EndLoad();
|
|
}
|
|
}
|
|
|
|
public void RefreshActualValue(string? processValue)
|
|
{
|
|
ActualValue = processValue ?? string.Empty;
|
|
}
|
|
|
|
public void BeginLoad() => _isLoading = true;
|
|
|
|
public void EndLoad() => _isLoading = false;
|
|
|
|
partial void OnCommentChanged(string value)
|
|
{
|
|
if (_isLoading)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_service.SaveComment(Field, value);
|
|
}
|
|
}
|