Skip to content

Commit

Permalink
Separated the file save interfaces and classes into separate files an…
Browse files Browse the repository at this point in the history
…d removed the redundant code
  • Loading branch information
Lisa Malenfant committed Feb 22, 2024
1 parent 5a1bf04 commit 72638b8
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 63 deletions.
11 changes: 3 additions & 8 deletions Vts.Gui.Wpf.Test/ViewModel/Panels/PlotViewModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Threading;
using System.Windows;
using Vts.Common;
using Vts.Gui.Wpf.FileSystem;
using Vts.Gui.Wpf.Model;
using Vts.Gui.Wpf.ViewModel;
using Vts.IO;
Expand Down Expand Up @@ -610,12 +611,9 @@ public void Verify_ExportDataToText_correct_when_X_and_Y_scaling_set_to_log()
//plotViewModel.PlotValues.Execute(_plotData);

// mock the IOpenFileDialog
var openFileDialogMock = Substitute.For<PlotViewModel.ISaveFileDialog>();
var openFileDialogMock = Substitute.For<ISaveFileDialog>();
openFileDialogMock.FileName.Returns(exportFilename);
openFileDialogMock.ShowDialog().Returns(true);
// mock IFileSystem
var fileSystemMock = Substitute.For<PlotViewModel.IFileSystem>();
fileSystemMock.WriteExportedData(exportFilename, Encoding.UTF8);

_plotViewModel = new PlotViewModel(0, openFileDialogMock);
_plotViewModel.PlotValues.Execute(plotData);
Expand Down Expand Up @@ -668,12 +666,9 @@ public void Verify_ExportDataToText_correct_when_complex_data_plotted()
var plotData = new[] { new PlotData(points, "Real and Imaginary Lines") };

// mock the IOpenFileDialog
var openFileDialogMock = Substitute.For<PlotViewModel.ISaveFileDialog>();
var openFileDialogMock = Substitute.For<ISaveFileDialog>();
openFileDialogMock.FileName.Returns(exportFilename);
openFileDialogMock.ShowDialog().Returns(true);
// mock IFileSystem
var fileSystemMock = Substitute.For<PlotViewModel.IFileSystem>();
fileSystemMock.WriteExportedData(exportFilename, Encoding.UTF8);

_plotViewModel = new PlotViewModel(0, openFileDialogMock);
_plotViewModel.PlotValues.Execute(plotData);
Expand Down
10 changes: 10 additions & 0 deletions Vts.Gui.Wpf/FileSystem/ISaveFileDialog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Vts.Gui.Wpf.FileSystem
{
public interface ISaveFileDialog
{
string Filter { get; set; }
bool? ShowDialog();
string FileName { get; set; }
string DefaultExtension { get; set; }
}
}
10 changes: 10 additions & 0 deletions Vts.Gui.Wpf/FileSystem/ITextFileService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.IO;

namespace Vts.Gui.Wpf.FileSystem
{
public interface ITextFileService
{
public Tuple<FileStream, string> OpenTextFile();
}
}
21 changes: 21 additions & 0 deletions Vts.Gui.Wpf/FileSystem/SaveFileDialog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Vts.Gui.Wpf.FileSystem
{
public class SaveFileDialog : ISaveFileDialog
{
public string Filter { get; set; }
public bool? ShowDialog()
{
var dialog = new Microsoft.Win32.SaveFileDialog
{
DefaultExt = DefaultExtension ?? ".txt",
Filter = Filter
};
var result = dialog.ShowDialog();
FileName = dialog.FileName;
return result;
}

public string FileName { get; set; }
public string DefaultExtension { get; set; }
}
}
57 changes: 2 additions & 55 deletions Vts.Gui.Wpf/ViewModel/Panels/PlotViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
using System.Windows.Input;
using Vts.Extensions;
using Vts.Gui.Wpf.Extensions;
using Vts.Gui.Wpf.FileSystem;
using Vts.Gui.Wpf.Model;
using static Vts.Gui.Wpf.ViewModel.PlotViewModel;
using FontWeights = OxyPlot.FontWeights;

namespace Vts.Gui.Wpf.ViewModel
Expand Down Expand Up @@ -192,51 +192,17 @@ public PlotViewModel(int plotViewId, ISaveFileDialog saveFileDialog) : this(plot
_saveFileDialog = saveFileDialog;
}

public interface ISaveFileDialog
{
string Filter { get; set; }
bool? ShowDialog();
string FileName { get; set; }
}

public class SaveFileDialog : ISaveFileDialog
{
public string Filter { get; set; }
public bool? ShowDialog()
{
var dialog = new Microsoft.Win32.SaveFileDialog
{
DefaultExt = ".txt",
Filter = Filter
};
var result = dialog.ShowDialog();
FileName = dialog.FileName;
return result;
}

public string FileName { get; set; }
}

public interface ITextFileService
{
public Tuple<FileStream, string> OpenTextFile();
}

public Tuple<FileStream, string> OpenTextFile()
{
_saveFileDialog ??= new SaveFileDialog();
_saveFileDialog.Filter = "Text |*.txt";
_saveFileDialog.DefaultExtension = ".txt";

var accept = _saveFileDialog.ShowDialog();

return accept.GetValueOrDefault(false) ? Tuple.Create(File.Open(_saveFileDialog.FileName, FileMode.Create), _saveFileDialog.FileName) : null;
}

public interface IFileSystem
{
string WriteExportedData(string path, Encoding encoding);
}

public RelayCommand<Array> PlotValues { get; set; }
public RelayCommand<object> SetAxesLabels { get; set; }
public RelayCommand<object> SetCustomPlotLabel { get; set; }
Expand Down Expand Up @@ -653,25 +619,6 @@ private void Plot_ExportDataToText_Executed(object sender, ExecutedRoutedEventAr
var file = OpenTextFile();
if (file == null || file.Item2 == "") return;
WriteExportedData(file, Encoding.UTF8);

//// Create SaveFileDialog
//var dialog = new SaveFileDialog
//{
// DefaultExt = ".txt",
// Filter = "Text Files (*.txt)|*.txt"
//};

//// Display OpenFileDialog by calling ShowDialog method
//var result = dialog.ShowDialog();

//// if the file dialog returns true - file was selected
//var filename = "";
//if (result == true)
//{
// // Get the filename from the dialog
// filename = dialog.FileName;
//}

}

protected virtual void WriteExportedData(Tuple<FileStream,string> file, Encoding encoding)
Expand Down

0 comments on commit 72638b8

Please sign in to comment.