fix(settings): an archive row edit reaches the folder it was made for

A retarget now returns the archive registered at its own folder, not at
the folder current when it finishes, so an edit made while folder A opens
no longer lands in the archive of a folder B chosen right after. The rows
and _archive follow only the latest folder; Reset section pushes to the
archive it asked about and reloads the rows when the folder changed. A
newer sidecar met while opening no longer faults the retarget silently:
it is reported like any open failure. The smoke test now waits for the
empty-folder retarget before editing, so the archives[0] fallback would
fail it, and covers the two-folder race.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 16:33:45 +00:00
1 parent d2e547f9d2
commit bf2183764f
2 files changed
+69 -18

No files matched your search

+43 -17
View File
@@ -313,6 +313,9 @@ public sealed class SettingsViewModel : SectionViewModel
/// <summary>Where "Start with Windows" is kept (the Run key); screenshots, tests and portable copies keep it in memory.</summary>
internal IStartWithWindows Startup { get; set; }
/// <summary>The latest archive folder change: done once the rows show what the folder holds (tests wait for it).</summary>
internal Task ArchiveRetarget => _retarget;
/// <summary>A portable copy leaves nothing in the registry (BRIEF §6.2): it offers no Startup row.</summary>
public bool ShowsStartup => !Shell.Services.Environment.IsPortable;
@@ -899,19 +902,28 @@ public sealed class SettingsViewModel : SectionViewModel
/// archive is the one at the archive folder of this PC, never another one of the index: with no folder, or none
/// registered there, the rows edit only this PC's defaults. Returns that archive (null for none).
/// </summary>
private async Task<Archive?> LoadArchiveAsync()
private Task<Archive?> LoadArchiveAsync() => LoadArchiveAsync(_s.LastArchivePath);
/// <summary>
/// <see cref="LoadArchiveAsync()"/> for the archive folder <paramref name="root"/>, which a folder change may have
/// replaced meanwhile: the rows are then left to the folder chosen since, and the archive registered at
/// <paramref name="root"/> is still returned, so edits made for that folder reach it.
/// </summary>
private async Task<Archive?> LoadArchiveAsync(string? root)
{
if (Shell.Services.Database is not { } db || Shell.Services.Archives is not { } archives) return null;
Archive? archive = null;
try
{
var registered = await db.Archives.ListAsync();
archive = _s.LastArchivePath is { Length: > 0 } root ? CurrentArchive.At(registered, root) : null;
archive = root is { Length: > 0 } ? CurrentArchive.At(registered, root) : null;
if (!IsArchiveFolder(root)) return archive;
_archive = archive;
if (archive is null) return null;
var settings = await Task.Run(() => archives.RefreshSettingsAsync(archive.Id))
?? new ArchiveSettings(archive.Name, new LayoutTemplates { Folder = archive.FolderTemplate, File = archive.FileTemplate },
new ProxyPolicy { LongEdge = archive.ProxyLongEdge }, new ThumbnailPolicy { LongEdge = _s.ThumbnailLongEdge });
if (!IsArchiveFolder(root)) return archive;
if (!FolderTemplate.IsDirty) FolderTemplate.Load(settings.Templates.Folder);
if (!FileTemplate.IsDirty) FileTemplate.Load(settings.Templates.File);
var changed = _s.FolderTemplate != settings.Templates.Folder || _s.FileTemplate != settings.Templates.File
@@ -935,14 +947,18 @@ public sealed class SettingsViewModel : SectionViewModel
return archive;
}
/// <summary>Whether <paramref name="root"/> is still this PC's archive folder (the one the rows show).</summary>
private bool IsArchiveFolder(string? root) => string.Equals(root, _s.LastArchivePath, StringComparison.Ordinal);
/// <summary>
/// The archive an Archive row edits, as the folder was when the row was edited (<paramref name="retarget"/>, the
/// latest folder change then): the open one, or, while the archive at that folder is still opening, the one found
/// there once it has (null for none), so the edit is neither lost nor sent to a folder chosen later.
/// latest folder change then): the open one, or, while the archive at that folder is still opening or once another
/// folder has been chosen, the one found there (null for none), so the edit is neither lost nor sent to a folder
/// chosen later.
/// </summary>
private async Task<Archive?> ArchiveAtFolderAsync(Task<Archive?> retarget)
{
if (retarget.IsCompleted) return _archive;
if (retarget.IsCompleted && retarget == _retarget) return _archive;
// Its failures were told already; only the archive it found matters here.
await ((Task)retarget).ConfigureAwait(ConfigureAwaitOptions.ContinueOnCapturedContext | ConfigureAwaitOptions.SuppressThrowing);
return retarget.IsCompletedSuccessfully ? retarget.Result : null;
@@ -952,11 +968,12 @@ public sealed class SettingsViewModel : SectionViewModel
/// Writes one Archive row into the open archive's archive.json and index through the archive engine (BRIEF §7). When
/// that fails (offline, read-only, busy) the user hears why and the rows show the archive's values again. An edit made
/// while the archive at a folder just chosen is opening waits for it, then goes to that archive (or stays this PC's
/// default when there is none), instead of being lost.
/// default when there is none), instead of being lost. <paramref name="retarget"/> is the folder change the edit was
/// made after, the latest one by default.
/// </summary>
private async Task UpdateArchiveAsync(Func<ArchiveSettings, ArchiveSettings> edit)
private async Task UpdateArchiveAsync(Func<ArchiveSettings, ArchiveSettings> edit, Task<Archive?>? retarget = null)
{
var retarget = _retarget;
retarget ??= _retarget;
var waited = !retarget.IsCompleted;
if (await ArchiveAtFolderAsync(retarget) is not { } archive || Shell.Services.Archives is not { } archives) return;
try
@@ -977,36 +994,42 @@ public sealed class SettingsViewModel : SectionViewModel
}
}
/// <summary>Every archive row into the open archive: Archive › Reset section, after the user confirmed it for that archive.</summary>
private Task PushArchiveSettingsAsync() => UpdateArchiveAsync(a => a with
/// <summary>
/// Every archive row into the archive found after <paramref name="retarget"/>: Archive › Reset section, after the user
/// confirmed it for that archive.
/// </summary>
private Task PushArchiveSettingsAsync(Task<Archive?> retarget) => UpdateArchiveAsync(a => a with
{
Templates = a.Templates with { Folder = _s.FolderTemplate, File = _s.FileTemplate },
Proxies = a.Proxies with { LongEdge = _s.ProxyLongEdge },
Thumbnails = a.Thumbnails with { LongEdge = _s.ThumbnailLongEdge },
});
}, retarget);
/// <summary>
/// The archive folder changed: an archive already there is registered as at start (relinked when it moved) and given
/// its rescan, and the rows show its settings; any other folder leaves them with this PC's defaults for new archives.
/// Later edits never reach the archive shown before. Returns the archive found there (null for none).
/// Later edits never reach the archive shown before. Returns the archive registered at that folder (null for none),
/// even when another folder has been chosen since: edits made for this one go there.
/// </summary>
private async Task<Archive?> RetargetArchiveAsync()
{
_archive = null;
if (_s.LastArchivePath is { } root && Shell.Services.Archives is { } archives)
var root = _s.LastArchivePath;
if (root is not null && Shell.Services.Archives is { } archives)
{
try
{
var opened = await Task.Run(async () => File.Exists(new ArchivePaths(root).ManifestPath) ? await archives.OpenAsync(root) : null);
if (opened is not null) await Shell.Services.QueueScanAsync(opened, full: false);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException or DatabaseException)
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException or NewerFormatException
or DatabaseException)
{
Log.Warning("settings", $"The archive at {root} could not be opened", ex);
Shell.Notify(Loc.L("Errors.Archive.Open"), ArchiveActions.Cause(ex), isError: true);
}
}
return await LoadArchiveAsync();
return await LoadArchiveAsync(root);
}
/// <summary>
@@ -1401,7 +1424,8 @@ public sealed class SettingsViewModel : SectionViewModel
/// </summary>
private async Task ResetArchiveAsync(AppSettings defaults)
{
var open = await ArchiveAtFolderAsync(_retarget);
var retarget = _retarget;
var open = await ArchiveAtFolderAsync(retarget);
if (open is not null && !ConfirmArchiveReset(open)) return;
_s.FolderTemplate = defaults.FolderTemplate;
_s.FileTemplate = defaults.FileTemplate;
@@ -1413,7 +1437,9 @@ public sealed class SettingsViewModel : SectionViewModel
UpdatePreview();
Save();
OnAllPropertiesChanged();
if (open is not null) await PushArchiveSettingsAsync();
if (open is not null) await PushArchiveSettingsAsync(retarget);
// Another folder was chosen while this one's archive opened: the rows show what that folder's archive holds again.
if (retarget != _retarget) await LoadArchiveAsync();
}
private void LoadImportFields()
@@ -183,11 +183,14 @@ public sealed class SettingsScreenSmokeTests
shell.Navigate(SectionKey.Settings);
settings.Page = SettingsPage.Archive;
// No archive folder: a template edit is this PC's default only, never the first archive of the index.
// No archive folder: a template edit is this PC's default only, never the first archive of the index. The edit is
// made once the rows follow the empty folder, so it cannot wait for anything that would hide a write.
settings.ArchiveRoot.Text = string.Empty;
Assert.True(settings.ArchiveRoot.Commit());
await settings.ArchiveRetarget;
settings.FolderTemplate.Text = "{camera}/{date}";
Assert.True(settings.FolderTemplate.Commit());
Assert.Equal("{camera}/{date}", shell.Services.Settings.FolderTemplate);
// The archive's folder is chosen and a template edited at once, while that archive is still opening: the edit
// reaches its archive.json instead of being undone when it opens, and the row shows it.
@@ -199,6 +202,28 @@ public sealed class SettingsScreenSmokeTests
await WaitAsync(() => settings.FileTemplate.Text == "{date}_{n:0000}" && shell.Services.Settings.FileTemplate == "{date}_{n:0000}", "the row shows it");
Assert.NotEqual("{camera}/{date}", Read(manifestPath).LayoutTemplates.Folder);
// An edit made before a second folder change, while the first folder's archive is still opening, goes to that
// archive: never to the archive at the folder chosen after it.
var otherRoot = System.IO.Path.Combine(shell.Services.Environment.DataDirectory, "other-archive");
await shell.Services.Archives!.CreateAsync(otherRoot, "Other");
var otherManifestPath = new Titano.Core.Archiving.ArchivePaths(otherRoot).ManifestPath;
settings.ArchiveRoot.Text = otherRoot;
Assert.True(settings.ArchiveRoot.Commit());
await settings.ArchiveRetarget;
settings.ArchiveRoot.Text = archive.RootPath;
Assert.True(settings.ArchiveRoot.Commit());
settings.FileTemplate.Text = "{date}-{n:0000}";
Assert.True(settings.FileTemplate.Commit());
settings.ArchiveRoot.Text = otherRoot;
Assert.True(settings.ArchiveRoot.Commit());
await WaitAsync(() => Read(manifestPath).LayoutTemplates.File == "{date}-{n:0000}", "the edit reached the archive of the folder it was made for");
await settings.ArchiveRetarget;
Assert.NotEqual("{date}-{n:0000}", Read(otherManifestPath).LayoutTemplates.File);
Assert.Equal(Read(otherManifestPath).LayoutTemplates.File, settings.FileTemplate.Text);
settings.ArchiveRoot.Text = archive.RootPath;
Assert.True(settings.ArchiveRoot.Commit());
await settings.ArchiveRetarget;
// Offline, only archive.json knows the thumbnail size: this PC's stays instead of becoming the default.
Titano.Core.Archiving.ManifestStore.WriteArchive(manifestPath, Read(manifestPath) with { ThumbnailPolicy = new Titano.Core.Archiving.ThumbnailPolicy { LongEdge = 480 } });
shell.Navigate(SectionKey.Library);