using Avalonia.Controls; using Avalonia.Platform.Storage; using JdeScoping.SecureStoreManager.Constants; using MsBox.Avalonia; using MsBox.Avalonia.Enums; namespace JdeScoping.SecureStoreManager.Services; /// /// Avalonia implementation of IDialogService using MsBox.Avalonia and platform storage. /// public class AvaloniaDialogService : IDialogService { private readonly Func _getOwnerWindow; /// /// Creates a new instance of AvaloniaDialogService. /// /// Factory function to get the owner window for dialogs. public AvaloniaDialogService(Func getOwnerWindow) { _getOwnerWindow = getOwnerWindow ?? throw new ArgumentNullException(nameof(getOwnerWindow)); } /// /// Displays an error dialog with the specified message and title. /// /// The error message to display. /// The title of the error dialog. /// A task that completes when the dialog is dismissed. 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(); } } /// /// Displays an informational dialog with the specified message and title. /// /// The information message to display. /// The title of the information dialog. /// A task that completes when the dialog is dismissed. 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(); } } /// /// Displays a confirmation dialog with Yes/No buttons. /// /// The confirmation message to display. /// The title of the confirmation dialog. /// A task that completes with true if Yes was clicked; otherwise, false. public async Task 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; } /// /// Displays a prompt asking the user whether to save unsaved changes. /// /// A task that completes with the user's choice: Save, DontSave, or Cancel. public async Task 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 }; } /// /// Displays a save file dialog allowing the user to select a file path. /// /// The title of the save file dialog. /// The friendly name of the file type (e.g., "Excel Files"). /// The file extension pattern (e.g., "*.xlsx"). /// The default file extension (e.g., ".xlsx"). /// A task that completes with the selected file path, or null if canceled. public async Task 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; } /// /// Displays an open file dialog allowing the user to select a file to open. /// /// The title of the open file dialog. /// The friendly name of the file type (e.g., "Excel Files"). /// The file extension pattern (e.g., "*.xlsx"). /// A task that completes with the selected file path, or null if canceled. public async Task 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; } }