Skip to content

Commit

Permalink
Fix problems if default data path cannot be created
Browse files Browse the repository at this point in the history
  • Loading branch information
DSPaul committed Nov 23, 2024
1 parent 1314364 commit d838304
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
7 changes: 2 additions & 5 deletions src/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using COMPASS.Interfaces;
using COMPASS.Models.Enums;
using COMPASS.Services;
using COMPASS.Tools;
using COMPASS.ViewModels;
using System;
using System.IO;
Expand All @@ -26,10 +25,8 @@ public App()
}
catch (Exception ex)

Check warning on line 26 in src/App.xaml.cs

View workflow job for this annotation

GitHub Actions / build-and-test

The variable 'ex' is declared but never used

Check warning on line 26 in src/App.xaml.cs

View workflow job for this annotation

GitHub Actions / build-and-test

The variable 'ex' is declared but never used

Check warning on line 26 in src/App.xaml.cs

View workflow job for this annotation

GitHub Actions / build-and-test

The variable 'ex' is declared but never used

Check warning on line 26 in src/App.xaml.cs

View workflow job for this annotation

GitHub Actions / build-and-test

The variable 'ex' is declared but never used
{
Logger.Error($"Failed to create folder at compass data path, so data cannot be saved", ex);
string msg = $"Failed to create a folder to store user data at {SettingsViewModel.CompassDataPath}, " +
$"please pick a new location to save your data. Creation failed with the following error {ex.Message}";
IOService.AskNewCodexFilePath(msg);
//Cannot show notification here because app needs to finish its constructor before any UI can be shown,
//Cannot log either because the log file is located in the CompassDataPath directory
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/ViewModels/CollectionViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public CollectionViewModel(MainViewModel? mainViewModel)
{
Logger.Error($"Failed to create folder to store user data, so data cannot be saved", ex);
string msg = $"Failed to create a folder to store user data at {SettingsViewModel.CompassDataPath}, " +
$"please pick a new location to save your data. Creation failed with the following error {ex.Message}";
$"please pick a new location to save your data. Creation failed with the following error: '{ex.Message}'";
IOService.AskNewCodexFilePath(msg);
}

Expand Down Expand Up @@ -140,7 +140,7 @@ private void LoadInitialCollection()
{
Logger.Error($"Failed to create folder to store user data, so data cannot be saved", ex);
string msg = $"Failed to create a folder to store user data at {SettingsViewModel.CompassDataPath}, " +
$"please pick a new location to save your data. Creation failed with the following error {ex.Message}";
$"please pick a new location to save your data. Creation failed with the following error: '{ex.Message}'";
IOService.AskNewCodexFilePath(msg);
}
}
Expand Down
16 changes: 13 additions & 3 deletions src/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using AutoUpdaterDotNET;
using Autofac;
using AutoUpdaterDotNET;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using COMPASS.Interfaces;
using COMPASS.Models;
using COMPASS.Models.CodexProperties;
using COMPASS.Models.Enums;
Expand Down Expand Up @@ -485,12 +487,17 @@ public bool SetNewDataPath(string newPath)

NewDataPath = newPath;

//Give users choice between moving or copying
//If there is existing data, Give users choice between moving or copying
if (Path.Exists(CompassDataPath))
{
ChangeDataLocationWindow window = new(this);
window.ShowDialog();
}
//If not, just change over
else
{
ChangeToNewDataPath();
}

return true;
}
Expand Down Expand Up @@ -542,10 +549,13 @@ private async Task CopyToNewDataPath()
/// </summary>
public void ChangeToNewDataPath()
{
MainViewModel.CollectionVM.CurrentCollection.Save();
MainViewModel.CollectionVM?.CurrentCollection.Save();

CompassDataPath = NewDataPath;

Notification changeSuccessful = new("Data path changed succesfully", $"Data path was successfully changed to {CompassDataPath}. COMPASS will now restart.");
App.Container.ResolveKeyed<INotificationService>(NotificationDisplayType.Windowed).Show(changeSuccessful);

//Restart COMPASS
var currentExecutablePath = Environment.ProcessPath;
var args = Environment.GetCommandLineArgs();
Expand Down
2 changes: 1 addition & 1 deletion src/Windows/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public MainWindow()
private void Window_Closing(object sender, CancelEventArgs e)
{
ProgressViewModel.GetInstance().CancelBackgroundTask();
MainViewModel.CollectionVM.CurrentCollection.Save();
MainViewModel.CollectionVM?.CurrentCollection.Save();
PreferencesService.GetInstance().SavePreferences();
}

Expand Down

0 comments on commit d838304

Please sign in to comment.