Files
jdescopingtool/Deprecated/JdeScoping.SecureStoreManager/Services/AvaloniaDialogService.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

173 lines
6.6 KiB
C#

using Avalonia.Controls;
using Avalonia.Platform.Storage;
using JdeScoping.SecureStoreManager.Constants;
using MsBox.Avalonia;
using MsBox.Avalonia.Enums;
namespace JdeScoping.SecureStoreManager.Services;
/// <summary>
/// Avalonia implementation of IDialogService using MsBox.Avalonia and platform storage.
/// </summary>
public class AvaloniaDialogService : IDialogService
{
private readonly Func<Window?> _getOwnerWindow;
/// <summary>
/// Creates a new instance of AvaloniaDialogService.
/// </summary>
/// <param name="getOwnerWindow">Factory function to get the owner window for dialogs.</param>
public AvaloniaDialogService(Func<Window?> getOwnerWindow)
{
_getOwnerWindow = getOwnerWindow ?? throw new ArgumentNullException(nameof(getOwnerWindow));
}
/// <summary>
/// Displays an error dialog with the specified message and title.
/// </summary>
/// <param name="message">The error message to display.</param>
/// <param name="title">The title of the error dialog.</param>
/// <returns>A task that completes when the dialog is dismissed.</returns>
public async Task ShowErrorAsync(string message, string title)
{
var box = MessageBoxManager.GetMessageBoxStandard(title, message, ButtonEnum.Ok, Icon.Error);
var window = _getOwnerWindow();
if (window != null)
{
await box.ShowWindowDialogAsync(window);
}
else
{
await box.ShowAsync();
}
}
/// <summary>
/// Displays an informational dialog with the specified message and title.
/// </summary>
/// <param name="message">The information message to display.</param>
/// <param name="title">The title of the information dialog.</param>
/// <returns>A task that completes when the dialog is dismissed.</returns>
public async Task ShowInfoAsync(string message, string title)
{
var box = MessageBoxManager.GetMessageBoxStandard(title, message, ButtonEnum.Ok, Icon.Info);
var window = _getOwnerWindow();
if (window != null)
{
await box.ShowWindowDialogAsync(window);
}
else
{
await box.ShowAsync();
}
}
/// <summary>
/// Displays a confirmation dialog with Yes/No buttons.
/// </summary>
/// <param name="message">The confirmation message to display.</param>
/// <param name="title">The title of the confirmation dialog.</param>
/// <returns>A task that completes with true if Yes was clicked; otherwise, false.</returns>
public async Task<bool> ShowConfirmationAsync(string message, string title)
{
var box = MessageBoxManager.GetMessageBoxStandard(title, message, ButtonEnum.YesNo, Icon.Warning);
var window = _getOwnerWindow();
ButtonResult result;
if (window != null)
{
result = await box.ShowWindowDialogAsync(window);
}
else
{
result = await box.ShowAsync();
}
return result == ButtonResult.Yes;
}
/// <summary>
/// Displays a prompt asking the user whether to save unsaved changes.
/// </summary>
/// <returns>A task that completes with the user's choice: Save, DontSave, or Cancel.</returns>
public async Task<UnsavedChangesResult> ShowUnsavedChangesPromptAsync()
{
var box = MessageBoxManager.GetMessageBoxStandard(
DialogStrings.UnsavedChangesTitle,
DialogStrings.UnsavedChangesMessage,
ButtonEnum.YesNoCancel,
Icon.Warning);
var window = _getOwnerWindow();
ButtonResult result;
if (window != null)
{
result = await box.ShowWindowDialogAsync(window);
}
else
{
result = await box.ShowAsync();
}
return result switch
{
ButtonResult.Yes => UnsavedChangesResult.Save,
ButtonResult.No => UnsavedChangesResult.DontSave,
_ => UnsavedChangesResult.Cancel
};
}
/// <summary>
/// Displays a save file dialog allowing the user to select a file path.
/// </summary>
/// <param name="title">The title of the save file dialog.</param>
/// <param name="fileTypeName">The friendly name of the file type (e.g., "Excel Files").</param>
/// <param name="pattern">The file extension pattern (e.g., "*.xlsx").</param>
/// <param name="defaultExtension">The default file extension (e.g., ".xlsx").</param>
/// <returns>A task that completes with the selected file path, or null if canceled.</returns>
public async Task<string?> ShowSaveFileDialogAsync(string title, string fileTypeName, string pattern, string defaultExtension)
{
var window = _getOwnerWindow();
if (window == null)
return null;
var file = await window.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions
{
Title = title,
DefaultExtension = defaultExtension,
FileTypeChoices = new[]
{
new FilePickerFileType(fileTypeName) { Patterns = new[] { pattern } },
new FilePickerFileType(FileExtensions.AllFilesTypeName) { Patterns = new[] { FileExtensions.AllFilesPattern } }
}
});
return file?.Path.LocalPath;
}
/// <summary>
/// Displays an open file dialog allowing the user to select a file to open.
/// </summary>
/// <param name="title">The title of the open file dialog.</param>
/// <param name="fileTypeName">The friendly name of the file type (e.g., "Excel Files").</param>
/// <param name="pattern">The file extension pattern (e.g., "*.xlsx").</param>
/// <returns>A task that completes with the selected file path, or null if canceled.</returns>
public async Task<string?> ShowOpenFileDialogAsync(string title, string fileTypeName, string pattern)
{
var window = _getOwnerWindow();
if (window == null)
return null;
var files = await window.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
{
Title = title,
AllowMultiple = false,
FileTypeFilter = new[]
{
new FilePickerFileType(fileTypeName) { Patterns = new[] { pattern } },
new FilePickerFileType(FileExtensions.AllFilesTypeName) { Patterns = new[] { FileExtensions.AllFilesPattern } }
}
});
return files.Count > 0 ? files[0].Path.LocalPath : null;
}
}