Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update recent list immediately after loading and saving project #253

Merged
merged 2 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions src/SimpleAccounting/Infrastructure/ProjectFileLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ namespace lg2de.SimpleAccounting.Infrastructure;

internal class ProjectFileLoader
{
private const int MaxRecentProjects = 10;
private readonly IFileSystem fileSystem;

private readonly IDialogs dialogs;
Expand Down Expand Up @@ -170,11 +169,6 @@ private void UpdateSettings(string projectFileName)
this.settings.SecuredDrives.Add(info.RootPath);
}

this.settings.RecentProjects.Remove(projectFileName);
this.settings.RecentProjects.Insert(0, projectFileName);
while (this.settings.RecentProjects.Count > MaxRecentProjects)
{
this.settings.RecentProjects.RemoveAt(MaxRecentProjects);
}
this.settings.SetRecentProject(projectFileName);
}
}
2 changes: 2 additions & 0 deletions src/SimpleAccounting/Model/ProjectData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/// It contains the persistent data according to <see cref="AccountingData" />
/// as well as the current state of the project.
/// </remarks>
internal class ProjectData : IProjectData

Check warning on line 27 in src/SimpleAccounting/Model/ProjectData.cs

View workflow job for this annotation

GitHub Actions / build

Split this class into smaller and more specialized ones to reduce its dependencies on other types from 33 to the maximum authorized 30 or less. (https://rules.sonarsource.com/csharp/RSPEC-1200)

Check warning on line 27 in src/SimpleAccounting/Model/ProjectData.cs

View workflow job for this annotation

GitHub Actions / build

Split this class into smaller and more specialized ones to reduce its dependencies on other types from 33 to the maximum authorized 30 or less. (https://rules.sonarsource.com/csharp/RSPEC-1200)
{
private readonly IDialogs dialogs;
private readonly IFileSystem fileSystem;
Expand Down Expand Up @@ -167,6 +167,8 @@
{
this.fileSystem.FileDelete(this.AutoSaveFileName);
}

this.Settings.SetRecentProject(this.FileName);
}

public async Task AutoSaveAsync(CancellationToken cancellationToken)
Expand Down Expand Up @@ -230,7 +232,7 @@
public void ShowAddBookingDialog(bool showInactiveAccounts)
{
var bookingModel =
new EditBookingViewModel(this, DateTime.Today, editMode: false);

Check warning on line 235 in src/SimpleAccounting/Model/ProjectData.cs

View workflow job for this annotation

GitHub Actions / build

Use a testable (date) time provider instead. (https://rules.sonarsource.com/csharp/RSPEC-6354)
var allAccounts = this.Storage.AllAccounts;
bookingModel.Accounts.AddRange(showInactiveAccounts ? allAccounts : allAccounts.Where(x => x.Active));
var bookingTemplates = this.Storage.Setup?.BookingTemplates;
Expand Down
31 changes: 18 additions & 13 deletions src/SimpleAccounting/Presentation/MenuViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public MenuViewModel(
_ => this.OnOpenProject());

public ICommand SaveProjectCommand => new RelayCommand(
_ => this.projectData.SaveProject(),
_ => this.OnSaveProject(),
_ => this.projectData.IsModified);

public ICommand ProjectOptionsCommand => new RelayCommand(
Expand Down Expand Up @@ -148,6 +148,7 @@ public void BuildRecentProjectsMenu()
return;
}

this.RecentProjects.Clear();
foreach (var project in this.projectData.Settings.RecentProjects.Cast<string>())
{
var command = new AsyncCommand(this.busy, () => this.OnLoadRecentProjectAsync(project));
Expand All @@ -167,20 +168,14 @@ public void OnDataLoaded()
private async Task OnLoadRecentProjectAsync(string project)
{
var loadResult = await this.projectData.LoadFromFileAsync(project);
if (loadResult != OperationResult.Failed)
if (loadResult == OperationResult.Failed)
{
return;
}

// failed to load, remove from menu
// keep in menu if aborted (e.g. SecureDrive not available)
var item = this.RecentProjects.FirstOrDefault(x => x.Header == project);
if (item != null)
{
this.RecentProjects.Remove(item);
// failed to load, remove from menu
// keep in menu if aborted (e.g. SecureDrive not available)
this.projectData.Settings.RecentProjects.Remove(project);
}

this.projectData.Settings.RecentProjects.Remove(project);
this.BuildRecentProjectsMenu();
}

private void OnOpenProject()
Expand All @@ -197,10 +192,20 @@ private void OnOpenProject()
async () =>
{
await this.projectData.LoadFromFileAsync(fileName);
await Execute.OnUIThreadAsync(() => this.busy.IsBusy = false);
await Execute.OnUIThreadAsync(() =>
{
this.busy.IsBusy = false;
this.BuildRecentProjectsMenu();
});
});
}

private void OnSaveProject()
{
this.projectData.SaveProject();
this.BuildRecentProjectsMenu();
}

private void UpdateBookingYears()
{
this.BookingYears.Clear();
Expand Down
28 changes: 28 additions & 0 deletions src/SimpleAccounting/Properties/Settings.Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// <copyright>
// Copyright (c) Lukas Grützmacher. All rights reserved.
// </copyright>

namespace lg2de.SimpleAccounting.Properties;

internal sealed partial class Settings
{
private const int MaxRecentProjects = 10;

/// <summary>
/// Sets the specified project file (path) as the most recent project.
/// </summary>
/// <remarks>
/// The list of recent projects is updated automatically.
/// </remarks>
/// <param name="projectFileName">The full path of the recent project.</param>
public void SetRecentProject(string projectFileName)
{
this.RecentProjects ??= [];
this.RecentProjects.Remove(projectFileName);
this.RecentProjects.Insert(0, projectFileName);
while (this.RecentProjects.Count > MaxRecentProjects)
{
this.RecentProjects.RemoveAt(MaxRecentProjects);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ public void SwitchCultureCommand_DummyLanguage_MessageBoxShownAndConfigurationUp
sut.IsSystemCulture.Should().BeFalse();
}

[Fact]
public void RecentProjects_SaveNewFile_AddedToList()
{
var sut = CreateSut(out ProjectData projectData);
sut.NewProjectCommand.Execute(null);
projectData.FileName = "this-is-the-file-name";

sut.SaveProjectCommand.Execute(null);

sut.RecentProjects.Should().BeEquivalentTo(new[] { new { Header = "this-is-the-file-name" } });
}

[Fact]
public void NewProjectCommand_ModifiedProjectNoDiscard_ProjectRemains()
{
Expand Down Expand Up @@ -376,7 +388,8 @@ private static MenuViewModel CreateSut(out ProjectData projectData, out IReportF
return sut;
}

private static MenuViewModel CreateSut(out ProjectData projectData, out IDialogs dialogs, out IReportFactory reportFactory)
private static MenuViewModel CreateSut(
out ProjectData projectData, out IDialogs dialogs, out IReportFactory reportFactory)
{
var windowManager = Substitute.For<IWindowManager>();
var fileSystem = Substitute.For<IFileSystem>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public async Task RecentFileCommand_FileOnSecureDriveNotStarted_ProjectKept()
((IActivate)sut).Activate();
sut.Menu.RecentProjects.Select(x => x.Header).Should().Equal("K:\\file1", "file2");

foreach (var viewModel in sut.Menu.RecentProjects)
foreach (var viewModel in sut.Menu.RecentProjects.ToList())
{
await viewModel.Command.ExecuteAsync(null);
}
Expand Down
Loading