Базовая работа на Linux
This commit is contained in:
113
src/Sms.TaskTwo.ViewModels/EnvironmentVariableRowViewModel.cs
Normal file
113
src/Sms.TaskTwo.ViewModels/EnvironmentVariableRowViewModel.cs
Normal file
@@ -0,0 +1,113 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
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 bool _isLoading;
|
||||
|
||||
public EnvironmentVariableRowViewModel(EnvironmentVariableRow row, EnvironmentVariablesService service)
|
||||
{
|
||||
Field = row.Field;
|
||||
IsFromAppSettings = row.IsFromAppSettings;
|
||||
IsCustom = row.IsCustom;
|
||||
_service = service;
|
||||
ApplySnapshot(row, suppressSave: true);
|
||||
}
|
||||
|
||||
public string Field { get; }
|
||||
|
||||
public bool IsFromAppSettings { get; }
|
||||
|
||||
public bool IsCustom { get; }
|
||||
|
||||
[ObservableProperty]
|
||||
private bool _isPersistedInUserStore;
|
||||
|
||||
public string UserStoreBadge => IsPersistedInUserStore ? "USER" : string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private string _value = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private string _comment = string.Empty;
|
||||
|
||||
partial void OnIsPersistedInUserStoreChanged(bool value)
|
||||
{
|
||||
OnPropertyChanged(nameof(UserStoreBadge));
|
||||
DeleteFromUserStoreCommand.NotifyCanExecuteChanged();
|
||||
RowAppearanceChanged?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
public event EventHandler? RowAppearanceChanged;
|
||||
|
||||
public void ApplySnapshot(EnvironmentVariableRow row, bool suppressSave = false)
|
||||
{
|
||||
if (suppressSave)
|
||||
{
|
||||
BeginLoad();
|
||||
}
|
||||
|
||||
Value = row.Value;
|
||||
Comment = row.Comment;
|
||||
IsPersistedInUserStore = row.IsPersistedInUserStore;
|
||||
|
||||
if (suppressSave)
|
||||
{
|
||||
EndLoad();
|
||||
}
|
||||
}
|
||||
|
||||
public void BeginLoad() => _isLoading = true;
|
||||
|
||||
public void EndLoad() => _isLoading = false;
|
||||
|
||||
partial void OnValueChanged(string value)
|
||||
{
|
||||
if (_isLoading)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_service.SaveValue(Field, value);
|
||||
if (!IsPersistedInUserStore)
|
||||
{
|
||||
IsPersistedInUserStore = true;
|
||||
}
|
||||
}
|
||||
|
||||
partial void OnCommentChanged(string value)
|
||||
{
|
||||
if (_isLoading)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_service.SaveComment(Field, value);
|
||||
}
|
||||
|
||||
[RelayCommand(CanExecute = nameof(CanDeleteFromUserStore))]
|
||||
private void DeleteFromUserStore()
|
||||
{
|
||||
if (IsCustom)
|
||||
{
|
||||
_service.RemoveVariable(Field);
|
||||
Removed?.Invoke(this, EventArgs.Empty);
|
||||
return;
|
||||
}
|
||||
|
||||
_service.DeleteFromUserStore(Field);
|
||||
BeginLoad();
|
||||
IsPersistedInUserStore = false;
|
||||
Value = _service.GetDisplayValue(Field);
|
||||
EndLoad();
|
||||
}
|
||||
|
||||
public event EventHandler? Removed;
|
||||
|
||||
private bool CanDeleteFromUserStore() => IsPersistedInUserStore || IsCustom;
|
||||
}
|
||||
Reference in New Issue
Block a user