From 328d4337b7386374ea8c95a9314d3b0ac19226fb Mon Sep 17 00:00:00 2001
From: "harald.martini@student.tugraz.at" <harald.martini@student.tugraz.at>
Date: Tue, 11 May 2021 13:46:50 +0200
Subject: [PATCH] Changed FileDialogHelper Binding to SingletonScope,

store last directory information in Dictionary
---
 VECTO3GUI2020/App.xaml.cs                     |  2 +-
 VECTO3GUI2020/Helper/DialogHelper.cs          | 41 +++++++++++++++----
 .../Implementation/JobListViewModel.cs        |  4 +-
 .../MultistageAirdragViewModel.cs             |  2 +-
 .../MultistageJobViewModel_v0_1.cs            |  2 +-
 .../NewMultiStageJobViewModel.cs              |  2 +-
 .../ViewModelTests/LoadAndSaveVehicleData.cs  |  2 +-
 7 files changed, 39 insertions(+), 16 deletions(-)

diff --git a/VECTO3GUI2020/App.xaml.cs b/VECTO3GUI2020/App.xaml.cs
index eb0f63b976..7475864d68 100644
--- a/VECTO3GUI2020/App.xaml.cs
+++ b/VECTO3GUI2020/App.xaml.cs
@@ -48,7 +48,7 @@ namespace VECTO3GUI2020
 
             container.Bind<ISettingsModel>().To<SettingsModel>();
 
-			container.Bind<IDialogHelper>().To<DialogHelper>();
+			container.Bind<IDialogHelper>().To<DialogHelper>().InSingletonScope();
 			container.Bind<IWindowHelper>().To<WindowHelper>();
 
 		}
diff --git a/VECTO3GUI2020/Helper/DialogHelper.cs b/VECTO3GUI2020/Helper/DialogHelper.cs
index 3c85504230..190866f8a6 100644
--- a/VECTO3GUI2020/Helper/DialogHelper.cs
+++ b/VECTO3GUI2020/Helper/DialogHelper.cs
@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.IO;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
@@ -14,23 +15,31 @@ namespace VECTO3GUI2020.Helper
 {
     public class DialogHelper : IDialogHelper
 	{
-		private readonly string _defaultInitialDirectory = System.IO.Path.GetDirectoryName(
-			System.Reflection.Assembly.GetExecutingAssembly().Location);
-
+		private readonly string _defaultInitialDirectory = Settings.Default.DefaultFilePath;
 
 		#region File and Folder Dialogs
 		private string _xmlFilter = "XML Files (*.xml)|*.xml";
+
+		private Dictionary<string, string> lastUsedDirectories = new Dictionary<string, string>();
+
+		private string lastUsedDirectoryFolderPicker = null;
 		private string[] OpenFilesDialog(string filter, string initialDirectory, bool multiselect)
 		{
+			if (initialDirectory == null) {
+				initialDirectory = LookUpLastDir(filter);
+			}
+
 
+			
 			using (OpenFileDialog fd = new OpenFileDialog {
 				InitialDirectory = initialDirectory ?? _defaultInitialDirectory,
 				Multiselect = multiselect,
-				Filter = filter
+				Filter = filter,
+				RestoreDirectory = true
 			}) {
 				var result = fd.ShowDialog();
-				if (result == DialogResult.OK)
-				{
+				if (result == DialogResult.OK) {
+					lastUsedDirectories[filter] = Path.GetDirectoryName(fd.FileName);
 					return fd.FileNames;
 				}
 			}
@@ -39,6 +48,16 @@ namespace VECTO3GUI2020.Helper
 			return null;
 		}
 
+		private string LookUpLastDir(string filter)
+		{
+			string lastUsedDirectory = null;
+			if (lastUsedDirectories.TryGetValue(filter, out lastUsedDirectory)) {
+				return lastUsedDirectory;
+			} else {
+				return Settings.Default.DefaultFilePath;
+			}
+		}
+
 		public string OpenFileDialog(string filter = "All files (*.*)|*.*", string initialDirectory = null)
 		{
 			return OpenFilesDialog(filter, initialDirectory)?[0];
@@ -69,14 +88,20 @@ namespace VECTO3GUI2020.Helper
 
 		public string OpenFolderDialog(string initialDirectory = null)
 		{
+
+			if (initialDirectory == null) {
+				initialDirectory = lastUsedDirectoryFolderPicker;
+			}
 			using (var dialog = new CommonOpenFileDialog())
 			{
 				dialog.InitialDirectory = initialDirectory;
 				dialog.IsFolderPicker = true;
+				dialog.Multiselect = false;
+				dialog.RestoreDirectory = true;
 
 				var result = dialog.ShowDialog();
-				if (result == CommonFileDialogResult.Ok)
-				{
+				if (result == CommonFileDialogResult.Ok) {
+					lastUsedDirectoryFolderPicker = Path.GetDirectoryName(dialog.FileName);
 					return dialog.FileName;
 				}
 			}
diff --git a/VECTO3GUI2020/ViewModel/Implementation/JobListViewModel.cs b/VECTO3GUI2020/ViewModel/Implementation/JobListViewModel.cs
index daef720bf3..014b95dc66 100644
--- a/VECTO3GUI2020/ViewModel/Implementation/JobListViewModel.cs
+++ b/VECTO3GUI2020/ViewModel/Implementation/JobListViewModel.cs
@@ -122,9 +122,7 @@ namespace VECTO3GUI2020.ViewModel.Implementation
         private void AddJobExecute()
         {
             IsLoading = true;
-			string path = _settings.DefaultFilePath;
-			var filename = _dialogHelper.OpenXMLFileDialog(path);
-
+			var filename = _dialogHelper.OpenXMLFileDialog();
 			if (filename != null)
             {
                 fileReadingBackgroundWorker.RunWorkerAsync(filename);
diff --git a/VECTO3GUI2020/ViewModel/MultiStage/Implementation/MultistageAirdragViewModel.cs b/VECTO3GUI2020/ViewModel/MultiStage/Implementation/MultistageAirdragViewModel.cs
index 240f3f990b..e3e54ba60d 100644
--- a/VECTO3GUI2020/ViewModel/MultiStage/Implementation/MultistageAirdragViewModel.cs
+++ b/VECTO3GUI2020/ViewModel/MultiStage/Implementation/MultistageAirdragViewModel.cs
@@ -93,7 +93,7 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation
 
 		public void LoadAirdragFileCommandExecute()
 		{
-			var fileName =_dependencies.DialogHelper.OpenXMLFileDialog(Settings.Default.DefaultFilePath);
+			var fileName =_dependencies.DialogHelper.OpenXMLFileDialog();
 			if (fileName == null) {
 				return;
 			}
diff --git a/VECTO3GUI2020/ViewModel/MultiStage/Implementation/MultistageJobViewModel_v0_1.cs b/VECTO3GUI2020/ViewModel/MultiStage/Implementation/MultistageJobViewModel_v0_1.cs
index 06594be48c..ab0664fc21 100644
--- a/VECTO3GUI2020/ViewModel/MultiStage/Implementation/MultistageJobViewModel_v0_1.cs
+++ b/VECTO3GUI2020/ViewModel/MultiStage/Implementation/MultistageJobViewModel_v0_1.cs
@@ -185,7 +185,7 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation
 
 		private void LoadVehicleDataExecute()
 		{
-			var fileName = _dialogHelper.Value.OpenXMLFileDialog(Settings.Default.DefaultFilePath);
+			var fileName = _dialogHelper.Value.OpenXMLFileDialog();
 			if (fileName == null) {
 				return;
 			}
diff --git a/VECTO3GUI2020/ViewModel/MultiStage/Implementation/NewMultiStageJobViewModel.cs b/VECTO3GUI2020/ViewModel/MultiStage/Implementation/NewMultiStageJobViewModel.cs
index 57cef69588..bb569a84ed 100644
--- a/VECTO3GUI2020/ViewModel/MultiStage/Implementation/NewMultiStageJobViewModel.cs
+++ b/VECTO3GUI2020/ViewModel/MultiStage/Implementation/NewMultiStageJobViewModel.cs
@@ -62,7 +62,7 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation
 		private void AddVifFileExecute()
 		{
 			
-			var fileName = _dialogHelper.OpenXMLFileDialog(_settings.DefaultFilePath);
+			var fileName = _dialogHelper.OpenXMLFileDialog();
 			if (fileName == null) {
 				return;
 			}
diff --git a/Vecto3GUI2020Test/ViewModelTests/LoadAndSaveVehicleData.cs b/Vecto3GUI2020Test/ViewModelTests/LoadAndSaveVehicleData.cs
index 14e2ce67f4..f02da4bbe7 100644
--- a/Vecto3GUI2020Test/ViewModelTests/LoadAndSaveVehicleData.cs
+++ b/Vecto3GUI2020Test/ViewModelTests/LoadAndSaveVehicleData.cs
@@ -115,7 +115,7 @@ namespace Vecto3GUI2020Test
 		public void loadInputFilePrimaryOnly()
 		{
 			var vm = loadFile(primary_vehicle_only);
-			Assert.AreEqual(1, vm.MultiStageJobViewModel.ManufacturingStageViewModel.StageCount);
+			Assert.AreEqual(2, vm.MultiStageJobViewModel.ManufacturingStageViewModel.StageCount);
 
 			var primaryVehicle = vm.MultiStageJobViewModel.PrimaryVehicle;
 			Assert.NotNull(primaryVehicle);
-- 
GitLab