Serialize converters were added
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace DataAccess.FileDialogs
|
||||
{
|
||||
public class FileDialogOpener
|
||||
{
|
||||
public void OpenFileAndRead()
|
||||
{
|
||||
//// Create an instance of OpenFileDialog
|
||||
//using OpenFileDialog openFileDialog = new();
|
||||
//// Set filter options and filter index
|
||||
//openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
|
||||
//openFileDialog.FilterIndex = 1;
|
||||
//openFileDialog.Multiselect = false; // Set to true if you want to allow multiple file selection
|
||||
//openFileDialog.Title = "Select a File";
|
||||
|
||||
//// Show the dialog and get result
|
||||
//if (openFileDialog.ShowDialog() == DialogResult.OK)
|
||||
//{
|
||||
// // Get the path of the selected file
|
||||
// string selectedFilePath = openFileDialog.FileName;
|
||||
|
||||
// // Read the content of the file
|
||||
// try
|
||||
// {
|
||||
// string fileContent = File.ReadAllText(selectedFilePath);
|
||||
// Console.WriteLine($"File Content of '{selectedFilePath}':");
|
||||
// Console.WriteLine(fileContent);
|
||||
// }
|
||||
// catch (IOException ex)
|
||||
// {
|
||||
// Console.WriteLine($"An error occurred while reading the file: {ex.Message}");
|
||||
// }
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// Console.WriteLine("File selection was cancelled.");
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using StructureHelperCommon.Infrastructures.Exceptions;
|
||||
using StructureHelperCommon.Infrastructures.Interfaces;
|
||||
using StructureHelperCommon.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace StructureHelperCommon.Services.FileServices
|
||||
{
|
||||
public class FileDialogSaver : IFileDialogSaver
|
||||
{
|
||||
private const string saveCanceledByUser = "Saving was canceled by user";
|
||||
SaveFileResult result;
|
||||
public SaveDialogInputData? InputData { get; set; }
|
||||
public IShiftTraceLogger? TraceLogger { get; set; }
|
||||
|
||||
public SaveFileResult SaveFile()
|
||||
{
|
||||
CheckInput();
|
||||
result = new();
|
||||
using SaveFileDialog saveFileDialog = new();
|
||||
saveFileDialog.Filter = InputData.FilterString;
|
||||
saveFileDialog.InitialDirectory = InputData.InitialDirectory;
|
||||
saveFileDialog.FilterIndex = InputData.FilterIndex;
|
||||
saveFileDialog.CheckFileExists = InputData.CheckFileExist;
|
||||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
TraceLogger?.AddMessage($"User selected file {saveFileDialog.FileName}", TraceLogStatuses.Debug);
|
||||
result.FileName = saveFileDialog.FileName;
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
TraceLogger?.AddMessage(saveCanceledByUser);
|
||||
result.IsValid = false;
|
||||
result.Description += saveCanceledByUser;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
private void CheckInput()
|
||||
{
|
||||
if (InputData is null)
|
||||
{
|
||||
string errorString = ErrorStrings.ParameterIsNull + ": Input Data";
|
||||
TraceLogger?.AddMessage(errorString, TraceLogStatuses.Error);
|
||||
throw new StructureHelperException(errorString);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
79
StructureHelperCommon/Services/FileServices/FileOpener.cs
Normal file
79
StructureHelperCommon/Services/FileServices/FileOpener.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using StructureHelperCommon.Models;
|
||||
using StructureHelperCommon.Infrastructures.Exceptions;
|
||||
|
||||
namespace StructureHelperCommon.Services.FileServices
|
||||
{
|
||||
public class FileOpener : IFileDialogOpener
|
||||
{
|
||||
private const string fileSelectionWasCanceled = "File selection was cancelled by user";
|
||||
OpenFileResult? result;
|
||||
IShiftTraceLogger? traceLogger;
|
||||
public OpenFileInputData? InputData { get; private set; }
|
||||
public FileOpener(OpenFileInputData inputData)
|
||||
{
|
||||
InputData = inputData;
|
||||
}
|
||||
public OpenFileResult OpenFile()
|
||||
{
|
||||
PrepareNewResult();
|
||||
CheckInputData();
|
||||
traceLogger = InputData.TraceLogger;
|
||||
ShowOpenFileDialog();
|
||||
return result;
|
||||
}
|
||||
|
||||
private void ShowOpenFileDialog()
|
||||
{
|
||||
using (OpenFileDialog openFileDialog = new OpenFileDialog())
|
||||
{
|
||||
// Set filter options and filter index
|
||||
openFileDialog.Filter = InputData.FilterString;
|
||||
openFileDialog.FilterIndex = InputData.FilterIndex;
|
||||
openFileDialog.Multiselect = InputData.MultiSelect;
|
||||
openFileDialog.Title = InputData.Title;
|
||||
|
||||
// Show the dialog and get result
|
||||
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
// Get the path of the selected file
|
||||
string selectedFilePath = openFileDialog.FileName;
|
||||
traceLogger?.AddMessage($"File {selectedFilePath} is selected by user", TraceLogStatuses.Debug);
|
||||
result.FilePath = selectedFilePath;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.IsValid = false;
|
||||
result.Description = fileSelectionWasCanceled;
|
||||
traceLogger?.AddMessage(fileSelectionWasCanceled, TraceLogStatuses.Debug);
|
||||
Console.WriteLine(fileSelectionWasCanceled);
|
||||
}
|
||||
}
|
||||
}
|
||||
private void CheckInputData()
|
||||
{
|
||||
if (InputData is null)
|
||||
{
|
||||
result.IsValid = false;
|
||||
string message = ErrorStrings.ParameterIsNull + ": Input Data";
|
||||
result.Description = message;
|
||||
throw new StructureHelperException(message);
|
||||
}
|
||||
}
|
||||
private void PrepareNewResult()
|
||||
{
|
||||
result = new()
|
||||
{
|
||||
IsValid = true
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace StructureHelperCommon.Services.FileServices
|
||||
{
|
||||
public interface IFileDialogOpener
|
||||
{
|
||||
OpenFileInputData? InputData { get; }
|
||||
|
||||
OpenFileResult OpenFile();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using StructureHelperCommon.Infrastructures.Interfaces;
|
||||
|
||||
namespace StructureHelperCommon.Services.FileServices
|
||||
{
|
||||
public interface IFileDialogSaver : ILogic
|
||||
{
|
||||
SaveFileResult SaveFile();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using StructureHelperCommon.Models;
|
||||
using StructureHelperCommon.Models.Calculators;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Services.FileServices
|
||||
{
|
||||
public class OpenFileInputData : IInputData
|
||||
{
|
||||
public IShiftTraceLogger? TraceLogger { get; set; }
|
||||
public string FilterString { get; set; } = string.Empty;
|
||||
public int FilterIndex { get; set; } = 1;
|
||||
public bool MultiSelect { get; set; } = false;
|
||||
public string Title { get; set; } = "Select a file";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using StructureHelperCommon.Models.Calculators;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Services.FileServices
|
||||
{
|
||||
public class OpenFileResult : IResult
|
||||
{
|
||||
public bool IsValid { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string FilePath { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using StructureHelperCommon.Models.Calculators;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Services.FileServices
|
||||
{
|
||||
public class SaveDialogInputData : IInputData
|
||||
{
|
||||
public string InitialDirectory { get; set; }
|
||||
public int FilterIndex { get; set; } = 1;
|
||||
public string FilterString { get; set; } = string.Empty;
|
||||
public bool CheckFileExist { get; set; } = false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using StructureHelperCommon.Models.Calculators;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StructureHelperCommon.Services.FileServices
|
||||
{
|
||||
public class SaveFileResult : IResult
|
||||
{
|
||||
public bool IsValid { get; set; } = true;
|
||||
public string? Description { get; set; } = string.Empty;
|
||||
public string FileName { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user