Files
jdescopingtool/Deprecated/JdeScoping.SecureStoreManager/ViewModels/SecretItemViewModel.cs
T
Joseph Doherty 1e21e33ade chore: deprecate standalone SecureStoreManager utility
Move SecureStoreManager project and tests to Deprecated folder and remove
from solution. SecureStore functionality is now integrated into ConfigManager.
2026-01-27 07:26:40 -05:00

93 lines
2.8 KiB
C#

using System.Windows.Input;
using JdeScoping.SecureStoreManager.Services;
namespace JdeScoping.SecureStoreManager.ViewModels;
/// <summary>
/// View model for an individual secret item with show/hide toggle.
/// </summary>
public class SecretItemViewModel : ViewModelBase
{
private readonly string _actualValue;
private readonly IClipboardService _clipboardService;
private bool _isValueVisible;
private const string MaskedValue = "********";
/// <summary>
/// Initializes a new instance of the <see cref="SecretItemViewModel"/> class.
/// </summary>
/// <param name="key">The secret key name.</param>
/// <param name="value">The secret value.</param>
/// <param name="clipboardService">The clipboard service for copy operations.</param>
public SecretItemViewModel(string key, string value, IClipboardService clipboardService)
{
Key = key;
_actualValue = value;
_clipboardService = clipboardService ?? throw new ArgumentNullException(nameof(clipboardService));
ToggleVisibilityCommand = new RelayCommand(ToggleVisibility);
CopyToClipboardCommand = new RelayCommand(CopyToClipboard);
}
/// <summary>
/// Gets the secret key.
/// </summary>
public string Key { get; }
/// <summary>
/// Gets the displayed value (masked or actual based on visibility).
/// </summary>
public string DisplayValue => _isValueVisible ? _actualValue : MaskedValue;
/// <summary>
/// Gets the actual unmasked value.
/// </summary>
public string ActualValue => _actualValue;
/// <summary>
/// Gets or sets whether the value is visible.
/// </summary>
public bool IsValueVisible
{
get => _isValueVisible;
set
{
if (SetProperty(ref _isValueVisible, value))
{
OnPropertyChanged(nameof(DisplayValue));
}
}
}
/// <summary>
/// Command to toggle visibility of the secret value.
/// </summary>
public ICommand ToggleVisibilityCommand { get; }
/// <summary>
/// Command to copy the secret value to clipboard.
/// </summary>
public ICommand CopyToClipboardCommand { get; }
/// <summary>
/// Event raised when clipboard copy fails.
/// </summary>
public event Action<string>? OnCopyFailed;
private void ToggleVisibility()
{
IsValueVisible = !IsValueVisible;
}
private async void CopyToClipboard()
{
try
{
await _clipboardService.SetTextAsync(_actualValue);
}
catch (Exception ex)
{
OnCopyFailed?.Invoke($"Failed to copy to clipboard: {ex.Message}");
}
}
}