From 743418d944515a09f71a29fdda49d097ea38f2f4 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 3 Oct 2026 16:44:03 +0000 Subject: [PATCH] fix(app): store and close the editor before its sequence moves to the trash Delete first lets the editor of a sequence it moves store its last edit and close (or stops when the user keeps an edit that cannot be stored), and reports a sidecar written by a newer Titano instead of failing. A remote card whose proxy download ended before it was recorded opens at once, as on the Server screen; the Library reads its saved views again on refresh; the CSV writer of the Library and the Archive manager moves into LibraryCsv. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- src/Titano.App/Archive/SequenceDeletion.cs | 19 ++++++-- src/Titano.App/Library/LibraryCsv.cs | 38 +++++++++++++++ src/Titano.App/Sections/ArchiveViewModel.cs | 2 +- src/Titano.App/Sections/LibraryViewModel.cs | 52 ++++++--------------- 4 files changed, 70 insertions(+), 41 deletions(-) create mode 100644 src/Titano.App/Library/LibraryCsv.cs diff --git a/src/Titano.App/Archive/SequenceDeletion.cs b/src/Titano.App/Archive/SequenceDeletion.cs index 99fde663..695b1980 100644 --- a/src/Titano.App/Archive/SequenceDeletion.cs +++ b/src/Titano.App/Archive/SequenceDeletion.cs @@ -1,6 +1,7 @@ using System.IO; using Titano.App.Infrastructure; using Titano.App.Shell; +using Titano.Core.Archiving; using Titano.Core.Diagnostics; using Titano.Core.Localization; using Titano.Data; @@ -27,7 +28,7 @@ public static class SequenceDeletion shell.Notify(Loc.L("Archive.Delete.Nothing"), string.Join(Environment.NewLine, DeletePlans.Blocked(plan, DeletePlans.ListedLines))); return false; } - if (!shell.Services.Dialogs.Confirm(request)) return false; + if (!shell.Services.Dialogs.Confirm(request) || !await CloseEditorAsync(shell, plan)) return false; var entries = await Task.Run(() => archives.DeleteAsync(plan, Environment.UserName)); Log.Info("archive", $"{entries.Count} sequence(s) moved to the trash, {plan.Steps.Count - entries.Count} removed from the index only"); await RefreshAsync(shell); @@ -36,8 +37,8 @@ public static class SequenceDeletion entries.Count == 0 ? null : () => _ = RestoreAsync(shell, entries)); return true; } - catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException or StaleDeletePlanException or ArgumentException - or DatabaseException) + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException or StaleDeletePlanException or NewerFormatException + or ArgumentException or DatabaseException) { Log.Warning("archive", "Delete failed", ex); shell.Notify(Loc.L("Archive.Delete.Failed"), ex.Message); @@ -69,6 +70,18 @@ public static class SequenceDeletion await RefreshAsync(shell); } + /// + /// The editor of a sequence about to move stores its last edit (so the trash keeps it) and closes; false when that + /// edit could not be stored and the user kept it. + /// + private static async Task CloseEditorAsync(ShellViewModel shell, DeletePlan plan) + { + if (shell.Sequence.Editor is null || shell.CurrentSequenceId is not { } open || plan.Steps.All(s => s.Sequence.Id != open)) return true; + if (!await shell.Sequence.ConfirmCloseAsync()) return false; + await shell.Sequence.CloseAsync(); + return true; + } + private static async Task RefreshAsync(ShellViewModel shell) { await shell.Library.RefreshAsync(); diff --git a/src/Titano.App/Library/LibraryCsv.cs b/src/Titano.App/Library/LibraryCsv.cs new file mode 100644 index 00000000..9342da91 --- /dev/null +++ b/src/Titano.App/Library/LibraryCsv.cs @@ -0,0 +1,38 @@ +using System.Globalization; +using System.Text; +using Titano.Core.Model; + +namespace Titano.App.Library; + +/// The Library's "Export list as CSV" (BRIEF ยง12.2): RFC 4180, UTF-8, invariant numbers and dates. +public static class LibraryCsv +{ + /// One row per sequence; the header is English because the file is data, not UI. + public static string Write(IEnumerable sequences) + { + var inv = CultureInfo.InvariantCulture; + var sb = new StringBuilder("title,first_capture,last_capture,camera_make,camera_model,frames,bytes,folder,tags,origin\r\n"); + foreach (var s in sequences) + { + sb.Append(Field(s.DisplayTitle)).Append(',') + .Append(s.FirstCapture?.ToString("yyyy-MM-dd HH:mm:ss", inv)).Append(',') + .Append(s.LastCapture?.ToString("yyyy-MM-dd HH:mm:ss", inv)).Append(',') + .Append(Field(s.CameraMake)).Append(',') + .Append(Field(s.CameraModel)).Append(',') + .Append(s.FrameCount.ToString(inv)).Append(',') + .Append(s.TotalBytes.ToString(inv)).Append(',') + .Append(Field(s.Folder)).Append(',') + .Append(Field(string.Join(';', s.Tags))).Append(',') + .Append(s is { Origin: SequenceOrigin.Server, SyncState: SyncState.RemoteOnly } ? "server" : "local").Append("\r\n"); + } + return sb.ToString(); + } + + private static string Field(string? value) + { + if (string.IsNullOrEmpty(value)) return string.Empty; + // Formula-looking values are prefixed so spreadsheets do not execute them. + if (value[0] is '=' or '+' or '-' or '@') value = "'" + value; + return value.IndexOfAny([',', '"', '\r', '\n']) >= 0 ? "\"" + value.Replace("\"", "\"\"", StringComparison.Ordinal) + "\"" : value; + } +} diff --git a/src/Titano.App/Sections/ArchiveViewModel.cs b/src/Titano.App/Sections/ArchiveViewModel.cs index 17118909..d2586a4e 100644 --- a/src/Titano.App/Sections/ArchiveViewModel.cs +++ b/src/Titano.App/Sections/ArchiveViewModel.cs @@ -742,7 +742,7 @@ public sealed class ArchiveViewModel : SectionViewModel var folder = Services.Dialogs.PickFolder(Loc.L("Library.Export.Title")); if (folder is null || set.Count == 0) return; var path = Path.Combine(folder, $"Titano sequences {DateTime.Now:yyyy-MM-dd HHmm}.csv"); - var csv = LibraryViewModel.Csv(set); + var csv = LibraryCsv.Write(set); await Task.Run(() => AtomicFile.WriteAllText(path, csv)); Shell.Notify(Loc.L("Library.Export.Done"), Path.GetFileName(path), Loc.L("Library.Export.Show"), Loc.L("Tooltips.Library.ShowExport"), () => ShellActions.Reveal(path)); diff --git a/src/Titano.App/Sections/LibraryViewModel.cs b/src/Titano.App/Sections/LibraryViewModel.cs index 17d27789..a4b768c2 100644 --- a/src/Titano.App/Sections/LibraryViewModel.cs +++ b/src/Titano.App/Sections/LibraryViewModel.cs @@ -2,7 +2,6 @@ using System.Collections.ObjectModel; using System.ComponentModel; using System.Globalization; using System.IO; -using System.Text; using System.Windows.Input; using System.Windows.Threading; using Titano.App.Archiving; @@ -498,9 +497,10 @@ public sealed class LibraryViewModel : SectionViewModel await ReloadAsync(); } - /// Re-reads filter choices, totals and the server's cards, then the list (after imports, archive changes). + /// Re-reads saved views, filter choices, totals and the server's cards, then the list (after imports, archive changes). public async Task RefreshAsync() { + await LoadViewsAsync(); await RefreshFiltersAsync(); await RefreshRemoteAsync(reload: false); await ReloadAsync(); @@ -907,13 +907,17 @@ public sealed class LibraryViewModel : SectionViewModel } foreach (var item in Items.Where(i => job.TargetIds.Contains(i.Id))) item.IsAnalysing = _analysing.Contains(item.Id); } - // A remote card's "Open" waited for this proxy download; it opens if the Library is still in front. - var ended = job.State is JobState.Done or JobState.Failed or JobState.Cancelled; - if (ended && _openWhenCached.Remove(job.Id, out var remoteId) && job.State == JobState.Done && ReferenceEquals(Shell.Current, this)) - Shell.OpenSequence(remoteId); + OpenWhenCached(job); if (job.State == JobState.Done) _ = RefreshTargetsAsync(job); } + /// A remote card's "Open" waited for this proxy download; it opens if the Library is still in front. + private void OpenWhenCached(Job job) + { + if (job.State is not (JobState.Done or JobState.Failed or JobState.Cancelled) || !_openWhenCached.Remove(job.Id, out var remoteId)) return; + if (job.State == JobState.Done && ReferenceEquals(Shell.Current, this)) Shell.OpenSequence(remoteId); + } + /// A finished job changed some sequences: refresh those rows in place and the totals. private async Task RefreshTargetsAsync(Job job) { @@ -943,7 +947,10 @@ public sealed class LibraryViewModel : SectionViewModel Shell.OpenSequence(item.Id); return; } - if (await _actions.OpenRemoteAsync(item.Sequence) is { } job) _openWhenCached[job] = item.Id; + if (await _actions.OpenRemoteAsync(item.Sequence) is not { } job) return; + _openWhenCached[job] = item.Id; + // A download that finished before it was recorded here opens at once. + if (_db is not null && await _db.Jobs.GetAsync(job) is { State: JobState.Done or JobState.Failed or JobState.Cancelled } ended) OpenWhenCached(ended); } /// The card's Sync: the transfer or settings exchange its sync row offers; a conflict is settled on the Server screen. @@ -1064,40 +1071,11 @@ public sealed class LibraryViewModel : SectionViewModel var folder = Shell.Services.Dialogs.PickFolder(Loc.L("Library.Export.Title")); if (folder is null) return; var path = Path.Combine(folder, $"Titano sequences {DateTime.Now:yyyy-MM-dd HHmm}.csv"); - var csv = Csv(chosen); + var csv = LibraryCsv.Write(chosen); await Task.Run(() => AtomicFile.WriteAllText(path, csv)); Shell.Notify(Loc.L("Library.Export.Done"), Path.GetFileName(path), Loc.L("Library.Export.Show"), Loc.L("Tooltips.Library.ShowExport"), () => ShellActions.Reveal(path)); } - /// RFC 4180 CSV of sequences (header in English: it is a data file). - public static string Csv(IEnumerable sequences) - { - var inv = CultureInfo.InvariantCulture; - var sb = new StringBuilder("title,first_capture,last_capture,camera_make,camera_model,frames,bytes,folder,tags,origin\r\n"); - foreach (var s in sequences) - { - sb.Append(Field(s.DisplayTitle)).Append(',') - .Append(s.FirstCapture?.ToString("yyyy-MM-dd HH:mm:ss", inv)).Append(',') - .Append(s.LastCapture?.ToString("yyyy-MM-dd HH:mm:ss", inv)).Append(',') - .Append(Field(s.CameraMake)).Append(',') - .Append(Field(s.CameraModel)).Append(',') - .Append(s.FrameCount.ToString(inv)).Append(',') - .Append(s.TotalBytes.ToString(inv)).Append(',') - .Append(Field(s.Folder)).Append(',') - .Append(Field(string.Join(';', s.Tags))).Append(',') - .Append(s is { Origin: SequenceOrigin.Server, SyncState: SyncState.RemoteOnly } ? "server" : "local").Append("\r\n"); - } - return sb.ToString(); - - static string Field(string? value) - { - if (string.IsNullOrEmpty(value)) return string.Empty; - // Formula-looking values are prefixed so spreadsheets do not execute them. - if (value[0] is '=' or '+' or '-' or '@') value = "'" + value; - return value.IndexOfAny([',', '"', '\r', '\n']) >= 0 ? "\"" + value.Replace("\"", "\"\"", StringComparison.Ordinal) + "\"" : value; - } - } - /// Option labels and row texts follow a language switch without reloading. private void OnCultureChanged(object? sender, EventArgs e) {