From 64509a020f45ce9b13a1189fa6daafa727cdeba9 Mon Sep 17 00:00:00 2001 From: rvost Date: Fri, 24 Mar 2023 14:30:46 +0300 Subject: [PATCH] Fix: Allow user to save files with validation errors --- .../Models/IConfirmationDialog.cs | 28 +++++++++++++++++++ .../Models/IMessageDialog.cs | 2 +- .../Services/IDialogFactory.cs | 1 + .../ViewModels/Base/ProjectFileViewModel.cs | 22 +++++++++++++-- .../Models/WindowsConfirmationDialog.cs | 23 +++++++++++++++ .../Services/WindowsDialogFactory.cs | 3 ++ 6 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 src/DayzServerTools.Application/Models/IConfirmationDialog.cs create mode 100644 src/DayzServerTools.Windows/Models/WindowsConfirmationDialog.cs diff --git a/src/DayzServerTools.Application/Models/IConfirmationDialog.cs b/src/DayzServerTools.Application/Models/IConfirmationDialog.cs new file mode 100644 index 0000000..da2561e --- /dev/null +++ b/src/DayzServerTools.Application/Models/IConfirmationDialog.cs @@ -0,0 +1,28 @@ +namespace DayzServerTools.Application.Models; + +public enum ConfirmationDialogResult +{ + None = 0, + OK = 1, + Cancel = 2, + Yes = 6, + No = 7 +} + +public enum ConfirmationDialogButton +{ + OK = 0, + OKCancel = 1, + YesNoCancel = 3, + YesNo = 4 +} + +public interface IConfirmationDialog +{ + string Message { get; set; } + string Title { get; set; } + MessageDialogImage Image { get; set; } + ConfirmationDialogButton Button { get; set; } + + ConfirmationDialogResult Show(); +} \ No newline at end of file diff --git a/src/DayzServerTools.Application/Models/IMessageDialog.cs b/src/DayzServerTools.Application/Models/IMessageDialog.cs index 3211dfb..3e35ddd 100644 --- a/src/DayzServerTools.Application/Models/IMessageDialog.cs +++ b/src/DayzServerTools.Application/Models/IMessageDialog.cs @@ -13,6 +13,6 @@ public interface IMessageDialog string Message { get; set; } string Title { get; set; } MessageDialogImage Image { get; set; } - + bool? Show(); } diff --git a/src/DayzServerTools.Application/Services/IDialogFactory.cs b/src/DayzServerTools.Application/Services/IDialogFactory.cs index f10083b..f707476 100644 --- a/src/DayzServerTools.Application/Services/IDialogFactory.cs +++ b/src/DayzServerTools.Application/Services/IDialogFactory.cs @@ -4,6 +4,7 @@ namespace DayzServerTools.Application.Services; public interface IDialogFactory { + IConfirmationDialog CreateConfirmationDialog(); IMessageDialog CreateMessageDialog(); IFileDialog CreateOpenFileDialog(); IFileDialog CreateSaveFileDialog(); diff --git a/src/DayzServerTools.Application/ViewModels/Base/ProjectFileViewModel.cs b/src/DayzServerTools.Application/ViewModels/Base/ProjectFileViewModel.cs index 61ea938..6c5609c 100644 --- a/src/DayzServerTools.Application/ViewModels/Base/ProjectFileViewModel.cs +++ b/src/DayzServerTools.Application/ViewModels/Base/ProjectFileViewModel.cs @@ -1,6 +1,6 @@ using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; - +using DayzServerTools.Application.Models; using DayzServerTools.Application.Services; using DayzServerTools.Library.Common; using FluentValidation; @@ -68,7 +68,25 @@ public void Close() protected abstract bool Validate(); - protected virtual bool CanSave() => Validate(); + protected virtual bool CanSave() + { + var isValid = Validate(); + + if (!isValid) + { + var confirmationDialog = _dialogFactory.CreateConfirmationDialog(); + confirmationDialog.Title = "Validation Errors"; + confirmationDialog.Message = "The file contains validation errors! Do you want to save it?"; + confirmationDialog.Button = ConfirmationDialogButton.YesNo; + confirmationDialog.Image = MessageDialogImage.Warning; + + var res = confirmationDialog.Show(); + return res == ConfirmationDialogResult.Yes; + } + + return true; + } + protected void SaveTo(string filePath) { if (!string.IsNullOrEmpty(filePath)) diff --git a/src/DayzServerTools.Windows/Models/WindowsConfirmationDialog.cs b/src/DayzServerTools.Windows/Models/WindowsConfirmationDialog.cs new file mode 100644 index 0000000..8596b09 --- /dev/null +++ b/src/DayzServerTools.Windows/Models/WindowsConfirmationDialog.cs @@ -0,0 +1,23 @@ +using System; +using System.Windows; + +using DayzServerTools.Application.Models; + +namespace DayzServerTools.Windows.Models; + +public class WindowsConfirmationDialog : IConfirmationDialog +{ + public string Message { get; set; } + public string Title { get; set; } + public MessageDialogImage Image { get; set; } = MessageDialogImage.None; + public ConfirmationDialogButton Button { get; set; } = ConfirmationDialogButton.YesNo; + + public ConfirmationDialogResult Show() + { + var button = (MessageBoxButton)Enum.Parse(typeof(MessageBoxButton), Button.ToString()); + var image = (MessageBoxImage)Enum.Parse(typeof(MessageBoxImage), Image.ToString()); + + var res = MessageBox.Show(Message, Title, button, image); + return (ConfirmationDialogResult)Enum.Parse(typeof(ConfirmationDialogResult), res.ToString()); + } +} \ No newline at end of file diff --git a/src/DayzServerTools.Windows/Services/WindowsDialogFactory.cs b/src/DayzServerTools.Windows/Services/WindowsDialogFactory.cs index 218af35..bbd471f 100644 --- a/src/DayzServerTools.Windows/Services/WindowsDialogFactory.cs +++ b/src/DayzServerTools.Windows/Services/WindowsDialogFactory.cs @@ -11,6 +11,9 @@ internal class WindowsDialogFactory : IDialogFactory public IClassnameImportDialog CreateClassnameImportDialog() => new WindowsClassnameImportDialog(); + public IConfirmationDialog CreateConfirmationDialog() + => new WindowsConfirmationDialog(); + public IExportDialog CreateExportDialog() => new WindowsExportDialog();