Skip to content

Commit

Permalink
Fix: Allow user to save files with validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rvost committed Mar 24, 2023
1 parent 0cce99a commit 64509a0
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 3 deletions.
28 changes: 28 additions & 0 deletions src/DayzServerTools.Application/Models/IConfirmationDialog.cs
Original file line number Diff line number Diff line change
@@ -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();
}
2 changes: 1 addition & 1 deletion src/DayzServerTools.Application/Models/IMessageDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public interface IMessageDialog
string Message { get; set; }
string Title { get; set; }
MessageDialogImage Image { get; set; }

bool? Show();
}
1 change: 1 addition & 0 deletions src/DayzServerTools.Application/Services/IDialogFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace DayzServerTools.Application.Services;

public interface IDialogFactory
{
IConfirmationDialog CreateConfirmationDialog();
IMessageDialog CreateMessageDialog();
IFileDialog CreateOpenFileDialog();
IFileDialog CreateSaveFileDialog();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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))
Expand Down
23 changes: 23 additions & 0 deletions src/DayzServerTools.Windows/Models/WindowsConfirmationDialog.cs
Original file line number Diff line number Diff line change
@@ -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());
}
}
3 changes: 3 additions & 0 deletions src/DayzServerTools.Windows/Services/WindowsDialogFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ internal class WindowsDialogFactory : IDialogFactory
public IClassnameImportDialog CreateClassnameImportDialog()
=> new WindowsClassnameImportDialog();

public IConfirmationDialog CreateConfirmationDialog()
=> new WindowsConfirmationDialog();

public IExportDialog CreateExportDialog()
=> new WindowsExportDialog();

Expand Down

0 comments on commit 64509a0

Please sign in to comment.